2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ihc.internal.ws.datatypes;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.charset.StandardCharsets;
19 import java.util.Iterator;
21 import javax.xml.namespace.NamespaceContext;
22 import javax.xml.xpath.XPath;
23 import javax.xml.xpath.XPathConstants;
24 import javax.xml.xpath.XPathExpression;
25 import javax.xml.xpath.XPathExpressionException;
26 import javax.xml.xpath.XPathFactory;
28 import org.w3c.dom.Node;
29 import org.w3c.dom.NodeList;
30 import org.xml.sax.InputSource;
33 * Class for XPath utils.
36 * @author Pauli Anttila - Initial contribution
38 public class XPathUtils {
39 private static NamespaceContext ihcNamespaceContext = new NamespaceContext() {
41 public String getNamespaceURI(String prefix) {
43 throw new IllegalArgumentException("Prefix argument can't be null");
44 } else if ("SOAP-ENV".equals(prefix)) {
45 return "http://schemas.xmlsoap.org/soap/envelope/";
46 } else if ("ns1".equals(prefix)) {
53 public String getPrefix(String uri) {
58 @SuppressWarnings("rawtypes")
59 public Iterator getPrefixes(String uri) {
60 throw new UnsupportedOperationException();
64 public static String parseXMLValue(String xml, String xpathExpression)
65 throws IOException, XPathExpressionException {
66 try (InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8.name()))) {
67 XPath xpath = XPathFactory.newInstance().newXPath();
68 InputSource inputSource = new InputSource(is);
70 xpath.setNamespaceContext(ihcNamespaceContext);
71 return (String) xpath.evaluate(xpathExpression, inputSource, XPathConstants.STRING);
75 public static boolean parseValueToBoolean(String xml, String xpathExpression)
76 throws IOException, XPathExpressionException {
77 return Boolean.parseBoolean(parseXMLValue(xml, xpathExpression));
80 public static String createIgnoreNameSpaceSyntaxExpr(String name) {
81 return "*[local-name() = '" + name + "']";
84 public static String getSpeficValueFromNode(Node n, String xpathExpr) throws XPathExpressionException {
85 XPath xpath = XPathFactory.newInstance().newXPath();
86 xpath.setNamespaceContext(ihcNamespaceContext);
87 XPathExpression pathExpr = xpath.compile(xpathExpr);
88 return (String) pathExpr.evaluate(n, XPathConstants.STRING);
91 public static NodeList parseList(String xml, String xpathExpression) throws XPathExpressionException, IOException {
92 try (InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8.name()))) {
93 XPath xpath = XPathFactory.newInstance().newXPath();
94 InputSource inputSource = new InputSource(is);
95 xpath.setNamespaceContext(ihcNamespaceContext);
96 return (NodeList) xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET);
100 public static String getValueFromNode(Node n, String value) throws XPathExpressionException {
101 return getSpeficValueFromNode(n, XPathUtils.createIgnoreNameSpaceSyntaxExpr(value));