]> git.basschouten.com Git - openhab-addons.git/blob
0e36cdf8cfcc43f6ed7a5ff0ceb438c7a70c8e5f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ihc.internal.ws.datatypes;
14
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;
20
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;
27
28 import org.w3c.dom.Node;
29 import org.w3c.dom.NodeList;
30 import org.xml.sax.InputSource;
31
32 /**
33  * Class for XPath utils.
34  *
35  *
36  * @author Pauli Anttila - Initial contribution
37  */
38 public class XPathUtils {
39     private static NamespaceContext ihcNamespaceContext = new NamespaceContext() {
40         @Override
41         public String getNamespaceURI(String prefix) {
42             if (prefix == null) {
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)) {
47                 return "utcs";
48             }
49             return "utcs.values";
50         }
51
52         @Override
53         public String getPrefix(String uri) {
54             return null;
55         }
56
57         @Override
58         @SuppressWarnings("rawtypes")
59         public Iterator getPrefixes(String uri) {
60             throw new UnsupportedOperationException();
61         }
62     };
63
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);
69
70             xpath.setNamespaceContext(ihcNamespaceContext);
71             return (String) xpath.evaluate(xpathExpression, inputSource, XPathConstants.STRING);
72         }
73     }
74
75     public static boolean parseValueToBoolean(String xml, String xpathExpression)
76             throws IOException, XPathExpressionException {
77         return Boolean.parseBoolean(parseXMLValue(xml, xpathExpression));
78     }
79
80     public static String createIgnoreNameSpaceSyntaxExpr(String name) {
81         return "*[local-name() = '" + name + "']";
82     }
83
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);
89     }
90
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);
97         }
98     }
99
100     public static String getValueFromNode(Node n, String value) throws XPathExpressionException {
101         return getSpeficValueFromNode(n, XPathUtils.createIgnoreNameSpaceSyntaxExpr(value));
102     }
103 }