1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.geodienstencentrum.maven.plugin.sass.report;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.StringWriter;
22 import javax.xml.transform.Result;
23 import javax.xml.transform.Source;
24 import javax.xml.transform.SourceLocator;
25 import javax.xml.transform.Templates;
26 import javax.xml.transform.Transformer;
27 import javax.xml.transform.TransformerConfigurationException;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.stream.StreamResult;
31 import javax.xml.transform.stream.StreamSource;
32 import org.apache.maven.doxia.sink.Sink;
33 import org.apache.maven.plugin.logging.Log;
34
35
36
37
38
39 public class SCSSLintReportGenerator {
40
41 private final Sink sink;
42 private final String description;
43 private final File xmlFile;
44 private final Log log;
45
46
47
48
49
50
51
52
53
54 public SCSSLintReportGenerator(final Sink sink, final String description,
55 final File xmlFile, final Log log) {
56 this.sink = sink;
57 this.description = description;
58 this.xmlFile = xmlFile;
59 this.log = log;
60 }
61
62
63
64
65 public void generateReport() {
66 sink.head();
67 sink.title();
68 sink.text(description);
69 sink.title_();
70 sink.head_();
71 sink.body();
72 sink.rawText(this.translateXML());
73 sink.body_();
74 sink.flush();
75 sink.close();
76 }
77
78
79
80
81
82 private String translateXML() {
83 String translated = null;
84 try {
85 final TransformerFactory factory = TransformerFactory.newInstance();
86 final Templates template = factory.newTemplates(
87 new StreamSource(
88 this.getClass().getClassLoader().getResourceAsStream("scss-report.xsl")));
89 final Transformer xformer = template.newTransformer();
90 log.info("Transforming scss-lint xml results file: " + xmlFile.getAbsolutePath());
91 final Source source = new StreamSource(new FileInputStream(this.xmlFile));
92 final StringWriter outWriter = new StringWriter();
93 final Result result = new StreamResult(outWriter);
94 xformer.transform(source, result);
95 translated = outWriter.toString();
96 } catch (FileNotFoundException | TransformerConfigurationException e) {
97
98 log.error("Error during xml conversion of " + this.xmlFile, e);
99 } catch (TransformerException e) {
100
101 final SourceLocator locator = e.getLocator();
102 final int col = locator.getColumnNumber();
103 final int line = locator.getLineNumber();
104 log.error("Error during xml transformation of " + this.xmlFile
105 + "line: " + line + ", col: " + col, e);
106 }
107
108 log.debug("Transformed scss-lint xml:\n" + translated);
109 return translated;
110 }
111 }