Implement spring Conduit bean registry and lots of 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.Util;
13 import de.j32.util.XmlUtil;
14
15 public class FritzAddressbookImporter
16         implements Importer
17 {
18         Document xml_;
19         
20         public FritzAddressbookImporter(InputStream is)
21                 throws SAXException, IOException
22         {
23                 try {
24                         xml_ = XmlUtil.parse(is);
25                         is.close();
26                         is = null;
27                 }
28                 finally {
29                         Util.nothrowClose(is);
30                 }
31         }
32         
33         @Override
34         public void sendTo(EntryConsumer consumer)
35         {
36                 for (Element node : XmlUtil.iterateElements(xml_.getElementsByTagName("contact"))) {
37                         /* subnodes:
38                          *   category (unused, always 0)
39                          *   person/realName
40                          *   person/imageURL
41                          *   telephony/number (@prio, @type, @vanity)
42                          *   services/email
43                          *   mod_time
44                          */
45                         Entry entry = new Entry();
46
47                         try {
48                                 entry.name(node.getElementsByTagName("realName").item(0).getTextContent());
49
50                                 for (Element phone : XmlUtil.iterateElements(node.getElementsByTagName("number"))) {
51                                         entry.attribute("phone", phone.getAttribute("type"), phone.getTextContent());
52                                 }
53                                 
54                                 try {
55                                         entry.attribute("email", "", 
56                                                         node.getElementsByTagName("email").item(0).getTextContent());
57                                 }
58                                 catch (NullPointerException e) {} // ignore missing optional email
59                         }
60                         catch (NullPointerException e) {
61                                 // Ignore incomplete entries
62                                 entry = null;
63                         }
64
65                         if (entry != null)
66                                 consumer.consume(entry);
67                 }
68         }
69
70         @Override
71         public void close()
72                 throws IOException
73         {}
74         
75 }