View Javadoc

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.lang.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.
45       * @param work not null
46       */
47      public RenderingHelper(final Descriptor work, final Configuration configuration) {
48          super();
49          this.work = work;
50          this.configuration = configuration;
51      }
52  
53      /**
54       * Should resources with the given license by
55       * the given organisation be rendered?
56       * @param organisation not null
57       * @param license not null
58       * @return true when resources should be rendered,
59       * false otherwise
60       */
61      public boolean renderResources(final ByOrganisation organisation,
62              final WithLicense license) {
63          return isNot(
64                  primary(organisation)) ||
65                  isNot(primary(license)) ||
66                  license.hasCopyrightNotice();
67      }
68  
69      /**
70       * Is this license the primary license for the work?
71       * @param license not null
72       * @return true when this is the primary license
73       * for the work, false otherwise
74       */
75      private boolean primary(final WithLicense license) {
76          return work.isPrimary(license.getLicense());
77      }
78  
79      /**
80       * Is this the primary organisation?
81       * @param organisation true when this is the primary license,
82       * false otherwise
83       * @return
84       */
85      private boolean primary(final ByOrganisation organisation) {
86          return work.isPrimary(organisation);
87      }
88  
89      /**
90       * Negates claim.
91       * @param claim to be negated
92       * @return true when claim is false,
93       * false when true
94       */
95      public boolean isNot(boolean claim) {
96          return !claim;
97      }
98  
99      /**
100      * Renders the resource source URL
101      * based on configuration setting suitable
102      * for the license file.
103      * @param resource not null
104      * @return not null, possible empty string
105      */
106     public String sourceUrl(final Resource resource) {
107         final String result;
108         final String source = resource.getSource();
109         if (StringUtils.isBlank(source) || isNot(configuration.includeSourceURLsInLicense())) {
110             result = "";
111         } else {
112             result = " from " + source;
113         }
114         return result;
115     }
116 }