1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.service.impl; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.service.XmlTransformationService; |
---|
20 | import java.io.IOException; |
---|
21 | import java.io.InputStream; |
---|
22 | import java.io.StringWriter; |
---|
23 | import java.net.URL; |
---|
24 | import java.util.Properties; |
---|
25 | import javax.xml.transform.Result; |
---|
26 | import javax.xml.transform.Source; |
---|
27 | import javax.xml.transform.Templates; |
---|
28 | import javax.xml.transform.Transformer; |
---|
29 | import javax.xml.transform.TransformerConfigurationException; |
---|
30 | import javax.xml.transform.TransformerException; |
---|
31 | import javax.xml.transform.TransformerFactory; |
---|
32 | import javax.xml.transform.stream.StreamResult; |
---|
33 | import javax.xml.transform.stream.StreamSource; |
---|
34 | import org.slf4j.Logger; |
---|
35 | import org.slf4j.LoggerFactory; |
---|
36 | |
---|
37 | /** |
---|
38 | * Transforms input documents to a fixed stylesheet (determined on construction) |
---|
39 | * using JAXP XSLT support. |
---|
40 | * |
---|
41 | * Thread-safe |
---|
42 | * |
---|
43 | * @author twagoo |
---|
44 | */ |
---|
45 | public class XmlTransformationServiceImpl implements XmlTransformationService { |
---|
46 | |
---|
47 | private final static Logger logger = LoggerFactory.getLogger(XmlTransformationServiceImpl.class); |
---|
48 | |
---|
49 | private final Templates template; |
---|
50 | private final Properties transformationProperties; |
---|
51 | |
---|
52 | /** |
---|
53 | * |
---|
54 | * @param xsltSource source of the stylesheet to use in {@link #transformXml(java.net.URL) |
---|
55 | * } |
---|
56 | * @ |
---|
57 | * param properties transformation properties to be passed to the |
---|
58 | * transformer object before transformation |
---|
59 | * @throws TransformerConfigurationException if the transformer could not be |
---|
60 | * configured correctly |
---|
61 | */ |
---|
62 | public XmlTransformationServiceImpl(Source xsltSource, Properties properties) throws TransformerConfigurationException { |
---|
63 | final TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
---|
64 | // create a template to derive transformers from, which makes this thread safe |
---|
65 | this.template = transformerFactory.newTemplates(xsltSource); |
---|
66 | this.transformationProperties = properties; |
---|
67 | |
---|
68 | logger.debug("Transformation service created for {} with properties {}", xsltSource, properties); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * |
---|
73 | * @param location location of XML document to transform (should not be |
---|
74 | * null) |
---|
75 | * @return the result of the XML transformation as a string |
---|
76 | * @throws TransformerException If an unrecoverable error occurs during the |
---|
77 | * course of the transformation or while opening the input stream. |
---|
78 | */ |
---|
79 | @Override |
---|
80 | public String transformXml(URL location) throws TransformerException { |
---|
81 | logger.debug("Transforming {}", location); |
---|
82 | |
---|
83 | // create a transformer based on the template |
---|
84 | final Transformer transformer = template.newTransformer(); |
---|
85 | transformer.setOutputProperties(transformationProperties); |
---|
86 | |
---|
87 | try { |
---|
88 | final InputStream inStream = location.openStream(); |
---|
89 | final StringWriter outWriter = new StringWriter(); |
---|
90 | |
---|
91 | // make input/output objects |
---|
92 | final Source source = new StreamSource(inStream); |
---|
93 | final Result result = new StreamResult(outWriter); |
---|
94 | |
---|
95 | // perform actual transformation (this will also close the streams) |
---|
96 | transformer.transform(source, result); |
---|
97 | |
---|
98 | // result has been written to string writer, return contents |
---|
99 | return outWriter.toString(); |
---|
100 | } catch (IOException ex) { |
---|
101 | throw new TransformerException("Error while opening input stream for " + location.toString(), ex); |
---|
102 | } |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | } |
---|