List of usage examples for javax.xml.xpath XPath setNamespaceContext
public void setNamespaceContext(NamespaceContext nsContext);
From source file:com.cloudseal.spring.client.namespace.Utility.java
public static void removeNode(final Element rootElement, final String xPathLocation) throws XPathExpressionException { final XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new CloudSealNamespaceContext()); final Node node = (Node) xPath.evaluate(xPathLocation, rootElement, XPathConstants.NODE); final short nodeType = node.getNodeType(); switch (nodeType) { case Node.ELEMENT_NODE: final Node parent = node.getParentNode(); parent.removeChild(node);//from www . j a va 2 s. co m break; case Node.ATTRIBUTE_NODE: final Attr attribute = (Attr) node; final Element element = attribute.getOwnerElement(); element.removeAttributeNode(attribute); break; default: throw new IllegalArgumentException("Not supported node type: " + nodeType); } }
From source file:ch.entwine.weblounge.bridge.oaipmh.harvester.OaiPmhResponse.java
/** * Create an XPath object suitable for processing OAI-PMH response documents. *//* w w w .j av a 2s .co m*/ public static XPath createXPath() { XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(OaiPmhNamespaceContext.getContext()); return xPath; }
From source file:Main.java
public static XPath getSchemaXPath() { XPath xPath = schemaXPathThreadLocal.get(); if (xPath == null) { xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new NamespaceContext() { @Override//from w w w. ja va2s. c om public String getNamespaceURI(String prefix) { return XMLConstants.W3C_XML_SCHEMA_NS_URI; } @Override public String getPrefix(String namespaceURI) { return "xs"; } @Override public Iterator getPrefixes(String namespaceURI) { return Arrays.asList("xs").iterator(); } }); schemaXPathThreadLocal.set(xPath); } return xPath; }
From source file:com.github.radium226.github.maven.MetaDataDownloader.java
public static String evaluateXPath(InputStream pomInputStream, String expression) throws XPathExpressionException { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); xPath.setNamespaceContext(new NamespaceContext() { @Override/*from w w w .j av a2s . c o m*/ public String getNamespaceURI(String prefix) { if (prefix.equals("ns")) { return "http://maven.apache.org/POM/4.0.0"; } return null; } @Override public String getPrefix(String namespaceURI) { return null; } @Override public Iterator getPrefixes(String namespaceURI) { return null; } }); XPathExpression xPathExpression = xPath.compile(expression); String version = (String) xPathExpression.evaluate(new InputSource(pomInputStream), XPathConstants.STRING); return version; }
From source file:net.bpelunit.test.util.TestUtil.java
public static Node getNode(Element literalData, NamespaceContextImpl context, String string) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(context); return (Node) xpath.evaluate(string, literalData, XPathConstants.NODE); }
From source file:io.wcm.testing.mock.osgi.OsgiMetadataUtil.java
public static Set<String> getServiceInterfaces(Document document) { Set<String> serviceInterfaces = new HashSet<>(); if (document != null) { try {//w ww . java 2s .c o m XPath xpath = XPATH_FACTORY.newXPath(); xpath.setNamespaceContext(NAMESPACE_CONTEXT); NodeList nodes = (NodeList) xpath.evaluate( "/components/component[1]/service/provide[@interface!='']", document, XPathConstants.NODESET); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String serviceInterface = node.getAttributes().getNamedItem("interface").getNodeValue(); if (StringUtils.isNotBlank(serviceInterface)) { serviceInterfaces.add(serviceInterface); } } } } catch (XPathExpressionException ex) { throw new RuntimeException("Error evaluating XPath.", ex); } } return serviceInterfaces; }
From source file:io.wcm.testing.mock.osgi.OsgiMetadataUtil.java
public static Map<String, Object> getProperties(Document document) { Map<String, Object> props = new HashMap<>(); if (document != null) { try {/*from w ww . ja v a 2 s . c om*/ XPath xpath = XPATH_FACTORY.newXPath(); xpath.setNamespaceContext(NAMESPACE_CONTEXT); NodeList nodes = (NodeList) xpath.evaluate( "/components/component[1]/property[@name!='' and @value!='']", document, XPathConstants.NODESET); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String name = node.getAttributes().getNamedItem("name").getNodeValue(); String value = node.getAttributes().getNamedItem("value").getNodeValue(); String type = null; Node typeAttribute = node.getAttributes().getNamedItem("type"); if (typeAttribute != null) { type = typeAttribute.getNodeValue(); } if (StringUtils.equals("Integer", type)) { props.put(name, Integer.parseInt(value)); } else { props.put(name, value); } } } } catch (XPathExpressionException ex) { throw new RuntimeException("Error evaluating XPath.", ex); } } return props; }
From source file:org.opencastproject.remotetest.util.Utils.java
public static Object xpath(Document document, String path, QName returnType) throws XPathExpressionException, TransformerException { XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new UniversalNamespaceResolver(document)); return xPath.compile(path).evaluate(document, returnType); }
From source file:be.fedict.eid.applet.service.signer.odf.ODFUtil.java
/** * Check if an ODF package is self-contained, i.e. content files don't have * OLE objects linked to external files/*from w w w.j a v a 2 s .c om*/ * * @param odfUrl * @return * @throws IOException * @throws ParserConfigurationException * @throws SAXException * @throws XPathExpressionException */ public static boolean isSelfContained(URL odfUrl) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException { InputStream odfInputStream = odfUrl.openStream(); List zipEntries = getZipEntriesAsList(odfInputStream); odfInputStream = odfUrl.openStream(); ZipInputStream odfZipInputStream = new ZipInputStream(odfInputStream); ZipEntry zipEntry; XPathFactory factory = XPathFactory.newInstance(); /* Maybe a bit overkill, but implementations can use other prefixes */ ODFNamespaceContext namespaceContext = new ODFNamespaceContext(); XPath xpath = factory.newXPath(); xpath.setNamespaceContext(namespaceContext); XPathExpression expression = xpath.compile("//draw:object/@xlink:href|" + "//draw:object-ole/@xlink:href|" + "//draw:image/@xlink:href|" + "//draw:floating-frame/@xlink:href"); while (null != (zipEntry = odfZipInputStream.getNextEntry())) { if (isContentFile(zipEntry)) { /* TODO: pure SAX is probably more memory-efficient */ Document content = ODFUtil.loadDocument(odfZipInputStream); NodeList nodes = (NodeList) expression.evaluate(content, XPathConstants.NODESET); return checkNodes(nodes, zipEntries); } } return true; }
From source file:Main.java
public static final String[] executeXPathExpression(InputSource inputSource, String xpathExpression, String namespace) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, TransformerException { // optional namespace spec: xmlns:prefix:URI String nsPrefix = null;/*from ww w. j av a2 s . c o m*/ String nsUri = null; if (namespace != null && namespace.startsWith("xmlns:")) { String[] nsDef = namespace.substring("xmlns:".length()).split("="); if (nsDef.length == 2) { nsPrefix = nsDef[0]; nsUri = nsDef[1]; } } // Parse XML to DOM DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); Document doc = dbFactory.newDocumentBuilder().parse(inputSource); // Find nodes by XPATH XPathFactory xpFactory = XPathFactory.newInstance(); XPath xpath = xpFactory.newXPath(); // namespace? if (nsPrefix != null) { final String myPrefix = nsPrefix; final String myUri = nsUri; xpath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { return myPrefix.equals(prefix) ? myUri : null; } public String getPrefix(String namespaceURI) { return null; // we are not using this. } public Iterator<?> getPrefixes(String namespaceURI) { return null; // we are not using this. } }); } XPathExpression expr = xpath.compile(xpathExpression); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); List<String> lines = new ArrayList<>(); for (int i = 0; i < nodes.getLength(); i++) { lines.add((indenting(nodes.item(i)))); } return lines.toArray(new String[lines.size()]); }