View Javadoc
1   /*
2    * Copyright 2015 Mark Prins, GeoDienstenCentrum
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Converts the xml report into a maven site html report.
37   * @author mprins
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  	 * Construct a configured instance of the report generator.
48  	 *
49  	 * @param sink (html) doxia sink to use
50  	 * @param description description for the report
51  	 * @param xmlFile input xml file to convert
52  	 * @param log maven log
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  	 * translate the xml report to the format of the sink (html).
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  	 * translate the xml using xslt.
80  	 * @return the transformed xml as string
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  			// error in the XSL file
98  			log.error("Error during xml conversion of " + this.xmlFile, e);
99  		} catch (TransformerException e) {
100 			// error while applying the XSL file
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 }