@Override public void describeMismatch(Object item, Description description) { XpathEngine engine = XMLUnit.newXpathEngine(); engine.setNamespaceContext(namespaceContext); try { List<Node> nodes = nodeCollection(engine.getMatchingNodes(xPath, (Document) item)); matcher.describeMismatch(nodes, description); if (showXML != null) { printDom((Document) item, showXML); } } catch (XpathException e) { description.appendText("exception occured: ").appendText(e.getMessage()); } } };
/** * Execute the specified xpath syntax <code>select</code> expression * on the specified document and return the list of nodes (could have * length zero) that match * @param select * @param document * @return list of matching nodes */ public NodeList getMatchingNodes(String select, Document document) throws XpathException { try { return (NodeList) xpath.evaluate(select, document, XPathConstants.NODESET); } catch (XPathExpressionException ex) { throw new XpathException(ex); } }
/** * @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); } } /**
/** * 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; }
static <R> R withExceptionHandling(Trans<R> trans) throws TransformerException { try { return trans.transform(); } catch (org.xmlunit.ConfigurationException ex) { throw new ConfigurationException(ex.getMessage(), ex.getCause()); } catch (XMLUnitException ex) { Throwable cause = ex.getCause(); if (cause instanceof TransformerException) { throw (TransformerException) cause; } throw new XMLUnitRuntimeException(ex.getMessage(), cause); } }
/** * @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); } } /**
/** * Evaluate the result of executing the specified xpath syntax * <code>select</code> expression on the specified document * @param select * @param document * @return evaluated result * @throws TransformerException * @throws TransformerConfigurationException */ public String evaluate(String select, Document document) throws XpathException { try { return xpath.evaluate(select, document); } catch (XPathExpressionException ex) { throw new XpathException(ex); } }
/** * 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; }
private static Document stripWhiteSpaceUsingXSLT(Document forDoc) { try { Transform whitespaceStripper = getStripWhitespaceTransform(forDoc); return whitespaceStripper.getResultDocument(); } catch (TransformerException e) { throw new XMLUnitRuntimeException(e.getMessage(), e.getCause()); } }
/** * Execute the specified xpath syntax <code>select</code> expression * on the specified document and return the list of nodes (could have * length zero) that match * @param select * @param document * @return list of matching nodes */ public NodeList getMatchingNodes(String select, Document document) throws ConfigurationException, XpathException { try { return getXPathResultNode(select, document).getChildNodes(); } catch (TransformerException ex) { throw new XpathException("Failed to apply stylesheet", ex); } }
/** * Factory method * @param stylesheetSource * @throws ConfigurationException * @return */ private Transformer getTransformer(Source stylesheetSource) throws ConfigurationException { try { TransformerFactory factory = XMLUnit.getTransformerFactory(); Transformer t = stylesheetSource == null ? factory.newTransformer() : factory.newTransformer(stylesheetSource); return t; } catch (javax.xml.transform.TransformerConfigurationException ex) { throw new ConfigurationException(ex); } }
private static Document stripWhiteSpaceUsingXSLT(Document forDoc) { try { Transform whitespaceStripper = getStripWhitespaceTransform(forDoc); return whitespaceStripper.getResultDocument(); } catch (TransformerException e) { throw new XMLUnitRuntimeException(e.getMessage(), e.getCause()); } }
/** * Execute the specified xpath syntax <code>select</code> expression * on the specified document and return the list of nodes (could have * length zero) that match * @param select * @param document * @return list of matching nodes */ public NodeList getMatchingNodes(String select, Document document) throws ConfigurationException, XpathException { try { return getXPathResultNode(select, document).getChildNodes(); } catch (TransformerException ex) { throw new XpathException("Failed to apply stylesheet", ex); } }
/** * Get the <code>DocumentBuilder</code> instance used to parse the test XML * in an XMLTestCase. * @return parser for test values * @throws ConfigurationException */ public static DocumentBuilder newTestParser() throws ConfigurationException { try { testBuilderFactory = getTestDocumentBuilderFactory(); DocumentBuilder builder = testBuilderFactory.newDocumentBuilder(); if (testEntityResolver!=null) { builder.setEntityResolver(testEntityResolver); } return builder; } catch (ParserConfigurationException ex) { throw new ConfigurationException(ex); } }
/** * Removes all comment nodes if {@link XMLUnit#getIgnoreComments * comments are ignored}. * * @param orig a document making up one half of this difference * @return manipulated doc */ private Document getCommentlessDocument(Document orig) { if (!XMLUnit.getIgnoreComments()) { return orig; } try { Transform commentStripper = XMLUnit.getStripCommentsTransform(orig); return commentStripper.getResultDocument(); } catch (TransformerException e) { throw new XMLUnitRuntimeException(e.getMessage(), e.getCause()); } }
/** * Evaluate the result of executing the specified xpath syntax * <code>select</code> expression on the specified document * @param select * @param document * @return evaluated result */ public String evaluate(String select, Document document) throws ConfigurationException, XpathException { try { StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); performTransform(getValueTransformation(select), document, result); return writer.toString(); } catch (TransformerException ex) { throw new XpathException("Failed to apply stylesheet", ex); } }
public Jaxp13XpathEngine() throws ConfigurationException { try { XPathFactory f = null; if (XMLUnit.getXPathFactory() != null) { f = (XPathFactory) Class.forName(XMLUnit.getXPathFactory()) .newInstance(); } else { f = XPathFactory.newInstance(); } xpath = f.newXPath(); } catch (Exception ex) { throw new ConfigurationException(ex); } }
/** * Removes all comment nodes if {@link XMLUnit.getIgnoreComments * comments are ignored}. * * @param originalDoc a document making up one half of this difference * @return manipulated doc */ private Document getCommentlessDocument(Document orig) { if (!XMLUnit.getIgnoreComments()) { return orig; } try { Transform commentStripper = XMLUnit.getStripCommentsTransform(orig); return commentStripper.getResultDocument(); } catch (TransformerException e) { throw new XMLUnitRuntimeException(e.getMessage(), e.getCause()); } }
/** * Evaluate the result of executing the specified xpath syntax * <code>select</code> expression on the specified document * @param select * @param document * @return evaluated result * @throws XpathException */ public String evaluate(String select, Document document) throws XpathException { try { return engine.evaluate(select, new DOMSource(document)); } catch (XMLUnitException ex) { throw new XpathException(ex.getCause()); } }
/** * Is the given schema instance valid according to the configured * schema definition(s)? * * @throws XMLUnitRuntimeException if the schema definition is * invalid or the Source is a SAXSource and the underlying * XMLReader throws an IOException (see {@link * javax.xml.validation.Validator#validate validate in * Validator}). */ public boolean isInstanceValid(Source instance) { try { return validator.validateInstance(instance).isValid(); } catch (XMLUnitException e) { throw new XMLUnitRuntimeException(e.getMessage(), e.getCause()); } }