X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fxml%2FXmlUtil.java;h=238249dbbd8d49086b38c1696cb6d0cb685909fe;hb=8f15fef884bc20239625850c4a2fcdaf36395526;hp=298038996eddf08382ebcabcb929d6a15ff2e5a6;hpb=4f623c74a703a31e787387b536981dcb344ca4f9;p=controller.git 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 298038996e..238249dbbd 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,11 +8,12 @@ package org.opendaylight.controller.netconf.util.xml; -import com.google.common.base.Charsets; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.xml.sax.SAXException; +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; @@ -32,16 +33,29 @@ import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; -public class XmlUtil { +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 final class XmlUtil { public static final String XMLNS_ATTRIBUTE_KEY = "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); @@ -57,13 +71,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 RuntimeException("Failed to parse XML document", e); } Document doc = dBuilder.parse(xmlContent); @@ -75,24 +91,13 @@ 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.setValidating(true); - factory.setIgnoringElementContentWhitespace(true); - factory.setIgnoringComments(true); - return factory; - } - public static Document newDocument() { - DocumentBuilderFactory factory = getDocumentBuilderFactory(); try { - DocumentBuilder builder = factory.newDocumentBuilder(); + DocumentBuilder builder = BUILDERFACTORY.newDocumentBuilder(); Document document = builder.newDocument(); return document; } catch (ParserConfigurationException e) { - throw new RuntimeException(e); + throw new RuntimeException("Failed to create document", e); } } @@ -110,8 +115,8 @@ public class XmlUtil { root.setAttribute(concat(XMLNS_ATTRIBUTE_KEY, prefix), namespace); } - public static Element createPrefixedTextElement(Document document, String key, String prefix, String moduleName) { - return createTextElement(document, key, concat(prefix, moduleName)); + public static Element createPrefixedTextElement(Document document, String key, String prefix, String content) { + return createTextElement(document, key, concat(prefix, content)); } private static String concat(String prefix, String value) { @@ -134,14 +139,13 @@ 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; + return result.getWriter().toString(); } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException("Unable to serialize xml element " + xml, e); }