Performance increase.
Change-Id: I41629b5ea356be697629eb7f9238344f87a62a5e
Signed-off-by: Tomas Cere <tcere@cisco.com>
/**
*
* @param tagName tag name without prefix
/**
*
* @param tagName tag name without prefix
+ * @return List of child elements
*/
public List<XmlElement> getChildElements(final String tagName) {
return getChildElementsInternal(new ElementFilteringStrategy() {
*/
public List<XmlElement> getChildElements(final String tagName) {
return getChildElementsInternal(new ElementFilteringStrategy() {
}
public Optional<XmlElement> getOnlyChildElementOptionally(String childName) {
}
public Optional<XmlElement> getOnlyChildElementOptionally(String childName) {
- try {
- return Optional.of(getOnlyChildElement(childName));
- } catch (Exception e) {
+ List<XmlElement> nameElements = getChildElements(childName);
+ if (nameElements.size() != 1) {
return Optional.absent();
}
return Optional.absent();
}
+ return Optional.of(nameElements.get(0));
- public Optional<XmlElement> getOnlyChildElementOptionally(String childName, String namespace) {
- try {
- return Optional.of(getOnlyChildElement(childName, namespace));
- } catch (Exception e) {
+ public Optional<XmlElement> getOnlyChildElementOptionally(final String childName, final String namespace) {
+ List<XmlElement> children = getChildElementsWithinNamespace(namespace);
+ children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
+ @Override
+ public boolean apply(XmlElement xmlElement) {
+ return xmlElement.getName().equals(childName);
+ }
+ }));
+ if (children.size() != 1){
return Optional.absent();
}
return Optional.absent();
}
+ return Optional.of(children.get(0));
}
public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws NetconfDocumentedException {
return getOnlyChildElement(childName, getNamespace());
}
}
public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws NetconfDocumentedException {
return getOnlyChildElement(childName, getNamespace());
}
- public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally(String childName) {
- try {
- return Optional.of(getOnlyChildElement(childName, getNamespace()));
- } catch (Exception e) {
- return Optional.absent();
+ public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally(final String childName) {
+ Optional<String> namespace = getNamespaceOptionally();
+ if (namespace.isPresent()) {
+ List<XmlElement> children = getChildElementsWithinNamespace(namespace.get());
+ children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
+ @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 {
}
public XmlElement getOnlyChildElementWithSameNamespace() throws NetconfDocumentedException {
}
public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally() {
}
public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally() {
- try {
- XmlElement childElement = getOnlyChildElement();
- childElement.checkNamespace(getNamespace());
- return Optional.of(childElement);
- } catch (Exception e) {
- return Optional.absent();
+ Optional<XmlElement> 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 {
}
public XmlElement getOnlyChildElement(final String childName, String namespace) throws NetconfDocumentedException {
return children.get(0);
}
return children.get(0);
}
+ public Optional<XmlElement> getOnlyChildElementOptionally() {
+ List<XmlElement> 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) {
public String getTextContent() throws NetconfDocumentedException {
NodeList childNodes = element.getChildNodes();
if (childNodes.getLength() == 0) {
+ public Optional<String> 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<String> getNamespaceOptionally() {
String namespaceURI = element.getNamespaceURI();
if (Strings.isNullOrEmpty(namespaceURI)) {
public Optional<String> getNamespaceOptionally() {
String namespaceURI = element.getNamespaceURI();
if (Strings.isNullOrEmpty(namespaceURI)) {
public String getNamespace() throws MissingNameSpaceException {
Optional<String> namespaceURI = getNamespaceOptionally();
public String getNamespace() throws MissingNameSpaceException {
Optional<String> 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,
throw new MissingNameSpaceException(String.format("No namespace defined for %s", this),
NetconfDocumentedException.ErrorType.application,
NetconfDocumentedException.ErrorTag.operation_failed,
XmlElement that = (XmlElement) o;
XmlElement that = (XmlElement) o;
- if (!element.isEqualNode(that.element)) {
- return false;
- }
+ return element.isEqualNode(that.element);
}
public boolean hasNamespace() {
}
public boolean hasNamespace() {
- try {
- getNamespaceAttribute();
- } catch (MissingNameSpaceException e) {
- try {
- getNamespace();
- } catch (MissingNameSpaceException e1) {
+ if (!getNamespaceAttributeOptionally().isPresent()) {
+ if (!getNamespaceOptionally().isPresent()) {