2ronwalf// The MIT License
2ronwalf//
2ronwalf// Copyright (c) 2004 Evren Sirin
2ronwalf//
2ronwalf// Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf// of this software and associated documentation files (the "Software"), to
2ronwalf// deal in the Software without restriction, including without limitation the
2ronwalf// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf// sell copies of the Software, and to permit persons to whom the Software is
2ronwalf// furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf// The above copyright notice and this permission notice shall be included in
2ronwalf// all copies or substantial portions of the Software.
2ronwalf//
2ronwalf// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf// IN THE SOFTWARE.
2ronwalf
2ronwalfpackage org.mindswap.utils;
2ronwalf
2ronwalfimport java.io.StringReader;
2ronwalfimport java.io.StringWriter;
2ronwalfimport java.util.Iterator;
2ronwalf
2ronwalfimport javax.xml.transform.Transformer;
2ronwalfimport javax.xml.transform.TransformerFactory;
2ronwalfimport javax.xml.transform.stream.StreamResult;
2ronwalfimport javax.xml.transform.stream.StreamSource;
2ronwalf
2ronwalfimport org.mindswap.owls.process.Parameter;
2ronwalfimport org.mindswap.query.ValueMap;
2ronwalf
2ronwalfpublic class XSLTEngine {
2ronwalf final static boolean DEBUG = false;
2ronwalf final static String header = "<?xml";
2ronwalf
2ronwalf public static String transform(String input, String xsltStylesheet) {
2ronwalf return transform(input, xsltStylesheet, new ValueMap() );
2ronwalf }
2ronwalf
2ronwalf public static String transform(String input, String xsltStylesheet, ValueMap parameters) {
2ronwalf String output = null;
2ronwalf
2ronwalf try {
2ronwalf StringWriter result = new StringWriter();
2ronwalf
2ronwalf if(DEBUG) {
2ronwalf System.out.println("Input " + input);
2ronwalf System.out.println("XSLT " + (xsltStylesheet==null) + " " +
2ronwalf xsltStylesheet.length() + " " + xsltStylesheet);
2ronwalf }
2ronwalf
2ronwalf if(xsltStylesheet == null)
2ronwalf return input;
2ronwalf
2ronwalf xsltStylesheet = xsltStylesheet.trim();
2ronwalf
2ronwalf TransformerFactory tFactory = TransformerFactory.newInstance();
2ronwalf
2ronwalf Transformer transformer = tFactory.newTransformer(
2ronwalf new StreamSource(new StringReader(xsltStylesheet)));
2ronwalf for(Iterator i = parameters.getVariables().iterator(); i.hasNext();) {
2ronwalf Parameter param = (Parameter) i.next();
2ronwalf String value = parameters.getStringValue( param );
2ronwalf transformer.setParameter( param.getLocalName(), value );
2ronwalf transformer.setParameter( param.getURI().toString(), value );
2ronwalf }
2ronwalf
2ronwalf transformer.transform(
2ronwalf new StreamSource(new StringReader(input)),
2ronwalf new StreamResult(result));
2ronwalf
2ronwalf output = result.toString().trim();
2ronwalf
2ronwalf if(output.startsWith(header)) {
2ronwalf int split = output.indexOf('>') + 1;
2ronwalf output = output.substring(split);
2ronwalf }
2ronwalf
2ronwalf if(DEBUG)
2ronwalf System.out.println("Output " + output);
2ronwalf } catch(Exception e) {
2ronwalf System.out.println("XSLT Engine cannot apply transformation " + e);
2ronwalf System.out.println("Input " + input);
2ronwalf System.out.println("XSLT " + xsltStylesheet);
2ronwalf }
2ronwalf
2ronwalf return output;
2ronwalf }
2ronwalf}