From: <bo...@us...> - 2010-05-19 06:56:24
|
Revision: 389 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=389&view=rev Author: bodewig Date: 2010-05-19 06:56:18 +0000 (Wed, 19 May 2010) Log Message: ----------- extract XSLT logic into something reusable Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/transform/ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/transform/Transformation.java trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/transform/ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/transform/TransformationTest.java Removed Paths: ------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/TransformHelper.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2010-05-17 15:46:34 UTC (rev 388) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2010-05-19 06:56:18 UTC (rev 389) @@ -26,7 +26,6 @@ import javax.xml.transform.Source; import javax.xml.transform.TransformerFactory; import javax.xml.transform.URIResolver; -import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import net.sf.xmlunit.exceptions.XMLUnitException; @@ -161,14 +160,14 @@ TransformationBuilder withParameter(String name, Object value); TransformationBuilder withStylesheet(Builder b); TransformationBuilder withStylesheet(Source s); - TransformationBuilder withUriResolver(URIResolver r); + TransformationBuilder withURIResolver(URIResolver r); } private static class Transformation implements TransformationBuilder { - private final TransformHelper helper; + private final net.sf.xmlunit.transform.Transformation helper; private Transformation(Source s) { - helper = new TransformHelper(s); + helper = new net.sf.xmlunit.transform.Transformation(s); } public TransformationBuilder withStylesheet(Source s) { helper.setStylesheet(s); @@ -179,12 +178,12 @@ } public TransformationBuilder withOutputProperty(String name, String value) { - helper.setOutputProperty(name, value); + helper.addOutputProperty(name, value); return this; } public TransformationBuilder withParameter(String name, Object value) { - helper.setParameter(name, value); + helper.addParameter(name, value); return this; } @@ -193,15 +192,13 @@ return this; } - public TransformationBuilder withUriResolver(URIResolver r) { - helper.setUriResolver(r); + public TransformationBuilder withURIResolver(URIResolver r) { + helper.setURIResolver(r); return this; } public Source build() { - DOMResult r = new DOMResult(); - helper.transformTo(r); - return new DOMSource(r.getNode()); + return new DOMSource(helper.transformToDocument()); } } Deleted: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/TransformHelper.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/TransformHelper.java 2010-05-17 15:46:34 UTC (rev 388) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/TransformHelper.java 2010-05-19 06:56:18 UTC (rev 389) @@ -1,78 +0,0 @@ -/* - This file is licensed 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 net.sf.xmlunit.builder; - -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.URIResolver; -import net.sf.xmlunit.exceptions.ConfigurationException; -import net.sf.xmlunit.exceptions.XMLUnitException; - -class TransformHelper { - private final Source source; - private Source styleSheet; - private TransformerFactory factory; - private URIResolver uriResolver; - private final Properties output = new Properties(); - private final Map<String, Object> params = new HashMap<String, Object>(); - TransformHelper(Source s) { - source = s; - } - void setStylesheet(Source s) { - styleSheet = s; - } - void setOutputProperty(String name, String value) { - output.setProperty(name, value); - } - void setParameter(String name, Object value) { - params.put(name, value); - } - void setFactory(TransformerFactory f) { - factory = f; - } - void setUriResolver(URIResolver r) { - uriResolver = r; - } - void transformTo(Result r) { - try { - TransformerFactory fac = factory; - if (fac == null) { - fac = TransformerFactory.newInstance(); - } - Transformer t = null; - if (styleSheet != null) { - t = fac.newTransformer(styleSheet); - } else { - t = fac.newTransformer(); - } - if (uriResolver != null) { - t.setURIResolver(uriResolver); - } - t.setOutputProperties(output); - for (Map.Entry<String, Object> ent : params.entrySet()) { - t.setParameter(ent.getKey(), ent.getValue()); - } - t.transform(source, r); - } catch (javax.xml.transform.TransformerConfigurationException e) { - throw new ConfigurationException(e); - } catch (javax.xml.transform.TransformerException e) { - throw new XMLUnitException(e); - } - } -} \ No newline at end of file Copied: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/transform/Transformation.java (from rev 388, trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/TransformHelper.java) =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/transform/Transformation.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/transform/Transformation.java 2010-05-19 06:56:18 UTC (rev 389) @@ -0,0 +1,222 @@ +/* + This file is licensed 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 net.sf.xmlunit.transform; + +import java.io.StringWriter; +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.TransformerFactory; +import javax.xml.transform.URIResolver; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.stream.StreamResult; +import net.sf.xmlunit.exceptions.ConfigurationException; +import net.sf.xmlunit.exceptions.XMLUnitException; +import org.w3c.dom.Document; + +/** + * Provides a convenience layer over TraX. + * + * <p>Apart from IllegalArgumentExceptions if you try to pass in null + * values only the transform methods will ever throw exceptions and + * these will be XMLUnit's runtime exceptions.</p> + * + * <p>Each invocation of a transform method will use a fresh + * Transformer instance, the transform methods are thread-safe.</p> + */ +public final class Transformation { + private Source source; + private Source styleSheet; + private TransformerFactory factory; + private URIResolver uriResolver; + private ErrorListener errorListener; + private final Properties output = new Properties(); + private final Map<String, Object> params = new HashMap<String, Object>(); + + public Transformation() { + } + /** + * @param s the source to transform - must not be null. + */ + public Transformation(Source s) { + setSource(s); + } + /** + * Set the source document to transform. + * @param s the source to transform - must not be null. + */ + public void setSource(Source s) { + if (s == null) { + throw new IllegalArgumentException("source must not be null"); + } + source = s; + } + /** + * Set the stylesheet to use. + * @param s the stylesheet to use - may be null in which case an + * identity transformation will be performed. + */ + public void setStylesheet(Source s) { + styleSheet = s; + } + /** + * Add a named output property. + * + * @param name name of the property - must not be null + * @param value value of the property - must not be null + */ + public void addOutputProperty(String name, String value) { + if (name == null) { + throw new IllegalArgumentException("name must not be null"); + } + if (value == null) { + throw new IllegalArgumentException("value must not be null"); + } + output.setProperty(name, value); + } + /** + * Clear all output properties. + */ + public void clearOutputProperties() { + output.clear(); + } + /** + * Add a named parameter. + * + * @param name name of the parameter - must not be null + * @param value value of the parameter - may be null + */ + public void addParameter(String name, Object value) { + if (name == null) { + throw new IllegalArgumentException("name must not be null"); + } + params.put(name, value); + } + /** + * Clear all output parameters. + */ + public void clearParameters() { + params.clear(); + } + /** + * Set the TraX factory to use. + * + * @param f the factory to use - may be null in which case the + * default factory will be used. + */ + public void setFactory(TransformerFactory f) { + factory = f; + } + /** + * Set the resolver to use for document() and xsl:include/import + * + * <p>The resolver will <b>not</b> be attached to the factory.</p> + * + * @param r the resolver - may be null in which case no explicit + * resolver will be used + */ + public void setURIResolver(URIResolver r) { + uriResolver = r; + } + /** + * Set the error listener for the transformation. + * + * <p>The listener will <b>not</b> be attached to the factory.</p> + * + * @param l the listener - may be null in which case no listener + * will be used + */ + public void setErrorListener(ErrorListener l) { + errorListener = l; + } + /** + * Perform the transformation. + * + * @param r where to send the transformation result - must not be null + * @exception IllegalArgumentException if source or result are null + * @exception ConfigurationException if the TraX system isn't + * configured properly + * @exception XMLUnitException if the transformation throws an + * exception + */ + public void transformTo(Result r) { + if (source == null) { + throw new IllegalArgumentException("source must not be null"); + } + if (r == null) { + throw new IllegalArgumentException("result must not be null"); + } + try { + TransformerFactory fac = factory; + if (fac == null) { + fac = TransformerFactory.newInstance(); + } + Transformer t = null; + if (styleSheet != null) { + t = fac.newTransformer(styleSheet); + } else { + t = fac.newTransformer(); + } + if (uriResolver != null) { + t.setURIResolver(uriResolver); + } + if (errorListener != null) { + t.setErrorListener(errorListener); + } + t.setOutputProperties(output); + for (Map.Entry<String, Object> ent : params.entrySet()) { + t.setParameter(ent.getKey(), ent.getValue()); + } + t.transform(source, r); + } catch (javax.xml.transform.TransformerConfigurationException e) { + throw new ConfigurationException(e); + } catch (javax.xml.transform.TransformerException e) { + throw new XMLUnitException(e); + } + } + /** + * Convenience method that returns the result of the + * transformation as a String. + * + * @exception IllegalArgumentException if source is null + * @exception ConfigurationException if the TraX system isn't + * configured properly + * @exception XMLUnitException if the transformation throws an + * exception + */ + public String transformToString() { + StringWriter sw = new StringWriter(); + transformTo(new StreamResult(sw)); + return sw.toString(); + } + /** + * Convenience method that returns the result of the + * transformation as a Document. + * + * @exception IllegalArgumentException if source is null + * @exception ConfigurationException if the TraX system isn't + * configured properly + * @exception XMLUnitException if the transformation throws an + * exception + */ + public Document transformToDocument() { + DOMResult r = new DOMResult(); + transformTo(r); + return (Document) r.getNode(); + } +} \ No newline at end of file Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java 2010-05-17 15:46:34 UTC (rev 388) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java 2010-05-19 06:56:18 UTC (rev 389) @@ -16,6 +16,8 @@ public final class TestResources { public static final String ANIMAL_FILE = "src/tests/resources/test1.xml"; public static final String BLAME_FILE = "src/tests/resources/test.blame.html"; + public static final String ANIMAL_XSL = "src/tests/resources/animal.xsl"; + public static final String DOG_FILE = "src/tests/resources/testAnimal.xml"; private TestResources() { } Added: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/transform/TransformationTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/transform/TransformationTest.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/transform/TransformationTest.java 2010-05-19 06:56:18 UTC (rev 389) @@ -0,0 +1,51 @@ +/* + This file is licensed 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 net.sf.xmlunit.transform; + +import javax.xml.transform.OutputKeys; +import net.sf.xmlunit.TestResources; +import net.sf.xmlunit.builder.Input; +import org.junit.Before; +import org.junit.Test; +import org.w3c.dom.Document; + +import static org.hamcrest.core.IsNot.*; +import static org.junit.Assert.*; + +public class TransformationTest { + private Transformation t; + + @Before public void createTransformation() { + t = new Transformation(Input.fromFile(TestResources.DOG_FILE) + .build()); + t.setStylesheet(Input.fromFile(TestResources.ANIMAL_XSL).build()); + } + + @Test public void transformAnimalToString() { + assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dog/>", + t.transformToString()); + } + + @Test public void transformAnimalToDocument() { + Document doc = t.transformToDocument(); + assertEquals("dog", doc.getDocumentElement().getTagName()); + } + + @Test public void transformAnimalToHtml() { + t.addOutputProperty(OutputKeys.METHOD, "html"); + assertThat(t.transformToString(), + not("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dog/>")); + } + +} Property changes on: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/transform/TransformationTest.java ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |