2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.infrastructure.xml;
24 import java.io.InputStream;
25 import javax.xml.XMLConstants;
26 import javax.xml.namespace.QName;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.xpath.XPath;
29 import javax.xml.xpath.XPathExpression;
30 import javax.xml.xpath.XPathExpressionException;
31 import javax.xml.xpath.XPathFactory;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34 import org.w3c.dom.Document;
37 * A generic class for applying the XPATH queries on XML files.
39 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
41 public class XPathReader {
43 // Logger for this class
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(XPathReader.class);
46 private String xmlFileName = null;
47 private InputStream xmlStream = null;
48 private Document xmlDocument;
52 * Construct Reader for the file passed in.
54 * @param xmlFileName the xml file name
56 public XPathReader(final String xmlFileName) {
57 this.xmlFileName = xmlFileName;
62 * Construct Reader for the stream passed in.
64 * @param xmlStream a stream of XML
66 public XPathReader(final InputStream xmlStream) {
67 this.xmlStream = xmlStream;
72 * Initialise the x-path reader.
76 LOGGER.info("Initializing XPath reader");
77 DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
78 df.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
80 // Check if this is operating on a file
81 if (xmlFileName != null) {
82 xmlDocument = df.newDocumentBuilder().parse(xmlFileName);
83 } else if (xmlStream != null) {
84 // Check if this is operating on a stream
85 xmlDocument = df.newDocumentBuilder().parse(xmlStream);
88 LOGGER.error("XPath reader not initialized with either a file or a stream");
92 xpath = XPathFactory.newInstance().newXPath();
93 LOGGER.info("Initialized XPath reader");
94 } catch (final Exception ex) {
95 LOGGER.error("Error parsing XML file/stream from XPath reading, reason :\n" + ex.getMessage(), ex);
100 * Read items from the file using xpath.
102 * @param expression x-path expression
103 * @param returnType XML node Set
104 * @return last node collected
106 public Object read(final String expression, final QName returnType) {
108 final XPathExpression xPathExpression = xpath.compile(expression);
109 return xPathExpression.evaluate(xmlDocument, returnType);
110 } catch (final XPathExpressionException ex) {
111 LOGGER.error("Failed to read XML file for XPath processing, reason:\n" + ex.getMessage(), ex);