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