2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.xml;
23 import java.io.InputStream;
25 import javax.xml.namespace.QName;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.xpath.XPath;
28 import javax.xml.xpath.XPathExpression;
29 import javax.xml.xpath.XPathExpressionException;
30 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 {
42 // Logger for this class
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(XPathReader.class);
45 private String xmlFileName = null;
46 private InputStream xmlStream = null;
47 private Document xmlDocument;
51 * Construct Reader for the file passed in.
53 * @param xmlFileName the xml file name
55 public XPathReader(final String xmlFileName) {
56 this.xmlFileName = xmlFileName;
61 * Construct Reader for the stream passed in.
63 * @param xmlStream a stream of XML
65 public XPathReader(final InputStream xmlStream) {
66 this.xmlStream = xmlStream;
71 * Initialise the x-path reader.
75 LOGGER.info("Initializing XPath reader");
77 // Check if this is operating on a file
78 if (xmlFileName != null) {
79 xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFileName);
81 // Check if this is operating on a stream
82 else if (xmlStream != null) {
83 xmlDocument = DocumentBuilderFactory.newInstance().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());
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());