initial commit
[jpim.git] / src / de / j32 / pimstuff / Main.java
1 package de.j32.pimstuff;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.util.Properties;
10
11 import org.xml.sax.SAXException;
12
13 import de.j32.avmfritz.FritzBox;
14 import de.j32.pimstuff.conduit.FritzAddressbookReader;
15 import de.j32.pimstuff.conduit.FritzAddressbookWriter;
16 import de.j32.pimstuff.data.Addressbook;
17 import de.j32.util.Util;
18
19 public class Main {
20
21         public static void main(String[] args)
22         {       
23                 try {
24                         
25                         System.out.println("Launching pimstuff ...");
26                         Properties config = new Properties();
27                         
28                         try {
29                                 config.loadFromXML(new FileInputStream("config.xml"));
30                         }
31                         catch (FileNotFoundException e) {
32                                 config.setProperty("password", "password");
33                                 config.setProperty("url", "http://fritz.box");
34                                 
35                                 config.storeToXML(new FileOutputStream("config.xml"), null, "UTF-8");
36                         }
37                         
38                         String pw, url;
39
40                         try {
41                                 pw = Util.nonnull(config.getProperty("password"));
42                                 url = Util.nonnull(config.getProperty("url"));
43                         }
44                         catch (NullPointerException e) {
45                                 throw new RuntimeException("Missing configuration parameter");
46                         }
47
48                         Addressbook ab = new Addressbook();
49                         FritzBox fb = new FritzBox(pw, url);
50
51                         System.out.println("loading ...");
52                         // Load Addressbook from FritzBox
53                         InputStream is = null;
54                         FritzAddressbookReader reader = null;
55                         try {
56                                 ab = new Addressbook();
57                                 is = fb.exportAddressbook();
58                                 reader = new FritzAddressbookReader(is);
59                                 reader.sendTo(ab);
60                                 is.close();
61                                 is = null;
62                                 reader.close();
63                                 reader = null;
64                         }
65                         finally {
66                                 Util.nothrowClose(is);
67                         }
68                         
69                         System.out.println("saving ...");
70                         // Save Addressbook back to FritzBox
71                         FritzAddressbookWriter writer = null;
72                         OutputStream os = null;
73                         try {
74                                 os = fb.importAddressbook();
75                                 writer = new FritzAddressbookWriter(os);
76                                 ab.sendTo(writer);
77                                 writer.close();
78                                 writer = null;
79                                 os.close();
80                                 os = null;
81                         }
82                         finally {
83                                 Util.nothrowClose(writer);
84                                 Util.nothrowClose(os);
85                         }
86
87                 } catch (SAXException e) {
88                         // TODO Auto-generated catch block
89                         e.printStackTrace();
90                 } catch (IOException e) {
91                         // TODO Auto-generated catch block
92                         e.printStackTrace();
93                 }
94         }
95
96 }