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.rat.api;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  /**
28   * Data about the subject.
29   */
30  public class MetaData {
31  
32      public static final String RAT_BASE_URL = "http://org/apache/rat/meta-data";
33      
34      // Document Categories
35      public static final String RAT_URL_DOCUMENT_CATEGORY = RAT_BASE_URL + "#FileCategory";
36      public static final String RAT_DOCUMENT_CATEGORY_VALUE_GENERATED = "GEN  ";
37      public static final String RAT_DOCUMENT_CATEGORY_VALUE_UNKNOWN = "?????";
38      public static final String RAT_DOCUMENT_CATEGORY_VALUE_ARCHIVE = "archive";
39      public static final String RAT_DOCUMENT_CATEGORY_VALUE_NOTICE = "notice";
40      public static final String RAT_DOCUMENT_CATEGORY_VALUE_BINARY = "binary";
41      public static final String RAT_DOCUMENT_CATEGORY_VALUE_STANDARD = "standard";
42      public static final Datum RAT_DOCUMENT_CATEGORY_DATUM_GENERATED = new Datum(RAT_URL_DOCUMENT_CATEGORY, RAT_DOCUMENT_CATEGORY_VALUE_GENERATED);
43      public static final Datum RAT_DOCUMENT_CATEGORY_DATUM_UNKNOWN = new Datum(RAT_URL_DOCUMENT_CATEGORY, RAT_DOCUMENT_CATEGORY_VALUE_UNKNOWN);
44      public static final Datum RAT_DOCUMENT_CATEGORY_DATUM_ARCHIVE = new Datum(RAT_URL_DOCUMENT_CATEGORY, RAT_DOCUMENT_CATEGORY_VALUE_ARCHIVE);
45      public static final Datum RAT_DOCUMENT_CATEGORY_DATUM_NOTICE = new Datum(RAT_URL_DOCUMENT_CATEGORY, RAT_DOCUMENT_CATEGORY_VALUE_NOTICE);
46      public static final Datum RAT_DOCUMENT_CATEGORY_DATUM_BINARY = new Datum(RAT_URL_DOCUMENT_CATEGORY, RAT_DOCUMENT_CATEGORY_VALUE_BINARY);
47      public static final Datum RAT_DOCUMENT_CATEGORY_DATUM_STANDARD = new Datum(RAT_URL_DOCUMENT_CATEGORY, RAT_DOCUMENT_CATEGORY_VALUE_STANDARD);
48      
49      // Header Categories
50      public static final String RAT_URL_HEADER_CATEGORY = RAT_BASE_URL + "#HeaderCategory";
51      
52      // License Family Categories
53      public static final String RAT_URL_LICENSE_FAMILY_CATEGORY= RAT_BASE_URL + "#LicenseFamilyCategory";
54      // Shortcuts used in report output, must be exactly 5 characters
55      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_GEN = "GEN  ";
56      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN = "?????";
57      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_ASL = "AL   ";
58      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_OASIS = "OASIS";
59      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_W3CD = "W3CD ";
60      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_W3C = "W3C  ";
61      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_DOJO = "DOJO ";
62      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_TMF = "TMF  ";
63      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL1 ="GPL1 ";
64      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL2 ="GPL2 ";
65      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL3 = "GPL3 ";
66      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_MIT = "MIT  ";
67      public static final String RAT_LICENSE_FAMILY_CATEGORY_VALUE_CDDL1 = "CDDL1";
68  
69      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_GEN = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_GEN);
70      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_UNKNOWN = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN);
71      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_ASL = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_ASL);
72      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_OASIS = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_OASIS);
73      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_W3CD = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_W3CD);
74      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_W3C = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_W3C);
75      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_DOJO = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_DOJO);
76      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_TMF = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY, RAT_LICENSE_FAMILY_CATEGORY_VALUE_TMF);
77      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_GPL1 = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY,RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL1);
78      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_GPL2 = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY,RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL2);
79      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_GPL3 = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY,RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL3);
80      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_MIT = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY,RAT_LICENSE_FAMILY_CATEGORY_VALUE_MIT);
81      public static final Datum RAT_LICENSE_FAMILY_CATEGORY_DATUM_CDLL1 = new Datum(RAT_URL_LICENSE_FAMILY_CATEGORY,RAT_LICENSE_FAMILY_CATEGORY_VALUE_CDDL1);
82  
83      // License Family Standard Names
84      public static final String RAT_URL_LICENSE_FAMILY_NAME= RAT_BASE_URL + "#LicenseFamilyName";
85      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_W3C_SOFTWARE_COPYRIGHT = "W3C Software Copyright";
86      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_W3C_DOCUMENT_COPYRIGHT = "W3C Document Copyright";
87      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_OASIS_OPEN_LICENSE = "OASIS Open License";
88      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_MODIFIED_BSD_LICENSE = "Modified BSD License";
89      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_APACHE_LICENSE_VERSION_2_0 = "Apache License Version 2.0";
90      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_GPL_VERSION_1 =
91              "GNU General Public License, version 1";
92      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_GPL_VERSION_2 =
93              "GNU General Public License, version 2";
94      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_GPL_VERSION_3 =
95              "GNU General Public License, version 3";
96      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_MIT =
97              "The MIT License";
98      public static final String RAT_LICENSE_FAMILY_NAME_VALUE_CDDL1 =
99              "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0";
100     public static final String RAT_LICENSE_FAMILY_NAME_VALUE_ACADEMIC_FREE_LICENSE_VERSION_2_1 = "Academic Free License, Version 2.1";
101     public static final String RAT_LICENSE_FAMILY_NAME_VALUE_UNKNOWN = "?????";
102     public static final Datum RAT_LICENSE_FAMILY_NAME_DATUM_W3C_SOFTWARE_COPYRIGHT 
103         = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_W3C_SOFTWARE_COPYRIGHT);
104     public static final Datum RAT_LICENSE_FAMILY_NAME_DATUM_W3C_DOCUMENT_COPYRIGHT 
105         = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_W3C_DOCUMENT_COPYRIGHT);
106     public static final Datum RAT_LICENSE_FAMILY_NAME_DATUM_OASIS_OPEN_LICENSE 
107         = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_OASIS_OPEN_LICENSE);
108     public static final Datum RAT_LICENSE_FAMILY_NAME_DATUM_MODIFIED_BSD_LICENSE 
109         = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_MODIFIED_BSD_LICENSE);
110     public static final Datum RAT_LICENSE_FAMILY_NAME_DATUM_APACHE_LICENSE_VERSION_2_0
111         = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_APACHE_LICENSE_VERSION_2_0);
112     public static final Datum
113             RAT_LICENSE_FAMILY_NAME_DATUM_GPL_VERSION_1 = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL1);
114     public static final Datum
115             RAT_LICENSE_FAMILY_NAME_DATUM_GPL_VERSION_2 = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_CATEGORY_VALUE_GPL2);
116     public static final Datum
117             RAT_LICENSE_FAMILY_NAME_DATUM_GPL_VERSION_3 = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_GPL_VERSION_3);
118     public static final Datum
119             RAT_LICENSE_FAMILY_NAME_DATUM_MIT = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_MIT);
120     public static final Datum
121             RAT_LICENSE_FAMILY_NAME_DATUM_CDDL1 = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_CDDL1);
122     public static final Datum RAT_LICENSE_FAMILY_NAME_DATUM_ACADEMIC_FREE_LICENSE_VERSION_2_1
123         = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_ACADEMIC_FREE_LICENSE_VERSION_2_1);
124     public static final Datum RAT_LICENSE_FAMILY_NAME_DATUM_UNKNOWN
125         = new Datum(RAT_URL_LICENSE_FAMILY_NAME, RAT_LICENSE_FAMILY_NAME_VALUE_UNKNOWN); 
126     
127     // Header sample
128     public static final String RAT_URL_HEADER_SAMPLE = RAT_BASE_URL + "#HeaderSample";
129     
130     // License Approval
131     public static final String RAT_URL_APPROVED_LICENSE = RAT_BASE_URL + "#ApprovedLicense";
132     public static final String RAT_APPROVED_LICENSE_VALUE_TRUE = Boolean.TRUE.toString();
133     public static final String RAT_APPROVED_LICENSE_VALUE_FALSE = Boolean.FALSE.toString();
134     public static final Datum RAT_APPROVED_LICENSE_DATIM_TRUE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_TRUE);
135     public static final Datum RAT_APPROVED_LICENSE_DATIM_FALSE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_FALSE);
136     
137     private ContentType contentType;
138     /** 
139      * Only likely to be a small quantity of data 
140      * so trade some performance for simplicity.
141      */
142     private final List<Datum> data;
143 
144     public MetaData() {
145         this(null);
146     }
147     
148     public MetaData(final ContentType contentType) {
149         this.contentType = contentType;
150         this.data = new ArrayList<Datum>(16);
151     }
152     
153     /**
154      * Gets the content type for the subject.
155      * @return or null when the type is unknown
156      */
157     public ContentType getContentType() {
158         return contentType;
159     }
160     
161     /**
162      * Sets the content type for this subject.
163      * @param contentType <code>ContentType</code>,
164      * or null when the content type is unknown
165      */
166     public void setContentType(final ContentType contentType) {
167         this.contentType = contentType;
168     }
169     
170     /**
171      * Gets all data.
172      * @return unmodifiable view of the meta data.
173      */
174     public Collection<Datum> getData() {
175         return Collections.unmodifiableCollection(data);
176     }
177     
178     /**
179      * Adds a new datum.
180      * Existing data with the same name are not replaced.
181      * @param datum datum to add.
182      * @see #set(org.apache.rat.api.MetaData.Datum)
183      */
184     public void add(final Datum datum) {
185        data.add(datum); 
186     }
187     
188     /**
189      * Puts in a new datum replacing any existing data.
190      * Any current data matching the name are removed.
191      * @param datum not null
192      * @see #add(org.apache.rat.api.MetaData.Datum)
193      */
194     public void set(final Datum datum) {
195        clear(datum.getName()); 
196        add(datum);
197     }
198     
199     /**
200      * Gets the first datum matching the given name.
201      * @param name not null
202      * @return the matching datum first added when there is any matching data,
203      * null otherwise
204      */
205     public Datum get(final String name) {
206         Datum result = null;
207         for (Datum next : data) {
208             if (name.equals(next.getName())) {
209                 result = next;
210                 break;
211             }
212         }
213         return result;
214     }
215     
216     /**
217      * Gets the value of the first datum matching the given name.
218      * @param name not null
219      * @return the value of the matchin datum first added when there is any matching data,
220      * null otherwise
221      */
222     public String value(final String name) {
223         final Datum datum = get(name);
224         final String result;
225         if (datum == null) {
226             result = null;
227         } else {
228             result = datum.getValue();
229         }
230         return result;
231     }
232     
233     /**
234      * Removes all data matching the given name.
235      * @param name not null
236      * @return true if any data match, false otherwise
237      */
238     public boolean clear(final String name) {
239         boolean dataRemoved = false;
240         for (final Iterator<Datum> it = data.iterator();it.hasNext();) {
241             final Datum datum = it.next();
242             if (datum.getName().equals(name)) {
243                 it.remove();
244                 dataRemoved = true;
245             }
246         }
247         return dataRemoved;
248     }
249     
250     /**
251      * Clears all data.
252      */
253     public void clear() {
254         data.clear();
255         this.contentType = null;
256     }
257     
258     /**
259      * A datum.
260      */
261     public static final class Datum {
262         private final String name;
263         private final String value;
264         
265         /**
266          * Constructs a datum.
267          * @param name not null
268          * @param value not null
269          */
270         public Datum(final String name, final String value) {
271             super();
272             this.name = name;
273             this.value = value;
274         }
275         
276         /**
277          * Gets the name of the data type.
278          * To avoid collisions, it is recommended that URLs are used.
279          * @return not null
280          */
281         public String getName() {
282             return name;
283         }
284      
285         /**
286          * Data type value.
287          * @return not null
288          */
289         public String getValue() {
290             return value;
291         }
292 
293         /**
294          * Constructs a <code>String</code> with all attributes
295          * in name = value format.
296          *
297          * @return a <code>String</code> representation 
298          * of this object.
299          */
300         @Override
301         public String toString()
302         {
303             return "Datum [ "
304                 + "name ='" + this.name + "',"
305                 + "value ='" + this.value + " "
306                 + "']";
307         }
308     }
309 }
310