1 18 19 package sync4j.exchange.xml; 20 21 import java.text.MessageFormat ; 22 import java.text.ParseException ; 23 import java.text.SimpleDateFormat ; 24 25 import java.util.ArrayList ; 26 import java.util.Date ; 27 import java.util.StringTokenizer ; 28 import java.util.TimeZone ; 29 30 37 public class XmlParser { 38 39 41 private static final String ANNIVERSARY_FORMAT_PDI = 42 "yyyy-MM-dd" ; 43 44 private static final String ANNIVERSARY_FORMAT_WEBDAV = 45 "yyyy-MM-dd'T'HH:mm:ss'.000Z'" ; 46 47 private static final String DATE_FORMAT_WEBDAV = 48 "yyyy-MM-dd HH:mm:ss" ; 49 50 private static final String DATE_FORMAT_PDI = 51 "yyyyMMdd'T'HHmmss'Z'" ; 52 53 private static final String DATETIME_FORMAT_CLIENT_UTC = 54 "yyyyMMdd'T'HHmmss'Z'" ; 55 56 private static final String DATETIME_FORMAT_CLIENT_NO_UTC = 57 "yyyyMMdd'T'HHmmss" ; 58 59 private static final String HEADER_REPLID_START = "<rid:" ; 60 61 63 70 public static String [] getXMLTag(String [] xmlInput, String tagContent) 71 throws XmlParseException { 72 73 ArrayList rows = null; 74 75 String xmlInputTag = null ; 76 String startTag = null ; 77 String endTag = null ; 78 79 int i = 0; 80 81 startTag = "<" + tagContent + ">" ; 82 endTag = "</" + tagContent + ">" ; 83 84 rows = new ArrayList (); 85 86 try { 87 88 for (int j=0, l = xmlInput.length; j < l; j++) { 89 90 xmlInputTag = xmlInput[j]; 91 92 while (xmlInputTag.indexOf(startTag) != -1){ 93 rows.add 94 (xmlInputTag.substring(xmlInputTag.indexOf(startTag) + 95 startTag.length(), xmlInputTag.indexOf(endTag))) ; 96 xmlInputTag = xmlInputTag.substring(xmlInputTag.indexOf(endTag) + 97 endTag.length()) ; 98 i++; 99 } 100 101 } 102 103 } catch (java.lang.StringIndexOutOfBoundsException e) { 104 throw new XmlParseException(e.toString() + " : " + tagContent + " TAG"); 105 } 106 107 return (String [])rows.toArray(new String [i]); 108 109 } 110 111 118 public static String getXMLTagValue(String xml, String tag) 119 throws XmlParseException { 120 121 String msg = null ; 122 String startTag = null ; 123 String endTag = null ; 124 125 String voidTag = null ; 126 127 voidTag = "<" + tag + "/>" ; 128 129 startTag = "<" + tag + ">" ; 130 endTag = "</" + tag + ">" ; 131 132 133 if (xml.indexOf(voidTag) != -1 || xml.indexOf(startTag) == -1) { 134 return null; 135 } 136 137 try { 138 msg = xml.substring(xml.indexOf(startTag) + 139 startTag.length() , 140 xml.indexOf(endTag)) ; 141 } catch (java.lang.StringIndexOutOfBoundsException e) { 142 throw new XmlParseException(e.toString() + " : " + tag + " TAG"); 143 } 144 145 return msg; 146 147 } 148 149 156 public static String getXMLInitTagValue(String xml, String tag) 157 throws XmlParseException { 158 159 String msg = null ; 160 String startTag = null ; 161 String endTag = null ; 162 String voidTag = null ; 163 164 int begin = 0; 165 int init = 0; 166 167 voidTag = "<" + tag + "/>" ; 168 startTag = "<" + tag ; 169 endTag = "</" + tag + ">" ; 170 171 if (xml.indexOf(voidTag) != -1 || xml.indexOf(startTag) == -1) { 172 return null; 173 } 174 175 try { 176 177 begin = xml.indexOf(startTag) ; 178 179 init = xml.indexOf(">", begin) + 1 ; 180 181 if (xml.charAt(init) == '<' && xml.charAt(init + 1) == "/".charAt(0)) { 185 return ""; 186 } 187 188 msg = xml.substring(init, xml.indexOf(endTag)) ; 189 190 } catch (java.lang.StringIndexOutOfBoundsException e) { 191 throw new XmlParseException(e.toString() + " : " + tag + " TAG"); 192 } 193 194 return msg; 195 } 196 197 198 205 public static String getHrefFromUri(String uri) 206 throws XmlParseException { 207 208 String href = null; 209 210 try { 211 212 href = uri.substring(uri.lastIndexOf("/") + 1); 213 214 } catch (java.lang.StringIndexOutOfBoundsException e) { 215 throw new XmlParseException(e.toString() + " URI"); 216 } 217 218 return href; 219 220 } 221 222 229 public static String getRuidFromResponse(String response) 230 throws XmlParseException { 231 232 String ruid = null; 233 234 try { 235 236 int start = response.indexOf(HEADER_REPLID_START) + 237 HEADER_REPLID_START.length() ; 238 239 int end = response.indexOf(">", start) ; 240 241 ruid = response.substring(start, end) ; 242 243 } catch (java.lang.StringIndexOutOfBoundsException e) { 244 throw new XmlParseException(e.toString() + " URI"); 245 } 246 247 return ruid; 248 249 } 250 251 252 259 public static String toClientDate (Date date) 260 throws XmlParseException { 261 262 SimpleDateFormat formatter = null ; 263 String value = null ; 264 265 try { 266 267 if (date != null) { 268 269 formatter = new SimpleDateFormat (DATETIME_FORMAT_CLIENT_UTC); 270 271 return formatter.format(date); 272 273 } else { 274 return null; 275 } 276 } catch (Exception e) { 277 throw new XmlParseException(e.toString()); 278 } 279 280 } 281 282 289 public static Date clientDateToDate (String date) 290 throws XmlParseException { 291 292 SimpleDateFormat formatter = null ; 293 Date timestamp = null ; 294 295 String format = null ; 296 297 try { 298 299 if (date != null) { 300 301 if (date.indexOf("Z") != -1) { 302 format = DATETIME_FORMAT_CLIENT_UTC; 303 } else { 304 format = DATETIME_FORMAT_CLIENT_NO_UTC; 308 } 309 310 formatter = new SimpleDateFormat (format); 311 312 timestamp = formatter.parse(date); 313 314 return timestamp; 315 316 } else { 317 return null; 318 } 319 } catch (ParseException e) { 320 throw new XmlParseException(e.toString()); 321 } 322 323 } 324 325 332 public static Date webDavTagToDate (String date) 333 throws XmlParseException { 334 335 SimpleDateFormat formatter = null ; 336 String value = null ; 337 Date timestamp = null ; 338 339 try { 340 341 if (date != null) { 342 value = date.substring(0, 10) + 343 " " + 344 date.substring(11, 19) ; 345 346 formatter = new SimpleDateFormat (DATE_FORMAT_WEBDAV); 347 348 timestamp = formatter.parse(value); 349 } else { 350 timestamp = null; 351 } 352 } catch (ParseException e) { 353 throw new XmlParseException(e.toString()); 354 } 355 356 return timestamp; 357 358 } 359 360 367 public static String webDavTagToPDIDate (String date) 368 throws XmlParseException { 369 370 SimpleDateFormat formatter = null ; 371 372 Date dt = null ; 373 String pdiDate = null ; 374 375 if (date != null && date.length() > 0) { 376 377 dt = webDavTagToDate (date); 378 379 formatter = new SimpleDateFormat (DATE_FORMAT_PDI); 380 381 pdiDate = formatter.format(dt); 382 383 } else { 384 pdiDate = null; 385 } 386 387 return pdiDate; 388 389 } 390 391 392 399 public static Date pdiToDate (String date) 400 throws XmlParseException { 401 402 SimpleDateFormat formatter = null ; 403 Date dt = null ; 404 405 try { 406 407 if (date != null) { 408 409 formatter = new SimpleDateFormat (DATE_FORMAT_PDI); 410 411 dt = formatter.parse(date); 412 } else { 413 dt = null; 414 } 415 } catch (ParseException e) { 416 throw new XmlParseException(e.toString()); 417 } 418 419 return dt; 420 421 } 422 423 430 public static String webDavToPDIAnniversary (String date) 431 throws Exception { 432 433 SimpleDateFormat formatter = null ; 434 435 Date dt = null ; 436 String pdiDate = null ; 437 438 if (date != null && date.length() > 0) { 439 440 formatter = new SimpleDateFormat (ANNIVERSARY_FORMAT_WEBDAV); 441 442 dt = formatter.parse(date); 443 444 formatter = new SimpleDateFormat (ANNIVERSARY_FORMAT_PDI); 445 446 pdiDate = formatter.format(dt); 447 448 } else { 449 pdiDate = null; 450 } 451 452 return pdiDate; 453 454 } 455 456 463 public static String PDIToWebDavAnniversary (String date) 464 throws Exception { 465 466 SimpleDateFormat formatter = null ; 467 468 Date dt = null ; 469 String pdiDate = null ; 470 471 if (date != null && date.length() > 0) { 472 473 formatter = new SimpleDateFormat (ANNIVERSARY_FORMAT_PDI); 474 475 dt = formatter.parse(date); 476 477 formatter = new SimpleDateFormat (ANNIVERSARY_FORMAT_WEBDAV); 478 479 pdiDate = formatter.format(dt); 480 481 } else { 482 pdiDate = null; 483 } 484 485 return pdiDate; 486 487 } 488 489 493 public static String getFieldFromMultipleTag (String value) 494 throws Exception { 495 496 String [] fields = null; 497 String field = null; 498 499 fields = getXMLTag(new String [] {value}, "c:v"); 500 501 502 for (int i = 0, l = fields.length; i < l; i++) { 503 504 if (i == 0) { 505 field = fields[0]; 506 } else { 507 field = field + "; " + fields[i]; 508 } 509 } 510 511 return field; 512 513 } 514 515 519 public static String fieldToMultipleTag (String categories) { 520 521 String fields = "" ; 522 523 StringTokenizer st = new StringTokenizer (categories, ";"); 524 525 while (st.hasMoreTokens()) { 526 fields = fields + "<x:v>" + 527 st.nextToken().trim() + 528 "</x:v>\n" ; 529 } 530 531 return fields; 532 } 533 534 538 public static String getString (String value) { 539 540 if (value == null) { 541 return ""; 542 } else { 543 return value; 544 } 545 546 } 547 548 549 } 550 | Popular Tags |