Merge "Added tests for yang.model.util"
[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.IOException;
11 import java.io.InputStream;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.xml.bind.JAXBContext;
18 import javax.xml.bind.JAXBElement;
19 import javax.xml.bind.JAXBException;
20 import javax.xml.bind.Unmarshaller;
21 import javax.xml.datatype.DatatypeConfigurationException;
22 import javax.xml.datatype.DatatypeFactory;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.transform.stream.StreamSource;
27
28 import org.opendaylight.yangtools.restconf.client.api.dto.RestEventStreamInfo;
29 import org.opendaylight.yangtools.restconf.client.api.dto.RestModule;
30 import org.opendaylight.yangtools.restconf.client.api.dto.RestRpcService;
31 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
32 import org.opendaylight.yangtools.yang.binding.RpcService;
33 import org.w3c.dom.DOMException;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.Node;
37 import org.w3c.dom.NodeList;
38 import org.xml.sax.SAXException;
39
40 import com.google.common.base.Preconditions;
41
42 public final class XmlTools {
43     private static final String JAXP_SCHEMA_LOCATION =
44             "http://java.sun.com/xml/jaxp/properties/schemaSource";
45
46     private XmlTools() {
47         throw new UnsupportedOperationException("Utility class should not be instantiated");
48     }
49
50     public static Object unmarshallXml(final Class<?> clazz,final InputStream xmlStream,final String namespace) throws JAXBException {
51         Preconditions.checkNotNull(xmlStream);
52
53         JAXBContext jc = JAXBContext.newInstance(clazz);
54
55         Unmarshaller unmarshaller = jc.createUnmarshaller();
56         StreamSource xmlInputSource = new StreamSource(xmlStream);
57         JAXBElement<?> obj = unmarshaller.unmarshal(xmlInputSource, clazz);
58         return obj;
59     }
60
61     public static Document fromXml(final InputStream is) throws IOException, ParserConfigurationException, SAXException {
62         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
63         dbFactory.setNamespaceAware(true);
64         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
65
66         Document doc = dBuilder.parse(is);
67         doc.getDocumentElement().normalize();
68         return doc;
69     }
70
71     public static Set<RpcService> fromInputStream(final InputStream is) throws IOException, ParserConfigurationException, SAXException {
72         Document doc = fromXml(is);
73
74         return fromNodeList(doc.getElementsByTagName("play"));
75     }
76
77     public static Set<RpcService> fromNodeList(final NodeList nodeList) {
78         Set<RpcService> rpcServices = new HashSet<RpcService>();
79         for (int i =0; i < nodeList.getLength(); i++){
80             org.w3c.dom.Node nNode = nodeList.item(i);
81             if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
82                 rpcServices.add(fromNode(nNode));
83             }
84         }
85         return rpcServices;
86     }
87
88     public static RestRpcService fromNode(final org.w3c.dom.Node node) {
89         Element eElement = (Element) node;
90         RestRpcService rpcService = new RestRpcService(eElement.getAttribute("xmlns"));
91
92         return rpcService;
93     }
94
95     private static EventStreamInfo restEventStreamInfoFromNode(final org.w3c.dom.Node node) throws DOMException, DatatypeConfigurationException {
96         Element eElement = (Element) node;
97         RestEventStreamInfo eventStreamInfo = new RestEventStreamInfo();
98         eventStreamInfo.setDescription(eElement.getElementsByTagName("description").item(0).getTextContent());
99         eventStreamInfo.setIdentifier(eElement.getElementsByTagName("identifier").item(0).getTextContent());
100         if (null != eElement.getElementsByTagName("replay-log-creation-time")
101                 && eElement.getElementsByTagName("replay-log-creation-time").getLength()>0
102                 && !eElement.getElementsByTagName("replay-log-creation-time").item(0).getTextContent().equals("")){
103             eventStreamInfo.setReplayLogCreationTime(DatatypeFactory
104                     .newInstance()
105                     .newXMLGregorianCalendar(eElement
106                             .getElementsByTagName("replay-log-creation-time")
107                             .item(0)
108                             .getTextContent())
109                     .toGregorianCalendar()
110                     .getTime());
111         }
112         eventStreamInfo.setReplaySupported(Boolean.parseBoolean(eElement.getElementsByTagName("replay-support").item(0).getTextContent()));
113         return eventStreamInfo;
114     }
115
116     public static Set<EventStreamInfo> evenStreamsFromInputStream(final InputStream is) throws DOMException, DatatypeConfigurationException, IOException, ParserConfigurationException, SAXException {
117         Document doc = fromXml(is);
118         return streamInfoFromNodeList(doc.getElementsByTagName("stream"));
119     }
120
121     private static Set<EventStreamInfo> streamInfoFromNodeList(final NodeList nodeList) throws DOMException, DatatypeConfigurationException {
122         Set<EventStreamInfo> rpcServices = new HashSet<EventStreamInfo>();
123         for (int i =0; i < nodeList.getLength(); i++){
124             org.w3c.dom.Node nNode = nodeList.item(i);
125             if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
126                 rpcServices.add(restEventStreamInfoFromNode(nNode));
127             }
128         }
129         return rpcServices;
130     }
131
132     public static List<RestModule> getModulesFromInputStream(final InputStream is) throws IOException, ParserConfigurationException, SAXException {
133         Document doc = fromXml(is);
134         return restModulesFromNodeList(doc.getElementsByTagName("module"));
135     }
136
137     private static List<RestModule> restModulesFromNodeList(final NodeList nodeList) {
138         List<RestModule> modules = new ArrayList<RestModule>();
139         for (int i =0; i < nodeList.getLength(); i++){
140             Node nNode = nodeList.item(i);
141             if (nNode.getNodeType() == Node.ELEMENT_NODE){
142                 modules.add(restModulefromNode(nNode));
143             }
144         }
145         return modules;
146     }
147
148     private static RestModule restModulefromNode(final Node node) {
149         Element eElement = (Element) node;
150         RestModule restModule = new RestModule();
151         try {
152             restModule.setName(eElement.getElementsByTagName("name").item(0).getTextContent());
153             restModule.setNamespace(eElement.getElementsByTagName("namespace").item(0).getTextContent());
154             restModule.setRevision(eElement.getElementsByTagName("revision").item(0).getTextContent());
155         } catch (NullPointerException e) {
156             throw new IllegalStateException("Incomplete module data in xml.", e);
157         }
158         return restModule;
159     }
160 }