From 0b2565d981fd98dd0bf5446e0134927f20ce1284 Mon Sep 17 00:00:00 2001 From: James Ball Date: Sat, 24 Oct 2020 13:05:55 +0100 Subject: [PATCH] Add XML Util class --- src/parser/SvgParser.java | 3 +++ src/parser/XmlUtil.java | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/parser/XmlUtil.java diff --git a/src/parser/SvgParser.java b/src/parser/SvgParser.java index 72b31c4..cb25b81 100644 --- a/src/parser/SvgParser.java +++ b/src/parser/SvgParser.java @@ -8,6 +8,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; +import org.w3c.dom.Node; import org.xml.sax.SAXException; import shapes.Shape; @@ -33,6 +34,8 @@ public class SvgParser extends FileParser { DocumentBuilder builder = factory.newDocumentBuilder(); File file = new File(path); Document doc = builder.parse(file); + + for (Node elem : doc.getElementsByTagName("path")) } @Override diff --git a/src/parser/XmlUtil.java b/src/parser/XmlUtil.java new file mode 100644 index 0000000..8d87a51 --- /dev/null +++ b/src/parser/XmlUtil.java @@ -0,0 +1,30 @@ +package parser; + +import java.util.AbstractList; +import java.util.Collections; +import java.util.List; +import java.util.RandomAccess; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public final class XmlUtil { + private XmlUtil(){} + + public static List asList(NodeList n) { + return n.getLength()==0? + Collections.emptyList(): new NodeListWrapper(n); + } + static final class NodeListWrapper extends AbstractList + implements RandomAccess { + private final NodeList list; + NodeListWrapper(NodeList l) { + list=l; + } + public Node get(int index) { + return list.item(index); + } + public int size() { + return list.getLength(); + } + } +}