]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.castor/src/main/java/org/argeo/slc/xml/test/tree/XsltReportGenerator.java
Start preparing OSGi runtime
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.castor / src / main / java / org / argeo / slc / xml / test / tree / XsltReportGenerator.java
1 package org.argeo.slc.xml.test.tree;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.transform.Templates;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerFactory;
13 import javax.xml.transform.dom.DOMResult;
14 import javax.xml.transform.dom.DOMSource;
15 import javax.xml.transform.stream.StreamResult;
16 import javax.xml.transform.stream.StreamSource;
17
18 import org.springframework.core.io.Resource;
19 import org.springframework.oxm.Marshaller;
20 import org.springframework.xml.transform.StringResult;
21 import org.w3c.dom.Document;
22
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.argeo.slc.SlcException;
28 import org.argeo.slc.core.test.tree.TreeTestResult;
29 import org.argeo.slc.test.TestResultListener;
30 import org.argeo.slc.test.TestResultPart;
31
32 public class XsltReportGenerator implements TestResultListener<TreeTestResult> {
33 private Log log = LogFactory.getLog(getClass());
34
35 private DocumentBuilder documentBuilder = null;
36
37 private Resource xsltStyleSheet;
38
39 private Templates templates;
40
41 private Marshaller marshaller;
42
43 private String outputDir;
44 private String outputFileExtension = "html";
45
46 private Boolean logXml = false;
47
48 public void init() {
49 if (templates != null)
50 return;
51
52 if (xsltStyleSheet == null)
53 throw new SlcException("XSLT style sheet not specified.");
54
55 InputStream in = null;
56 try {
57 TransformerFactory transformerFactory = TransformerFactory
58 .newInstance();
59 in = xsltStyleSheet.getInputStream();
60 StreamSource xsltSource = new StreamSource(in);
61 templates = transformerFactory.newTemplates(xsltSource);
62 } catch (Exception e) {
63 throw new SlcException("Could not initialize templates", e);
64 } finally {
65 IOUtils.closeQuietly(in);
66 }
67 }
68
69 public void resultPartAdded(TreeTestResult testResult,
70 TestResultPart testResultPart) {
71
72 }
73
74 public void close(TreeTestResult testResult) {
75 if (templates == null)
76 throw new SlcException("XSLT template not initialized");
77
78 File file = getFile(testResult);
79 OutputStream out = null;
80
81 try {
82 Transformer transformer = templates.newTransformer();
83
84 if (documentBuilder == null)
85 documentBuilder = DocumentBuilderFactory.newInstance()
86 .newDocumentBuilder();
87
88 Document document = documentBuilder.newDocument();
89 DOMResult marshallResult = new DOMResult(document);
90 marshaller.marshal(testResult, marshallResult);
91
92 if (logXml) {
93 Transformer identityTransformer = TransformerFactory
94 .newInstance().newTransformer();
95 StringResult xmlResult = new StringResult();
96 identityTransformer.transform(new DOMSource(marshallResult
97 .getNode()), xmlResult);
98 log.info("Marshalled XML:\n" + xmlResult);
99 }
100
101 DOMSource transfoSource = new DOMSource(marshallResult.getNode());
102
103 if (outputDir != null) {
104 File dir = new File(outputDir);
105 dir.mkdirs();
106 out = new FileOutputStream(file);
107 StreamResult outputResult = new StreamResult(out);
108 transformer.transform(transfoSource, outputResult);
109 } else {
110 // print on console if no output dir
111 StringResult result = new StringResult();
112 transformer.transform(transfoSource, result);
113 log.info("Generated report:\n" + result);
114 }
115 } catch (Exception e) {
116 throw new SlcException(
117 "Could not transform test result to " + file, e);
118 } finally {
119 IOUtils.closeQuietly(out);
120 }
121 }
122
123 public Resource getXsltStyleSheet() {
124 return xsltStyleSheet;
125 }
126
127 public void setXsltStyleSheet(Resource xsltStyleSheet) {
128 this.xsltStyleSheet = xsltStyleSheet;
129 }
130
131 public void setTemplates(Templates templates) {
132 this.templates = templates;
133 }
134
135 public void setMarshaller(Marshaller marshaller) {
136 this.marshaller = marshaller;
137 }
138
139 public void setOutputDir(String outputDir) {
140 this.outputDir = outputDir;
141 }
142
143 public void setOutputFileExtension(String outputFileExtension) {
144 this.outputFileExtension = outputFileExtension;
145 }
146
147 protected File getFile(TreeTestResult result) {
148 Long time = System.currentTimeMillis();
149 return new File(outputDir + File.separator + time + "-"
150 + result.getUuid() + "." + outputFileExtension);
151 }
152
153 public void setLogXml(Boolean logXml) {
154 this.logXml = logXml;
155 }
156
157 }