KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > parser > XCSDocumentParser


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.parser;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24
25 import org.columba.calendar.base.UUIDGenerator;
26 import org.columba.calendar.model.Recurrence;
27 import org.columba.calendar.model.api.ICALENDAR;
28 import org.columba.calendar.model.api.IComponent.TYPE;
29 import org.jdom.Document;
30 import org.jdom.Element;
31
32 import com.miginfocom.calendar.activity.recurrence.ByXXXRuleData;
33 import com.miginfocom.calendar.activity.recurrence.RecurrenceRule;
34
35 public class XCSDocumentParser {
36
37     /** JDK 1.4+ logging framework logger, used for logging. */
38     private static final Logger JavaDoc LOG = Logger
39             .getLogger("org.columba.calendar.store");
40
41     private Document doc;
42
43     private Element root;
44
45     protected Element parentElement;
46
47     protected Element vcalendarElement;
48
49     protected Element componentElement;
50
51     public XCSDocumentParser(TYPE type) {
52         if (type == null)
53             throw new IllegalArgumentException JavaDoc("type == null");
54
55         doc = new Document();
56         root = new Element(ICALENDAR.ICALENDAR);
57         doc.addContent(root);
58
59         // TODO fix doctype
60
/*
61          * DocType docType = factory .docType("iCalendar", "-//IETF//DTD
62          * XCAL/iCalendar XML//EN",
63          * "http://www.ietf.org/internet-drafts/draft-hare-xcalendar-01.txt");
64          * doc.setDocType(docType);
65          */

66
67         vcalendarElement = new Element(ICALENDAR.VCALENDAR);
68         root.addContent(vcalendarElement);
69
70         vcalendarElement.setAttribute(ICALENDAR.VCALENDAR_METHOD, "PUBLISH");
71         vcalendarElement.setAttribute(ICALENDAR.VCALENDAR_VERSION, "2.0");
72         vcalendarElement.setAttribute(ICALENDAR.VCALENDAR_PRODID,
73                 "-//Columba Project //NONSGML Columba v1.0//EN");
74
75         if (type == TYPE.EVENT) {
76             componentElement = new Element(ICALENDAR.VEVENT);
77             vcalendarElement.addContent(componentElement);
78
79         } else if (type == TYPE.TODO) {
80             componentElement = new Element(ICALENDAR.VTODO);
81             vcalendarElement.addContent(componentElement);
82         } else
83             throw new IllegalArgumentException JavaDoc("invalid component specified: "
84                     + type);
85
86         String JavaDoc uuid = new UUIDGenerator().newUUID();
87         Element uuidElement = new Element(ICALENDAR.UID);
88         uuidElement.setText(uuid);
89
90         componentElement.addContent(uuidElement);
91
92         parentElement = componentElement;
93     }
94
95     public XCSDocumentParser(Document document)
96             throws IllegalArgumentException JavaDoc, SyntaxException {
97         if (document == null)
98             throw new IllegalArgumentException JavaDoc("document == null");
99
100         this.doc = document;
101
102         this.root = doc.getRootElement();
103
104         if (!root.getName().equalsIgnoreCase("icalendar")) {
105             // wrong xml-format
106
throw new SyntaxException("Root element must be <icalendar>!");
107         }
108
109         vcalendarElement = root.getChild("vcalendar");
110         if (vcalendarElement == null)
111             throw new SyntaxException("element name <vcalendar> expected");
112
113         componentElement = vcalendarElement.getChild(ICALENDAR.VEVENT);
114         if (componentElement == null)
115             componentElement = vcalendarElement.getChild(ICALENDAR.VTODO);
116
117         if (componentElement == null)
118             throw new SyntaxException(
119                     "wrong component type. Must be either <vevent> or <vtodo>.");
120
121         parentElement = componentElement;
122     }
123
124     public String JavaDoc getComponentType() {
125         return componentElement.getName();
126     }
127
128     /**
129      * @see org.columba.calendar.model.api.IBasicDocumentModel#getRootElement()
130      */

131     public Element getRootElement() {
132         return root;
133     }
134
135     /**
136      * @see org.columba.calendar.model.api.IBasicModel#set(java.lang.String,
137      * java.lang.String)
138      */

139     protected void set(String JavaDoc key, String JavaDoc value) {
140         Element child = getParentElement().getChild(key);
141         if (child == null) {
142             child = new Element(key);
143             getParentElement().addContent(child);
144         }
145         child.setText(value);
146     }
147
148     /**
149      * @see org.columba.calendar.model.api.IBasicModel#set(java.lang.String,
150      * java.lang.String, java.lang.String)
151      */

152     protected void set(String JavaDoc key, String JavaDoc prefix, String JavaDoc value) {
153         Element child = getParentElement().getChild(key);
154         if (child == null) {
155             child = new Element(key);
156             getParentElement().addContent(child);
157         }
158         Element prefixchild = child.getChild(prefix);
159         if (prefixchild == null) {
160             prefixchild = new Element(prefix);
161             child.addContent(prefixchild);
162         }
163         prefixchild.setText(value);
164     }
165
166     /**
167      * @see org.columba.calendar.model.api.IBasicModel#get(java.lang.String)
168      */

169     protected String JavaDoc get(String JavaDoc key) {
170         Element child = getParentElement().getChild(key);
171         if (child == null) {
172             child = new Element(key);
173             getParentElement().addContent(child);
174         }
175         return child.getTextNormalize();
176     }
177
178     /**
179      * @see org.columba.calendar.model.api.IBasicModel#get(java.lang.String,
180      * java.lang.String)
181      */

182     protected String JavaDoc get(String JavaDoc key, String JavaDoc prefix) {
183         Element child = getParentElement().getChild(key);
184         if (child == null) {
185             child = new Element(key);
186             getParentElement().addContent(child);
187         }
188         Element prefixchild = child.getChild(prefix);
189         if (prefixchild == null) {
190             prefixchild = new Element(prefix);
191             child.addContent(prefixchild);
192         }
193
194         return prefixchild.getTextNormalize();
195     }
196
197     /**
198      * @see org.columba.calendar.model.api.IBasicModel#getDocument()
199      */

200     public Document getDocument() {
201         return doc;
202     }
203
204     /**
205      * @return Returns the id.
206      */

207     public String JavaDoc getId() {
208         return get(ICALENDAR.UID);
209     }
210
211     public void setId(String JavaDoc id) {
212         set(ICALENDAR.UID, id);
213     }
214
215     /**
216      * @return Returns the parentElement.
217      */

218     protected Element getParentElement() {
219         return parentElement;
220     }
221
222     public void setSummary(String JavaDoc s) {
223         set(ICALENDAR.SUMMARY, s);
224     }
225
226     public String JavaDoc getSummary() {
227         return get(ICALENDAR.SUMMARY);
228     }
229
230     public void setDescription(String JavaDoc s) {
231         set(ICALENDAR.DESCRIPTION, s);
232     }
233
234     public String JavaDoc getDescription() {
235         return get(ICALENDAR.DESCRIPTION);
236     }
237     
238     public void setCalendar(String JavaDoc s) {
239         set(ICALENDAR.X_COLUMBA_CALENDAR, s);
240     }
241
242     public String JavaDoc getCalendar() {
243         return get(ICALENDAR.X_COLUMBA_CALENDAR);
244     }
245
246     public void setPriority(String JavaDoc s) {
247         set(ICALENDAR.PRIORITY, s);
248     }
249
250     public String JavaDoc getPriority() {
251         return get(ICALENDAR.PRIORITY);
252     }
253
254     public void setEventClass(String JavaDoc s) {
255         set(ICALENDAR.CLASS, s);
256     }
257
258     public String JavaDoc getEventClass() {
259         return get(ICALENDAR.CLASS);
260     }
261
262     public void setLocation(String JavaDoc s) {
263         set(ICALENDAR.LOCATION, s);
264     }
265
266     public String JavaDoc getLocation() {
267         return get(ICALENDAR.LOCATION);
268     }
269
270     public void setDTStart(Calendar JavaDoc c) {
271         set(ICALENDAR.DTSTART, DateParser.createStringFromCalendar(c));
272     }
273
274     public Calendar JavaDoc getDTStart() {
275         String JavaDoc s = get(ICALENDAR.DTSTART);
276         Calendar JavaDoc c;
277         try {
278             c = DateParser.createCalendarFromString(s);
279             return c;
280         } catch (IllegalArgumentException JavaDoc e) {
281             LOG.severe("date parsing exception");
282
283             e.printStackTrace();
284         }
285
286         return Calendar.getInstance();
287     }
288
289     public void setDTEnd(Calendar JavaDoc c) {
290         set(ICALENDAR.DTEND, DateParser.createStringFromCalendar(c));
291     }
292
293     public Calendar JavaDoc getDTEnd() {
294         String JavaDoc s = get(ICALENDAR.DTEND);
295         Calendar JavaDoc c;
296         try {
297             c = DateParser.createCalendarFromString(s);
298             return c;
299         } catch (IllegalArgumentException JavaDoc e) {
300             LOG.severe("date parsing exception");
301
302             e.printStackTrace();
303         }
304
305         return Calendar.getInstance();
306     }
307
308     public void setDTStamp(Calendar JavaDoc c) {
309         set(ICALENDAR.DTSTAMP, DateParser.createStringFromCalendar(c));
310     }
311
312     public Calendar JavaDoc getDTStamp() {
313         String JavaDoc s = get(ICALENDAR.DTSTAMP);
314
315         Calendar JavaDoc c;
316         try {
317             c = DateParser.createCalendarFromString(s);
318             return c;
319         } catch (IllegalArgumentException JavaDoc e) {
320             LOG.severe("date parsing exception");
321
322             e.printStackTrace();
323         }
324
325         return Calendar.getInstance();
326     }
327
328     public void addCategory(String JavaDoc category) {
329         set(ICALENDAR.CATEGORIES, ICALENDAR.ITEM, category);
330     }
331
332     public void removeCategory(String JavaDoc category) {
333         Element child = getParentElement().getChild(ICALENDAR.CATEGORIES);
334         List JavaDoc list = child.getChildren();
335         for (int i = 0; i < list.size(); i++) {
336             Element e = (Element) list.get(i);
337             if (e.getText().equals(category)) {
338                 // found category
339
child.removeContent(e);
340             }
341         }
342     }
343     
344     public Enumeration JavaDoc getCategoryEnumeration() {
345         //Element child = getParentElement().getChild(ICALENDAR.CATEGORIES);
346
//List list = child.getChildren();
347

348         // TODO categoryEnumeration
349
return null;
350     }
351     
352     public RecurrenceRule getRecurrenceRule() {
353         // RRULE:FREQ=YEARLY;COUNT=5;INTERVAL=1
354
// RRULE:FREQ=WEEKLY;UNTIL=20060725T215959;INTERVAL=1;BYDAY=TU
355
// RRULE:FREQ=YEARLY;INTERVAL=1
356
String JavaDoc rrule = get(ICALENDAR.RRULE);
357         String JavaDoc freqS = getRValue(rrule, "FREQ");
358         String JavaDoc intervalS = getRValue(rrule, "INTERVAL");
359         String JavaDoc countS = getRValue(rrule, "COUNT");
360         String JavaDoc untilS = getRValue(rrule, "UNTIL");
361         int freq = -1;
362         int interval = -1;
363         int count = -1;
364         Calendar JavaDoc untildate = null;
365         try {
366             if (freqS.equals("YEARLY"))
367                 freq = Recurrence.RECURRENCE_ANNUALLY;
368             else if (freqS.equals("MONTHLY"))
369                 freq = Recurrence.RECURRENCE_MONTHLY;
370             else if (freqS.equals("WEEKLY"))
371                 freq = Recurrence.RECURRENCE_WEEKLY;
372             else if (freqS.equals("DAILY"))
373                 freq = Recurrence.RECURRENCE_DAILY;
374             
375             /*
376             else if (freqS.equals("HOURLY"))
377                 freq = Recurrence.RECURRENCE_HOURLY;
378             else if (freqS.equals("MINUTELY"))
379                 freq = Recurrence.RECURRENCE_HOURLY;
380             else if (freqS.equals("SECONDLY"))
381                 freq = Recurrence.RECURRENCE_SECONDLY;
382             */

383             
384             if (intervalS.length() > 0)
385                 interval = Integer.parseInt(intervalS);
386             if (countS.length() > 0)
387                 count = Integer.parseInt(countS);
388             if (untilS.length() > 0)
389             try {
390                 untildate = DateParser.createCalendarFromDateString(untilS);
391             } catch (IllegalArgumentException JavaDoc e) {
392                 LOG.severe("getRecurrenceRule: date parsing exception");
393             }
394         } catch (NumberFormatException JavaDoc e) {
395             LOG.severe("getRecurrenceRule: number parsing exception");
396         }
397         
398         if (freq < 0)
399             return null;
400         
401         RecurrenceRule r = new RecurrenceRule();
402         r.setFrequency(Recurrence.toFrequency(freq));
403         if (interval > -1)
404             r.setInterval(interval);
405         if (count > -1)
406             r.setRepetitionCount(count);
407         if (untildate != null)
408             r.setUntilDate(untildate);
409         return r;
410     }
411     
412     public static String JavaDoc getRValue(String JavaDoc rrule, String JavaDoc property) {
413         // find the property in the rrule string
414
String JavaDoc irrule = rrule.toLowerCase();
415         String JavaDoc iproperty = property.toLowerCase() + "=";
416         String JavaDoc DELIMITER = ";";
417         if (irrule.indexOf(iproperty) > -1) {
418             int valuestart = irrule.indexOf(iproperty) + iproperty.length();
419             int valueend = irrule.indexOf(DELIMITER, valuestart);
420             if (valueend < 0)
421                 valueend = irrule.length();
422             return rrule.substring(valuestart, valueend);
423         }
424         return "";
425     }
426
427     public void setRecurrenceRule(RecurrenceRule r) {
428         
429         // no recurrency, so nothing in the string
430
if (r == null) {
431             set(ICALENDAR.RRULE, "");
432             return;
433         }
434         
435         // build string
436
String JavaDoc freqString = null;
437         switch (r.getFrequency()) {
438             case Calendar.YEAR: freqString = "YEARLY"; break;
439             case Calendar.MONTH: freqString = "MONTHLY"; break;
440             case Calendar.WEEK_OF_YEAR: freqString = "WEEKLY"; break;
441             case Calendar.DAY_OF_YEAR: freqString = "DAILY"; break;
442             case Calendar.HOUR: freqString = "HOURLY"; break;
443             case Calendar.MINUTE: freqString = "MINUTELY"; break;
444             case Calendar.SECOND: freqString = "SECONDLY";
445         }
446         if (freqString == null)
447             throw new IllegalArgumentException JavaDoc("freqString = null");
448         
449         if (r.getByXXXRules() != null) {
450             for (int i = 0; i < r.getByXXXRules().length; i++) {
451                 ByXXXRuleData rr = r.getByXXXRules()[i];
452                 System.out.println("weekdayiny = " + rr.getRelativeWeekDayInYearOrMonth());
453                 for (int j = 0; j < rr.getValues().length; j++) {
454                     System.out.println(" value " + j + " = " + rr.getValues()[j]);
455                 }
456                 System.out.println("calendarfield = " + rr.getCalendarField());
457             }
458         }
459         
460         String JavaDoc rruleString = "FREQ="+freqString;
461         // optional parts
462
if (r.getRepetitionCount() != null)
463             rruleString += ";COUNT="+r.getRepetitionCount();
464         if (r.getInterval() > 0)
465             rruleString += ";INTERVAL="+r.getInterval();
466         if (r.getUntilDate() != null)
467             //UNTIL=20070627T215959
468
rruleString += ";UNTIL="+dateString(r.getUntilDate());
469         
470         set(ICALENDAR.RRULE, rruleString);
471     }
472
473     private String JavaDoc dateString(Calendar JavaDoc d) {
474         String JavaDoc s = "";
475         s += d.get(Calendar.YEAR);
476         s += (d.get(Calendar.MONTH) < Calendar.OCTOBER ? "0" + (d.get(Calendar.MONTH)+1) : d.get(Calendar.MONTH)+1);
477         s += d.get(Calendar.DAY_OF_MONTH);
478         s += "T";
479         s += (d.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + d.get(Calendar.HOUR_OF_DAY) : d.get(Calendar.HOUR_OF_DAY));
480         s += (d.get(Calendar.MINUTE) < 10 ? "0" + d.get(Calendar.MINUTE) : d.get(Calendar.MINUTE));
481         s += (d.get(Calendar.SECOND) < 10 ? "0" + d.get(Calendar.SECOND) : d.get(Calendar.SECOND));
482         return s;
483     }
484
485     public void setCategories(String JavaDoc categories) {
486         set(ICALENDAR.CATEGORIES, categories);
487     }
488     
489     public String JavaDoc getCategories() {
490         return get(ICALENDAR.CATEGORIES);
491     }
492
493 }
494
Popular Tags