1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.creadur.whisker.out.velocity; 20 21 import org.apache.commons.lang3.StringUtils; 22 import org.apache.creadur.whisker.app.Configuration; 23 import org.apache.creadur.whisker.model.ByOrganisation; 24 import org.apache.creadur.whisker.model.Descriptor; 25 import org.apache.creadur.whisker.model.Resource; 26 import org.apache.creadur.whisker.model.WithLicense; 27 28 /** 29 * Factors out rendering logic from template. 30 */ 31 public class RenderingHelper { 32 33 /** 34 * The work being rendered, not null 35 */ 36 private final Descriptor work; 37 38 /** 39 * Configuration for the rendering. 40 */ 41 private final Configuration configuration; 42 43 /** 44 * Constructs a helper for the given work and configuration. 45 * @param work not null 46 * @param configuration not null 47 */ 48 public RenderingHelper(final Descriptor work, final Configuration configuration) { 49 super(); 50 this.work = work; 51 this.configuration = configuration; 52 } 53 54 /** 55 * Should resources with the given license by 56 * the given organisation be rendered? 57 * @param organisation not null 58 * @param license not null 59 * @return true when resources should be rendered, 60 * false otherwise 61 */ 62 public boolean renderResources(final ByOrganisation organisation, 63 final WithLicense license) { 64 return isNot( 65 primary(organisation)) || 66 isNot(primary(license)) || 67 license.hasCopyrightNotice(); 68 } 69 70 /** 71 * Is this license the primary license for the work? 72 * @param license not null 73 * @return true when this is the primary license 74 * for the work, false otherwise 75 */ 76 private boolean primary(final WithLicense license) { 77 return work.isPrimary(license.getLicense()); 78 } 79 80 /** 81 * Is this the primary organisation? 82 * @param organisation true when this is the primary license, 83 * false otherwise 84 * @return 85 */ 86 private boolean primary(final ByOrganisation organisation) { 87 return work.isPrimary(organisation); 88 } 89 90 /** 91 * Negates claim. 92 * @param claim to be negated 93 * @return true when claim is false, 94 * false when true 95 */ 96 public boolean isNot(boolean claim) { 97 return !claim; 98 } 99 100 /** 101 * Renders the resource source URL 102 * based on configuration setting suitable 103 * for the license file. 104 * @param resource not null 105 * @return not null, possible empty string 106 */ 107 public String sourceUrl(final Resource resource) { 108 final String result; 109 final String source = resource.getSource(); 110 if (StringUtils.isBlank(source) || isNot(configuration.includeSourceURLsInLicense())) { 111 result = ""; 112 } else { 113 result = " from " + source; 114 } 115 return result; 116 } 117 }