1 18 19 package sync4j.syncclient.util; 20 21 import java.util.Hashtable ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 25 34 35 public class StaticDataHelper { 36 37 39 final private static String KEY_HOMEPAGE = "homepage-default" ; 40 final private static String KEY_LOGIN = "login-default" ; 41 final private static String KEY_CONTACT_SOURCE_URI = "contact-source-uri-default" ; 42 final private static String KEY_CALENDAR_SOURCE_URI = "calendar-source-uri-default" ; 43 final private static String KEY_GATEWAY_APN = "gateway-apn-default" ; 44 final private static String KEY_GATEWAY_IP = "gateway-ip-default" ; 45 final private static String KEY_GATEWAY_PORT = "gateway-port-default" ; 46 final private static String KEY_SYNC_CONTACT = "sync-contact-default" ; 47 final private static String KEY_SYNC_CALENDAR = "sync-calendar-default" ; 48 final private static String KEY_ENCODE = "encode-default" ; 49 50 final private static String HOST_PORT_DEFAULT = "8080" ; 51 final private static String GATEWAY_PORT_DEFAULT = "9201" ; 52 53 final private static String XML_LANGUAGE = "/xml/language.xml" ; 54 55 57 private static String homePageDefault = null ; 58 private static String loginDefault = null ; 59 private static String userDefault = null ; 60 private static String passwordDefault = null ; 61 private static String contactSourceUriDefault = null ; 62 private static String calendarSourceUriDefault = null ; 63 private static String gatewayApnDefault = null ; 64 private static String gatewayIpDefault = null ; 65 private static String 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 contactSourceUri = null ; 71 private static String calendarSourceUri = null ; 72 73 74 private static boolean debug = false ; 75 76 77 private static String language = null ; 78 79 80 private static Hashtable languageHash = new Hashtable (); 81 82 84 public String getHomePageDefault () { 85 return homePageDefault; 86 } 87 88 public String getUserDefault () { 89 return userDefault; 90 } 91 92 public String getPasswordDefault () { 93 return passwordDefault; 94 } 95 96 public String getContactSourceUriDefault () { 97 return contactSourceUriDefault; 98 } 99 100 public String getCalendarSourceUriDefault () { 101 return calendarSourceUriDefault; 102 } 103 104 public String getGatewayApnDefault () { 105 return gatewayApnDefault; 106 } 107 108 public String getGatewayIpDefault () { 109 return gatewayIpDefault; 110 } 111 112 public String 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 getContactSourceUri () { 125 return contactSourceUri; 126 } 127 128 129 130 public void setContactSourceUri (String contactSUri) { 131 contactSourceUri = contactSUri; 132 } 133 134 public void setCalendarSourceUri (String calendarSUri) { 135 calendarSourceUri = calendarSUri; 136 } 137 138 public boolean isEncode () { 139 return encodeDefault; 140 } 141 142 147 public static void log(String msg) { 148 if (debug) 149 System.out.println(msg); 150 } 151 152 158 public static String getLanguage(String key) { 159 160 String startTag = null ; 161 String endTag = null ; 162 163 String value = null ; 164 165 Object valueFromHash = null ; 166 167 valueFromHash = languageHash.get(key); 168 169 if (valueFromHash != null) { 170 return (String ) valueFromHash; 171 } 172 173 startTag = "<" + key + ">" ; 174 endTag = "</" + key + ">" ; 175 176 String 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 191 public static void loadLanguage (Class c) 192 throws Exception { 193 try { 194 language = read(c.getResourceAsStream(XML_LANGUAGE)); 195 } catch (IOException e ) { 196 197 String msg = "Error: load " + 198 XML_LANGUAGE + 199 " file: " + 200 e.getMessage() ; 201 202 StaticDataHelper.log (msg); 203 throw e; 204 } 205 } 206 207 212 public static String read(InputStream is) throws IOException { 213 StringBuffer sb = new StringBuffer (); 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 (buf, 0, nbyte)); 222 } 223 } finally { 224 is.close(); 225 } 226 227 return sb.toString(); 228 } 229 230 233 public void loadDefaultValue() 234 throws Exception { 235 236 String enableContact = null ; 237 String enableCalendar = null ; 238 String 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 msg = "Error: load jad properties wrong login"; 260 StaticDataHelper.log (msg); 261 throw new Exception (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 e){ 289 String msg = "Error: load jad properties wrong gateway-port"; 290 StaticDataHelper.log (msg); 291 throw new Exception (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 |