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