KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > util > StaticDataHelper


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.util;
20
21 import java.util.Hashtable JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * This class supports
27  * general using, language dictionary access
28  * statics data and static methods
29  *
30  * @author Fabio Maggi @ Funambol
31  * @version $Id: StaticDataHelper.java,v 1.11 2005/03/11 15:13:05 fabius Exp $
32  *
33  **/

34
35 public class StaticDataHelper {
36
37     //----------------------------------------------------------------- Constants
38

39     final private static String JavaDoc KEY_HOMEPAGE = "homepage-default" ;
40     final private static String JavaDoc KEY_LOGIN = "login-default" ;
41     final private static String JavaDoc KEY_CONTACT_SOURCE_URI = "contact-source-uri-default" ;
42     final private static String JavaDoc KEY_CALENDAR_SOURCE_URI = "calendar-source-uri-default" ;
43     final private static String JavaDoc KEY_GATEWAY_APN = "gateway-apn-default" ;
44     final private static String JavaDoc KEY_GATEWAY_IP = "gateway-ip-default" ;
45     final private static String JavaDoc KEY_GATEWAY_PORT = "gateway-port-default" ;
46     final private static String JavaDoc KEY_SYNC_CONTACT = "sync-contact-default" ;
47     final private static String JavaDoc KEY_SYNC_CALENDAR = "sync-calendar-default" ;
48     final private static String JavaDoc KEY_ENCODE = "encode-default" ;
49
50     final private static String JavaDoc HOST_PORT_DEFAULT = "8080" ;
51     final private static String JavaDoc GATEWAY_PORT_DEFAULT = "9201" ;
52
53     final private static String JavaDoc XML_LANGUAGE = "/xml/language.xml" ;
54
55    //----------------------------------------------------------------- Private data
56

57     private static String JavaDoc homePageDefault = null ;
58     private static String JavaDoc loginDefault = null ;
59     private static String JavaDoc userDefault = null ;
60     private static String JavaDoc passwordDefault = null ;
61     private static String JavaDoc contactSourceUriDefault = null ;
62     private static String JavaDoc calendarSourceUriDefault = null ;
63     private static String JavaDoc gatewayApnDefault = null ;
64     private static String JavaDoc gatewayIpDefault = null ;
65     private static String JavaDoc gatewayPortDefault = null ;
66     private static boolean syncContactDefault = false ;
67     private static boolean syncCalendarDefault = false ;
68     private static boolean encodeDefault = false ;
69
70     private static String JavaDoc contactSourceUri = null ;
71     private static String JavaDoc calendarSourceUri = null ;
72  
73     /** debug enable */
74     private static boolean debug = false ;
75
76     /** language words */
77     private static String JavaDoc language = null ;
78
79     /** language words */
80     private static Hashtable JavaDoc languageHash = new Hashtable JavaDoc();
81
82     //----------------------------------------------------------------- Public methods
83

84     public String JavaDoc getHomePageDefault () {
85         return homePageDefault;
86     }
87
88     public String JavaDoc getUserDefault () {
89         return userDefault;
90     }
91
92     public String JavaDoc getPasswordDefault () {
93         return passwordDefault;
94     }
95
96     public String JavaDoc getContactSourceUriDefault () {
97         return contactSourceUriDefault;
98     }
99
100     public String JavaDoc getCalendarSourceUriDefault () {
101         return calendarSourceUriDefault;
102     }
103
104     public String JavaDoc getGatewayApnDefault () {
105         return gatewayApnDefault;
106     }
107
108     public String JavaDoc getGatewayIpDefault () {
109         return gatewayIpDefault;
110     }
111
112     public String JavaDoc getGatewayPortDefault () {
113         return gatewayPortDefault;
114     }
115
116     public boolean getSyncContactDefault () {
117         return syncContactDefault;
118     }
119
120     public boolean getSyncCalendarDefault () {
121         return syncCalendarDefault;
122     }
123
124     public String JavaDoc getContactSourceUri () {
125         return contactSourceUri;
126     }
127
128    
129
130     public void setContactSourceUri (String JavaDoc contactSUri) {
131         contactSourceUri = contactSUri;
132     }
133
134     public void setCalendarSourceUri (String JavaDoc calendarSUri) {
135         calendarSourceUri = calendarSUri;
136     }
137
138     public boolean isEncode () {
139         return encodeDefault;
140     }
141
142     /**
143      * Logs a string.
144      *
145      * @param msg the string to be logged
146      **/

147     public static void log(String JavaDoc msg) {
148         if (debug)
149             System.out.println(msg);
150     }
151
152     /**
153      * Make a String by value of <i>tag</i>.
154      *
155      * @param key key to find
156      * @return key value
157      **/

158     public static String JavaDoc getLanguage(String JavaDoc key) {
159
160         String JavaDoc startTag = null ;
161         String JavaDoc endTag = null ;
162
163         String JavaDoc value = null ;
164
165         Object JavaDoc valueFromHash = null ;
166
167         valueFromHash = languageHash.get(key);
168
169         if (valueFromHash != null) {
170             return (String JavaDoc) valueFromHash;
171         }
172
173         startTag = "<" + key + ">" ;
174         endTag = "</" + key + ">" ;
175
176         String JavaDoc msg = "Parsing XML, TAG " + key ;
177         StaticDataHelper.log (msg);
178
179         value = language.substring
180                 (language.indexOf(startTag) + startTag.length(), language.indexOf(endTag)).trim();
181
182         languageHash.put(key, value);
183
184         return value;
185
186     }
187
188     /**
189      * load xml language file
190      */

191     public static void loadLanguage (Class JavaDoc c)
192     throws Exception JavaDoc{
193         try {
194             language = read(c.getResourceAsStream(XML_LANGUAGE));
195         } catch (IOException JavaDoc e ) {
196
197             String JavaDoc msg = "Error: load " +
198                          XML_LANGUAGE +
199                          " file: " +
200                          e.getMessage() ;
201
202             StaticDataHelper.log (msg);
203             throw e;
204         }
205     }
206
207     /**
208      * Reads the content of the given input stream.
209      *
210      * @param is the input stream
211      **/

212     public static String JavaDoc read(InputStream JavaDoc is) throws IOException JavaDoc {
213         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
214
215         try {
216
217             byte[] buf = new byte[1024];
218
219             int nbyte = -1;
220             while ((nbyte = is.read(buf)) >= 0) {
221                 sb.append(new String JavaDoc(buf, 0, nbyte));
222             }
223         } finally {
224             is.close();
225         }
226
227         return sb.toString();
228     }
229
230     /**
231      * Load default properties form jad file
232      */

233     public void loadDefaultValue()
234     throws Exception JavaDoc {
235
236         String JavaDoc enableContact = null ;
237         String JavaDoc enableCalendar = null ;
238         String JavaDoc encode = null ;
239
240         homePageDefault = getLanguage(KEY_HOMEPAGE );
241         loginDefault = getLanguage(KEY_LOGIN );
242         contactSourceUriDefault = getLanguage(KEY_CONTACT_SOURCE_URI );
243         calendarSourceUriDefault = getLanguage(KEY_CALENDAR_SOURCE_URI );
244         gatewayApnDefault = getLanguage(KEY_GATEWAY_APN );
245         gatewayIpDefault = getLanguage(KEY_GATEWAY_IP );
246         gatewayPortDefault = getLanguage(KEY_GATEWAY_PORT );
247         enableContact = getLanguage(KEY_SYNC_CONTACT );
248         enableCalendar = getLanguage(KEY_SYNC_CALENDAR );
249         encode = getLanguage(KEY_ENCODE );
250
251         if (homePageDefault == null && !(homePageDefault.length() > 0)) {
252             homePageDefault = "";
253         }
254
255         if (loginDefault == null) {
256             userDefault = "" ;
257             passwordDefault = "" ;
258         } else if ((loginDefault.length() > 0) && loginDefault.indexOf(":") == -1) {
259             String JavaDoc msg = "Error: load jad properties wrong login";
260             StaticDataHelper.log (msg);
261             throw new Exception JavaDoc(msg);
262         }
263
264         userDefault = loginDefault.substring(0, loginDefault.indexOf(":") );
265         passwordDefault = loginDefault.substring(loginDefault.indexOf(":") + 1 );
266
267         if (contactSourceUriDefault == null) {
268             contactSourceUriDefault = "";
269         }
270
271         if (calendarSourceUriDefault == null) {
272             calendarSourceUriDefault = "";
273         }
274
275         if (gatewayApnDefault == null) {
276             gatewayApnDefault = "";
277         }
278
279         if (gatewayIpDefault == null) {
280             gatewayIpDefault = "";
281         }
282
283         if (gatewayPortDefault == null || !(gatewayPortDefault.length() > 0)) {
284             gatewayPortDefault = GATEWAY_PORT_DEFAULT;
285         } else {
286             try {
287                 Integer.parseInt(gatewayPortDefault);
288             } catch (NumberFormatException JavaDoc e){
289                 String JavaDoc msg = "Error: load jad properties wrong gateway-port";
290                 StaticDataHelper.log (msg);
291                 throw new Exception JavaDoc(msg);
292             }
293         }
294
295         if ("true".equals(enableContact)) {
296             syncContactDefault = true;
297         }
298
299         if ("true".equals(enableCalendar)) {
300             syncCalendarDefault = true;
301         }
302
303         if ("true".equals(encode)) {
304             encodeDefault = true;
305         }
306
307     }
308
309 }
310
Popular Tags