KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > blackberry > parser > XMLEventParser


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.blackberry.parser;
20
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.TimeZone JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import javax.microedition.pim.Event;
28 import javax.microedition.pim.EventList;
29 import net.rim.device.api.ui.component.Dialog;
30 import net.rim.device.api.i18n.SimpleDateFormat;
31
32 import sync4j.syncclient.common.StringTools;
33
34 import sync4j.syncclient.util.StaticDataHelper;
35
36 import sync4j.syncclient.util.StaticDataHelper;
37
38 /**
39  * Provides with the user interface
40  * for sync events with SyncServer.
41  *
42  * @author: Fabio Maggi @ Funambol
43  * $Id: XMLEventParser.java,v 1.0
44  */

45
46 public class XMLEventParser implements EventParser{
47
48     //------------------------------------------Private Data Constants
49

50     private static final String JavaDoc SUMMARY = "Subject" ;
51     private static final String JavaDoc LOCATION = "Location" ;
52     private static final String JavaDoc NOTE = "Body" ;
53     private static final String JavaDoc START = "Start" ;
54     private static final String JavaDoc END = "End" ;
55     private static final String JavaDoc REVISION = "Revision" ;
56     private static final String JavaDoc ALARM = "ReminderMinutesBeforeStart" ;
57     private static final String JavaDoc CLASS = "Class" ;
58
59     private static final String JavaDoc START_MARKER = "<appointment>" ;
60     private static final String JavaDoc END_MARKER = "</appointment>" ;
61
62     private static final String JavaDoc DATETIME_FORMAT_UTC
63                                                  = "yyyyMMdd'T'HHmmss'Z'" ;
64
65     private static final String JavaDoc UTC_TIMEZONE = "UTC" ;
66
67     //------------------------------------------Private Data menmbers
68

69     private String JavaDoc[] tagArray = {
70             SUMMARY ,
71             LOCATION ,
72             NOTE ,
73             START ,
74             END ,
75             REVISION ,
76             ALARM ,
77             CLASS
78     };
79
80     private EventList list ;
81     private Event event ;
82     private boolean modify ;
83
84     /**
85      * This is an initialization constructor for XMLEventParser class.
86      *
87      * @param EventList: Blackberry event list
88      * @param Event: Blackberry event
89      * @param boolean: to modify or not to modify
90      * @return XMLEventParser
91      */

92     public XMLEventParser(EventList list, Event event, boolean modify) {
93
94         this.list = list ;
95         this.event = event ;
96         this.modify = modify ;
97     }
98
99     /**
100      * This method is invoked to obtain Event object from a event String.
101      * if the modify flag is set to true it modifies the event.
102      * if the modify flag is set to false it simply adds a event to blackberry calendar.
103      *
104      * @param String:String containing event information to be parsed.
105      * @return Event: that has been added newly/modified in blackberry calendar.
106      */

107     public Event parseEvent(String JavaDoc eventString) {
108
109         if(modify) {
110             return modifyEvent (eventString);
111         } else {
112             return addEvent (eventString);
113         }
114
115     }
116
117     /**
118      * This method is invoked to obtain Event object from a event String,
119      * when a event is added to the blackberry event.This method
120      * adds a new event and returns the reference of that object.
121      *
122      * @param String: String containing event information to be parsed.
123      * @return Event: Event object that has been added newly.
124      */

125     public Event addEvent(String JavaDoc eventString) {
126                 
127         Hashtable JavaDoc eventMap = buildMapFromXML(eventString);
128         if (eventMap != null) {
129             try {
130                                          
131                 String JavaDoc field = getValue(eventMap, SUMMARY);
132                 if (isSupportedField(Event.SUMMARY, field)) {
133                     event.addString(Event.SUMMARY, Event.ATTR_NONE, field);
134                 }
135                              
136                 field = getValue(eventMap, LOCATION);
137                 if (isSupportedField(Event.LOCATION, field)) {
138                     event.addString(Event.LOCATION, Event.ATTR_NONE, field);
139                 }
140                                                 
141                 field = getValue(eventMap, NOTE);
142                 if (isSupportedField(Event.NOTE, field)) {
143                     event.addString(Event.NOTE, Event.ATTR_NONE, field);
144                 }
145                               
146                 field = getValue(eventMap, START);
147                 if (field != null && field.length() > 0) {
148                     long dateStart = getDate(field);
149                     if (isSupportedField(Event.START, field)) {
150                         event.addDate(Event.START, Event.ATTR_NONE, dateStart);
151                     }
152                 }
153                              
154                 field = getValue(eventMap, END);
155                 if (field != null && field.length() > 0) {
156                     long dateEnd = getDate(field);
157                     if (isSupportedField(Event.END, field)) {
158                         event.addDate(Event.END, Event.ATTR_NONE, dateEnd);
159                     }
160                 }
161                              
162                 field = getValue(eventMap, REVISION);
163                 if (field != null && field.length() > 0) {
164                     long dateRevision = getDate(field);
165                     if (isSupportedField(Event.REVISION, field)) {
166                         event.addDate(Event.REVISION, Event.ATTR_NONE, dateRevision);
167                     }
168                 }
169                              
170                 field = getValue(eventMap, ALARM);
171                 if (field != null && field.length() > 0 && isSupportedField(Event.ALARM, field)) {
172                     event.addInt(Event.ALARM, Event.ATTR_NONE, Integer.parseInt(field));
173                 }
174                                            
175                 field = getValue(eventMap, CLASS);
176                 if (isSupportedField(Event.CLASS, field)) {
177                     event.addInt(Event.CLASS, Event.ATTR_NONE, Integer.parseInt(field));
178                 }
179                          
180                 return event;
181
182             } catch (Exception JavaDoc e) {
183                 e.printStackTrace();
184                 return null;
185             }
186         }
187                 
188         return null;
189     }
190
191     /**
192      * This method is invoked to obtain Event object from a event String,
193      * when a event is modified in the blackberry calendar. This method
194      * modifies the object and returns the reference to that object.
195      *
196      * @param String: String containing event information to be parsed.
197      * @return Event:Event that has been modified.
198      */

199     public Event modifyEvent(String JavaDoc eventString) {
200
201         Hashtable JavaDoc eventMap = buildMapFromXML(eventString);
202
203         if (eventMap != null) {
204
205             try {
206                 String JavaDoc field = getValue(eventMap, SUMMARY);
207                 if (isSupportedField(Event.SUMMARY, field) && event.countValues(Event.SUMMARY) > 0) {
208                         event.setString(Event.SUMMARY, 0, Event.ATTR_NONE, field);
209                  } else {
210                         event.removeValue(Event.SUMMARY, 0);
211                         event.addString(Event.SUMMARY, Event.ATTR_NONE, field);
212                 }
213
214                 field = getValue(eventMap, LOCATION);
215                 if (isSupportedField(Event.LOCATION, field) && event.countValues(Event.LOCATION) > 0) {
216                         event.setString(Event.LOCATION, 0, Event.ATTR_NONE, field);
217                 } else {
218                         event.addString(Event.LOCATION, Event.ATTR_NONE, field);
219                 }
220
221                 field = getValue(eventMap, NOTE);
222
223                 if (isSupportedField(Event.NOTE, field) && event.countValues(Event.NOTE) > 0) {
224                         event.setString(Event.NOTE, 0, Event.ATTR_NONE, field);
225                 } else {
226                         event.addString(Event.NOTE, Event.ATTR_NONE, field);
227                 }
228
229                 field = getValue(eventMap, START);
230                 if (field != null && field.length() > 0) {
231                     long dateStart = getDate(field);
232                     event.removeValue(Event.START, 0);
233                     event.addDate(Event.START, Event.ATTR_NONE, dateStart);
234                 }
235
236                 field = getValue(eventMap, END);
237                 if (field != null && field.length() > 0) {
238                     long dateEnd = getDate(field);
239                     event.removeValue(Event.END, 0);
240                     event.addDate(Event.END, Event.ATTR_NONE, dateEnd);
241                 }
242
243                 field = getValue(eventMap, REVISION);
244                 if (field != null && field.length() > 0) {
245                     long dateRevision = getDate(field);
246                     if (isSupportedAtrributedField(Event.END, Event.ATTR_NONE, field)) {
247                             event.removeValue(Event.REVISION, 0);
248                             event.addDate(Event.REVISION, Event.ATTR_NONE, dateRevision);
249                     }
250                 }
251
252                 field = getValue(eventMap, ALARM);
253
254                 if (field != null && field.length() > 0) {
255
256                     int alarmSecondsBefore = getAlarmSecondsBefore(field);
257                     if (isSupportedAtrributedField(Event.ALARM, Event.ATTR_NONE, field) && event.countValues(Event.ALARM) > 0) {
258                             event.setInt(Event.ALARM, 0, Event.ATTR_NONE,alarmSecondsBefore);
259                     } else {
260                             event.addInt(Event.ALARM, Event.ATTR_NONE, alarmSecondsBefore);
261                     }
262
263                 }
264
265                 field = getValue(eventMap, CLASS);
266                 if (isSupportedAtrributedField(Event.CLASS, Event.ATTR_NONE, field)) {
267                         event.removeValue(Event.ALARM, 0);
268                         event.addInt(Event.CLASS, Event.ATTR_NONE, Integer.parseInt(field));
269                 }
270
271                 return event;
272             } catch (Exception JavaDoc e) {
273                 e.printStackTrace();
274                 return null;
275             }
276         }
277         return null;
278     }
279
280     /**
281      * This method appends all the event information passed to it
282      * in the form of event object.
283      *
284      * @param Event: Blackberry event.
285      * @return String: all event information in a string format.
286      */

287     public String JavaDoc toString(Event event) {
288
289         StringBuffer JavaDoc eventBuffer = new StringBuffer JavaDoc();
290
291         eventBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
292
293         eventBuffer.append("<appointment>\n");
294
295         appendField (eventBuffer ,
296                                event ,
297                                Event.SUMMARY ,
298                                SUMMARY );
299
300         appendField (eventBuffer ,
301                                event ,
302                                Event.LOCATION ,
303                                LOCATION );
304
305         appendField (eventBuffer ,
306                                event ,
307                                Event.NOTE ,
308                                NOTE );
309
310         appendFieldDate (eventBuffer ,
311                                event ,
312                                Event.START ,
313                                START );
314
315         appendFieldDate (eventBuffer ,
316                                event ,
317                                Event.END ,
318                                END );
319
320         appendFieldAlarmMB (eventBuffer ,
321                                event ,
322                                Event.ALARM ,
323                                ALARM );
324
325         eventBuffer.append("</appointment>");
326
327         return eventBuffer.toString();
328     }
329
330     //-----------------------------------------Private Methods
331

332     /**
333      * This method appends the event information String Type
334      * supported by blackberry to a
335      * string buffer.
336      *
337      * @param StringBuffer: StringBuffer containing event information.
338      * @param Event: Blackberry event
339      * @param int: type of field
340      * @param String: Name of field.
341      * @return void.
342      */

343     private void appendField(StringBuffer JavaDoc eventBuffer ,
344                              Event event ,
345                              int field ,
346                              String JavaDoc tag ) {
347
348         String JavaDoc value = null;
349
350         if (list.isSupportedField(field)) {
351
352             value = "";
353
354             try {
355                 value = event.getString(field, 0);
356             } catch (Exception JavaDoc e) {
357                 StaticDataHelper.log("Not set field: " + tag);
358             }
359
360             appendToEvent(eventBuffer, tag, value);
361         }
362     }
363
364     /**
365      * This method appends the event information Date Type
366      * supported by blackberry to a
367      * string buffer.
368      *
369      * @param StringBuffer: StringBuffer containing event information.
370      * @param Event: Blackberry event
371      * @param int: type of field
372      * @param String: Name of field.
373      * @return void.
374      */

375     private void appendFieldDate(StringBuffer JavaDoc eventBuffer ,
376                                  Event event ,
377                                  int field ,
378                                  String JavaDoc tag) {
379
380         StaticDataHelper sdh = null ;
381
382         long value = 0 ;
383         Calendar JavaDoc rightNow = null ;
384         String JavaDoc formattedDate = null ;
385         SimpleDateFormat formatter = null ;
386
387         String JavaDoc format = null ;
388
389         long offset = 0 ;
390
391         sdh = new StaticDataHelper();
392
393         if (list.isSupportedField(field)) {
394
395             try {
396                 value = event.getDate(field, 0);
397             } catch (Exception JavaDoc e) {
398                 StaticDataHelper.log("Not set field: " + tag);
399             }
400
401             format = DATETIME_FORMAT_UTC;
402             offset = getTimeZoneOffSet();
403
404             rightNow = Calendar.getInstance();
405
406             rightNow.setTime(new Date JavaDoc(value));
407
408             formatter = new SimpleDateFormat (format);
409             formattedDate = formatter.format
410                                 (new Date JavaDoc(rightNow.getTime().getTime() - offset));
411
412             appendToEvent(eventBuffer, tag, formattedDate);
413         }
414     }
415
416     /**
417      * This method appends the event information int Type
418      * supported by blackberry to a
419      * string buffer.
420      *
421      * @param StringBuffer: StringBuffer containing event information.
422      * @param Event: Blackberry event
423      * @param int: type of field
424      * @param String: Name of field.
425      * @return void.
426      */

427     private void appendFieldInt(StringBuffer JavaDoc eventBuffer ,
428                                 Event event ,
429                                 int field ,
430                                 String JavaDoc tag ) {
431
432         String JavaDoc value = null ;
433
434         if (list.isSupportedField(field)) {
435
436             value = "";
437
438             try {
439                 value = String.valueOf(event.getInt(field, 0));
440             } catch (Exception JavaDoc e) {
441                 StaticDataHelper.log("Not set field: " + tag);
442             }
443
444             appendToEvent(eventBuffer, tag, value);
445         }
446     }
447
448     /**
449      * This method appends the event information int Type supported by blackberry to a
450      * string buffer.
451      *
452      * @param StringBuffer: StringBuffer containing event information.
453      * @param Event: Blackberry event
454      * @param int: type of field
455      * @param String: Name of field.
456      * @return void.
457      */

458     private void appendFieldAlarmMB(StringBuffer JavaDoc eventBuffer ,
459                                     Event event ,
460                                     int field ,
461                                     String JavaDoc tag ) {
462
463         String JavaDoc value = null ;
464
465         if (list.isSupportedField(field)) {
466
467             value = "";
468
469             try {
470                 value = String.valueOf((event.getInt(field, 0))/60);
471             } catch (Exception JavaDoc e) {
472                 StaticDataHelper.log("Not set field: " + tag);
473             }
474
475             appendToEvent(eventBuffer, tag, value);
476         }
477     }
478
479     /**
480      * This method appends the event information supported by blackberry to a
481      * string buffer.
482      *
483      * @param StringBuffer: StringBuffer containing event information.
484      * @param String: name of tag associated with event information.
485      * @param String: value corresponding to tag.
486      * @return void
487      */

488     private void appendToEvent(StringBuffer JavaDoc eventBuffer, String JavaDoc tag, String JavaDoc value) {
489
490         value = (value == null) ? "" : value;
491
492         if (value.length() > 0) {
493             value = StringTools.escapeXml(value);
494         }
495
496         eventBuffer.append("<").append(tag).append(">").append(value);
497
498         eventBuffer.append("</").append(tag).append(">\n");
499
500     }
501
502     /**
503      * This method returns the value associated with a key in a hashtable
504      * containing eventMap.
505      *
506      * @param Hashtable: eventMap
507      * @param String: key to which associated value it to be found.
508      * @return String: Value associated with the key
509      */

510     private String JavaDoc getValue(Hashtable JavaDoc eventMap, String JavaDoc key) {
511         Object JavaDoc val = eventMap.get(key);
512         String JavaDoc value = val == null ? "" : val.toString();
513         return value.trim();
514     }
515
516     /**
517      * This method checks if the field and attibute are supported by the event list
518      * of blackberry address book
519      *
520      * @param int: field
521      * @param int: attribute
522      * @param String: value
523      * @return boolean:returns true if both the field and attribute
524      * associated with it r supported
525      */

526     private boolean isSupportedAtrributedField(int field, int attribute, String JavaDoc value) {
527         return isSupportedField(field, value) && list.isSupportedAttribute(field, attribute);
528     }
529
530     /**
531      * This method checks if the type of field is supported by the event list
532      * of blackberry address book.
533      *
534      * @param int: type
535      * @param String: value
536      * @return boolean:returns true if event list supports this field
537      */

538     private boolean isSupportedField(int type, String JavaDoc value) {
539         return list.isSupportedField(type);
540     }
541
542     /**
543      * This method creates a hashtable with keys and values associated with
544      * blackberry eventlist attributes.
545      *
546      * @param String: eventString
547      * @return Hashtable
548      */

549     private Hashtable JavaDoc buildMapFromXML(String JavaDoc eventString) {
550         int startMarkerIndex = eventString.indexOf(START_MARKER);
551         int endMarkerIndex = eventString.indexOf(END_MARKER);
552       
553         if(startMarkerIndex == -1 || endMarkerIndex == -1) {
554             Dialog.inform("Improper data from the server.");
555             return null;
556         }
557         
558         Hashtable JavaDoc eventMap = new Hashtable JavaDoc();
559         String JavaDoc startTag = "";
560         String JavaDoc endTag = "";
561         String JavaDoc degeneratedTag = "";
562         String JavaDoc data = "";
563
564         for (int i = 0; i < tagArray.length; i++) {
565             String JavaDoc tag = tagArray[i];
566           
567             startTag = "<"+tag+">";
568
569             int startTagIndex = eventString.indexOf(startTag);
570             if(startTagIndex == -1) {
571                 continue;
572             }
573
574             degeneratedTag = "<"+tag+"/>";
575             int degeneratedTagIndex = eventString.indexOf(degeneratedTag);
576             if(degeneratedTagIndex != -1) {
577                 continue;
578             }
579
580             endTag = "</"+tag+">";
581
582             int endTagIndex = eventString.indexOf(endTag);
583             if(endTagIndex != -1) {
584                 data = eventString.substring(startTagIndex+startTag.length(), endTagIndex);
585             } else {
586                 data = "";
587             }
588         
589             if (data.length() > 0) {
590                 data = StringTools.unescapeXml(data);
591             }
592
593             eventMap.put(tag, data);
594         }
595         return eventMap;
596     }
597
598     /**
599      * Get time from date in "yyyyMMddTHHmmssZ" String format
600      * @param field date in "yyyyMMddTHHmmssZ" String format
601      * @return time from date in "yyyyMMddTHHmmssZ" String format
602      */

603     private long getDate(String JavaDoc field) {
604
605         int day = 0 ;
606         int mounth = 0 ;
607         int year = 0 ;
608         int hour = 0 ;
609         int minute = 0 ;
610         int second = 0 ;
611
612         long offset = 0 ;
613
614         Calendar JavaDoc date = null ;
615                
616         offset = getTimeZoneOffSet();
617
618         day = Integer.parseInt (field.substring( 6 , 8 ));
619         mounth = Integer.parseInt (field.substring( 4 , 6 ));
620         year = Integer.parseInt (field.substring( 0 , 4 ));
621         hour = Integer.parseInt (field.substring( 9 , 11 ));
622         minute = Integer.parseInt (field.substring( 11 , 13 ));
623         second = Integer.parseInt (field.substring( 13 , 15 ));
624
625         date = Calendar.getInstance();
626
627         date.set(Calendar.DAY_OF_MONTH , day ) ;
628         date.set(Calendar.MONTH , mounth - 1 ) ;
629         date.set(Calendar.YEAR , year ) ;
630         date.set(Calendar.HOUR_OF_DAY , hour ) ;
631         date.set(Calendar.MINUTE , minute ) ;
632         date.set(Calendar.SECOND , second ) ;
633        
634         return date.getTime().getTime() + offset;
635
636     }
637
638     /**
639      * return seconds from minutes in String format
640      * @param field minutes in String format
641      * @return seconds
642      */

643     private int getAlarmSecondsBefore(String JavaDoc field)
644     throws NumberFormatException JavaDoc {
645         int alarmMinutesBefore = Integer.parseInt(field) * 60;
646         return alarmMinutesBefore;
647     }
648
649     /**
650      * return timezone offser
651      */

652     private long getTimeZoneOffSet() {
653
654         Calendar JavaDoc now = null;
655         TimeZone JavaDoc zn = null;
656         long offset = 0;
657
658         now = Calendar.getInstance();
659
660         now.setTime(new Date JavaDoc());
661
662         zn = TimeZone.getDefault();
663        
664         offset = zn.getOffset(1 ,
665                               now.get(Calendar.YEAR) ,
666                               now.get(Calendar.MONTH) ,
667                               now.get(Calendar.DAY_OF_MONTH) ,
668                               now.get(Calendar.DAY_OF_WEEK) ,
669                                 now.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 +
670                                 now.get(Calendar.MINUTE) * 60 * 1000 +
671                                 now.get(Calendar.SECOND) * 1000 );
672         
673         
674         return offset;
675
676     }
677
678
679 }
680
Popular Tags