From: <bo...@us...> - 2008-06-06 14:16:30
|
Revision: 268 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=268&view=rev Author: bodewig Date: 2008-06-06 07:16:39 -0700 (Fri, 06 Jun 2008) Log Message: ----------- Try to get more information about the error if the XPath expression is invalid, Issue 1985229 Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java 2008-06-06 14:00:14 UTC (rev 267) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/SimpleXpathEngine.java 2008-06-06 14:16:39 UTC (rev 268) @@ -42,9 +42,11 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.Iterator; -import javax.xml.transform.Result; +import javax.xml.transform.ErrorListener; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.Result; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; @@ -132,14 +134,31 @@ throws TransformerException, ConfigurationException, XpathException { try { StreamSource source = new StreamSource(new StringReader(xslt)); - Transformer transformer = - XMLUnit.getTransformerFactory().newTransformer(source); + TransformerFactory tf = XMLUnit.newTransformerFactory(); + ErrorListener el = new ErrorListener() { + public void error(TransformerException ex) + throws TransformerException { + // any error in our simple stylesheet must be fatal + throw ex; + } + public void fatalError(TransformerException ex) + throws TransformerException { + throw ex; + } + public void warning(TransformerException ex) { + // there shouldn't be any warning + ex.printStackTrace(); + } + }; + tf.setErrorListener(el); + Transformer transformer = tf.newTransformer(source); // Issue 1985229 says Xalan-J 2.7.0 may return null for // illegal input if (transformer == null) { throw new XpathException("failed to obtain an XSLT transformer" + " for XPath expression."); } + transformer.setErrorListener(el); transformer.transform(new DOMSource(document), result); } catch (javax.xml.transform.TransformerConfigurationException ex) { throw new ConfigurationException(ex); Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2008-06-06 14:00:14 UTC (rev 267) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2008-06-06 14:16:39 UTC (rev 268) @@ -402,15 +402,25 @@ */ public static TransformerFactory getTransformerFactory() { if (transformerFactory == null) { - transformerFactory = TransformerFactory.newInstance(); - if (uriResolver != null) { - transformerFactory.setURIResolver(uriResolver); - } + transformerFactory = newTransformerFactory(); } return transformerFactory; } /** + * Get a fresh transformer to use for XSLT transformations (and by + * implication serialization and XPaths). + * @return a new instance of the default transformer factory + */ + static TransformerFactory newTransformerFactory() { + TransformerFactory tf = TransformerFactory.newInstance(); + if (uriResolver != null) { + tf.setURIResolver(uriResolver); + } + return tf; + } + + /** * Sets the URIResolver to use during transformations. */ public static void setURIResolver(URIResolver resolver) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |