Code formating and whitespace 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 implements Exporter
15 {
16     OutputStream os_;
17     SimpleXmlGenerator gen_;
18
19     /**
20      * Write Addressbook to Fritzbox XML stream
21      * 
22      * @param os
23      *            OutputStrea to which the XML data is sent.
24      *            <em>After successful</em> construction, the class takes
25      *            responsibility for closing the streanm.
26      */
27     public FritzAddressbookExporter(OutputStream os)
28     {
29         try {
30             os_ = os;
31             gen_ = new SimpleXmlGenerator(os_, "iso-8859-1");
32             gen_.startDocument();
33             gen_.nl();
34             gen_.start("phonebooks");
35             gen_.nl();
36             gen_.start("phonebook");
37             gen_.nl();
38         }
39         catch (SAXException e) {
40             throw new AssertionError("Invalid XML/SAX document generated.");
41         }
42         catch (UnsupportedEncodingException e) {
43             throw new AssertionError("Unsopported encoding iso-8859-1 ??");
44         }
45     }
46
47     @Override
48     public void consume(Entry entry)
49     {
50         try {
51             gen_.start("contact");
52             gen_.nl();
53
54             gen_.start("category");
55             gen_.text("0");
56             gen_.end();
57             gen_.nl();
58
59             gen_.start("person");
60             gen_.nl();
61             gen_.start("realName");
62             gen_.text(entry.name());
63             gen_.end();
64             gen_.nl();
65             gen_.empty("imageURL");
66             gen_.nl();
67             gen_.end();
68             gen_.nl();
69
70             gen_.start("telephony");
71             gen_.nl();
72             for (Attribute number : entry.attributes("phone")) {
73                 gen_.start(
74                         "number",
75                         gen_.attribute("prio", number.index > 0 ? "0" : "1")
76                                 .attribute("type", number.rel)
77                                 .attribute("vanity", ""));
78                 gen_.text(number.value);
79                 gen_.end();
80                 gen_.nl();
81             }
82             gen_.end();
83             gen_.nl();
84
85             Attribute email = Util.first(entry.attributes("email"));
86             if (email != null) {
87                 gen_.start("services");
88                 gen_.nl();
89                 gen_.start("email", gen_.attribute("classifier", "private"));
90                 gen_.text(email.value);
91                 gen_.end();
92                 gen_.nl();
93                 gen_.end();
94                 gen_.nl();
95             }
96             else {
97                 gen_.empty("services");
98                 gen_.nl();
99             }
100
101             gen_.end();
102             gen_.nl();
103         }
104         catch (SAXException e) {
105             throw new AssertionError("Invalid XML/SAX document generated.");
106         }
107     }
108
109     @Override
110     public void close() throws IOException
111     {
112         try {
113             gen_.end();
114             gen_.nl();
115             gen_.end();
116             gen_.endDocument();
117             os_.close();
118             os_ = null;
119         }
120         catch (SAXException e) {
121             throw new AssertionError("Invalid XML/SAX document generated.");
122         }
123         finally {
124             Util.nothrowClose(os_);
125         }
126     }
127
128 }