5f16a55be6cabfd771cb9704a1f051cd34724220
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / ExtractXMLTag.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.transportpce.device.tools;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.StringWriter;
21
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.TransformerFactory;
28 import javax.xml.transform.dom.DOMSource;
29 import javax.xml.transform.stream.StreamResult;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
37 import org.xml.sax.SAXException;
38
39 /**
40  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
41  *
42  */
43 public class ExtractXMLTag {
44     private static final Logger LOG = LoggerFactory.getLogger(ExtractXMLTag.class);
45     private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
46
47     private ExtractXMLTag() {
48
49     }
50
51     /**
52      * Extract tag xml element
53      * from xml file.
54      *
55      * @param oper_path path to operational file
56      * @param tag XML tag to be extracted
57      * @param namespace XML tag namespace (optional)
58      * @return String XML extract element
59      */
60     public static String extractTagElement(String oper_path, String tag, String namespace) {
61         String result = null;
62         LOG.info("Getting {} xml data", tag);
63         DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
64         try {
65             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
66             Document doc = docBuilder.parse(new File(classLoader.getResource(oper_path).getFile()));
67             Document extract = extractDom(doc, tag, namespace);
68             if (extract != null) {
69                 result = getStringFromDocument(extract);
70             } else {
71                 throw new NullPointerException("Failed to extract data from document !");
72             }
73         } catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
74             LOG.error("failed to extract data from document", e);
75         }
76         return result;
77     }
78
79     public static Document extractDom(Document doc, String tag, String namespace) throws ParserConfigurationException {
80         NodeList nodeList = doc.getElementsByTagName(tag);
81         if (nodeList.getLength() > 0) {
82             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
83             DocumentBuilder builder = dbf.newDocumentBuilder();
84             Document result = builder.newDocument();
85             Element root = result.createElementNS("urn:ietf:params:xml:ns:netconf:base:1.0", "data");
86             result.appendChild(root);
87             Node newNode = result.importNode(nodeList.item(0), true);
88             result.getDocumentElement().appendChild(newNode);
89             return result;
90         } else {
91             LOG.warn("no {} object present in doc",tag);
92             return null;
93         }
94     }
95
96     public static String getStringFromDocument(Document doc) throws TransformerException {
97         DOMSource domSource = new DOMSource(doc);
98         StringWriter writer = new StringWriter();
99         StreamResult result = new StreamResult(writer);
100         TransformerFactory tf = TransformerFactory.newInstance();
101         Transformer transformer = tf.newTransformer();
102         transformer.transform(domSource, result);
103         return writer.toString();
104     }
105
106 }