XmlUtil cleanup
Stefan Bund [Mon, 20 Sep 2010 20:35:29 +0000 (22:35 +0200)]
src/de/j32/pimstuff/conduit/FritzAddressbookImporter.java
src/de/j32/util/Util.java
src/de/j32/util/XmlUtil.java

index 5330cc6..f434185 100644 (file)
@@ -33,7 +33,7 @@ public class FritzAddressbookImporter
        @Override
        public void sendTo(EntryConsumer consumer)
        {
-               for (Element node : XmlUtil.iterateElements(xml_.getElementsByTagName("contact"))) {
+               for (Element node : XmlUtil.iterate(xml_.getElementsByTagName("contact"),Element.class)) {
                        /* subnodes:
                         *   category (unused, always 0)
                         *   person/realName
@@ -47,7 +47,7 @@ public class FritzAddressbookImporter
                        try {
                                entry.name(node.getElementsByTagName("realName").item(0).getTextContent());
 
-                               for (Element phone : XmlUtil.iterateElements(node.getElementsByTagName("number"))) {
+                               for (Element phone : XmlUtil.iterate(node.getElementsByTagName("number"),Element.class)) {
                                        entry.attribute("phone", phone.getAttribute("type"), phone.getTextContent());
                                }
                                
index 95a3f78..db43919 100644 (file)
@@ -8,25 +8,29 @@ import java.util.Iterator;
 
 public class Util {
 
-       public static <E> E nonnull(E ob) {
+       public static <E> E nonnull(E ob)
+       {
                if (ob == null)
                        throw new NullPointerException();
                return ob;
        }
 
        public static void transfer(InputStream is, OutputStream os)
-                       throws IOException {
+                       throws IOException
+       {
                byte[] buffer = new byte[16384];
                int len = -1;
                while ((len = is.read(buffer)) != -1)
                        os.write(buffer, 0, len);
        }
 
-       public static void nothrowClose(Closeable c) {
+       public static void nothrowClose(Closeable c)
+       {
                if (c != null) {
                        try {
                                c.close();
-                       } catch (IOException e) {}
+                       }
+                       catch (IOException e) {}
                }
        }
        
index 693e402..0bc9ebc 100644 (file)
@@ -10,7 +10,6 @@ import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.w3c.dom.Document;
-import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.EntityResolver;
@@ -40,24 +39,24 @@ public class XmlUtil
                }
        }
        
-       public static Iterable<Element> iterateElements(final NodeList nodes)
+       public static <E extends Node> Iterable<E> iterate(final NodeList nodes, final Class<E> type)
        {
-               return new Iterable<Element>() {
-                       public Iterator<Element> iterator() {
-                               return new NodeListIterator<Element>(nodes, Element.class);
+               return new Iterable<E>() {
+                       public Iterator<E> iterator() {
+                               return new NodeListIterator<E>(nodes, type);
                        }
                };
        }
-
+       
        static class NodeListIterator<E extends Node>
                implements Iterator<E>
        {
-               Class<?> nodeType_;
+               Class<E> nodeType_;
                NodeList nodes_;
                int i_ = 0;
                E next_;
                
-               public NodeListIterator(NodeList nodes, Class<?> nodeType)
+               public NodeListIterator(NodeList nodes, Class<E> nodeType)
                {
                        nodes_ = nodes;
                        nodeType_ = nodeType;