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  
20  package org.apache.rat.report.claim.impl;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.rat.api.Document;
26  import org.apache.rat.api.RatException;
27  import org.apache.rat.api.MetaData;
28  import org.apache.rat.report.claim.ClaimStatistic;
29  
30  
31  /**
32   * The aggregator is used to create a numerical statistic
33   * of claims.
34   */
35  public class ClaimAggregator extends AbstractClaimReporter {
36      private final ClaimStatistic statistic;
37      private final Map numsByLicenseFamilyName = new HashMap();
38      private final Map numsByLicenseFamilyCode = new HashMap();
39      private final Map numsByFileType = new HashMap();
40      private int numApproved, numUnApproved, numGenerated, numUnknown;
41  
42      public ClaimAggregator(ClaimStatistic pStatistic) {
43          statistic = pStatistic;
44      }
45      
46      private void incMapValue(Map pMap, Object pKey) {
47          final Integer num = (Integer) pMap.get(pKey);
48          final int newNum;
49          if (num == null) {
50              newNum = 1;
51          } else {
52              newNum = num.intValue() + 1;
53          }
54          pMap.put(pKey, new Integer(newNum));
55      }
56      
57      protected void handleDocumentCategoryClaim(String documentCategoryName) {
58          incMapValue(numsByFileType, documentCategoryName);
59      }
60  
61      protected void handleApprovedLicenseClaim(String licenseApproved) {
62          if (MetaData.RAT_APPROVED_LICENSE_VALUE_TRUE.equals(licenseApproved)) {
63              numApproved++;
64          } else {
65              numUnApproved++;
66          }
67      }
68  
69      protected void handleLicenseFamilyNameClaim(String licenseFamilyName) {
70          incMapValue(numsByLicenseFamilyName, licenseFamilyName);
71      }
72  
73      protected void handleHeaderCategoryClaim(String headerCategory) {
74          
75          if (MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_GEN.equals(headerCategory)) {
76              numGenerated++;
77              incMapValue(numsByLicenseFamilyCode, MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_GEN);
78          } else if (MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN.equals(headerCategory)) {
79              numUnknown++;
80              incMapValue(numsByLicenseFamilyCode, MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN);
81          }
82      }
83  
84      public void fillClaimStatistic(ClaimStatistic pStatistic) {
85          pStatistic.setDocumentCategoryMap(numsByFileType);
86          pStatistic.setLicenseFileCodeMap(numsByLicenseFamilyCode);
87          pStatistic.setLicenseFileNameMap(numsByLicenseFamilyName);
88          pStatistic.setNumApproved(numApproved);
89          pStatistic.setNumGenerated(numGenerated);
90          pStatistic.setNumUnApproved(numUnApproved);
91          pStatistic.setNumUnknown(numUnknown);
92      }
93  
94      public void endReport() throws RatException {
95          super.endReport();
96          fillClaimStatistic(statistic);
97      }
98  }