Updated sal-netconf-connector mountpoint integration
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / XmlDocumentUtils.java
1 package org.opendaylight.controller.sal.connect.netconf;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.opendaylight.yangtools.yang.common.QName;
8 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
9 import org.opendaylight.yangtools.yang.data.api.Node;
10 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
11 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.NodeList;
15
16 public class XmlDocumentUtils {
17
18     public static CompositeNode toCompositeNode(Document doc) {
19         return (CompositeNode) toCompositeNode(doc.getDocumentElement());
20     }
21
22     private static Node<?> toCompositeNode(Element element) {
23         String orgNamespace = element.getNamespaceURI();
24         URI biNamespace = null;
25         if (orgNamespace != null) {
26             biNamespace = URI.create(orgNamespace);
27         }
28         QName qname = new QName(biNamespace, element.getLocalName());
29
30         List<Node<?>> values = new ArrayList<>();
31         NodeList nodes = element.getChildNodes();
32         boolean isSimpleObject = false;
33         String value = null;
34         for (int i = 0; i < nodes.getLength(); i++) {
35             org.w3c.dom.Node child = nodes.item(i);
36             if (child instanceof Element) {
37                 isSimpleObject = false;
38                 values.add(toCompositeNode((Element) child));
39             }
40             if (!isSimpleObject && child instanceof org.w3c.dom.Text) {
41                 value = element.getTextContent();
42                 if (value.matches(".*\\w.*")) {
43                     isSimpleObject = true;
44                     break;
45                 }
46             }
47         }
48
49         if (isSimpleObject) {
50             return new SimpleNodeTOImpl<>(qname, null, value);
51         }
52         return new CompositeNodeTOImpl(qname, null, values);
53     }
54 }