Implement spring Conduit bean registry and lots of cleanup
[jpim.git] / src / de / j32 / pimstuff / conduit / FritzAddressbookExporter.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 FritzAddressbookExporter
15         implements Exporter
16 {
17         OutputStream os_;
18         SimpleXmlGenerator gen_;
19         
20         public FritzAddressbookExporter(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                         throw new AssertionError("Invalid XML/SAX document generated.");
31                 }
32                 catch (UnsupportedEncodingException e) {
33                         throw new AssertionError("Unsopported encoding iso-8859-1 ??");
34                 }
35                 finally {
36                         Util.nothrowClose(os);
37                 }
38         }
39
40         @Override
41         public void consume(Entry entry)
42         {
43                 try {
44                         gen_.start("contact"); gen_.nl();
45
46                                 gen_.start("category"); gen_.text("0"); gen_.end(); gen_.nl();
47                         
48                                 gen_.start("person"); gen_.nl();
49                                         gen_.start("realName"); gen_.text(entry.name()); gen_.end(); gen_.nl();
50                                         gen_.empty("imageURL"); gen_.nl();
51                                 gen_.end(); gen_.nl();
52                                 
53                                 gen_.start("telephony"); gen_.nl();
54                                         for (Attribute number : entry.attributes("phone")) {
55                                                 gen_.start("number",
56                                                                 gen_.attribute("prio",   number.index > 0 ? "0" : "1")
57                                                                         .attribute("type",   number.rel)
58                                                                         .attribute("vanity", ""));
59                                                     gen_.text(number.value);    gen_.end(); gen_.nl();
60                                         }
61                                 gen_.end(); gen_.nl();
62                                 
63                                 Attribute email  = Util.first(entry.attributes("email"));
64                                 if (email != null) {
65                                         gen_.start("services"); gen_.nl();
66                                                 gen_.start("email", gen_.attribute("classifier","private"));
67                                                         gen_.text(email.value); gen_.end(); gen_.nl();
68                                         gen_.end(); gen_.nl();
69                                 }
70                                 else {
71                                         gen_.empty("services"); gen_.nl();
72                                 }
73                                 
74                         gen_.end(); gen_.nl();
75                 }
76                 catch (SAXException e)
77                 {
78                         throw new AssertionError("Invalid XML/SAX document generated.");
79                 }
80         }
81
82         @Override
83         public void close()
84                 throws IOException
85         {
86                 try {
87                         gen_.end(); gen_.nl();
88                         gen_.end();
89                         gen_.endDocument();
90                         os_.close();
91                         os_ = null;
92                 }
93                 catch (SAXException e)
94                 {
95                         throw new AssertionError("Invalid XML/SAX document generated.");
96                 }
97                 finally
98                 {
99                         Util.nothrowClose(os_);
100                 }
101         }
102         
103 }