Eliminate XmlUtil.createElement() et al. 87/108087/3
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 1 Oct 2023 12:33:11 +0000 (14:33 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 1 Oct 2023 20:33:07 +0000 (22:33 +0200)
These users are just shortcuts to Document.createElementNS(). Migrate
them over for clarity.

Change-Id: I9f2ec11bc0c7b5f8df86c15a0a0ca4a55c16ed54
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/NetconfStateSchemas.java
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/impl/NetconfMessageTransformUtil.java
plugins/netconf-server-mdsal/src/main/java/org/opendaylight/netconf/server/mdsal/monitoring/GetSchema.java
plugins/netconf-server-mdsal/src/main/java/org/opendaylight/netconf/server/mdsal/operations/RuntimeRpc.java
protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/xml/XmlUtil.java
protocol/netconf-api/src/test/java/org/opendaylight/netconf/api/xml/XmlUtilTest.java
protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/api/operations/AbstractNetconfOperation.java

index f10c684d244b65fef950ec8571c73e6e8b492ad5..bbd9f774a8faea5c3fef54a385d4ceeebe1e45fd 100644 (file)
@@ -81,13 +81,13 @@ public final class NetconfStateSchemas implements NetconfDeviceSchemas {
     static {
         final Document document = XmlUtil.newDocument();
 
-        final Element filterElem = XmlUtil.createElement(document, "filter", Optional.of(NamespaceURN.BASE));
+        final Element filterElem = document.createElementNS(NamespaceURN.BASE, "filter");
         filterElem.setAttribute("type", "subtree");
 
-        final Element stateElem = XmlUtil.createElement(document, NetconfState.QNAME.getLocalName(),
-            Optional.of(NetconfState.QNAME.getNamespace().toString()));
-        stateElem.appendChild(XmlUtil.createElement(document, Schemas.QNAME.getLocalName(),
-            Optional.of(Schemas.QNAME.getNamespace().toString())));
+        final Element stateElem = document.createElementNS(NetconfState.QNAME.getNamespace().toString(),
+            NetconfState.QNAME.getLocalName());
+        stateElem.appendChild(document.createElementNS(Schemas.QNAME.getNamespace().toString(),
+            Schemas.QNAME.getLocalName()));
         filterElem.appendChild(stateElem);
 
         GET_SCHEMAS_RPC = Builders.containerBuilder()
index 01394503ad3edfd9035b2a5cde78a0b5396f5cc1..16e915e3aa5d63638fc8be29d1b9739912671016 100644 (file)
@@ -248,7 +248,7 @@ public final class NetconfMessageTransformUtil {
     }
 
     private static Element newFilterElement() {
-        final var element = XmlUtil.createElement(BLANK_DOCUMENT, "filter", Optional.of(NamespaceURN.BASE));
+        final var element = BLANK_DOCUMENT.createElementNS(NamespaceURN.BASE, "filter");
         element.setAttribute("type", "subtree");
         return element;
     }
@@ -307,8 +307,8 @@ public final class NetconfMessageTransformUtil {
                     + "but was: %s", override);
         }
 
-        final var element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(),
-                Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString()));
+        final var element = BLANK_DOCUMENT.createElementNS(NETCONF_CONFIG_QNAME.getNamespace().toString(),
+            NETCONF_CONFIG_QNAME.getLocalName());
         final var metadata = operation.map(o -> leafMetadata(dataPath, o)).orElse(null);
         try {
             if (lastChildOverride.isPresent()) {
index 0805109426ca535a0f446dc184c00b986a0c24a1..c4198ecec4917b61d3cc857936064581c3fe97c0 100644 (file)
@@ -16,7 +16,6 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.netconf.server.api.operations.AbstractSingletonNetconfOperation;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
@@ -70,10 +69,11 @@ public final class GetSchema extends AbstractSingletonNetconfOperation {
                 Map.of(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage()));
         }
 
-        final var getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
-                Optional.of(URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
+        final var ret = document.createElementNS(URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING,
+            XmlNetconfConstants.DATA_KEY);
+        ret.appendChild(document.createTextNode(schema));
         LOG.trace("{} operation successful", GET_SCHEMA);
-        return getSchemaResult;
+        return ret;
     }
 
     private static final class GetSchemaEntry {
index 24503f00aa307c9a37c9cd20955f1d26b7e2fde1..2eb4a7aab56b34cd1a6ae59b3abda127bc903561 100644 (file)
@@ -155,7 +155,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
             throw DocumentedException.wrap(e);
         }
         if (result.value() == null) {
-            return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.of(NamespaceURN.BASE));
+            return document.createElementNS(NamespaceURN.BASE, XmlNetconfConstants.OK);
         }
         return transformNormalizedNode(document, result.value(),
                 Absolute.of(rpcDefinition.getQName(), rpcDefinition.getOutput().getQName()));
@@ -170,8 +170,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
         final Map<String, Attr> attributes = requestElement.getAttributes();
 
         final Element response = handle(document, operationElement, subsequentOperation);
