From: Tomas Cere Date: Tue, 7 Apr 2015 15:54:26 +0000 (+0200) Subject: Stop swallowing exceptions in XmlElement optional methods. X-Git-Tag: release/lithium~273^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=e9ada4abe7f68175775e0ca9a12a6ab3c47fd329 Stop swallowing exceptions in XmlElement optional methods. Performance increase. Change-Id: I41629b5ea356be697629eb7f9238344f87a62a5e Signed-off-by: Tomas Cere --- diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlElement.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlElement.java index e17cad977c..4529f81e57 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlElement.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlElement.java @@ -237,7 +237,7 @@ public final class XmlElement { /** * * @param tagName tag name without prefix - * @return + * @return List of child elements */ public List getChildElements(final String tagName) { return getChildElementsInternal(new ElementFilteringStrategy() { @@ -261,31 +261,47 @@ public final class XmlElement { } public Optional getOnlyChildElementOptionally(String childName) { - try { - return Optional.of(getOnlyChildElement(childName)); - } catch (Exception e) { + List nameElements = getChildElements(childName); + if (nameElements.size() != 1) { return Optional.absent(); } + return Optional.of(nameElements.get(0)); } - public Optional getOnlyChildElementOptionally(String childName, String namespace) { - try { - return Optional.of(getOnlyChildElement(childName, namespace)); - } catch (Exception e) { + public Optional getOnlyChildElementOptionally(final String childName, final String namespace) { + List children = getChildElementsWithinNamespace(namespace); + children = Lists.newArrayList(Collections2.filter(children, new Predicate() { + @Override + public boolean apply(XmlElement xmlElement) { + return xmlElement.getName().equals(childName); + } + })); + if (children.size() != 1){ return Optional.absent(); } + return Optional.of(children.get(0)); } public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws NetconfDocumentedException { return getOnlyChildElement(childName, getNamespace()); } - public Optional getOnlyChildElementWithSameNamespaceOptionally(String childName) { - try { - return Optional.of(getOnlyChildElement(childName, getNamespace())); - } catch (Exception e) { - return Optional.absent(); + public Optional getOnlyChildElementWithSameNamespaceOptionally(final String childName) { + Optional namespace = getNamespaceOptionally(); + if (namespace.isPresent()) { + List children = getChildElementsWithinNamespace(namespace.get()); + children = Lists.newArrayList(Collections2.filter(children, new Predicate() { + @Override + public boolean apply(XmlElement xmlElement) { + return xmlElement.getName().equals(childName); + } + })); + if (children.size() != 1){ + return Optional.absent(); + } + return Optional.of(children.get(0)); } + return Optional.absent(); } public XmlElement getOnlyChildElementWithSameNamespace() throws NetconfDocumentedException { @@ -295,13 +311,14 @@ public final class XmlElement { } public Optional getOnlyChildElementWithSameNamespaceOptionally() { - try { - XmlElement childElement = getOnlyChildElement(); - childElement.checkNamespace(getNamespace()); - return Optional.of(childElement); - } catch (Exception e) { - return Optional.absent(); + Optional child = getOnlyChildElementOptionally(); + if (child.isPresent() + && child.get().getNamespaceOptionally().isPresent() + && getNamespaceOptionally().isPresent() + && getNamespaceOptionally().get().equals(child.get().getNamespaceOptionally().get())) { + return child; } + return Optional.absent(); } public XmlElement getOnlyChildElement(final String childName, String namespace) throws NetconfDocumentedException { @@ -335,6 +352,14 @@ public final class XmlElement { return children.get(0); } + public Optional getOnlyChildElementOptionally() { + List children = getChildElements(); + if (children.size() != 1) { + return Optional.absent(); + } + return Optional.of(children.get(0)); + } + public String getTextContent() throws NetconfDocumentedException { NodeList childNodes = element.getChildNodes(); if (childNodes.getLength() == 0) { @@ -377,6 +402,14 @@ public final class XmlElement { return attribute; } + public Optional getNamespaceAttributeOptionally(){ + String attribute = element.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY); + if (attribute == null || attribute.equals(DEFAULT_NAMESPACE_PREFIX)){ + return Optional.absent(); + } + return Optional.of(attribute); + } + public Optional getNamespaceOptionally() { String namespaceURI = element.getNamespaceURI(); if (Strings.isNullOrEmpty(namespaceURI)) { @@ -388,7 +421,7 @@ public final class XmlElement { public String getNamespace() throws MissingNameSpaceException { Optional namespaceURI = getNamespaceOptionally(); - if (namespaceURI.isPresent() == false){ + if (!namespaceURI.isPresent()){ throw new MissingNameSpaceException(String.format("No namespace defined for %s", this), NetconfDocumentedException.ErrorType.application, NetconfDocumentedException.ErrorTag.operation_failed, @@ -482,11 +515,8 @@ public final class XmlElement { XmlElement that = (XmlElement) o; - if (!element.isEqualNode(that.element)) { - return false; - } + return element.isEqualNode(that.element); - return true; } @Override @@ -495,15 +525,10 @@ public final class XmlElement { } public boolean hasNamespace() { - try { - getNamespaceAttribute(); - } catch (MissingNameSpaceException e) { - try { - getNamespace(); - } catch (MissingNameSpaceException e1) { + if (!getNamespaceAttributeOptionally().isPresent()) { + if (!getNamespaceOptionally().isPresent()) { return false; } - return true; } return true; }