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