Merge "Added initial draft of Normalized Yang Data Tree model."
[yangtools.git] / restconf / restconf-util / src / main / java / org / opendaylight / yangtools / restconf / utils / XmlTools.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.restconf.utils;
9
10 import java.io.InputStream;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15 import javax.xml.bind.JAXBContext;
16 import javax.xml.bind.JAXBElement;
17 import javax.xml.bind.Unmarshaller;
18 import javax.xml.datatype.DatatypeFactory;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.transform.stream.StreamSource;
22 import org.opendaylight.yangtools.restconf.client.api.dto.RestEventStreamInfo;
23 import org.opendaylight.yangtools.restconf.client.api.dto.RestModule;
24 import org.opendaylight.yangtools.restconf.client.api.dto.RestRpcService;
25 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
26 import org.opendaylight.yangtools.yang.binding.RpcService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33
34 public class XmlTools {
35
36     private static final Logger logger = LoggerFactory.getLogger(XmlTools.class.toString());
37     static final String JAXP_SCHEMA_LOCATION =
38             "http://java.sun.com/xml/jaxp/properties/schemaSource";
39
40     public static Object unmarshallXml(Class<?> clazz,InputStream xmlStream,String namespace) throws Exception{
41         if (null == xmlStream){
42             throw new Exception("XML input stream can't be null");
43         }
44         JAXBContext jc = JAXBContext.newInstance(clazz);
45
46         Unmarshaller unmarshaller = jc.createUnmarshaller();
47         StreamSource xmlInputSource = new StreamSource(xmlStream);
48         JAXBElement<?> obj = unmarshaller.unmarshal(xmlInputSource, clazz);
49         return obj;
50     }
51
52     public static Document fromXml(InputStream is) throws Exception {
53         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
54         dbFactory.setNamespaceAware(true);
55         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
56
57         Document doc = dBuilder.parse(is);
58         doc.getDocumentElement().normalize();
59         return doc;
60     }
61     public static Set<RpcService> fromInputStream(InputStream is) throws Exception {
62         Document doc = fromXml(is);
63
64         return fromNodeList(doc.getElementsByTagName("play"));
65
66     }
67     public static Set<RpcService> fromNodeList(NodeList nodeList) throws Exception {
68         Set<RpcService> rpcServices = new HashSet<RpcService>();
69         for (int i =0; i < nodeList.getLength(); i++){
70             org.w3c.dom.Node nNode = nodeList.item(i);
71             if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
72                 rpcServices.add(fromNode(nNode));
73             }
74         }
75         return rpcServices;
76
77     }
78     public static RestRpcService fromNode(org.w3c.dom.Node node){
79         Element eElement = (Element) node;
80         RestRpcService rpcService = new RestRpcService(eElement.getAttribute("xmlns"));
81
82         return rpcService;
83     }
84     private static EventStreamInfo restEventStreamInfoFromNode(org.w3c.dom.Node node) throws Exception {
85         Element eElement = (Element) node;
86         RestEventStreamInfo eventStreamInfo = new RestEventStreamInfo();
87         eventStreamInfo.setDescription(eElement.getElementsByTagName("description").item(0).getTextContent());
88         eventStreamInfo.setIdentifier(eElement.getElementsByTagName("identifier").item(0).getTextContent());
89         if (null != eElement.getElementsByTagName("replay-log-creation-time")
90                 && eElement.getElementsByTagName("replay-log-creation-time").getLength()>0
91                 && !eElement.getElementsByTagName("replay-log-creation-time").item(0).getTextContent().equals("")){
92             eventStreamInfo.setReplayLogCreationTime(DatatypeFactory
93                     .newInstance()
94                     .newXMLGregorianCalendar(eElement
95                             .getElementsByTagName("replay-log-creation-time")
96                             .item(0)
97                             .getTextContent())
98                     .toGregorianCalendar()
99                     .getTime());
100         }
101         eventStreamInfo.setReplaySupported(Boolean.parseBoolean(eElement.getElementsByTagName("replay-support").item(0).getTextContent()));
102         return eventStreamInfo;
103     }
104
105     public static Set<EventStreamInfo> evenStreamsFromInputStream(InputStream is) throws Exception {
106         Document doc = fromXml(is);
107         return streamInfoFromNodeList(doc.getElementsByTagName("stream"));
108     }
109     private static Set<EventStreamInfo> streamInfoFromNodeList(NodeList nodeList) throws Exception {
110         Set<EventStreamInfo> rpcServices = new HashSet<EventStreamInfo>();
111         for (int i =0; i < nodeList.getLength(); i++){
112             org.w3c.dom.Node nNode = nodeList.item(i);
113             if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
114                 rpcServices.add(restEventStreamInfoFromNode(nNode));
115             }
116         }
117         return rpcServices;
118     }
119
120     public static List<RestModule> getModulesFromInputStream(InputStream is) throws Exception {
121         Document doc = fromXml(is);
122         return restModulesFromNodeList(doc.getElementsByTagName("module"));
123     }
124     private static List<RestModule> restModulesFromNodeList(NodeList nodeList) throws Exception {
125         List<RestModule> modules = new ArrayList<RestModule>();
126         for (int i =0; i < nodeList.getLength(); i++){
127             Node nNode = nodeList.item(i);
128             if (nNode.getNodeType() == Node.ELEMENT_NODE){
129                 modules.add(restModulefromNode(nNode));
130             }
131         }
132         return modules;
133     }
134     private static RestModule restModulefromNode(Node node){
135         Element eElement = (Element) node;
136         RestModule restModule = new RestModule();
137         try {
138             restModule.setName(eElement.getElementsByTagName("name").item(0).getTextContent());
139             restModule.setNamespace(eElement.getElementsByTagName("namespace").item(0).getTextContent());
140             restModule.setRevision(eElement.getElementsByTagName("revision").item(0).getTextContent());
141         } catch (NullPointerException npe){
142             throw new IllegalStateException("Incomplete module data in xml.");
143         }
144         return restModule;
145     }
146
147 }