Use XMLConstants for "xmlns" and its URI 91/79591/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 17 Jan 2019 00:54:01 +0000 (01:54 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 17 Jan 2019 01:21:05 +0000 (02:21 +0100)
Rather than spelling this out in XmlUtil, use well-known place
defining these constants. While we are at it, eliminate one copy
of XmlUtil.createElement().

Change-Id: Ie4f91a731b3b5e5ce3584514f69e183506f2097c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/messagebus-netconf/src/main/java/org/opendaylight/netconf/messagebus/eventsources/netconf/ConnectionNotificationTopicRegistration.java
netconf/netconf-api/src/main/java/org/opendaylight/netconf/api/xml/XmlElement.java
netconf/netconf-api/src/main/java/org/opendaylight/netconf/api/xml/XmlUtil.java
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SendErrorExceptionUtil.java
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SubtreeFilter.java

index 34a0fb9e9f5f38716dcf1a22e2440e93c44c1e0a..acf72e79186548d4a055032366fb6acf0dd9b203 100644 (file)
@@ -7,14 +7,15 @@
  */
 package org.opendaylight.netconf.messagebus.eventsources.netconf;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.mdsal.dom.api.DOMNotification;
 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatus;
 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotification;
@@ -42,8 +43,6 @@ class ConnectionNotificationTopicRegistration extends NotificationTopicRegistrat
             .create(true, QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
     private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = NodeIdentifier.create(
             EventSourceStatusNotification.QNAME);
-    private static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
-    private static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
 
     private final DOMNotificationListener domNotificationListener;
 
@@ -142,8 +141,8 @@ class ConnectionNotificationTopicRegistration extends NotificationTopicRegistrat
     private static AnyXmlNode encapsulate(final EventSourceStatusNotification notification) {
         Document doc = UntrustedXML.newDocumentBuilder().newDocument();
 
-        final Optional<String> namespace = Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString());
-        final Element rootElement = createElement(doc, "EventSourceStatusNotification", namespace);
+        final Element rootElement = XmlUtil.createElement(doc, "EventSourceStatusNotification",
+            Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString()));
 
         final Element sourceElement = doc.createElement("status");
         sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
@@ -152,19 +151,4 @@ class ConnectionNotificationTopicRegistration extends NotificationTopicRegistrat
         return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
                 .withValue(new DOMSource(rootElement)).build();
     }
-
-    // Helper to create root XML element with correct namespace and attribute
-    private static Element createElement(final Document document, final String qualifiedName,
-                                         final Optional<String> namespaceURI) {
-        if (namespaceURI.isPresent()) {
-            final Element element = document.createElementNS(namespaceURI.get(), qualifiedName);
-            String name = XMLNS_ATTRIBUTE_KEY;
-            if (element.getPrefix() != null) {
-                name += ":" + element.getPrefix();
-            }
-            element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
-            return element;
-        }
-        return document.createElement(qualifiedName);
-    }
 }
