Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / xml / XPathReader.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.infrastructure.xml;
23
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;
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
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(XPathReader.class);
45
46     private String xmlFileName = null;
47     private InputStream xmlStream = null;
48     private Document xmlDocument;
49     private XPath xpath;
50
51     /**
52      * Construct Reader for the file passed in.
53      *
54      * @param xmlFileName the xml file name
55      */
56     public XPathReader(final String xmlFileName) {
57         this.xmlFileName = xmlFileName;
58         init();
59     }
60
61     /**
62      * Construct Reader for the stream passed in.
63      *
64      * @param xmlStream a stream of XML
65      */
66     public XPathReader(final InputStream xmlStream) {
67         this.xmlStream = xmlStream;
68         init();
69     }
70
71     /**
72      * Initialise the x-path reader.
73      */
74     private void init() {
75         try {
76             LOGGER.info("Initializing XPath reader");
77             DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
78             df.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
79
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);
86             } else {
87                 // We have an error
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(), ex);
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(), ex);
112             return null;
113         }
114     }
115 }