From 853ed35d873b5a3e95d5d332ffeb2211f772edfe Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 Dec 2015 17:22:57 +0000 Subject: Add reading of numbering.xml --- .../java/org/zwobble/mammoth/xml/XmlElement.java | 28 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/main/java/org/zwobble/mammoth/xml/XmlElement.java') diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java index a42a1b7..8c3a011 100644 --- a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java +++ b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java @@ -2,15 +2,15 @@ package org.zwobble.mammoth.xml; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; import java.util.List; import java.util.Map; import java.util.Optional; import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Iterables.getFirst; -public class XmlElement implements XmlNode { +public class XmlElement implements XmlNode, XmlElementLike { public XmlElement(String name) { this(name, ImmutableMap.of()); } @@ -39,7 +39,12 @@ public class XmlElement implements XmlNode { public String getAttribute(String name) { // TODO: throw more informative error message - return Optional.ofNullable(attributes.get(name)).get(); + return getAttributeOrNone(name).get(); + } + + @Override + public Optional getAttributeOrNone(String name) { + return Optional.ofNullable(attributes.get(name)); } public List getChildren() { @@ -62,9 +67,22 @@ public class XmlElement implements XmlNode { } public XmlElementList findChildren(String name) { - Iterable filtered = filter( + Iterable filtered = findChildrenIterable(name); + return new XmlElementList(ImmutableList.copyOf(filtered)); + } + + public XmlElement findChild(String name) { + return findChildrenIterable(name).iterator().next(); + } + + @Override + public XmlElementLike findChildOrEmpty(String name) { + return getFirst(findChildrenIterable(name), NullXmlElement.INSTANCE); + } + + private Iterable findChildrenIterable(String name) { + return filter( filter(children, XmlElement.class), child -> child.getName().equals(name)); - return new XmlElementList(ImmutableList.copyOf(filtered)); } } -- cgit v1.2.3