@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
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());
}
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) {}
}
}
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;
}
}
- 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;