List of usage examples for javax.xml.xpath XPathExpression evaluate
public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;
From source file:Main.java
private static Object evaluateXPath(String path, Node e, QName type) { try {//from ww w .j av a2 s .c om XPathExpression expr = compiledString.get(path); if (expr == null) { expr = xpath.compile(path); compiledString.put(path, expr); } return expr.evaluate(e, type); } catch (XPathExpressionException e1) { throw new IllegalStateException("Wrong XPATH expression: " + path, e1); //SHOULD NEVER HAPPEN IF THE EXPRESSIONS ARE WELL MADE INSIDE THE CODE } }
From source file:Main.java
public static Iterable<Node> eval(Node element, String path) throws XPathExpressionException { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); // xpath.setNamespaceContext(new NamespaceContext() { ////from w w w . ja v a 2s . c om // /** // * @WARNING this code will work only if the namespace is present at // * each node of the xml dom and within the xpath queries. // * Otherwise you can fix like that: // * // * <pre> // * // FIXME: this is a // * hack!! if ("atom".equals(prefix)) return // * "http://www.w3.org/2005/Atom"; but it wont work with // * </pre> // * // * different namespaces // * @see // javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String) // */ // public String getNamespaceURI(String prefix) { // String namespace = DOMUtil.lookupNamespaceURI(node, prefix); // return namespace; // } // // public String getPrefix(String namespaceURI) { // String prefix = node.lookupPrefix(namespaceURI); // return prefix; // } // // public Iterator<?> getPrefixes(final String namespaceURI) { // return new Iterator<String>() { // String ns = getPrefix(namespaceURI); // // public boolean hasNext() { // return ns != null; // } // // public String next() { // String r = ns; // ns = null; // return r; // } // // public void remove() { // } // // }; // } // }); XPathExpression expr = xpath.compile(path); final NodeList result = (NodeList) expr.evaluate(element, XPathConstants.NODESET); return new Iterable<Node>() { public Iterator<Node> iterator() { return new Iterator<Node>() { int len = result.getLength(); int pos; public boolean hasNext() { return pos < len; } public Node next() { if (pos >= len) { return null; } return result.item(pos++); } public void remove() { throw new IllegalAccessError(); } }; } }; }
From source file:com.github.robozonky.app.version.UpdateMonitor.java
/** * Parse XML using XPath and retrieve the version string. * @param xml XML in question.//w w w . j a v a 2s . c o m * @return The version string. * @throws ParserConfigurationException Failed parsing XML. * @throws IOException Failed I/O. * @throws SAXException Failed reading XML. * @throws XPathExpressionException XPath parsing problem. */ private static VersionIdentifier parseVersionString(final InputStream xml) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { final DocumentBuilderFactory factory = XmlUtil.getDocumentBuilderFactory(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.parse(xml); final XPathFactory xPathfactory = XPathFactory.newInstance(); final XPath xpath = xPathfactory.newXPath(); final XPathExpression expr = xpath.compile("/metadata/versioning/versions/version"); return UpdateMonitor.parseNodeList((NodeList) expr.evaluate(doc, XPathConstants.NODESET)); }
From source file:Main.java
/** * Checks for a given element whether it can find an attribute which matches * the XPath expression supplied. Returns {@link Node} if exists. * //from w w w . ja v a2s .co m * @param xPathExpression the xPathExpression (required) * @param element (required) * @return the Node if discovered (null if not found) */ public static Node findFirstAttribute(final String xPathExpression, final Element element) { Node attr = null; try { XPathExpression expr = COMPILED_EXPRESSION_CACHE.get(xPathExpression); if (expr == null) { expr = XPATH.compile(xPathExpression); COMPILED_EXPRESSION_CACHE.put(xPathExpression, expr); } attr = (Node) expr.evaluate(element, XPathConstants.NODE); } catch (final XPathExpressionException e) { throw new IllegalArgumentException("Unable evaluate xpath expression", e); } return attr; }
From source file:Main.java
/** * Use an Xpath expression on the given node or document. * * @param _xpathExpression xpath expression string * @param _xmlDocumentOrNode a {@link Document} or {@link Node} object * @return NodeList never null/*w w w .jav a 2 s. c om*/ * @throws IOException on error */ public static NodeList applyXpathExpressionToDocument(String _xpathExpression, Node _xmlDocumentOrNode) throws IOException { XPathFactory xfactory = XPathFactory.newInstance(); XPath xpath = xfactory.newXPath(); XPathExpression expr = null; try { expr = xpath.compile(_xpathExpression); } catch (XPathExpressionException _ex) { throw new IOException(_ex); } Object result = null; try { result = expr.evaluate(_xmlDocumentOrNode, XPathConstants.NODESET); } catch (Exception _ex) { throw new IOException(_ex); } return (NodeList) result; }
From source file:Main.java
/** * Checks for a given element whether it can find an attribute which matches the * XPath expression supplied. Returns {@link Node} if exists. * //from w ww . j av a 2 s .com * @param xPathExpression the xPathExpression (required) * @param element (required) * * @return the Node if discovered (null if not found) */ public static Node findFirstAttribute(String xPathExpression, Element element) { Node attr = null; try { XPathExpression expr = compiledExpressionCache.get(xPathExpression); if (expr == null) { expr = xpath.compile(xPathExpression); compiledExpressionCache.put(xPathExpression, expr); } attr = (Node) expr.evaluate(element, XPathConstants.NODE); } catch (XPathExpressionException e) { throw new IllegalArgumentException("Unable evaluate xpath expression", e); } return attr; }
From source file:org.ensembl.gti.seqstore.database.cramstore.EnaCramSubmitter.java
protected static boolean isSuccess(Document doc) { try {/*from w w w .ja va 2 s . c o m*/ XPathExpression success = xpath.compile("/RECEIPT[@success]"); Node nl = (Node) success.evaluate(doc, XPathConstants.NODE); return "true".equals(nl.getAttributes().getNamedItem("success").getTextContent()); } catch (XPathExpressionException e) { throw new EnaSubmissionException("Could not parse submission receipt", e); } }
From source file:org.ensembl.gti.seqstore.database.cramstore.EnaCramSubmitter.java
protected static List<String> getElems(Document doc, String tag) { try {//from w w w .ja v a2 s . c o m List<String> elems = new ArrayList<>(); XPathExpression success = xpath.compile("//" + tag); NodeList nl = (NodeList) success.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { elems.add(nl.item(i).getTextContent()); } return elems; } catch (XPathExpressionException e) { throw new EnaSubmissionException("Could not parse submission receipt", e); } }
From source file:Main.java
/** * Checks in under a given root element whether it can find a child elements * which match the XPath expression supplied. Returns a {@link List} of * {@link Element} if they exist./*from w w w .j av a 2 s . com*/ * * Please note that the XPath parser used is NOT namespace aware. So if you * want to find a element <beans><sec:http> you need to use the following * XPath expression '/beans/http'. * * @param xPathExpression the xPathExpression * @param root the parent DOM element * @return a {@link List} of type {@link Element} if discovered, otherwise an empty list (never null) */ public static List<Element> findElements(String xPathExpression, Element root) { List<Element> elements = new ArrayList<Element>(); NodeList nodes = null; try { XPathExpression expr = compiledExpressionCache.get(xPathExpression); if (expr == null) { expr = xpath.compile(xPathExpression); compiledExpressionCache.put(xPathExpression, expr); } nodes = (NodeList) expr.evaluate(root, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new IllegalArgumentException("Unable evaluate xpath expression", e); } for (int i = 0, n = nodes.getLength(); i < n; i++) { elements.add((Element) nodes.item(i)); } return elements; }
From source file:io.apigee.buildTools.enterprise4g.utils.PackageConfigurer.java
public static void configurePackage(String env, File configFile) throws Exception { Transformer transformer = tltf.get(); // get the list of files in proxies folder XMLFileListUtil listFileUtil = new XMLFileListUtil(); ConfigTokens conf = FileReader.getBundleConfigs(configFile); Map<String, List<File>> filesToProcess = new HashMap<String, List<File>>(); filesToProcess.put("proxy", listFileUtil.getProxyFiles(configFile)); filesToProcess.put("policy", listFileUtil.getPolicyFiles(configFile)); filesToProcess.put("target", listFileUtil.getTargetFiles(configFile)); doTokenReplacement(env, conf, filesToProcess); // special case ... File proxyFile = listFileUtil.getAPIProxyFiles(configFile).get(0); Document xmlDoc = FileReader.getXMLDocument(proxyFile); // there would be only one file, at least one file XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expression = xpath.compile("/APIProxy/Description"); NodeList nodes = (NodeList) expression.evaluate(xmlDoc, XPathConstants.NODESET); if (nodes.item(0).hasChildNodes()) { // sets the description to whatever is in the <proxyname>.xml file nodes.item(0).setTextContent(expression.evaluate(xmlDoc)); } else {//from w ww .j a v a 2 s . com // if Description is empty, then it reverts back to appending the username, git hash, etc nodes.item(0).setTextContent(getComment(proxyFile)); } DOMSource source = new DOMSource(xmlDoc); StreamResult result = new StreamResult(proxyFile); transformer.transform(source, result); }