index 039a45ed613619386158962c4eaad5458e39e70e..4a92fddb52b09bb00b890d1715bcf9659e14b7f3 100644 (file)
@@ -18,6 +18,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import javax.xml.XMLConstants;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -77,18 +78,18 @@ public final class XmlElement {
         for (int i = 0; i < attributes.getLength(); i++) {
             Node attribute = attributes.item(i);
             String attribKey = attribute.getNodeName();
-            if (attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
+            if (attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
                 String prefix;
-                if (attribKey.equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
+                if (attribKey.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
                     prefix = DEFAULT_NAMESPACE_PREFIX;
                 } else {
-                    if (!attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY + ":")) {
+                    if (!attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")) {
                         throw new DocumentedException("Attribute doesn't start with :",
                                 DocumentedException.ErrorType.APPLICATION,
                                 DocumentedException.ErrorTag.INVALID_VALUE,
                                 DocumentedException.ErrorSeverity.ERROR);
                     }
-                    prefix = attribKey.substring(XmlUtil.XMLNS_ATTRIBUTE_KEY.length() + 1);
+                    prefix = attribKey.substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1);
                 }
                 namespaces.put(prefix, attribute.getNodeValue());
             }
@@ -361,7 +362,7 @@ public final class XmlElement {
     }
 
     public String getNamespaceAttribute() throws MissingNameSpaceException {
-        String attribute = element.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY);
+        String attribute = element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE);
         if (attribute.isEmpty() || attribute.equals(DEFAULT_NAMESPACE_PREFIX)) {
             throw new MissingNameSpaceException(String.format("Element %s must specify namespace",
                     toString()),
@@ -373,7 +374,7 @@ public final class XmlElement {
     }
 
     public Optional<String> getNamespaceAttributeOptionally() {
-        String attribute = element.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY);
+        String attribute = element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE);
         if (attribute.isEmpty() || attribute.equals(DEFAULT_NAMESPACE_PREFIX)) {
             return Optional.empty();
         }
index 458d4e4965a44d2dcdee555390310625bad8025c..b3d9d3e872399b5f69bb8ed94372f5225920fe0e 100644 (file)
@@ -7,6 +7,9 @@
  */
 package org.opendaylight.netconf.api.xml;
 
+import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE;
+import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -40,8 +43,6 @@ import org.xml.sax.SAXException;
 
 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 BUILDER_FACTORY;
     private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
     private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
@@ -127,11 +128,11 @@ public final class XmlUtil {
             final Optional<String> namespaceURI) {
         if (namespaceURI.isPresent()) {
             final Element element = document.createElementNS(namespaceURI.get(), qname);
-            String name = XMLNS_ATTRIBUTE_KEY;
+            String name = XMLNS_ATTRIBUTE;
             if (element.getPrefix() != null) {
                 name += ":" + element.getPrefix();
             }
-            element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
+            element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, name, namespaceURI.get());
             return element;
         }
         return document.createElement(qname);
@@ -157,8 +158,8 @@ public final class XmlUtil {
 
         String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix);
         Element element = createTextElement(document, qname, content, namespaceURI);
-        String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE_KEY, prefix);
-        element.setAttributeNS(XMLNS_URI, prefixedNamespaceAttr, namespace);
+        String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE, prefix);
+        element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, prefixedNamespaceAttr, namespace);
         return element;
     }
 
