From: <bo...@us...> - 2007-08-28 10:51:20
|
Revision: 231 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=231&view=rev Author: bodewig Date: 2007-08-28 03:51:17 -0700 (Tue, 28 Aug 2007) Log Message: ----------- Strip element content whitespace manually if using JDK 1.5's XSLTC, bug 1779701 Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/Diff.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/Diff.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/Diff.java 2007-08-23 10:59:48 UTC (rev 230) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/Diff.java 2007-08-28 10:51:17 UTC (rev 231) @@ -178,16 +178,9 @@ * differences ignore whitespace */ private Document getWhitespaceManipulatedDocument(Document originalDoc) { - if (!XMLUnit.getIgnoreWhitespace()) { - return originalDoc; - } - try { - Transform whitespaceStripper = XMLUnit.getStripWhitespaceTransform( - originalDoc); - return whitespaceStripper.getResultDocument(); - } catch (TransformerException e) { - throw new XMLUnitRuntimeException(e.getMessage(), e.getCause()); - } + return XMLUnit.getIgnoreWhitespace() + ? XMLUnit.getWhitespaceStrippedDocument(originalDoc) + : originalDoc; } /** Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-08-23 10:59:48 UTC (rev 230) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-08-28 10:51:17 UTC (rev 231) @@ -37,11 +37,13 @@ package org.custommonkey.xmlunit; import org.custommonkey.xmlunit.exceptions.ConfigurationException; +import org.custommonkey.xmlunit.exceptions.XMLUnitRuntimeException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; +import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.URIResolver; @@ -52,6 +54,8 @@ import java.text.ParseException; import java.util.Locale; import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.EntityResolver; @@ -458,6 +462,54 @@ return new Transform(forDocument, getStripWhitespaceStylesheet()); } + /** + * Returns a new Document instance that is identical to the one + * passed in with element content whitespace removed. + * + * <p>Will use {@link #getStripWhitespaceTransform + * getStripWhitespaceTransform} unless we are operating under the + * severly broken XSLTC Transformer shipping with JDK 1.5.</p> + */ + public static Document getWhitespaceStrippedDocument(Document forDoc) { + String factory = getTransformerFactory().getClass().getName(); + if (XSLTConstants.JAVA5_XSLTC_FACTORY_NAME.equals(factory)) { + return stripWhiteSpaceWithoutXSLT(forDoc); + } else { + return stripWhiteSpaceUsingXSLT(forDoc); + } + } + + private static Document stripWhiteSpaceUsingXSLT(Document forDoc) { + try { + Transform whitespaceStripper = getStripWhitespaceTransform(forDoc); + return whitespaceStripper.getResultDocument(); + } catch (TransformerException e) { + throw new XMLUnitRuntimeException(e.getMessage(), e.getCause()); + } + } + + private static Document stripWhiteSpaceWithoutXSLT(Document forDoc) { + Document copy = (Document) forDoc.cloneNode(true); + stripEmptyTextNodes(copy); + return copy; + } + + private static void stripEmptyTextNodes(Node n) { + final NodeList nl = n.getChildNodes(); + for (int i = 0; i < nl.getLength(); i++) { + Node child = nl.item(i); + if (child.getNodeType() == Node.ELEMENT_NODE) { + stripEmptyTextNodes(child); + } else if (child.getNodeType() == Node.TEXT_NODE) { + String value = child.getNodeValue(); + if (value == null || value.trim().length() == 0) { + n.removeChild(child); + --i; + } + } + } + } + private static String getStripCommentsStylesheet() { return STRIP_COMMENTS_STYLESHEET_START + getXSLTVersion() + STRIP_COMMENTS_STYLESHEET_END; Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java 2007-08-23 10:59:48 UTC (rev 230) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XSLTConstants.java 2007-08-28 10:51:17 UTC (rev 231) @@ -83,4 +83,11 @@ * </xsl:stylesheet> */ String XSLT_END = "</xsl:stylesheet>"; + + /** + * Factory class of the XSLTC version shipping with JDK 1.5 which + * is pretty broken. + */ + String JAVA5_XSLTC_FACTORY_NAME = + "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |