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  public class SimpleXmlClaimReporter extends AbstractReport {
33      private static final String RESOURCE = "resource";
34      private static final String LICENSE = "license";
35      private static final String APPROVAL = "approval";
36      private static final String FAMILY = "family";
37      private static final String NOTES = "notes";
38      private static final String SAMPLE = "sample";
39      private static final String TYPE = "type";
40      private static final String ID = "id";
41  
42      private final IXmlWriter writer;
43      private static final String NAME = "name";
44  
45      public SimpleXmlClaimReporter(final IXmlWriter writer) {
46          this.writer = writer;
47      }
48  
49      @Override
50      public void report(final Document subject) throws RatException {
51          try {
52              writeDocumentClaims(subject);
53          } catch (IOException e) {
54              throw new RatException("XML writing failure: " + e.getMessage() + " subject: " + subject, e);
55          }
56      }
57  
58      private void writeLicenseClaims(ILicense license, MetaData metaData) throws IOException {
59          writer.openElement(LICENSE).attribute(ID, license.getId()).attribute(NAME, license.getName())
60                  .attribute(APPROVAL, Boolean.valueOf(metaData.isApproved(license)).toString())
61                  .attribute(FAMILY, license.getLicenseFamily().getFamilyCategory());
62          if (StringUtils.isNotBlank(license.getNote())) {
63              writer.openElement(NOTES).cdata(license.getNote()).closeElement();
64          }
65          writer.closeElement();
66      }
67  
68      private void writeDocumentClaims(final Document subject) throws IOException {
69          final MetaData metaData = subject.getMetaData();
70          writer.openElement(RESOURCE).attribute(NAME, subject.getName()).attribute(TYPE,
71                  metaData.getDocumentType().toString());
72          for (Iterator<ILicense> iter = metaData.licenses().iterator(); iter.hasNext();) {
73              writeLicenseClaims(iter.next(), metaData);
74          }
75          writeHeaderSample(metaData);
76          writer.closeElement();
77      }
78  
79      private void writeHeaderSample(final MetaData metaData) throws IOException {
80          final String sample = metaData.getSampleHeader();
81          if (StringUtils.isNotBlank(sample)) {
82              writer.openElement(SAMPLE).cdata(sample).closeElement();
83          }
84      }
85  
86      @Override
87      public void startReport() throws RatException {
88      }
89  
90      @Override
91      public void endReport() throws RatException {
92      }
93  }