2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.restconf.utils;
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;
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;
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;
40 import com.google.common.base.Preconditions;
42 public final class XmlTools {
43 private static final String JAXP_SCHEMA_LOCATION =
44 "http://java.sun.com/xml/jaxp/properties/schemaSource";
47 throw new UnsupportedOperationException("Utility class should not be instantiated");
50 public static Object unmarshallXml(final Class<?> clazz,final InputStream xmlStream,final String namespace) throws JAXBException {
51 Preconditions.checkNotNull(xmlStream);
53 JAXBContext jc = JAXBContext.newInstance(clazz);
55 Unmarshaller unmarshaller = jc.createUnmarshaller();
56 StreamSource xmlInputSource = new StreamSource(xmlStream);
57 JAXBElement<?> obj = unmarshaller.unmarshal(xmlInputSource, clazz);
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();
66 Document doc = dBuilder.parse(is);
67 doc.getDocumentElement().normalize();
71 public static Set<RpcService> fromInputStream(final InputStream is) throws IOException, ParserConfigurationException, SAXException {
72 Document doc = fromXml(is);
74 return fromNodeList(doc.getElementsByTagName("play"));
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));
88 public static RestRpcService fromNode(final org.w3c.dom.Node node) {
89 Element eElement = (Element) node;
90 RestRpcService rpcService = new RestRpcService(eElement.getAttribute("xmlns"));
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
105 .newXMLGregorianCalendar(eElement
106 .getElementsByTagName("replay-log-creation-time")
109 .toGregorianCalendar()
112 eventStreamInfo.setReplaySupported(Boolean.parseBoolean(eElement.getElementsByTagName("replay-support").item(0).getTextContent()));
113 return eventStreamInfo;
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"));
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));
132 public static List<RestModule> getModulesFromInputStream(final InputStream is) throws IOException, ParserConfigurationException, SAXException {
133 Document doc = fromXml(is);
134 return restModulesFromNodeList(doc.getElementsByTagName("module"));
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));
148 private static RestModule restModulefromNode(final Node node) {
149 Element eElement = (Element) node;
150 RestModule restModule = new RestModule();
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);