f677e6b4a328b253910e6acba08794710a9a93f8
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.xml;
22
23 import java.io.InputStream;
24
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;
31
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34 import org.w3c.dom.Document;
35
36 /**
37  * A generic class for applying the XPATH queries on XML files.
38  *
39  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
40  */
41 public class XPathReader {
42     // Logger for this class
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(XPathReader.class);
44
45     private String xmlFileName = null;
46     private InputStream xmlStream = null;
47     private Document xmlDocument;
48     private XPath xPath;
49
50     /**
51      * Construct Reader for the file passed in.
52      *
53      * @param xmlFileName the xml file name
54      */
55     public XPathReader(final String xmlFileName) {
56         this.xmlFileName = xmlFileName;
57         init();
58     }
59
60     /**
61      * Construct Reader for the stream passed in.
62      *
63      * @param xmlStream a stream of XML
64      */
65     public XPathReader(final InputStream xmlStream) {
66         this.xmlStream = xmlStream;
67         init();
68     }
69
70     /**
71      * Initialise the x-path reader.
72      */
73     private void init() {
74         try {
75             LOGGER.info("Initializing XPath reader");
76
77             // Check if this is operating on a file
78             if (xmlFileName != null) {
79                 xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFileName);
80             }
81             // Check if this is operating on a stream
82             else if (xmlStream != null) {
83                 xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlStream);
84
85             }
86             // We have an error
87             else {
88                 LOGGER.error("XPath reader not initialized with either a file or a stream");
89                 return;
90             }
91
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());
96         }
97     }
98
99     /**
100      * Read items from the file using xpath.
101      *
102      * @param expression x-path expression
103      * @param returnType XML node Set
104      * @return last node collected
105      */
106     public Object read(final String expression, final QName returnType) {
107         try {
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());
112             return null;
113         }
114     }
115 }