KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > xml > XmlParser


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.exchange.xml;
20
21 import java.text.MessageFormat JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.TimeZone JavaDoc;
29
30 /**
31  * This class provides commons methods
32  * to parser XML msg
33  *
34  * @author Fabio Maggi @ Funambol
35  * @version $Id: XmlParser.java,v 1.19 2005/06/24 09:31:23 fabius Exp $
36  **/

37 public class XmlParser {
38
39     //---------------------------------------------------------------- Constants
40

41     private static final String JavaDoc ANNIVERSARY_FORMAT_PDI =
42             "yyyy-MM-dd" ;
43
44     private static final String JavaDoc ANNIVERSARY_FORMAT_WEBDAV =
45             "yyyy-MM-dd'T'HH:mm:ss'.000Z'" ;
46
47     private static final String JavaDoc DATE_FORMAT_WEBDAV =
48             "yyyy-MM-dd HH:mm:ss" ;
49
50     private static final String JavaDoc DATE_FORMAT_PDI =
51             "yyyyMMdd'T'HHmmss'Z'" ;
52
53     private static final String JavaDoc DATETIME_FORMAT_CLIENT_UTC =
54              "yyyyMMdd'T'HHmmss'Z'" ;
55
56     private static final String JavaDoc DATETIME_FORMAT_CLIENT_NO_UTC =
57              "yyyyMMdd'T'HHmmss" ;
58
59     private static final String JavaDoc HEADER_REPLID_START = "<rid:" ;
60
61     //---------------------------------------------------------------- Public methods
62

63     /**
64      * Make a String[] by tags find with search.
65      *
66      * @param xmlInput tags about search
67      * @param tagContent tag to find
68      * @return find tags
69      **/

70     public static String JavaDoc[] getXMLTag(String JavaDoc[] xmlInput, String JavaDoc tagContent)
71     throws XmlParseException {
72
73         ArrayList JavaDoc rows = null;
74
75         String JavaDoc xmlInputTag = null ;
76         String JavaDoc startTag = null ;
77         String JavaDoc endTag = null ;
78
79         int i = 0;
80
81         startTag = "<" + tagContent + ">" ;
82         endTag = "</" + tagContent + ">" ;
83
84         rows = new ArrayList JavaDoc();
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 JavaDoc e) {
104             throw new XmlParseException(e.toString() + " : " + tagContent + " TAG");
105         }
106
107         return (String JavaDoc[])rows.toArray(new String JavaDoc[i]);
108
109     }
110
111     /**
112      * Make a String by value of <i>tag</i>.
113      *
114      * @param xml xml msg
115      * @param tag tag to find
116      * @return tag value
117      **/

118     public static String JavaDoc getXMLTagValue(String JavaDoc xml, String JavaDoc tag)
119     throws XmlParseException {
120
121         String JavaDoc msg = null ;
122         String JavaDoc startTag = null ;
123         String JavaDoc endTag = null ;
124
125         String JavaDoc 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 JavaDoc e) {
142             throw new XmlParseException(e.toString() + " : " + tag + " TAG");
143         }
144
145         return msg;
146
147     }
148
149     /**
150      * Make a String by value of <i>tag</i>.
151      *
152      * @param xml xml msg
153      * @param tag tag to find
154      * @return tag value
155      **/

156     public static String JavaDoc getXMLInitTagValue(String JavaDoc xml, String JavaDoc tag)
157     throws XmlParseException {
158
159         String JavaDoc msg = null ;
160         String JavaDoc startTag = null ;
161         String JavaDoc endTag = null ;
162         String JavaDoc 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             //
182
// tag void
183
//
184
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 JavaDoc e) {
191             throw new XmlParseException(e.toString() + " : " + tag + " TAG");
192         }
193
194         return msg;
195     }
196
197
198    /**
199     * Make a href (reource name) from uri webdavtag.
200     *
201     * @param uri
202     * @return id
203     * @throws XmlParseException
204     **/

205    public static String JavaDoc getHrefFromUri(String JavaDoc uri)
206     throws XmlParseException {
207
208         String JavaDoc href = null;
209
210         try {
211
212             href = uri.substring(uri.lastIndexOf("/") + 1);
213
214         } catch (java.lang.StringIndexOutOfBoundsException JavaDoc e) {
215             throw new XmlParseException(e.toString() + " URI");
216         }
217
218         return href;
219
220     }
221
222     /**
223      * Make a ruid from a response header.
224      *
225      * @param response
226      * @return ruid
227      * @throws XmlParseException
228      **/

229     public static String JavaDoc getRuidFromResponse(String JavaDoc response)
230     throws XmlParseException {
231
232         String JavaDoc 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 JavaDoc e) {
244             throw new XmlParseException(e.toString() + " URI");
245         }
246
247         return ruid;
248
249     }
250
251
252     /**
253      * Make a date from a webdav tag date to client date
254      *
255      * @param date webdav tag content
256      * @return a Date
257      * @throws XmlParseException
258      **/

259     public static String JavaDoc toClientDate (Date JavaDoc date)
260     throws XmlParseException {
261
262         SimpleDateFormat JavaDoc formatter = null ;
263         String JavaDoc value = null ;
264
265         try {
266
267             if (date != null) {
268
269                 formatter = new SimpleDateFormat JavaDoc(DATETIME_FORMAT_CLIENT_UTC);
270
271                 return formatter.format(date);
272
273             } else {
274                 return null;
275             }
276         } catch (Exception JavaDoc e) {
277             throw new XmlParseException(e.toString());
278         }
279
280     }
281
282     /**
283      * Make a date from a client date to webdav date
284      *
285      * @param date client date
286      * @return a Date
287      * @throws XmlParseException
288      **/

