From: <bo...@us...> - 2007-04-17 04:02:13
|
Revision: 189 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=189&view=rev Author: bodewig Date: 2007-04-16 21:02:14 -0700 (Mon, 16 Apr 2007) Log Message: ----------- Make XSLT version used for internal stylesheets configurable Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/exceptions/ConfigurationException.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Constants.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Transform.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java 2007-04-17 03:59:29 UTC (rev 188) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java 2007-04-17 04:02:14 UTC (rev 189) @@ -73,7 +73,7 @@ */ private StringBuffer getXSLTBase() { StringBuffer result = new StringBuffer(XML_DECLARATION) - .append(XSLT_START); + .append(XMLUnit.getXSLTStart()); String tmp = result.toString(); int close = tmp.lastIndexOf('>'); if (close == -1) { Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-04-17 03:59:29 UTC (rev 188) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-04-17 04:02:14 UTC (rev 189) @@ -1,6 +1,6 @@ /* ***************************************************************** -Copyright (c) 2001, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -48,6 +48,9 @@ import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Locale; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -72,19 +75,33 @@ private static boolean normalize = false; private static boolean normalizeWhitespace = false; private static boolean ignoreAttributeOrder = false; + private static String xsltVersion = "1.0"; - private static final String STRIP_WHITESPACE_STYLESHEET + private static final String XSLT_VERSION_START = " version=\""; + private static final String XSLT_VERSION_END = "\">"; + + private static final String STRIP_WHITESPACE_STYLESHEET_START = new StringBuffer(XMLConstants.XML_DECLARATION) - .append(XSLTConstants.XSLT_START) + .append(XSLTConstants.XSLT_START_NO_VERSION) + .append(XSLT_VERSION_START) + .toString(); + + private static final String STRIP_WHITESPACE_STYLESHEET_END + = new StringBuffer(XSLT_VERSION_END) .append(XSLTConstants.XSLT_XML_OUTPUT_NOINDENT) .append(XSLTConstants.XSLT_STRIP_WHITESPACE) .append(XSLTConstants.XSLT_IDENTITY_TEMPLATE) .append(XSLTConstants.XSLT_END) .toString(); - private static final String STRIP_COMMENTS_STYLESHEET + private static final String STRIP_COMMENTS_STYLESHEET_START = new StringBuffer(XMLConstants.XML_DECLARATION) - .append(XSLTConstants.XSLT_START) + .append(XSLTConstants.XSLT_START_NO_VERSION) + .append(XSLT_VERSION_START) + .toString(); + + private static final String STRIP_COMMENTS_STYLESHEET_END + = new StringBuffer(XSLT_VERSION_END) .append(XSLTConstants.XSLT_XML_OUTPUT_NOINDENT) .append(XSLTConstants.XSLT_STRIP_COMMENTS_TEMPLATE) .append(XSLTConstants.XSLT_END) @@ -418,23 +435,33 @@ return newFactory; } + private static String getStripWhitespaceStylesheet() { + return STRIP_WHITESPACE_STYLESHEET_START + getXSLTVersion() + + STRIP_WHITESPACE_STYLESHEET_END; + } + /** - * Obtain the transformation that will strip whitespace from a DOM containing - * empty Text nodes + * Obtain the transformation that will strip whitespace from a DOM + * containing empty Text nodes * @param forDocument * @return a <code>Transform</code> to do the whitespace stripping */ public static Transform getStripWhitespaceTransform(Document forDocument) { - return new Transform(forDocument, STRIP_WHITESPACE_STYLESHEET); + return new Transform(forDocument, getStripWhitespaceStylesheet()); } + private static String getStripCommentsStylesheet() { + return STRIP_COMMENTS_STYLESHEET_START + getXSLTVersion() + + STRIP_COMMENTS_STYLESHEET_END; + } + /** * Obtain the transformation that will strip comments from a DOM. * @param forDocument * @return a <code>Transform</code> to do the whitespace stripping */ public static Transform getStripCommentsTransform(Document forDocument) { - return new Transform(forDocument, STRIP_COMMENTS_STYLESHEET); + return new Transform(forDocument, getStripCommentsStylesheet()); } /** @@ -685,5 +712,43 @@ public static boolean getIgnoreAttributeOrder() { return ignoreAttributeOrder; } + + /** + * Sets the XSLT version to set on stylesheets used internally. + * + * <p>Defaults to "1.0".</p> + * + * @throws ConfigurationException if the argument cannot be parsed + * as a positive number. + */ + public static void setXSLTVersion(String s) { + try { + Number n = NumberFormat.getInstance(Locale.US).parse(s); + if (n.doubleValue() < 0) { + throw new ConfigurationException(s + " doesn't reperesent a" + + " positive number."); + } + } catch (ParseException e) { + throw new ConfigurationException(e); + } + xsltVersion = s; + } + + /** + * The XSLT version set on stylesheets used internally. + * + * <p>Defaults to "1.0".</p> + */ + public static String getXSLTVersion() { + return xsltVersion; + } + + /** + * XSLT stylesheet element using the configured XSLT version. + */ + static String getXSLTStart() { + return XSLTConstants.XSLT_START_NO_VERSION + + XSLT_VERSION_START + getXSLTVersion() + XSLT_VERSION_END; + } } Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java 2007-04-17 03:59:29 UTC (rev 188) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java 2007-04-17 04:02:14 UTC (rev 189) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -41,34 +41,40 @@ */ public interface XSLTConstants extends XMLConstants { /** - * <xsl:stylesheet> + * <xsl:stylesheet */ - public static final String XSLT_START = - "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"; + String XSLT_START_NO_VERSION = + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""; /** + * <xsl:stylesheet ... version="1.0"> + */ + String XSLT_START = + XSLT_START_NO_VERSION + " version=\"1.0\">"; + + /** * <xsl:output> for XML with no indentation */ - public static final String XSLT_XML_OUTPUT_NOINDENT = + String XSLT_XML_OUTPUT_NOINDENT = "<xsl:output method=\"xml\" version=\"1.0\" indent=\"no\"/>"; /** * <xsl:strip-space> for all elements */ - public static final String XSLT_STRIP_WHITESPACE = + String XSLT_STRIP_WHITESPACE = "<xsl:strip-space elements=\"*\"/>"; /** * <xsl:template> to copy the current nodeset into the output tree */ - public static final String XSLT_IDENTITY_TEMPLATE = + String XSLT_IDENTITY_TEMPLATE = "<xsl:template match=\"/\"><xsl:copy-of select=\".\"/></xsl:template>"; /** * <xsl:template> to copy the current nodeset into the * output tree while stripping comments. */ - public static final String XSLT_STRIP_COMMENTS_TEMPLATE = + String XSLT_STRIP_COMMENTS_TEMPLATE = "<xsl:template match=\"node()[not(self::comment())]|@*\">" + "<xsl:copy><xsl:apply-templates select=\"node()[not(self::comment())]|@*\"/></xsl:copy>" + "</xsl:template>"; @@ -76,5 +82,5 @@ /** * </xsl:stylesheet> */ - public static final String XSLT_END = "</xsl:stylesheet>"; + String XSLT_END = "</xsl:stylesheet>"; } Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/exceptions/ConfigurationException.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/exceptions/ConfigurationException.java 2007-04-17 03:59:29 UTC (rev 188) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/exceptions/ConfigurationException.java 2007-04-17 04:02:14 UTC (rev 189) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001, Jeff Martin, Tim Bacon +Copyright (c) 2006-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,4 +44,7 @@ public ConfigurationException(Throwable t) { super(t != null ? t.getMessage() : null, t); } + public ConfigurationException(String s) { + super(s, null); + } } \ No newline at end of file Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Constants.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Constants.java 2007-04-17 03:59:29 UTC (rev 188) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Constants.java 2007-04-17 04:02:14 UTC (rev 189) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 200, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Transform.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Transform.java 2007-04-17 03:59:29 UTC (rev 188) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Transform.java 2007-04-17 04:02:14 UTC (rev 189) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 200, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java 2007-04-17 03:59:29 UTC (rev 188) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java 2007-04-17 04:02:14 UTC (rev 189) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 200, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -40,6 +40,7 @@ import junit.framework.TestCase; import junit.framework.TestSuite; +import org.custommonkey.xmlunit.exceptions.ConfigurationException; import org.w3c.dom.Document; import org.xml.sax.EntityResolver; import org.xml.sax.helpers.DefaultHandler; @@ -112,10 +113,26 @@ assertTrue(diff.similar()); } - /** - * Returns a TestSuite containing this test case. - */ - public static TestSuite suite(){ - return new TestSuite(test_XMLUnit.class); + public void testXSLTVersion() { + try { + assertEquals("1.0", XMLUnit.getXSLTVersion()); + assertEquals(XSLTConstants.XSLT_START, XMLUnit.getXSLTStart()); + XMLUnit.setXSLTVersion("2.0"); + assertTrue(XMLUnit.getXSLTStart() + .startsWith(XSLTConstants.XSLT_START_NO_VERSION)); + assertTrue(XMLUnit.getXSLTStart().endsWith("\"2.0\">")); + try { + XMLUnit.setXSLTVersion("foo"); + fail("foo is not a number"); + } catch (ConfigurationException expected) { + } + try { + XMLUnit.setXSLTVersion("-1.0"); + fail("-1.0 is negative"); + } catch (ConfigurationException expected) { + } + } finally { + XMLUnit.setXSLTVersion("1.0"); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |