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