1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
50
51
52
53
54
55
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 }