From 5e035029b351317b24cf2dfb45504bf5a830d2d1 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 16 Feb 2014 06:44:49 +0100 Subject: [PATCH] Do not instantiate multiple DocumentBuilderFactories We're instantiating builders themsenves, so we can safely reuse a single factory. No sense in initiating it more than once. Change-Id: I70dfa2d5cc51a09722bb7ad41ce4d61f8691ac3d Signed-off-by: Robert Varga --- .../controller/netconf/util/xml/XmlUtil.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) 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..bb6c35130e 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 @@ -42,8 +42,17 @@ import org.xml.sax.SAXException; import com.google.common.base.Charsets; public 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; + } public static Element readXmlToElement(String xmlContent) throws SAXException, IOException { Document doc = readXmlToDocument(xmlContent); @@ -60,12 +69,11 @@ public class XmlUtil { } 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); @@ -77,23 +85,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.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); } } -- 2.36.6