Use of instanceof pattern match in XmlElement
[netconf.git] / netconf / netconf-api / src / main / java / org / opendaylight / netconf / api / xml / XmlElement.java
index ad90fb3cb2d9ff20a21bbd614de82949b1863da5..caba82a5157e41e98a20bd8bbf34acded64fe8ad 100644 (file)
@@ -5,21 +5,24 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.api.xml;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Strings;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 import java.io.IOException;
+import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.ArrayList;
 import java.util.Collections;
 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.opendaylight.yangtools.yang.common.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Attr;
@@ -78,18 +81,16 @@ 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);
+                                ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
                     }
-                    prefix = attribKey.substring(XmlUtil.XMLNS_ATTRIBUTE_KEY.length() + 1);
+                    prefix = attribKey.substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1);
                 }
                 namespaces.put(prefix, attribute.getNodeValue());
             }
@@ -108,35 +109,27 @@ public final class XmlElement {
 
     public void checkName(final String expectedName) throws UnexpectedElementException {
         if (!getName().equals(expectedName)) {
-            throw new UnexpectedElementException(String.format("Expected %s xml element but was %s", expectedName,
-                    getName()),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.OPERATION_FAILED,
-                    DocumentedException.ErrorSeverity.ERROR);
+            throw new UnexpectedElementException(
+                    String.format("Expected %s xml element but was %s", expectedName, getName()),
+                    ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
         }
     }
 
     public void checkNamespaceAttribute(final String expectedNamespace)
             throws UnexpectedNamespaceException, MissingNameSpaceException {
         if (!getNamespaceAttribute().equals(expectedNamespace)) {
-            throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
-                    getNamespaceAttribute(),
-                    expectedNamespace),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.OPERATION_FAILED,
-                    DocumentedException.ErrorSeverity.ERROR);
+            throw new UnexpectedNamespaceException(
+                    String.format("Unexpected namespace %s should be %s", getNamespaceAttribute(), expectedNamespace),
+                    ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
         }
     }
 
     public void checkNamespace(final String expectedNamespace)
             throws UnexpectedNamespaceException, MissingNameSpaceException {
         if (!getNamespace().equals(expectedNamespace)) {
-            throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
-                    getNamespace(),
-                    expectedNamespace),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.OPERATION_FAILED,
-                    DocumentedException.ErrorSeverity.ERROR);
+            throw new UnexpectedNamespaceException(
+                    String.format("Unexpected namespace %s should be %s", getNamespace(), expectedNamespace),
+                    ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
         }
     }
 
