initial commit
[jpim.git] / src / de / j32 / pimstuff / conduit / FritzAddressbookWriter.java
1 package de.j32.pimstuff.conduit;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.UnsupportedEncodingException;
6
7 import org.xml.sax.SAXException;
8
9 import de.j32.pimstuff.data.Attribute;
10 import de.j32.pimstuff.data.Entry;
11 import de.j32.util.SimpleXmlGenerator;
12 import de.j32.util.Util;
13
14 public class FritzAddressbookWriter
15         implements Exporter
16 {
17         OutputStream os_;
18         SimpleXmlGenerator gen_;
19         
20         public FritzAddressbookWriter(OutputStream os)
21         {
22                 try {
23                         os_ = os;
24                         gen_ = new SimpleXmlGenerator(os_, "iso-8859-1");
25                         gen_.startDocument(); gen_.nl();
26                         gen_.start("phonebooks"); gen_.nl();
27                         gen_.start("phonebook"); gen_.nl();
28                 }
29                 catch (SAXException e)
30                 {
31                         throw new AssertionError("Invalid XML/SAX document generated.");
32                 } catch (UnsupportedEncodingException e) {
33                         throw new AssertionError("Unsopported encoding iso-8859-1 ??");
34                 }
35         }
36
37         @Override
38         public void consume(Entry entry)
39         {
40                 try {
41                         gen_.start("contact"); gen_.nl();
42
43                                 gen_.start("category"); gen_.text("0"); gen_.end(); gen_.nl();
44                         
45                                 gen_.start("person"); gen_.nl();
46                                         gen_.start("realName"); gen_.text(entry.name()); gen_.end(); gen_.nl();
47                                         gen_.empty("imageURL"); gen_.nl();
48                                 gen_.end(); gen_.nl();
49                                 
50                                 gen_.start("telephony"); gen_.nl();
51                                         for (Attribute number : entry.attributes("phone")) {
52                                                 gen_.start("number",
53                                                                 gen_.attribute("prio",   number.index > 0 ? "0" : "1")
54                                                                         .attribute("type",   number.rel)
55                                                                         .attribute("vanity", ""));
56                                                     gen_.text(number.value);    gen_.end(); gen_.nl();
57                                         }
58                                 gen_.end(); gen_.nl();
59                                 
60                                 Attribute email  = Util.first(entry.attributes("email"));
61                                 if (email != null) {
62                                         gen_.start("services"); gen_.nl();
63                                                 gen_.start("email", gen_.attribute("classifier","private"));
64                                                         gen_.text(email.value); gen_.end(); gen_.nl();
65                                         gen_.end(); gen_.nl();
66                                 }
67                                 else {
68                                         gen_.empty("services"); gen_.nl();
69                                 }
70                                 
71                         gen_.end(); gen_.nl();
72                 }
73                 catch (SAXException e)
74                 {
75                         throw new AssertionError("Invalid XML/SAX document generated.");
76                 }
77         }
78
79         @Override
80         public void close()
81                 throws IOException
82         {
83                 try {
84                         gen_.end(); gen_.nl();
85                         gen_.end();
86                         gen_.endDocument();
87                 }
88                 catch (SAXException e)
89                 {
90                         throw new AssertionError("Invalid XML/SAX document generated.");
91                 }
92         }
93         
94 }