-        final Element rpcReply = XmlUtil.createElement(document, RpcReplyMessage.ELEMENT_NAME,
-                Optional.of(NamespaceURN.BASE));
+        final Element rpcReply = document.createElementNS(NamespaceURN.BASE, RpcReplyMessage.ELEMENT_NAME);
 
         if (XmlElement.fromDomElement(response).hasNamespace()) {
             rpcReply.appendChild(response);
index fac5afa009bf3a516e10919eb710699f0a089b69..951d927d489ab1b5f074461b65bfe2f8304bbdb9 100644 (file)
@@ -7,9 +7,6 @@
  */
 package org.opendaylight.netconf.api.xml;
 
-import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE;
-import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
-
 import com.google.common.io.Resources;
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -18,7 +15,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
 import java.nio.charset.StandardCharsets;
-import java.util.Optional;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -130,51 +126,6 @@ public final class XmlUtil {
         return DEFAULT_DOM_BUILDER.get().newDocument();
     }
 
-    public static Element createElement(final Document document, final String qname,
-            final Optional<String> namespaceURI) {
-        if (namespaceURI.isEmpty()) {
-            return document.createElement(qname);
-        }
-
-        final String uri = namespaceURI.orElseThrow();
-        final Element element = document.createElementNS(uri, qname);
-        String name = XMLNS_ATTRIBUTE;
-        if (element.getPrefix() != null) {
-            name += ":" + element.getPrefix();
-        }
-        element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, name, uri);
-        return element;
-    }
-
-    public static Element createTextElement(final Document document, final String qname, final String content,
-            final Optional<String> namespaceURI) {
-        Element typeElement = createElement(document, qname, namespaceURI);
-        typeElement.appendChild(document.createTextNode(content));
-        return typeElement;
-    }
-
-    public static Element createTextElementWithNamespacedContent(final Document document, final String qname,
-            final String prefix, final String namespace, final String contentWithoutPrefix) {
-
-        return createTextElementWithNamespacedContent(document, qname, prefix, namespace, contentWithoutPrefix,
-                Optional.empty());
-    }
-
-    public static Element createTextElementWithNamespacedContent(final Document document, final String qname,
-            final String prefix, final String namespace, final String contentWithoutPrefix,
-            final Optional<String> namespaceURI) {
-
-        String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix);
-        Element element = createTextElement(document, qname, content, namespaceURI);
-        String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE, prefix);
-        element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, prefixedNamespaceAttr, namespace);
-        return element;
-    }
-
-    public static String createPrefixedValue(final String prefix, final String value) {
-        return prefix + ":" + value;
-    }
-
     /**
      * Return a new {@link Transformer} which performs indentation.
      *
index 9fea9256478d4b5fca4a7700dcb306f73831bd8e..b0fe1cf3e79c86395c21ad5e5898c7ba3a95c23f 100644 (file)
@@ -8,52 +8,14 @@
 package org.opendaylight.netconf.api.xml;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThrows;
 
-import java.util.Optional;
 import org.junit.Test;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
 import org.xml.sax.SAXParseException;
-import org.xmlunit.builder.DiffBuilder;
 import org.xmlunit.builder.Input;
 import org.xmlunit.builder.Transform;
-import org.xmlunit.diff.DefaultNodeMatcher;
-import org.xmlunit.diff.ElementSelectors;
 
 public class XmlUtilTest {
-    private static final String XML_SNIPPET = """
-        <top xmlns="namespace">
-            <innerText>value</innerText>
-            <innerPrefixedText xmlns:pref="prefixNamespace">prefix:value</innerPrefixedText>
-            <innerPrefixedText xmlns="randomNamespace" xmlns:pref="prefixNamespace">prefix:value</innerPrefixedText>
-        </top>""";
-
-    @Test
-    public void testCreateElement() throws Exception {
-        final Document document = XmlUtil.newDocument();
-        final Element top = XmlUtil.createElement(document, "top", Optional.of("namespace"));
-
-        top.appendChild(XmlUtil.createTextElement(document, "innerText", "value", Optional.of("namespace")));
-        top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref",
-                "prefixNamespace", "value", Optional.of("namespace")));
-        top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref",
-                "prefixNamespace", "value", Optional.of("randomNamespace")));
-
-        document.appendChild(top);
-        assertEquals("top", XmlUtil.createDocumentCopy(document).getDocumentElement().getTagName());
-
-        final var diff = DiffBuilder.compare(document)
-            .withTest(XML_SNIPPET)
-            .ignoreWhitespace()
-            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
-            .checkForSimilar()
-            .build();
-
-        assertFalse(diff.toString(), diff.hasDifferences());
-    }
-
     @Test
     public void testXXEFlaw() {
         assertThrows(SAXParseException.class, () -> XmlUtil.readXmlToDocument("""
index deb8efe06733787136fa0feadbeb9b73ebb7074a..56f102f8877b3d0c4b16343b4a47e4abe05092a1 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.netconf.server.api.operations;
 
 import static java.util.Objects.requireNonNull;
 
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.NamespaceURN;
@@ -95,13 +94,12 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         final var attributes = requestElement.getAttributes();
 
         final var response = handle(document, operationElement, subsequentOperation);
-        final var rpcReply = XmlUtil.createElement(document, RpcReplyMessage.ELEMENT_NAME,
-            Optional.of(NamespaceURN.BASE));
+        final var rpcReply = document.createElementNS(NamespaceURN.BASE, RpcReplyMessage.ELEMENT_NAME);
         if (XmlElement.fromDomElement(response).hasNamespace()) {
             rpcReply.appendChild(response);
         } else {
-            final var responseNS = XmlUtil.createElement(document, response.getNodeName(),
-                    Optional.of(NamespaceURN.BASE));
+            // FIXME: use getLocalName() instead
+            final var responseNS = document.createElementNS(NamespaceURN.BASE, response.getNodeName());
             final var list = response.getChildNodes();
             while (list.getLength() != 0) {
                 responseNS.appendChild(list.item(0));