2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.xml
.test
.tree
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.InputStream
;
21 import java
.io
.OutputStream
;
22 import java
.util
.HashMap
;
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
;
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
;
40 import org
.apache
.commons
.io
.IOUtils
;
41 import org
.apache
.commons
.logging
.Log
;
42 import org
.apache
.commons
.logging
.LogFactory
;
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
;
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);
53 private DocumentBuilder documentBuilder
= null;
55 private Resource xsltStyleSheet
;
57 private Templates templates
;
59 private Marshaller marshaller
;
61 private String outputDir
;
62 private String outputFileExtension
= "html";
64 private Boolean logXml
= false;
66 private Map
<String
, String
> xsltParameters
= new HashMap
<String
, String
>();
69 if (templates
!= null)
72 if (xsltStyleSheet
== null)
73 throw new SlcException("XSLT style sheet not specified.");
75 InputStream in
= null;
77 TransformerFactory transformerFactory
= TransformerFactory
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
);
85 IOUtils
.closeQuietly(in
);
89 public void resultPartAdded(TreeTestResult testResult
,
90 TestResultPart testResultPart
) {
94 public void close(TreeTestResult testResult
) {
95 if (templates
== null)
96 throw new SlcException("XSLT template not initialized");
98 File file
= getFile(testResult
);
99 OutputStream out
= null;
102 Transformer transformer
= templates
.newTransformer();
103 for (String paramKey
: xsltParameters
.keySet()) {
105 .setParameter(paramKey
, xsltParameters
.get(paramKey
));
106 if (log
.isTraceEnabled())
107 log
.trace("Set XSLT parameter " + paramKey
+ " to "
108 + xsltParameters
.get(paramKey
));
111 if (documentBuilder
== null)
112 documentBuilder
= DocumentBuilderFactory
.newInstance()
113 .newDocumentBuilder();
115 Document document
= documentBuilder
.newDocument();
116 DOMResult marshallResult
= new DOMResult(document
);
117 marshaller
.marshal(testResult
, marshallResult
);
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
);
128 DOMSource transfoSource
= new DOMSource(marshallResult
.getNode());
130 if (outputDir
!= null) {
131 File dir
= new File(outputDir
);
133 out
= new FileOutputStream(file
);
134 StreamResult outputResult
= new StreamResult(out
);
135 transformer
.transform(transfoSource
, outputResult
);
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
);
142 } catch (Exception e
) {
143 throw new SlcException(
144 "Could not transform test result to " + file
, e
);
146 IOUtils
.closeQuietly(out
);
150 public Resource
getXsltStyleSheet() {
151 return xsltStyleSheet
;
154 public void setXsltStyleSheet(Resource xsltStyleSheet
) {
155 this.xsltStyleSheet
= xsltStyleSheet
;
158 public void setTemplates(Templates templates
) {
159 this.templates
= templates
;
162 public void setMarshaller(Marshaller marshaller
) {
163 this.marshaller
= marshaller
;
166 public void setOutputDir(String outputDir
) {
167 this.outputDir
= outputDir
;
170 public void setOutputFileExtension(String outputFileExtension
) {
171 this.outputFileExtension
= outputFileExtension
;
174 protected File
getFile(TreeTestResult result
) {
175 Long time
= System
.currentTimeMillis();
176 return new File(outputDir
+ File
.separator
+ time
+ "-"
177 + result
.getUuid() + "." + outputFileExtension
);
180 public void setLogXml(Boolean logXml
) {
181 this.logXml
= logXml
;
184 public void setXsltParameters(Map
<String
, String
> xsltParameters
) {
185 this.xsltParameters
= xsltParameters
;