Code formating and whitespace cleanup
[jpim.git] / src / de / j32 / pimstuff / conduit / FritzAddressbookImporter.java
1 package de.j32.pimstuff.conduit;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
8 import org.xml.sax.SAXException;
9
10 import de.j32.pimstuff.data.Entry;
11 import de.j32.pimstuff.data.EntryConsumer;
12 import de.j32.util.XmlUtil;
13
14 public class FritzAddressbookImporter implements Importer
15 {
16     Document xml_;
17
18     /**
19      * Importer reading Addressbook from Fritzbox XML file
20      * 
21      * @param is InputStream providing the XML data. <em>After successful</em>
22      *            construction, the class takes responsibility for closing the
23      *            stream.
24      * @throws SAXException
25      * @throws IOException
26      */
27     public FritzAddressbookImporter(InputStream is) throws SAXException,
28             IOException
29     {
30         // It does not make sens to try / finally here, at least conceptually:
31         // Since
32         // the base-class constructor might throw we would never get a chance to
33         // properly close is. Thus I deem it safer to only take responsibility
34         // for is
35         // when the constructor does NOT throw and place the try / finally into
36         // the
37         // callers code.
38         xml_ = XmlUtil.parse(is);
39         is.close();
40     }
41
42     @Override
43     public void sendTo(EntryConsumer consumer)
44     {
45         for (Element node : XmlUtil.iterate(
46                 xml_.getElementsByTagName("contact"), Element.class)) {
47             /*
48              * subnodes: category (unused, always 0) person/realName
49              * person/imageURL telephony/number (@prio, @type, @vanity)
50              * services/email mod_time
51              */
52             Entry entry = new Entry();
53
54             try {
55                 entry.name(node.getElementsByTagName("realName").item(0)
56                         .getTextContent());
57
58                 for (Element phone : XmlUtil.iterate(
59                         node.getElementsByTagName("number"), Element.class)) {
60                     entry.attribute("phone", phone.getAttribute("type"),
61                             phone.getTextContent());
62                 }
63
64                 try {
65                     entry.attribute("email", "",
66                             node.getElementsByTagName("email").item(0)
67                                     .getTextContent());
68                 }
69                 catch (NullPointerException e) {} // ignore missing optional
70                                                   // email
71             }
72             catch (NullPointerException e) {
73                 // Ignore incomplete entries
74                 entry = null;
75             }
76
77             if (entry != null) consumer.consume(entry);
78         }
79     }
80
81     @Override
82     public void close() throws IOException
83     {}
84
85 }