@@ -161,7 +154,7 @@ public final class XmlElement {
     }
 
     public void appendChild(final Element toAppend) {
-        this.element.appendChild(toAppend);
+        element.appendChild(toAppend);
     }
 
     public Element getDomElement() {
@@ -170,7 +163,7 @@ public final class XmlElement {
 
     public Map<String, Attr> getAttributes() {
 
-        Map<String, Attr> mappedAttributes = Maps.newHashMap();
+        Map<String, Attr> mappedAttributes = new HashMap<>();
 
         NamedNodeMap attributes = element.getAttributes();
         for (int i = 0; i < attributes.getLength(); i++) {
@@ -188,12 +181,8 @@ public final class XmlElement {
         NodeList childNodes = element.getChildNodes();
         final List<XmlElement> result = new ArrayList<>();
         for (int i = 0; i < childNodes.getLength(); i++) {
-            Node item = childNodes.item(i);
-            if (!(item instanceof Element)) {
-                continue;
-            }
-            if (strat.accept((Element) item)) {
-                result.add(new XmlElement((Element) item));
+            if (childNodes.item(i) instanceof Element elem && strat.accept(elem)) {
+                result.add(new XmlElement(elem));
             }
         }
 
@@ -211,10 +200,7 @@ public final class XmlElement {
      * @return List of child elements
      */
     public List<XmlElement> getChildElements(final String tagName) {
-        return getChildElementsInternal(e -> {
-            // localName returns pure localName without prefix
-            return e.getLocalName().equals(tagName);
-        });
+        return getChildElementsInternal(e -> e.getLocalName().equals(tagName));
     }
 
     public List<XmlElement> getChildElementsWithinNamespace(final String childName, final String namespace) {
@@ -235,7 +221,7 @@ public final class XmlElement {
     public Optional<XmlElement> getOnlyChildElementOptionally(final String childName) {
         List<XmlElement> nameElements = getChildElements(childName);
         if (nameElements.size() != 1) {
-            return Optional.absent();
+            return Optional.empty();
         }
         return Optional.of(nameElements.get(0));
     }
@@ -245,7 +231,7 @@ public final class XmlElement {
         children = Lists.newArrayList(Collections2.filter(children,
             xmlElement -> xmlElement.getName().equals(childName)));
         if (children.size() != 1) {
-            return Optional.absent();
+            return Optional.empty();
         }
         return Optional.of(children.get(0));
     }
@@ -253,7 +239,7 @@ public final class XmlElement {
     public Optional<XmlElement> getOnlyChildElementOptionally() {
         List<XmlElement> children = getChildElements();
         if (children.size() != 1) {
-            return Optional.absent();
+            return Optional.empty();
         }
         return Optional.of(children.get(0));
     }
@@ -275,11 +261,11 @@ public final class XmlElement {
             children = Lists.newArrayList(Collections2.filter(children,
                 xmlElement -> xmlElement.getName().equals(childName)));
             if (children.size() != 1) {
-                return Optional.absent();
+                return Optional.empty();
             }
             return Optional.of(children.get(0));
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally() {
@@ -290,7 +276,7 @@ public final class XmlElement {
                 && getNamespaceOptionally().get().equals(child.get().getNamespaceOptionally().get())) {
             return child;
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     public XmlElement getOnlyChildElement(final String childName, final String namespace) throws DocumentedException {
@@ -300,9 +286,7 @@ public final class XmlElement {
         if (children.size() != 1) {
             throw new DocumentedException(String.format("One element %s:%s expected in %s but was %s", namespace,
                     childName, toString(), children.size()),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.INVALID_VALUE,
-                    DocumentedException.ErrorSeverity.ERROR);
+                    ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
         }
 
         return children.get(0);
@@ -312,9 +296,7 @@ public final class XmlElement {
         List<XmlElement> nameElements = getChildElements(childName);
         if (nameElements.size() != 1) {
             throw new DocumentedException("One element " + childName + " expected in " + toString(),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.INVALID_VALUE,
-                    DocumentedException.ErrorSeverity.ERROR);
+                    ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
         }
         return nameElements.get(0);
     }
@@ -322,11 +304,9 @@ public final class XmlElement {
     public XmlElement getOnlyChildElement() throws DocumentedException {
         List<XmlElement> children = getChildElements();
         if (children.size() != 1) {
-            throw new DocumentedException(String.format("One element expected in %s but was %s", toString(),
-                    children.size()),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.INVALID_VALUE,
-                    DocumentedException.ErrorSeverity.ERROR);
+            throw new DocumentedException(
+                    String.format("One element expected in %s but was %s", toString(), children.size()),
+                    ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
         }
         return children.get(0);
     }
@@ -337,46 +317,37 @@ public final class XmlElement {
             return DEFAULT_NAMESPACE_PREFIX;
         }
         for (int i = 0; i < childNodes.getLength(); i++) {
-            Node textChild = childNodes.item(i);
-            if (textChild instanceof Text) {
-                String content = textChild.getTextContent();
-                return content.trim();
+            if (childNodes.item(i) instanceof Text textChild) {
+                return textChild.getTextContent().trim();
             }
         }
         throw new DocumentedException(getName() + " should contain text.",
-                DocumentedException.ErrorType.APPLICATION,
-                DocumentedException.ErrorTag.INVALID_VALUE,
-                DocumentedException.ErrorSeverity.ERROR
-        );
+                ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
     }
 
     public Optional<String> getOnlyTextContentOptionally() {
         // only return text content if this node has exactly one Text child node
         if (element.getChildNodes().getLength() == 1) {
-            Node item = element.getChildNodes().item(0);
-            if (item instanceof Text) {
-                return Optional.of(((Text) item).getWholeText());
+            if (element.getChildNodes().item(0) instanceof Text textChild) {
+                return Optional.of(textChild.getWholeText());
             }
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     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()),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.OPERATION_FAILED,
-                    DocumentedException.ErrorSeverity.ERROR);
+            throw new MissingNameSpaceException(String.format("Element %s must specify namespace", toString()),
+                    ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
         }
         return attribute;
     }
 
     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.absent();
+            return Optional.empty();
         }
         return Optional.of(attribute);
     }
@@ -384,7 +355,7 @@ public final class XmlElement {
     public Optional<String> getNamespaceOptionally() {
         String namespaceURI = element.getNamespaceURI();
         if (Strings.isNullOrEmpty(namespaceURI)) {
-            return Optional.absent();
+            return Optional.empty();
         } else {
             return Optional.of(namespaceURI);
         }
@@ -392,11 +363,9 @@ public final class XmlElement {
 
     public String getNamespace() throws MissingNameSpaceException {
         Optional<String> namespaceURI = getNamespaceOptionally();
-        if (!namespaceURI.isPresent()) {
+        if (namespaceURI.isEmpty()) {
             throw new MissingNameSpaceException(String.format("No namespace defined for %s", this),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.OPERATION_FAILED,
-                    DocumentedException.ErrorSeverity.ERROR);
+                    ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
         }
         return namespaceURI.get();
     }
@@ -446,7 +415,7 @@ public final class XmlElement {
             throw new IllegalArgumentException("Cannot find namespace for " + XmlUtil.toString(element)
                 + ". Prefix from content is " + prefix + ". Found namespaces " + namespaces);
         }
-        return Maps.immutableEntry(prefix, namespaces.get(prefix));
+        return new SimpleImmutableEntry<>(prefix, namespaces.get(prefix));
     }
 
     public List<XmlElement> getChildElementsWithSameNamespace(final String childName) throws MissingNameSpaceException {
@@ -463,14 +432,12 @@ public final class XmlElement {
         }
         if (!childElements.isEmpty()) {
             throw new DocumentedException(String.format("Unrecognised elements %s in %s", childElements, this),
-                    DocumentedException.ErrorType.APPLICATION,
-                    DocumentedException.ErrorTag.INVALID_VALUE,
-                    DocumentedException.ErrorSeverity.ERROR);
+                    ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
         }
     }
 
     public void checkUnrecognisedElements(final XmlElement... additionalRecognisedElements) throws DocumentedException {
-        checkUnrecognisedElements(Collections.<XmlElement>emptyList(), additionalRecognisedElements);
+        checkUnrecognisedElements(Collections.emptyList(), additionalRecognisedElements);
     }
 
     @Override