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  import java.util.Iterator;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.rat.api.Document;
26  import org.apache.rat.api.MetaData;
27  import org.apache.rat.api.RatException;
28  import org.apache.rat.license.ILicense;
29  import org.apache.rat.report.AbstractReport;
30  import org.apache.rat.report.xml.writer.IXmlWriter;
31  
32  /**
33   * A claim reporter to write the XML document.
34   */
35  public class SimpleXmlClaimReporter extends AbstractReport {
36      /** the resource element name */
37      private static final String RESOURCE = "resource";
38      /** the license element name */
39      private static final String LICENSE = "license";
40      /** the approval attribute name */
41      private static final String APPROVAL = "approval";
42      /** the family attribute name */
43      private static final String FAMILY = "family";
44      /** the notes attribute name */
45      private static final String NOTES = "notes";
46      /** the sample attribute name */
47      private static final String SAMPLE = "sample";
48      /** the type attribute name */
49      private static final String TYPE = "type";
50      /** the ID attribute name */
51      private static final String ID = "id";
52      /** name attribute name */
53      private static final String NAME = "name";
54  
55      /** the writer to write to */
56      private final IXmlWriter writer;
57  
58      /**
59       * Constructor.
60       * @param writer The writer to write the report to.
61       */
62      public SimpleXmlClaimReporter(final IXmlWriter writer) {
63          this.writer = writer;
64      }
65  
66      @Override
67      public void report(final Document subject) throws RatException {
68          try {
69              writeDocumentClaims(subject);
70          } catch (IOException e) {
71              throw new RatException("XML writing failure: " + e.getMessage() + " subject: " + subject, e);
72          }
73      }
74  
75      private void writeLicenseClaims(final ILicense license, final MetaData metaData) throws IOException {
76          writer.openElement(LICENSE).attribute(ID, license.getId()).attribute(NAME, license.getName())
77                  .attribute(APPROVAL, Boolean.valueOf(metaData.isApproved(license)).toString())
78                  .attribute(FAMILY, license.getLicenseFamily().getFamilyCategory());
79          if (StringUtils.isNotBlank(license.getNote())) {
80              writer.openElement(NOTES).cdata(license.getNote()).closeElement();
81          }
82          writer.closeElement();
83      }
84  
85      private void writeDocumentClaims(final Document document) throws IOException {
86          final MetaData metaData = document.getMetaData();
87          writer.openElement(RESOURCE).attribute(NAME, document.getName().localized("/")).attribute(TYPE,
88                  metaData.getDocumentType().toString());
89          for (Iterator<ILicense> iter = metaData.licenses().iterator(); iter.hasNext();) {
90              writeLicenseClaims(iter.next(), metaData);
91          }
92          writeHeaderSample(metaData);
93          writer.closeElement();
94      }
95  
96      private void writeHeaderSample(final MetaData metaData) throws IOException {
97          final String sample = metaData.getSampleHeader();
98          if (StringUtils.isNotBlank(sample)) {
99              writer.openElement(SAMPLE).cdata(sample).closeElement();
100         }
101     }
102 
103     @Override
104     public void startReport() {
105         // does nothing
106     }
107 
108     @Override
109     public void endReport() {
110         // does nothing
111     }
112 }