From b4bb718158ed274e664ea2796198597b1c1dfa9c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 Dec 2015 16:32:17 +0000 Subject: Read defaults from content types XML --- .../java/org/zwobble/mammoth/xml/XmlElement.java | 20 ++++++++++++++++++-- .../org/zwobble/mammoth/xml/XmlElementList.java | 17 +++++++++++++++++ src/main/java/org/zwobble/mammoth/xml/XmlNodes.java | 21 +++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/xml/XmlElementList.java create mode 100644 src/main/java/org/zwobble/mammoth/xml/XmlNodes.java (limited to 'src/main/java/org/zwobble/mammoth/xml') diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java index f138917..a42a1b7 100644 --- a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java +++ b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java @@ -1,10 +1,14 @@ 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 com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; +import static com.google.common.collect.Iterables.filter; public class XmlElement implements XmlNode { public XmlElement(String name) { @@ -33,6 +37,11 @@ public class XmlElement implements XmlNode { return attributes; } + public String getAttribute(String name) { + // TODO: throw more informative error message + return Optional.ofNullable(attributes.get(name)).get(); + } + public List getChildren() { return children; } @@ -51,4 +60,11 @@ public class XmlElement implements XmlNode { return "XmlElement(name=" + name + ", attributes=" + attributes + ", children=" + children + ")"; } + + public XmlElementList findChildren(String name) { + Iterable filtered = filter( + filter(children, XmlElement.class), + child -> child.getName().equals(name)); + return new XmlElementList(ImmutableList.copyOf(filtered)); + } } diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElementList.java b/src/main/java/org/zwobble/mammoth/xml/XmlElementList.java new file mode 100644 index 0000000..9e46314 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/xml/XmlElementList.java @@ -0,0 +1,17 @@ +package org.zwobble.mammoth.xml; + +import java.util.Iterator; +import java.util.List; + +public class XmlElementList implements Iterable { + private final List elements; + + public XmlElementList(List elements) { + this.elements = elements; + } + + @Override + public Iterator iterator() { + return elements.iterator(); + } +} diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java b/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java new file mode 100644 index 0000000..272a710 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java @@ -0,0 +1,21 @@ +package org.zwobble.mammoth.xml; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +import java.util.List; +import java.util.Map; + +public class XmlNodes { + public static XmlElement element(String name, Map attributes) { + return element(name, attributes, ImmutableList.of()); + } + + public static XmlElement element(String name, List children) { + return element(name, ImmutableMap.of(), children); + } + + public static XmlElement element(String name, Map attributes, List children) { + return new XmlElement(name, attributes, children); + } +} -- cgit v1.2.3