]> git.basschouten.com Git - openhab-addons.git/blob
f9958bbf47a40894e1e376573274629cc2926fe6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.transform.xpath.internal;
14
15 import java.io.StringReader;
16
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.xpath.XPath;
20 import javax.xml.xpath.XPathConstants;
21 import javax.xml.xpath.XPathExpression;
22 import javax.xml.xpath.XPathFactory;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.transform.TransformationException;
27 import org.openhab.core.transform.TransformationService;
28 import org.osgi.service.component.annotations.Component;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.xml.sax.InputSource;
33
34 /**
35  * <p>
36  * The implementation of {@link TransformationService} which transforms the input by XPath Expressions.
37  *
38  * @author Thomas.Eichstaedt-Engelen - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(property = { "openhab.transform=XPATH" })
42 public class XPathTransformationService implements TransformationService {
43
44     private final Logger logger = LoggerFactory.getLogger(XPathTransformationService.class);
45
46     @Override
47     public @Nullable String transform(String xpathExpression, String source) throws TransformationException {
48         if (xpathExpression == null || source == null) {
49             throw new TransformationException("the given parameters 'xpath' and 'source' must not be null");
50         }
51
52         logger.debug("about to transform '{}' by the function '{}'", source, xpathExpression);
53
54         StringReader stringReader = null;
55
56         try {
57             DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
58             // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
59             domFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
60             domFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
61             domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
62             domFactory.setXIncludeAware(false);
63             domFactory.setExpandEntityReferences(false);
64             domFactory.setNamespaceAware(true);
65             domFactory.setValidating(false);
66             DocumentBuilder builder = domFactory.newDocumentBuilder();
67
68             stringReader = new StringReader(source);
69             InputSource inputSource = new InputSource(stringReader);
70             inputSource.setEncoding("UTF-8");
71
72             Document doc = builder.parse(inputSource);
73
74             XPath xpath = XPathFactory.newInstance().newXPath();
75             XPathExpression expr = xpath.compile(xpathExpression);
76
77             String transformationResult = (String) expr.evaluate(doc, XPathConstants.STRING);
78
79             logger.debug("transformation resulted in '{}'", transformationResult);
80
81             return transformationResult;
82         } catch (Exception e) {
83             throw new TransformationException("transformation throws exceptions", e);
84         } finally {
85             if (stringReader != null) {
86                 stringReader.close();
87             }
88         }
89     }
90 }