diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-12-31 17:22:57 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-12-31 17:22:57 +0000 |
| commit | 853ed35d873b5a3e95d5d332ffeb2211f772edfe (patch) | |
| tree | d53e752bf0bb6d4fabb05aa376b88f8cdf7a4f42 /src/main/java/org/zwobble/mammoth/xml/XmlElement.java | |
| parent | 9fd7f2dbe1f5a68e4355b4e08def3eb074251a04 (diff) | |
Add reading of numbering.xml
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/xml/XmlElement.java')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/xml/XmlElement.java | 28 |
1 files changed, 23 insertions, 5 deletions
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<String> getAttributeOrNone(String name) { + return Optional.ofNullable(attributes.get(name)); } public List<XmlNode> getChildren() { @@ -62,9 +67,22 @@ public class XmlElement implements XmlNode { } public XmlElementList findChildren(String name) { - Iterable<XmlElement> filtered = filter( + Iterable<XmlElement> 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<XmlElement> findChildrenIterable(String name) { + return filter( filter(children, XmlElement.class), child -> child.getName().equals(name)); - return new XmlElementList(ImmutableList.copyOf(filtered)); } } |
