X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Frestconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Frestconf%2Futils%2FXmlTools.java;h=76ed6a9404d1ecf5a6e197ac29ac7031de506c24;hb=7105bdc8fbe8be04459ce0f12cf0ebec7a81f9c2;hp=ecbd5cfb4f10ec0ad7ed3d3895c29e644b50e61d;hpb=a2f6807928883c4381188ff97c882748828577f4;p=yangtools.git diff --git a/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/XmlTools.java b/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/XmlTools.java index ecbd5cfb4f..76ed6a9404 100644 --- a/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/XmlTools.java +++ b/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/XmlTools.java @@ -7,60 +7,74 @@ */ package org.opendaylight.yangtools.restconf.utils; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; + import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.stream.StreamSource; + import org.opendaylight.yangtools.restconf.client.api.dto.RestEventStreamInfo; +import org.opendaylight.yangtools.restconf.client.api.dto.RestModule; import org.opendaylight.yangtools.restconf.client.api.dto.RestRpcService; import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo; -import org.opendaylight.yangtools.restconf.client.api.dto.RestModule; import org.opendaylight.yangtools.yang.binding.RpcService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import com.google.common.base.Preconditions; public class XmlTools { + private static final String JAXP_SCHEMA_LOCATION = + "http://java.sun.com/xml/jaxp/properties/schemaSource"; - private static final Logger logger = LoggerFactory.getLogger(XmlTools.class.toString()); + private XmlTools() { + throw new UnsupportedOperationException("Utility class should not be instantiated"); + } + public static Object unmarshallXml(final Class clazz,final InputStream xmlStream,final String namespace) throws JAXBException { + Preconditions.checkNotNull(xmlStream); - public static Object unmarshallXml(Class clazz,InputStream xmlStream) throws Exception{ - if (null == xmlStream){ - throw new Exception("XML input stream can't be null"); - } - JAXBContext jc = null; - jc = JAXBContext.newInstance(clazz); + JAXBContext jc = JAXBContext.newInstance(clazz); - Unmarshaller unmarshaller = null; - Object o = null; - unmarshaller = jc.createUnmarshaller(); - return unmarshaller.unmarshal(xmlStream); + Unmarshaller unmarshaller = jc.createUnmarshaller(); + StreamSource xmlInputSource = new StreamSource(xmlStream); + JAXBElement obj = unmarshaller.unmarshal(xmlInputSource, clazz); + return obj; } - public static Document fromXml(InputStream is) throws Exception { + public static Document fromXml(final InputStream is) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + dbFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(is); doc.getDocumentElement().normalize(); return doc; } - public static Set fromInputStream(InputStream is) throws Exception { + + public static Set fromInputStream(final InputStream is) throws IOException, ParserConfigurationException, SAXException { Document doc = fromXml(is); return fromNodeList(doc.getElementsByTagName("play")); - } - public static Set fromNodeList(NodeList nodeList) throws Exception { + + public static Set fromNodeList(final NodeList nodeList) { Set rpcServices = new HashSet(); for (int i =0; i < nodeList.getLength(); i++){ org.w3c.dom.Node nNode = nodeList.item(i); @@ -69,15 +83,16 @@ public class XmlTools { } } return rpcServices; - } - public static RestRpcService fromNode(org.w3c.dom.Node node){ + + public static RestRpcService fromNode(final org.w3c.dom.Node node) { Element eElement = (Element) node; RestRpcService rpcService = new RestRpcService(eElement.getAttribute("xmlns")); return rpcService; } - private static EventStreamInfo restEventStreamInfoFromNode(org.w3c.dom.Node node) throws Exception { + + private static EventStreamInfo restEventStreamInfoFromNode(final org.w3c.dom.Node node) throws DOMException, DatatypeConfigurationException { Element eElement = (Element) node; RestEventStreamInfo eventStreamInfo = new RestEventStreamInfo(); eventStreamInfo.setDescription(eElement.getElementsByTagName("description").item(0).getTextContent()); @@ -98,11 +113,12 @@ public class XmlTools { return eventStreamInfo; } - public static Set evenStreamsFromInputStream(InputStream is) throws Exception { + public static Set evenStreamsFromInputStream(final InputStream is) throws DOMException, DatatypeConfigurationException, IOException, ParserConfigurationException, SAXException { Document doc = fromXml(is); return streamInfoFromNodeList(doc.getElementsByTagName("stream")); } - private static Set streamInfoFromNodeList(NodeList nodeList) throws Exception { + + private static Set streamInfoFromNodeList(final NodeList nodeList) throws DOMException, DatatypeConfigurationException { Set rpcServices = new HashSet(); for (int i =0; i < nodeList.getLength(); i++){ org.w3c.dom.Node nNode = nodeList.item(i); @@ -113,11 +129,12 @@ public class XmlTools { return rpcServices; } - public static List getModulesFromInputStream(InputStream is) throws Exception { + public static List getModulesFromInputStream(final InputStream is) throws IOException, ParserConfigurationException, SAXException { Document doc = fromXml(is); return restModulesFromNodeList(doc.getElementsByTagName("module")); } - private static List restModulesFromNodeList(NodeList nodeList) throws Exception { + + private static List restModulesFromNodeList(final NodeList nodeList) { List modules = new ArrayList(); for (int i =0; i < nodeList.getLength(); i++){ Node nNode = nodeList.item(i); @@ -127,7 +144,8 @@ public class XmlTools { } return modules; } - private static RestModule restModulefromNode(Node node){ + + private static RestModule restModulefromNode(final Node node) { Element eElement = (Element) node; RestModule restModule = new RestModule(); try { @@ -139,5 +157,4 @@ public class XmlTools { } return restModule; } - }