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.report.claim.impl.xml;
20  
21  import org.apache.commons.lang3.time.DateFormatUtils;
22  import org.apache.rat.api.Document;
23  import org.apache.rat.api.MetaData;
24  import org.apache.rat.api.RatException;
25  import org.apache.rat.report.AbstractReport;
26  import org.apache.rat.report.xml.writer.IXmlWriter;
27  import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
28  
29  import java.io.IOException;
30  import java.util.Calendar;
31  
32  public class SimpleXmlClaimReporter extends AbstractReport {
33      private static final String RAT_REPORT = "rat-report";
34      private static final String TIMESTAMP = "timestamp";
35      private static final String LICENSE_APPROVAL_PREDICATE = "license-approval";
36      private static final String LICENSE_FAMILY_PREDICATE = "license-family";
37      private static final String HEADER_SAMPLE_PREDICATE = "header-sample";
38      private static final String HEADER_TYPE_PREDICATE = "header-type";
39      private static final String FILE_TYPE_PREDICATE = "type";
40  
41      private final IXmlWriter writer;
42      private static final String NAME = "name";
43      private boolean firstTime = true;
44  
45      public SimpleXmlClaimReporter(final IXmlWriter writer) {
46          this.writer = writer;
47      }
48  
49  
50      /**
51       * Writes a single claim to the XML file.
52       * @param pPredicate The claims predicate.
53       * @param pObject The claims object.
54       * @param pLiteral Whether to write the object as an element (true),
55       *   or an attribute (false).
56       * @throws IOException An I/O error occurred while writing the claim.
57       * @throws RatException Another error occurred while writing the claim.
58       */
59      protected void writeClaim(String pPredicate, String pObject, boolean pLiteral)
60      throws IOException, RatException {
61          if (pLiteral) {
62              writer.openElement(pPredicate).content(pObject).closeElement();
63          } else {
64              writer.openElement(pPredicate).attribute(NAME, pObject).closeElement();
65          }
66      }
67  
68      @Override
69      public void report(final Document subject) throws RatException {
70          try {
71              if (firstTime) {
72                  firstTime = false;
73              } else {
74                  writer.closeElement();
75              }
76              writer.openElement("resource").attribute(NAME, subject.getName());
77              writeDocumentClaims(subject);
78          } catch (IOException e) {
79              throw new RatException("XML writing failure: " + e.getMessage()
80                      + " subject: " + subject, e);
81          }
82      }
83  
84      private void writeDocumentClaims(final Document subject) throws IOException, RatException {
85          final MetaData metaData = subject.getMetaData();
86          writeHeaderSample(metaData);
87          writeHeaderCategory(metaData);
88          writeLicenseFamilyName(metaData);
89          writeApprovedLicense(metaData);
90          writeDocumentCategory(metaData);
91      }
92  
93      private void writeApprovedLicense(final MetaData metaData) throws IOException, RatException {
94          final String approvedLicense = metaData.value(MetaData.RAT_URL_APPROVED_LICENSE);
95          if (approvedLicense != null) {
96              writeClaim(LICENSE_APPROVAL_PREDICATE, approvedLicense, false);
97          }
98      }
99  
100     private void writeLicenseFamilyName(final MetaData metaData) throws IOException, RatException {
101         final String licenseFamilyName = metaData.value(MetaData.RAT_URL_LICENSE_FAMILY_NAME);
102         if (licenseFamilyName != null) {
103             writeClaim(LICENSE_FAMILY_PREDICATE, licenseFamilyName, false);
104         }
105     }
106 
107     private void writeHeaderCategory(final MetaData metaData) throws IOException, RatException {
108         final String headerCategory = metaData.value(MetaData.RAT_URL_HEADER_CATEGORY);
109         if (headerCategory != null) {
110             writeClaim(HEADER_TYPE_PREDICATE, headerCategory, false);
111         }
112     }
113 
114     private void writeHeaderSample(final MetaData metaData) throws IOException, RatException {
115         final String sample = metaData.value(MetaData.RAT_URL_HEADER_SAMPLE);
116         if (sample != null) {
117             writeClaim(HEADER_SAMPLE_PREDICATE, sample, true);
118         }
119     }
120 
121     private void writeDocumentCategory(final MetaData metaData) throws IOException, RatException {
122         final String documentCategory = metaData.value(MetaData.RAT_URL_DOCUMENT_CATEGORY);
123         if (documentCategory != null) {
124             writeClaim(FILE_TYPE_PREDICATE, documentCategory, false);
125         }
126     }
127 
128     @Override
129     public void startReport() throws RatException {
130         try {
131             writer.openElement(RAT_REPORT)
132                 .attribute(TIMESTAMP,
133                            DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
134                            .format(Calendar.getInstance()));
135         } catch (IOException e) {
136             throw new RatException("Cannot open start element", e);
137         }
138     }
139 
140     @Override
141     public void endReport() throws RatException {
142         try {
143             writer.closeDocument();
144         } catch (IOException e) {
145             throw new RatException("Cannot close last element", e);
146         }
147     }
148 }