List of usage examples for javax.xml.xpath XPathExpression toString
public String toString()
From source file:org.callimachusproject.rdfa.test.RDFaGenerationTest.java
XPathExpression conjoinXPaths(Document fragment, String base) throws Exception { String path = "//*"; final Element e = fragment.getDocumentElement(); if (e == null) return null; final XPathIterator conjunction = new XPathIterator(e, base); if (conjunction.hasNext()) { path += "["; boolean first = true; while (conjunction.hasNext()) { if (!first) path += " and "; XPathExpression x = conjunction.next(); String exp = x.toString(); boolean positive = true; if (exp.startsWith("-")) { positive = false;/* ww w .j a va2 s .c o m*/ exp = exp.substring(1); } // remove the initial '/' exp = exp.substring(1); if (positive) path += exp; else path += "not(" + exp + ")"; first = false; } path += "]"; } XPath xpath = xPathFactory.newXPath(); // add namespace prefix resolver to the xpath based on the current element xpath.setNamespaceContext(new AbstractNamespaceContext() { public String getNamespaceURI(String prefix) { // for the empty prefix lookup the default namespace if (prefix.isEmpty()) return e.lookupNamespaceURI(null); for (int i = 0; i < conjunction.contexts.size(); i++) { NamespaceContext c = conjunction.contexts.get(i); String ns = c.getNamespaceURI(prefix); if (ns != null) return ns; } return null; } }); final String exp = path; final XPathExpression compiled = xpath.compile(path); if (verbose) System.out.println(exp); return new XPathExpression() { public String evaluate(Object source) throws XPathExpressionException { return compiled.evaluate(source); } public String evaluate(InputSource source) throws XPathExpressionException { return compiled.evaluate(source); } public Object evaluate(Object source, QName returnType) throws XPathExpressionException { return compiled.evaluate(source, returnType); } public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException { return compiled.evaluate(source, returnType); } public String toString() { return exp; } }; }
From source file:org.callimachusproject.rdfa.test.RDFaGenerationTest.java
XPathExpression evaluateXPaths(XPathIterator iterator, Document doc) throws Exception { while (iterator.hasNext()) { XPathExpression exp = iterator.next(); NodeList result = (NodeList) exp.evaluate(doc, XPathConstants.NODESET); boolean negativeTest = exp.toString().startsWith("-"); int solutions = result.getLength(); // a positive test should return exactly one solution boolean failure = ((solutions < 1 && !negativeTest) // a negative test should return no solutions || (solutions > 0 && negativeTest)); if (failure) return exp; // fail }/*from w ww . j a v a 2s.co m*/ return null; }
From source file:net.solarnetwork.support.XmlSupport.java
/** * Extract a String value via an XPath expression. * //w w w . j av a2 s .c om * @param xml * the XML * @param xpath * the XPath expression * @return the String */ public String extractStringFromXml(Node xml, XPathExpression xpath) { try { return (String) xpath.evaluate(xml, XPathConstants.STRING); } catch (XPathExpressionException e) { throw new RuntimeException("Unable to extract string from XPath [" + xpath.toString() + "]", e); } }
From source file:org.apache.openaz.xacml.std.StdRequestAttributes.java
@Override public Node getContentNodeByXpathExpression(XPathExpression xpathExpression) { if (xpathExpression == null) { throw new NullPointerException("Null XPathExpression"); }//from ww w .j a va 2 s .c o m Node nodeRootThis = this.getContentRoot(); if (nodeRootThis == null) { return null; } Node matchingNode = null; try { matchingNode = (Node) xpathExpression.evaluate(nodeRootThis, XPathConstants.NODE); } catch (XPathExpressionException ex) { this.logger.warn("Failed to retrieve node for \"" + xpathExpression.toString() + "\"", ex); } return matchingNode; }
From source file:org.apache.openaz.xacml.std.StdRequestAttributes.java
@Override public NodeList getContentNodeListByXpathExpression(XPathExpression xpathExpression) { if (xpathExpression == null) { throw new NullPointerException("Null XPathExpression"); }//from w ww . ja va2s . co m Node nodeRootThis = this.getContentRoot(); if (nodeRootThis == null) { return null; } NodeList matchingNodeList = null; try { matchingNodeList = (NodeList) xpathExpression.evaluate(nodeRootThis, XPathConstants.NODESET); } catch (XPathExpressionException ex) { this.logger.warn("Failed to retrieve nodelist for \"" + xpathExpression.toString() + "\"", ex); } return matchingNodeList; }