1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.report.xml;
20
21 import java.io.IOException;
22 import java.util.Calendar;
23 import java.util.Locale;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.commons.lang3.time.DateFormatUtils;
27 import org.apache.rat.VersionInfo;
28 import org.apache.rat.api.Document;
29 import org.apache.rat.api.MetaData;
30 import org.apache.rat.api.RatException;
31 import org.apache.rat.config.results.ClaimValidator;
32 import org.apache.rat.license.ILicense;
33 import org.apache.rat.report.claim.ClaimStatistic;
34 import org.apache.rat.report.xml.writer.XmlWriter;
35 import org.apache.rat.utils.CasedString;
36
37
38
39
40 public final class XmlElements {
41 private XmlElements() {
42
43 }
44
45
46
47
48
49
50
51 private static String normalizeName(final String name) {
52 CasedString casedName = new CasedString(CasedString.StringCase.SNAKE, name.toLowerCase(Locale.ROOT));
53 return casedName.toCase(CasedString.StringCase.PASCAL);
54 }
55
56
57
58
59
60
61
62 public static void ratReport(final XmlWriter writer) throws RatException {
63 try {
64 writer.startElement(Elements.RAT_REPORT.elementName)
65 .attribute(Attributes.TIMESTAMP.attributeName(),
66 DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format(Calendar.getInstance()));
67 version(writer);
68 } catch (IOException e) {
69 throw new RatException(e);
70 }
71 }
72
73
74
75
76
77
78 public static void version(final XmlWriter writer) throws RatException {
79 VersionInfo versionInfo = new VersionInfo();
80 try {
81 writer.startElement(Elements.VERSION.elementName)
82 .attribute(Attributes.PRODUCT.attributeName(), versionInfo.getTitle())
83 .attribute(Attributes.VENDOR.attributeName(), versionInfo.getVendor())
84 .attribute(Attributes.VERSION.attributeName(), versionInfo.getVersion())
85 .closeElement();
86 } catch (IOException e) {
87 throw new RatException(e);
88 }
89 }
90
91
92
93
94
95
96
97
98 public static void license(final XmlWriter writer, final ILicense license, final boolean approved) throws RatException {
99 try {
100 writer.startElement(Elements.LICENSE.elementName)
101 .attribute(Attributes.ID.attributeName(), license.getId())
102 .attribute(Attributes.NAME.attributeName(), license.getName())
103 .attribute(Attributes.APPROVAL.attributeName(), Boolean.toString(approved))
104 .attribute(Attributes.FAMILY.attributeName(), license.getLicenseFamily().getFamilyCategory());
105
106 if (StringUtils.isNotBlank(license.getNote())) {
107 writer.startElement(Elements.NOTES.elementName).cdata(license.getNote()).closeElement();
108 }
109 writer.closeElement();
110 } catch (IOException e) {
111 throw new RatException(e);
112 }
113 }
114
115
116
117
118
119
120
121 public static void document(final XmlWriter writer, final Document document) throws RatException {
122 final MetaData metaData = document.getMetaData();
123 try {
124 writer.startElement(Elements.RESOURCE.elementName)
125 .attribute(Attributes.NAME.attributeName(), document.getName().localized("/"))
126 .attribute(Attributes.TYPE.attributeName(), metaData.getDocumentType().toString())
127 .attribute(Attributes.MEDIA_TYPE.attributeName(), metaData.getMediaType().toString());
128 if (Document.Type.STANDARD == metaData.getDocumentType() || metaData.hasCharset()) {
129 writer.attribute(Attributes.ENCODING.attributeName(), metaData.getCharset().displayName());
130 }
131 if (document.isIgnored()) {
132 writer.attribute(Attributes.IS_DIRECTORY.attributeName(), Boolean.toString(document.isDirectory()));
133 }
134 } catch (IOException e) {
135 throw new RatException(e);
136 }
137 }
138
139
140
141
142
143
144 public static void statistics(final XmlWriter writer, final ClaimStatistic statistic, final ClaimValidator validator) throws RatException {
145 try {
146 writer.startElement(Elements.STATISTICS.elementName);
147 for (ClaimStatistic.Counter counter : ClaimStatistic.Counter.values()) {
148 int count = statistic.getCounter(counter);
149 XmlElements.statistic(writer, counter.displayName(), count, counter.getDescription(), validator.isValid(counter, count));
150 }
151 for (String category : statistic.getLicenseFamilyCategories()) {
152 XmlElements.licenseCategory(writer, category, statistic.getLicenseCategoryCount(category));
153 }
154 for (String category : statistic.getLicenseNames()) {
155 XmlElements.licenseName(writer, category, statistic.getLicenseNameCount(category));
156 }
157 for (Document.Type type : statistic.getDocumentTypes()) {
158 XmlElements.documentType(writer, type.name(), statistic.getCounter(type));
159 }
160
161 writer.closeElement();
162 } catch (IOException e) {
163 throw new RatException(e);
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176 public static void statistic(final XmlWriter writer, final String name, final int count, final String description, final boolean isOk) throws RatException {
177 try {
178 writer.startElement(Elements.STATISTIC.elementName)
179 .attribute(Attributes.NAME.attributeName(), name)
180 .attribute(Attributes.COUNT.attributeName(), Integer.toString(count))
181 .attribute(Attributes.APPROVAL.attributeName(), Boolean.toString(isOk))
182 .attribute(Attributes.DESCRIPTION.attributeName(), description)
183 .closeElement();
184 } catch (IOException e) {
185 throw new RatException(e);
186 }
187 }
188
189
190
191
192
193
194
195
196 public static void licenseCategory(final XmlWriter writer, final String name, final int count) throws RatException {
197 try {
198 writer.startElement(Elements.LICENSE_CATEGORY.elementName)
199 .attribute(Attributes.NAME.attributeName(), name)
200 .attribute(Attributes.COUNT.attributeName(), Integer.toString(count))
201 .closeElement();
202 } catch (IOException e) {
203 throw new RatException(e);
204 }
205 }
206
207
208
209
210
211
212
213
214 public static void licenseName(final XmlWriter writer, final String name, final int count) throws RatException {
215 try {
216 writer.startElement(Elements.LICENSE_NAME.elementName)
217 .attribute(Attributes.NAME.attributeName(), name)
218 .attribute(Attributes.COUNT.attributeName(), Integer.toString(count))
219 .closeElement();
220 } catch (IOException e) {
221 throw new RatException(e);
222 }
223 }
224
225
226
227
228
229
230
231
232 public static void documentType(final XmlWriter writer, final String name, final int count) throws RatException {
233 try {
234 writer.startElement(Elements.DOCUMENT_TYPE.elementName)
235 .attribute(Attributes.NAME.attributeName(), name)
236 .attribute(Attributes.COUNT.attributeName(), Integer.toString(count))
237 .closeElement();
238 } catch (IOException e) {
239 throw new RatException(e);
240 }
241 }
242
243
244
245
246 public enum Elements {
247
248
249
250 RAT_REPORT("rat-report"),
251
252
253
254 VERSION(),
255
256
257
258 RESOURCE(),
259
260
261
262 LICENSE(),
263
264
265
266 NOTES(),
267
268
269
270 STATISTICS(),
271
272
273
274 STATISTIC(),
275
276
277
278 LICENSE_NAME(),
279
280
281
282 LICENSE_CATEGORY(),
283
284
285
286 DOCUMENT_TYPE();
287
288
289
290
291 private final String elementName;
292
293
294
295
296
297
298 Elements(final String elementName) {
299 this.elementName = elementName;
300 }
301
302 Elements() {
303 this.elementName = normalizeName(name());
304 }
305 }
306
307
308
309
310 public enum Attributes {
311
312
313
314 TIMESTAMP,
315
316
317
318 VERSION,
319
320
321
322 PRODUCT,
323
324
325
326 VENDOR,
327
328
329
330 APPROVAL,
331
332
333
334 FAMILY,
335
336
337
338 TYPE,
339
340
341
342 ID,
343
344
345
346 NAME,
347
348
349
350 COUNT,
351
352
353
354 DESCRIPTION,
355
356
357
358 MEDIA_TYPE,
359
360
361
362 ENCODING,
363
364
365
366 IS_DIRECTORY;
367
368 String attributeName() {
369 return normalizeName(this.name());
370 }
371 }
372 }