289     public static Date JavaDoc clientDateToDate (String JavaDoc date)
290     throws XmlParseException {
291
292         SimpleDateFormat JavaDoc formatter = null ;
293         Date JavaDoc timestamp = null ;
294
295         String JavaDoc 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                     //
305
// for nokia phone utc bug
306
//
307
format = DATETIME_FORMAT_CLIENT_NO_UTC;
308                 }
309
310                 formatter = new SimpleDateFormat JavaDoc(format);
311
312                 timestamp = formatter.parse(date);
313
314                 return timestamp;
315
316             } else {
317                 return null;
318             }
319         } catch (ParseException JavaDoc e) {
320             throw new XmlParseException(e.toString());
321         }
322
323     }
324
325     /**
326      * Make a date from a webdav tag date
327      *
328      * @param date webdav tag content
329      * @return a Date
330      * @throws XmlParseException
331      **/

332     public static Date JavaDoc webDavTagToDate (String JavaDoc date)
333     throws XmlParseException {
334
335         SimpleDateFormat JavaDoc formatter = null ;
336         String JavaDoc value = null ;
337         Date JavaDoc 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 JavaDoc(DATE_FORMAT_WEBDAV);
347
348                 timestamp = formatter.parse(value);
349             } else {
350                 timestamp = null;
351             }
352         } catch (ParseException JavaDoc e) {
353             throw new XmlParseException(e.toString());
354         }
355
356         return timestamp;
357
358     }
359
360     /**
361      * Make a date from a webdav tag date
362      *
363      * @param date webdav tag content
364      * @return a Date
365      * @throws XmlParseException
366      **/

367     public static String JavaDoc webDavTagToPDIDate (String JavaDoc date)
368     throws XmlParseException {
369
370         SimpleDateFormat JavaDoc formatter = null ;
371
372         Date JavaDoc dt = null ;
373         String JavaDoc pdiDate = null ;
374
375         if (date != null && date.length() > 0) {
376
377             dt = webDavTagToDate (date);
378
379             formatter = new SimpleDateFormat JavaDoc(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     /**
393      * Make a date from a webdav tag date
394      *
395      * @param date webdav tag content
396      * @return a Date
397      * @throws XmlParseException
398      **/

399     public static Date JavaDoc pdiToDate (String JavaDoc date)
400     throws XmlParseException {
401
402         SimpleDateFormat JavaDoc formatter = null ;
403         Date JavaDoc dt = null ;
404
405         try {
406
407             if (date != null) {
408
409                 formatter = new SimpleDateFormat JavaDoc(DATE_FORMAT_PDI);
410
411                 dt = formatter.parse(date);
412             } else {
413                 dt = null;
414             }
415         } catch (ParseException JavaDoc e) {
416             throw new XmlParseException(e.toString());
417         }
418
419         return dt;
420
421     }
422
423     /**
424      * Make a date from a webdav tag date
425      *
426      * @param date webdav tag content
427      * @return a Date
428      * @throws XmlParseException
429      **/

430     public static String JavaDoc webDavToPDIAnniversary (String JavaDoc date)
431     throws Exception JavaDoc {
432
433         SimpleDateFormat JavaDoc formatter = null ;
434
435         Date JavaDoc dt = null ;
436         String JavaDoc pdiDate = null ;
437
438         if (date != null && date.length() > 0) {
439
440             formatter = new SimpleDateFormat JavaDoc(ANNIVERSARY_FORMAT_WEBDAV);
441
442             dt = formatter.parse(date);
443
444             formatter = new SimpleDateFormat JavaDoc(ANNIVERSARY_FORMAT_PDI);
445
446             pdiDate = formatter.format(dt);
447
448         } else {
449             pdiDate = null;
450         }
451
452         return pdiDate;
453
454     }
455
456     /**
457      * Make a date from a webdav tag date
458      *
459      * @param date webdav tag content
460      * @return a Date
461      * @throws XmlParseException
462      **/

463     public static String JavaDoc PDIToWebDavAnniversary (String JavaDoc date)
464     throws Exception JavaDoc {
465
466         SimpleDateFormat JavaDoc formatter = null ;
467
468         Date JavaDoc dt = null ;
469         String JavaDoc pdiDate = null ;
470
471         if (date != null && date.length() > 0) {
472
473             formatter = new SimpleDateFormat JavaDoc(ANNIVERSARY_FORMAT_PDI);
474
475             dt = formatter.parse(date);
476
477             formatter = new SimpleDateFormat JavaDoc(ANNIVERSARY_FORMAT_WEBDAV);
478
479             pdiDate = formatter.format(dt);
480
481         } else {
482             pdiDate = null;
483         }
484
485         return pdiDate;
486
487     }
488
489     /**
490      * @param value
491      * @return <p>value<p> if value is not null, else ""
492      **/

493     public static String JavaDoc getFieldFromMultipleTag (String JavaDoc value)
494     throws Exception JavaDoc {
495
496         String JavaDoc [] fields = null;
497         String JavaDoc field = null;
498
499         fields = getXMLTag(new String JavaDoc[] {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     /**
516      * @param value
517      * @return <p>value<p> if value is not null, else ""
518      **/

519     public static String JavaDoc fieldToMultipleTag (String JavaDoc categories) {
520
521         String JavaDoc fields = "" ;
522
523         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(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     /**
535      * @param value
536      * @return <p>value<p> if value is not null, else ""
537      **/

538     public static String JavaDoc getString (String JavaDoc value) {
539
540         if (value == null) {
541             return "";
542         } else {
543             return value;
544         }
545
546     }
547
548
549 }
550
Popular Tags