X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fxml%2FXmlUtil.java;h=b2b202b69ef024569d032771ecd35e116812769e;hp=137e215a31eb13423b6438671faed529a2fc7ce3;hb=d42fc809d1d74240b7933d74cdbaff428773ad26;hpb=07c30f447bd1366ba18e6f0f41ac768a16dbc901 diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java index 137e215a31..b2b202b69e 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java @@ -8,13 +8,14 @@ package org.opendaylight.controller.netconf.util.xml; +import com.google.common.base.Charsets; +import com.google.common.base.Optional; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; - import javax.xml.XMLConstants; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; @@ -23,7 +24,6 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; @@ -33,17 +33,27 @@ import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; - import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.xml.sax.SAXException; -import com.google.common.base.Charsets; - -public class XmlUtil { +public final class XmlUtil { public static final String XMLNS_ATTRIBUTE_KEY = "xmlns"; + public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/"; + private static final DocumentBuilderFactory BUILDERFACTORY; + + static { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setCoalescing(true); + factory.setIgnoringElementContentWhitespace(true); + factory.setIgnoringComments(true); + BUILDERFACTORY = factory; + } + + private XmlUtil() {} public static Element readXmlToElement(String xmlContent) throws SAXException, IOException { Document doc = readXmlToDocument(xmlContent); @@ -59,13 +69,15 @@ public class XmlUtil { return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8))); } + // TODO improve exceptions throwing + // along with XmlElement + public static Document readXmlToDocument(InputStream xmlContent) throws SAXException, IOException { - DocumentBuilderFactory factory = getDocumentBuilderFactory(); DocumentBuilder dBuilder; try { - dBuilder = factory.newDocumentBuilder(); + dBuilder = BUILDERFACTORY.newDocumentBuilder(); } catch (ParserConfigurationException e) { - throw new RuntimeException(e); + throw new IllegalStateException("Failed to parse XML document", e); } Document doc = dBuilder.parse(xmlContent); @@ -77,45 +89,45 @@ public class XmlUtil { return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement(); } - private static final DocumentBuilderFactory getDocumentBuilderFactory() { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setCoalescing(true); - factory.setIgnoringElementContentWhitespace(true); - factory.setIgnoringComments(true); - return factory; - } - public static Document newDocument() { - DocumentBuilderFactory factory = getDocumentBuilderFactory(); try { - DocumentBuilder builder = factory.newDocumentBuilder(); - Document document = builder.newDocument(); - return document; + DocumentBuilder builder = BUILDERFACTORY.newDocumentBuilder(); + return builder.newDocument(); } catch (ParserConfigurationException e) { - throw new RuntimeException(e); + throw new IllegalStateException("Failed to create document", e); } } - public static Element createTextElement(Document document, String name, String content) { - Element typeElement = document.createElement(name); - typeElement.appendChild(document.createTextNode(content)); - return typeElement; + public static Element createElement(final Document document, String qName, Optional namespaceURI) { + if(namespaceURI.isPresent()) { + final Element element = document.createElementNS(namespaceURI.get(), qName); + String name = XMLNS_ATTRIBUTE_KEY; + if(element.getPrefix() != null) { + name += ":" + element.getPrefix(); + } + element.setAttributeNS(XMLNS_URI, name, namespaceURI.get()); + return element; + } + return document.createElement(qName); } - public static void addNamespaceAttr(Element root, String namespace) { - root.setAttribute(XMLNS_ATTRIBUTE_KEY, namespace); + public static Element createTextElement(Document document, String qName, String content, Optional namespaceURI) { + Element typeElement = createElement(document, qName, namespaceURI); + typeElement.appendChild(document.createTextNode(content)); + return typeElement; } - public static void addPrefixedNamespaceAttr(Element root, String prefix, String namespace) { - root.setAttribute(concat(XMLNS_ATTRIBUTE_KEY, prefix), namespace); - } + public static Element createTextElementWithNamespacedContent(Document document, String qName, String prefix, + String namespace, String contentWithoutPrefix) { - public static Element createPrefixedTextElement(Document document, String key, String prefix, String content) { - return createTextElement(document, key, concat(prefix, content)); + String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix); + Element element = createTextElement(document, qName, content, Optional.absent()); + String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE_KEY, prefix); + element.setAttributeNS(XMLNS_URI, prefixedNamespaceAttr, namespace); + return element; } - private static String concat(String prefix, String value) { + public static String createPrefixedValue(String prefix, String value) { return prefix + ":" + value; } @@ -135,16 +147,15 @@ public class XmlUtil { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration == true ? "no" : "yes"); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration ? "no" : "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(xml); transformer.transform(source, result); - String xmlString = result.getWriter().toString(); - return xmlString; - } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) { - throw new RuntimeException("Unable to serialize xml element " + xml, e); + return result.getWriter().toString(); + } catch (Exception | TransformerFactoryConfigurationError e) { + throw new IllegalStateException("Unable to serialize xml element " + xml, e); } }