Improve XmlUtil.toString() 23/105223/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 4 Apr 2023 12:19:18 +0000 (14:19 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 4 Apr 2023 14:38:53 +0000 (16:38 +0200)
Simplify the try-catch block to cover the minimum required scope.

Change-Id: I61b0bf2d104ff7f272ac335207993d432c11c8ed
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 208021a60b33a23f8b925ebb3b346ee976e3bbab)

netconf/netconf-api/src/main/java/org/opendaylight/netconf/api/xml/XmlUtil.java

index c563ec3a496e68a171a14b044681debdb987d053..0f432e0b649a77f60d6d20da46f3023a2e6096b0 100644 (file)
@@ -178,19 +178,18 @@ public final class XmlUtil {
     }
 
     public static String toString(final Element xml, final boolean addXmlDeclaration) {
+        final StringWriter writer = new StringWriter();
+
         try {
             Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
             transformer.setOutputProperty(OutputKeys.INDENT, "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);
-
-            return result.getWriter().toString();
+            transformer.transform(new DOMSource(xml), new StreamResult(writer));
         } catch (TransformerFactoryConfigurationError | TransformerException e) {
             throw new IllegalStateException("Unable to serialize xml element " + xml, e);
         }
+
+        return writer.toString();
     }
 
     public static String toString(final Document doc, final boolean addXmlDeclaration) {