]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.support.castor/src/main/java/org/argeo/slc/xml/test/tree/XsltReportGenerator.java
0a99356dfc299cf414942b1b43485f7aa75b8026
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.support.castor / src / main / java / org / argeo / slc / xml / test / tree / XsltReportGenerator.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 org.argeo.slc.xml.test.tree;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.transform.Templates;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMResult;
31 import javax.xml.transform.dom.DOMSource;
32 import javax.xml.transform.stream.StreamResult;
33 import javax.xml.transform.stream.StreamSource;
34
35 import org.springframework.core.io.Resource;
36 import org.springframework.oxm.Marshaller;
37 import org.springframework.xml.transform.StringResult;
38 import org.w3c.dom.Document;
39
40 import org.apache.commons.io.IOUtils;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 import org.argeo.slc.SlcException;
45 import org.argeo.slc.core.test.tree.TreeTestResult;
46 import org.argeo.slc.test.TestResultListener;
47 import org.argeo.slc.test.TestResultPart;
48
49 /** Build a report based on a tree test result using an XSLT stylesheet. */
50 public class XsltReportGenerator implements TestResultListener<TreeTestResult> {
51 private final static Log log = LogFactory.getLog(XsltReportGenerator.class);
52
53 private DocumentBuilder documentBuilder = null;
54
55 private Resource xsltStyleSheet;
56
57 private Templates templates;
58
59 private Marshaller marshaller;
60
61 private String outputDir;
62 private String outputFileExtension = "html";
63
64 private Boolean logXml = false;
65
66 private Map<String, String> xsltParameters = new HashMap<String, String>();
67
68 public void init() {
69 if (templates != null)
70 return;
71
72 if (xsltStyleSheet == null)
73 throw new SlcException("XSLT style sheet not specified.");
74
75 InputStream in = null;
76 try {
77 TransformerFactory transformerFactory = TransformerFactory
78 .newInstance();
79 in = xsltStyleSheet.getInputStream();
80 StreamSource xsltSource = new StreamSource(in);
81 templates = transformerFactory.newTemplates(xsltSource);
82 } catch (Exception e) {
83 throw new SlcException("Could not initialize templates", e);
84 } finally {
85 IOUtils.closeQuietly(in);
86 }
87 }
88
89 public void resultPartAdded(TreeTestResult testResult,
90 TestResultPart testResultPart) {
91
92 }
93
94 public void close(TreeTestResult testResult) {
95 if (templates == null)
96 throw new SlcException("XSLT template not initialized");
97
98 File file = getFile(testResult);
99 OutputStream out = null;
100
101 try {
102 Transformer transformer = templates.newTransformer();
103 for (String paramKey : xsltParameters.keySet()) {
104 transformer
105 .setParameter(paramKey, xsltParameters.get(paramKey));
106 if (log.isTraceEnabled())
107 log.trace("Set XSLT parameter " + paramKey + " to "
108 + xsltParameters.get(paramKey));
109 }
110
111 if (documentBuilder == null)
112 documentBuilder = DocumentBuilderFactory.newInstance()
113 .newDocumentBuilder();
114
115 Document document = documentBuilder.newDocument();
116 DOMResult marshallResult = new DOMResult(document);
117 marshaller.marshal(testResult, marshallResult);
118
119 if (logXml) {
120 Transformer identityTransformer = TransformerFactory
121 .newInstance().newTransformer();
122 StringResult xmlResult = new StringResult();
123 identityTransformer.transform(new DOMSource(marshallResult
124 .getNode()), xmlResult);
125 log.info("Marshalled XML:\n" + xmlResult);
126 }
127
128 DOMSource transfoSource = new DOMSource(marshallResult.getNode());
129
130 if (outputDir != null) {
131 File dir = new File(outputDir);
132 dir.mkdirs();
133 out = new FileOutputStream(file);
134 StreamResult outputResult = new StreamResult(out);
135 transformer.transform(transfoSource, outputResult);
136 } else {
137 // print on console if no output dir
138 StringResult result = new StringResult();
139 transformer.transform(transfoSource, result);
140 log.info("Generated report:\n" + result);
141 }
142 } catch (Exception e) {
143 throw new SlcException(
144 "Could not transform test result to " + file, e);
145 } finally {
146 IOUtils.closeQuietly(out);
147 }
148 }
149
150 public Resource getXsltStyleSheet() {
151 return xsltStyleSheet;
152 }
153
154 public void setXsltStyleSheet(Resource xsltStyleSheet) {
155 this.xsltStyleSheet = xsltStyleSheet;
156 }
157
158 public void setTemplates(Templates templates) {
159 this.templates = templates;
160 }
161
162 public void setMarshaller(Marshaller marshaller) {
163 this.marshaller = marshaller;
164 }
165
166 public void setOutputDir(String outputDir) {
167 this.outputDir = outputDir;
168 }
169
170 public void setOutputFileExtension(String outputFileExtension) {
171 this.outputFileExtension = outputFileExtension;
172 }
173
174 protected File getFile(TreeTestResult result) {
175 Long time = System.currentTimeMillis();
176 return new File(outputDir + File.separator + time + "-"
177 + result.getUuid() + "." + outputFileExtension);
178 }
179
180 public void setLogXml(Boolean logXml) {
181 this.logXml = logXml;
182 }
183
184 public void setXsltParameters(Map<String, String> xsltParameters) {
185 this.xsltParameters = xsltParameters;
186 }
187
188 }