2 * Copyright (c) 2018 Orange and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.fd.honeycomb.transportpce.device.tools;
19 import java.io.IOException;
20 import java.io.StringWriter;
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;
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;
40 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
43 public class ExtractXMLTag {
44 private static final Logger LOG = LoggerFactory.getLogger(ExtractXMLTag.class);
45 private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
47 private ExtractXMLTag() {
52 * Extract tag xml element
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
60 public static String extractTagElement(String oper_path, String tag, String namespace) {
62 LOG.info("Getting {} xml data", tag);
63 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
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);
71 throw new NullPointerException("Failed to extract data from document !");
73 } catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
74 LOG.error("failed to extract data from document", e);
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);
91 LOG.warn("no {} object present in doc",tag);
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();