source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/XmlTransformationServiceImpl.java @ 4669

Last change on this file since 4669 was 4669, checked in by Twan Goosen, 10 years ago

Added CMDI view (obtained through XSLT transform) to record page

File size: 4.0 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.service.impl;
18
19import eu.clarin.cmdi.vlo.service.XmlTransformationService;
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.StringWriter;
23import java.net.URL;
24import java.util.Properties;
25import javax.xml.transform.Result;
26import javax.xml.transform.Source;
27import javax.xml.transform.Templates;
28import javax.xml.transform.Transformer;
29import javax.xml.transform.TransformerConfigurationException;
30import javax.xml.transform.TransformerException;
31import javax.xml.transform.TransformerFactory;
32import javax.xml.transform.stream.StreamResult;
33import javax.xml.transform.stream.StreamSource;
34import org.slf4j.Logger;
35import 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 */
45public 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}
Note: See TracBrowser for help on using the repository browser.