org.custommonkey.xmlunit.Transform Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xmlunit-legacy Show documentation
Show all versions of xmlunit-legacy Show documentation
XMLUnit 1.x Compatibility Layer
The newest version!
/*
******************************************************************
Copyright (c) 2001-2007,2015,2022 Jeff Martin, Tim Bacon
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the XMLUnit nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
******************************************************************
*/
package org.custommonkey.xmlunit;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;
import org.custommonkey.xmlunit.exceptions.ConfigurationException;
import org.custommonkey.xmlunit.exceptions.XMLUnitRuntimeException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xmlunit.XMLUnitException;
import org.xmlunit.builder.Input;
import org.xmlunit.transform.Transformation;
/**
* Handy wrapper for an XSLT transformation performed using JAXP/Trax.
* Note that transformation is not actually performed until a call to
* getResultXXX
method, and Templates are not used.
*/
public class Transform {
private static final File PWD = new File(".");
private final Transformation transformation;
// only here in order to support the getParameter method
private final Map parameters = new HashMap();
/**
* Create a transformation using String input XML and String stylesheet
* @param input document to transform
* @param stylesheet stylesheet to use for transformation
*/
public Transform(String input, String stylesheet) {
this(input == null ? null : Input.fromString(input),
stylesheet == null ? null : Input.fromString(stylesheet));
}
/**
* Create a transformation using String input XML and stylesheet in a File
* @param input document to transform
* @param stylesheet stylesheet to use for transformation
*/
public Transform(String input, File stylesheet) {
this(input == null ? null : Input.fromString(input),
stylesheet == null ? null : Input.fromFile(stylesheet));
}
/**
* Create a transformation using InputSource input XML and
* InputSource stylesheet
* @param input document to transform
* @param stylesheet stylesheet to use for transformation
*/
public Transform(InputSource input, InputSource stylesheet) {
this(new SAXSource(input), new SAXSource(stylesheet));
}
/**
* Create a transformation using InputSource input XML and
* stylesheet in a File
* @param input document to transform
* @param stylesheet stylesheet to use for transformation
*/
public Transform(InputSource input, File stylesheet) {
this(new SAXSource(input),
stylesheet == null ? null : Input.fromFile(stylesheet).build());
}
/**
* Create a transformation that allows us to serialize a DOM Node
* @param sourceNode document to transform
*/
public Transform(Node sourceNode) {
this(sourceNode, (String) null);
}
/**
* Create a transformation from an input Node and stylesheet in a String
* @param sourceNode document to transform
* @param stylesheet stylesheet to use for transformation
*/
public Transform(Node sourceNode, String stylesheet) {
this(sourceNode == null ? null : Input.fromNode(sourceNode),
stylesheet == null ? null : Input.fromString(stylesheet));
}
/**
* Create a transformation from an input Node and stylesheet in a File
* @param sourceNode document to transform
* @param stylesheet stylesheet to use for transformation
*/
public Transform(Node sourceNode, File stylesheet) {
this(sourceNode == null ? null : Input.fromNode(sourceNode),
stylesheet == null ? null : Input.fromFile(stylesheet));
}
private Transform(Input.Builder input, Input.Builder stylesheet) {
this(input == null ? null : input.build(),
stylesheet == null ? null : stylesheet.build());
}
/**
* Create a transformation using Source input XML and Source stylesheet
* @param inputSource document to transform
* @param stylesheetSource stylesheet to use for transformation
*/
public Transform(Source inputSource, Source stylesheetSource) {
transformation = new Transformation(inputSource);
transformation.setStylesheet(stylesheetSource);
transformation.setFactory(XMLUnit.getTransformerFactory());
provideSystemIdIfRequired(inputSource);
provideSystemIdIfRequired(stylesheetSource);
}
/**
* Ensure that the source has a systemId
*/
private void provideSystemIdIfRequired(Source source) {
if (source!=null && (source.getSystemId() == null
|| source.getSystemId().length() == 0)) {
source.setSystemId(getDefaultSystemId());
}
}
/**
* @return the current working directory as an URL-form string
*/
private String getDefaultSystemId() {
try {
return PWD.toURL().toExternalForm();
} catch (MalformedURLException e) {
throw new XMLUnitRuntimeException("Unable to determine current "
+ "working directory!", e);
}
}
/**
* Perform the actual transformation
* @param result result to output transformation result to
* @throws TransformerException if transformation fails
*/
protected void transformTo(final Result result) throws TransformerException {
withExceptionHandling(new Trans