index 1f09f55edcf2a57a338831f4b05d464ff62acfa1..11ad160a26aff8fd8d57725cd0a0b716bb007fdf 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.base.Preconditions;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelFutureListener;
+import javax.xml.XMLConstants;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.NetconfSession;
@@ -74,7 +75,7 @@ public final class SendErrorExceptionUtil {
             for (int i = 0; i < incomingAttributes.getLength(); i++) {
                 final Attr attr = (Attr) incomingAttributes.item(i);
                 // skip namespace
-                if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
+                if (attr.getNodeName().equals(XMLConstants.XMLNS_ATTRIBUTE)) {
                     continue;
                 }
                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
index fdc3831c32fc9a19e112ccc601d2f4f9a5dac8fb..3b4043aa20980fc9ba54a1f0560443dd6945daf0 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.netconf.util.messages;
 
 import java.util.Map;
 import java.util.Optional;
+import javax.xml.XMLConstants;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
@@ -31,8 +32,8 @@ public final class SubtreeFilter {
 
     }
 
-    public static Document applyRpcSubtreeFilter(Document requestDocument,
-                                                 Document rpcReply) throws DocumentedException {
+    public static Document applyRpcSubtreeFilter(final Document requestDocument,
+                                                 final Document rpcReply) throws DocumentedException {
         OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(requestDocument);
         if (XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(operationNameAndNamespace.getNamespace())
                 && XmlNetconfConstants.GET.equals(operationNameAndNamespace.getOperationName())
@@ -64,8 +65,8 @@ public final class SubtreeFilter {
      * @return document containing filtered notification content
      * @throws DocumentedException if operation fails
      */
-    public static Optional<Document> applySubtreeNotificationFilter(XmlElement filter,
-                                                                    Document notification) throws DocumentedException {
+    public static Optional<Document> applySubtreeNotificationFilter(final XmlElement filter,
+            final Document notification) throws DocumentedException {
         removeEventTimeNode(notification);
         if (isSupported(filter)) {
             return Optional.ofNullable(filteredNotification(filter, notification));
@@ -73,19 +74,19 @@ public final class SubtreeFilter {
         return Optional.of(extractNotificationContent(notification));
     }
 
-    private static void removeEventTimeNode(Document document) {
+    private static void removeEventTimeNode(final Document document) {
         final Node eventTimeNode = document.getDocumentElement().getElementsByTagNameNS(XmlNetconfConstants
                 .URN_IETF_PARAMS_NETCONF_CAPABILITY_NOTIFICATION_1_0, XmlNetconfConstants.EVENT_TIME).item(0);
         document.getDocumentElement().removeChild(eventTimeNode);
     }
 
-    private static boolean isSupported(XmlElement filter) {
+    private static boolean isSupported(final XmlElement filter) {
         return "subtree".equals(filter.getAttribute("type"))
                 || "subtree".equals(filter.getAttribute("type",
                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
     }
 
-    private static Document extractNotificationContent(Document notification) throws DocumentedException {
+    private static Document extractNotificationContent(final Document notification) throws DocumentedException {
         XmlElement root = XmlElement.fromDomElement(notification.getDocumentElement());
         XmlElement content = root.getOnlyChildElement();
         notification.removeChild(root.getDomElement());
@@ -93,8 +94,8 @@ public final class SubtreeFilter {
         return notification;
     }
 
-    private static Document filteredNotification(XmlElement filter,
-                                                 Document originalNotification) throws DocumentedException {
+    private static Document filteredNotification(final XmlElement filter,
+                                                 final Document originalNotification) throws DocumentedException {
         Document result = XmlUtil.newDocument();
         XmlElement dataSrc = XmlElement.fromDomDocument(originalNotification);
         Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false);
@@ -104,12 +105,12 @@ public final class SubtreeFilter {
         if (dataDst.getFirstChild() != null) {
             result.appendChild(dataDst.getFirstChild());
             return result;
-        } else {
-            return null;
         }
+        return null;
     }
 
-    private static Document filtered(XmlElement filter, Document originalReplyDocument) throws DocumentedException {
+    private static Document filtered(final XmlElement filter, final Document originalReplyDocument)
+            throws DocumentedException {
         Document result = XmlUtil.newDocument();
         // even if filter is empty, copy /rpc/data
         Element rpcReply = originalReplyDocument.getDocumentElement();
@@ -124,7 +125,8 @@ public final class SubtreeFilter {
         return result;
     }
 
-    private static void addSubtree(XmlElement filter, XmlElement src, XmlElement dst) throws DocumentedException {
+    private static void addSubtree(final XmlElement filter, final XmlElement src, final XmlElement dst)
+            throws DocumentedException {
         for (XmlElement srcChild : src.getChildElements()) {
             for (XmlElement filterChild : filter.getChildElements()) {
                 addSubtree2(filterChild, srcChild, dst);
@@ -132,8 +134,8 @@ public final class SubtreeFilter {
         }
     }
 
-    private static MatchingResult addSubtree2(XmlElement filter, XmlElement src,
-                                              XmlElement dstParent) throws DocumentedException {
+    private static MatchingResult addSubtree2(final XmlElement filter, final XmlElement src,
+                                              final XmlElement dstParent) throws DocumentedException {
         Document document = dstParent.getDomElement().getOwnerDocument();
         MatchingResult matches = matches(src, filter);
         if (matches != MatchingResult.NO_MATCH && matches != MatchingResult.CONTENT_MISMATCH) {
@@ -176,7 +178,7 @@ public final class SubtreeFilter {
      * Shallow compare src node to filter: tag name and namespace must match.
      * If filter node has no children and has text content, it also must match.
      */
-    private static MatchingResult matches(XmlElement src, XmlElement filter) throws DocumentedException {
+    private static MatchingResult matches(final XmlElement src, final XmlElement filter) throws DocumentedException {
         boolean tagMatch = src.getName().equals(filter.getName())
                 && src.getNamespaceOptionally().equals(filter.getNamespaceOptionally());
         MatchingResult result = null;
@@ -194,7 +196,7 @@ public final class SubtreeFilter {
             if (result == null) {
                 for (Attr attr : filter.getAttributes().values()) {
                     // ignore namespace declarations
-                    if (!XmlUtil.XMLNS_URI.equals(attr.getNamespaceURI())) {
+                    if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) {
                         // find attr with matching localName(),  namespaceURI(),  == value() in src
                         String found = src.getAttribute(attr.getLocalName(), attr.getNamespaceURI());
                         if (attr.getValue().equals(found) && result != MatchingResult.NO_MATCH) {