/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.qetest.xslwrapper; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Source; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import static jaxp.library.JAXPTestUtilities.filenameToURL; import org.w3c.dom.Document; import org.w3c.dom.DocumentFragment; import org.w3c.dom.Node; import org.xml.sax.InputSource; /** * Implementation of TransformWrapper that uses the TrAX API and uses DOMs for * it's sources. * * This implementation records separate times for xslRead (time to parse the xsl * and build a DOM) and xslBuild (time to take the DOMSource object until it's * built the templates); xmlRead (time to parse the xml and build a DOM). Note * xmlBuild is not timed since it's not easily measureable in TrAX. The * transform time is just the time to create the DOMResult object; the * resultsWrite is the separate time it takes to serialize that to disk. * * Important! The underlying System property of * javax.xml.transform.TransformerFactory will determine the actual TrAX * implementation used. This value will be reported out in our * getProcessorInfo() method. * */ public class TraxDOMWrapper extends TransformWrapperHelper { /** * TransformerFactory to use; constructed in newProcessor(). */ private TransformerFactory factory; /** * Templates to use for buildStylesheet(). */ private Templates builtTemplates; /** * Cached copy of newProcessor() properties. */ private Properties newProcessorOpts; /** * Get a general description of this wrapper itself. * * @return Uses TrAX to perform transforms from DOMSource(node) */ public String getDescription() { return "Uses TrAX to perform transforms from DOMSource(node)"; } /** * Get a specific description of the wrappered processor. * * @return specific description of the underlying processor or transformer * implementation: this should include both the general product name, as * well as specific version info. If possible, should be implemented without * actively creating an underlying processor. */ @Override public Properties getProcessorInfo() { Properties p = TraxWrapperUtils.getTraxInfo(); p.put("traxwrapper.method", "dom"); p.put("traxwrapper.desc", getDescription()); return p; } /** * Actually create/initialize an underlying processor or factory. * * For TrAX/javax.xml.transform implementations, this creates a new * TransformerFactory. For Xalan-J 1.x this creates an XSLTProcessor. Other * implementations may or may not actually do any work in this method. * * @param options Properties of options, unused. * * @return (Object)getProcessor() as a side-effect, this will be null if * there was any problem creating the processor OR if the underlying * implementation doesn't use this * @throws javax.xml.transform.TransformerConfigurationException when * actual implementation doesn't support StreamSource or StreamResult. */ @Override public TransformerFactory newProcessor(Properties options) throws TransformerConfigurationException { newProcessorOpts = options; reset(false); factory = TransformerFactory.newInstance(); // Verify the factory supports DOM! if (!(factory.getFeature(DOMSource.FEATURE) && factory.getFeature(DOMResult.FEATURE))) { throw new TransformerConfigurationException("TraxDOMWrapper.newProcessor: factory does not support DOM!"); } // Set any of our options as Attributes on the factory TraxWrapperUtils.setAttributes(factory, options); return factory; } /** * Transform supplied xmlName file with the stylesheet in the xslName file * into a resultName file. * * Names are assumed to be local path\filename references, and will be * converted to URLs as needed for any underlying processor implementation. * * @param xmlName local path\filename of XML file to transform * @param xslName local path\filename of XSL stylesheet to use * @param resultName local path\filename to put result in * * @throws Exception any underlying exceptions from the wrapped processor * are simply allowed to propagate; throws a RuntimeException if any other * problems prevent us from actually completing the operation */ @Override public void transform(String xmlName, String xslName, String resultName) throws Exception { preventFootShooting(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); // Timed: read xsl into a DOM Node xslNode = docBuilder.parse(new InputSource(filenameToURL(xslName))); // Untimed: create DOMSource and setSystemId DOMSource xslSource = new DOMSource(xslNode); xslSource.setSystemId(filenameToURL(xslName)); // Timed: build Transformer from DOMSource Transformer transformer = factory.newTransformer(xslSource); // Timed: read xml into a DOM Node xmlNode = docBuilder.parse(new InputSource(filenameToURL(xmlName))); // Untimed: create DOMSource and setSystemId DOMSource xmlSource = new DOMSource(xmlNode); xmlSource.setSystemId(filenameToURL(xmlName)); // Untimed: create DOMResult Document outDoc = docBuilder.newDocument(); DocumentFragment outNode = outDoc.createDocumentFragment(); DOMResult domResult = new DOMResult(outNode); // Untimed: Set any of our options as Attributes on the transformer TraxWrapperUtils.setAttributes(transformer, newProcessorOpts); // Untimed: Apply any parameters needed applyParameters(transformer); // Timed: build xml (so to speak) and transform transformer.transform(xmlSource, domResult); // Untimed: prepare serializer with outputProperties // from the stylesheet Transformer resultSerializer = factory.newTransformer(); Properties serializationProps = transformer.getOutputProperties(); resultSerializer.setOutputProperties(serializationProps); // Timed: writeResults from the DOMResult resultSerializer.transform(new DOMSource(outNode), new StreamResult(resultName)); } /** * Pre-build/pre-compile a stylesheet. * * Although the actual mechanics are implementation-dependent, most * processors have some method of pre-setting up the data needed by the * stylesheet itself for later use in transforms. In * TrAX/javax.xml.transform, this equates to creating a Templates object. * * Sets isStylesheetReady() to true if it succeeds. Users can then call * transformWithStylesheet(xmlName, resultName) to actually perform a * transformation with this pre-built stylesheet. * * @param xslName local path\filename of XSL stylesheet to use * * @throws Exception any underlying exceptions from the wrapped processor * are simply allowed to propagate; throws a RuntimeException if any other * problems prevent us from actually completing the operation * * @see #transformWithStylesheet(String xmlName, String resultName) */ @Override public void buildStylesheet(String xslName) throws Exception { preventFootShooting(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); // Read xsl into a DOM Node xslNode = docBuilder.parse(new InputSource(filenameToURL(xslName))); // Untimed: create DOMSource and setSystemId DOMSource xslSource = new DOMSource(xslNode); xslSource.setSystemId(filenameToURL(xslName)); // Build Templates from DOMSource builtTemplates = factory.newTemplates(xslSource); m_stylesheetReady = true; } /** * Transform supplied xmlName file with a pre-built/pre-compiled stylesheet * into a resultName file. * * User must have called buildStylesheet(xslName) beforehand, obviously. * Names are assumed to be local path\filename references, and will be * converted to URLs as needed. * * @param xmlName local path\filename of XML file to transform * @param resultName local path\filename to put result in * * @throws Exception any underlying exceptions from the wrappered processor * are simply allowed to propagate; throws a RuntimeException if any other * problems prevent us from actually completing the operation; throws an * IllegalStateException if isStylesheetReady() == false. * * @see #buildStylesheet(String xslName) */ @Override public void transformWithStylesheet(String xmlName, String resultName) throws Exception { if (!isStylesheetReady()) { throw new IllegalStateException("transformWithStylesheet() when isStylesheetReady() == false"); } preventFootShooting(); // Get Transformer from Templates Transformer transformer = builtTemplates.newTransformer(); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); // Read xml into a DOM Node xmlNode = docBuilder.parse(new InputSource(filenameToURL(xmlName))); // Create DOMSource and setSystemId DOMSource xmlSource = new DOMSource(xmlNode); xmlSource.setSystemId(filenameToURL(xmlName)); // Create DOMResult Document outNode = docBuilder.newDocument(); DOMResult domResult = new DOMResult(outNode); // Set any of our options as Attributes on the transformer TraxWrapperUtils.setAttributes(transformer, newProcessorOpts); // Apply any parameters needed applyParameters(transformer); // Build xml (so to speak) and transform transformer.transform(xmlSource, domResult); // Prepare serializer with outputProperties from the stylesheet Transformer resultSerializer = factory.newTransformer(); Properties serializationProps = transformer.getOutputProperties(); resultSerializer.setOutputProperties(serializationProps); // writeResults from the DOMResult resultSerializer.transform(new DOMSource(outNode), new StreamResult(resultName)); } /** * Transform supplied xmlName file with a stylesheet found in an * xml-stylesheet PI into a resultName file. * * Names are assumed to be local path\filename references, and will be * converted to URLs as needed. Implementations will use whatever facilities * exist in their wrapped processor to fetch and build the stylesheet to * use for the transform. * * @param xmlName local path\filename of XML file to transform * @param resultName local path\filename to put result in * * @throws Exception any underlying exceptions from the wrapped processor * are simply allowed to propagate; throws a RuntimeException if any other * problems prevent us from actually completing the operation */ @Override public void transformEmbedded(String xmlName, String resultName) throws Exception { DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); // read xml into a DOM Node xmlNode = docBuilder.parse(new InputSource(filenameToURL(xmlName))); // create DOMSource and setSystemId DOMSource xmlSource = new DOMSource(xmlNode); xmlSource.setSystemId(filenameToURL(xmlName)); // readxsl from the xml document Source xslSource = factory.getAssociatedStylesheet(xmlSource, null, null, null); // build Transformer from Source Transformer transformer = factory.newTransformer(xslSource); // create DOMResult Document outNode = docBuilder.newDocument(); DOMResult domResult = new DOMResult(outNode); // Set any of our options as Attributes on the transformer TraxWrapperUtils.setAttributes(transformer, newProcessorOpts); // Apply any parameters needed applyParameters(transformer); // Build xml (so to speak) and transform transformer.transform(xmlSource, domResult); // Prepare serializer with outputProperties from the stylesheet Transformer resultSerializer = factory.newTransformer(); Properties serializationProps = transformer.getOutputProperties(); resultSerializer.setOutputProperties(serializationProps); // writeResults from the DOMResult resultSerializer.transform(new DOMSource(outNode), new StreamResult(resultName)); } /** * Reset our parameters and wrapper state, and optionally force creation of * a new underlying processor implementation. * * This always clears our built stylesheet and any parameters that have been * set. If newProcessor is true, also forces a re-creation of our underlying * processor as if by calling newProcessor(). * * @param newProcessor if we should reset our underlying processor * implementation as well */ @Override public void reset(boolean newProcessor) { super.reset(newProcessor); // clears indent and parameters m_stylesheetReady = false; builtTemplates = null; if (newProcessor) { try { newProcessor(newProcessorOpts); } catch (TransformerConfigurationException ignore) {} } } /** * Apply a single parameter to a Transformer. * * Overridden to take a Transformer and call setParameter(). * * @param transformer a Transformer object. * @param namespace for the parameter, may be null * @param name for the parameter, should not be null * @param value for the parameter, may be null */ @Override protected void applyParameter(Transformer transformer, String namespace, String name, String value) { try { // Munge the namespace into the name per // javax.xml.transform.Transformer.setParameter() if (null != namespace) { name = "{" + namespace + "}" + name; } transformer.setParameter(name, value); } catch (Exception e) { throw new IllegalArgumentException("applyParameter threw: " + e.toString()); } } /** * Ensure newProcessor has been called when needed. * * Prevent users from shooting themselves in the foot by calling a * transform* API before newProcessor(). * * @throws java.lang.Exception */ public void preventFootShooting() throws Exception { if (null == factory) { newProcessor(newProcessorOpts); } } }