1 package org.columba.calendar.parser; 19 20 import java.util.Calendar ; 21 import java.util.Enumeration ; 22 import java.util.List ; 23 import java.util.logging.Logger ; 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 38 private static final Logger 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 ("type == null"); 54 55 doc = new Document(); 56 root = new Element(ICALENDAR.ICALENDAR); 57 doc.addContent(root); 58 59 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 ("invalid component specified: " 84 + type); 85 86 String 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 , SyntaxException { 97 if (document == null) 98 throw new IllegalArgumentException ("document == null"); 99 100 this.doc = document; 101 102 this.root = doc.getRootElement(); 103 104 if (!root.getName().equalsIgnoreCase("icalendar")) { 105 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 getComponentType() { 125 return componentElement.getName(); 126 } 127 128 131 public Element getRootElement() { 132 return root; 133 } 134 135 139 protected void set(String key, String 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 152 protected void set(String key, String prefix, String 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 169 protected String get(String 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 182 protected String get(String key, String 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 200 public Document getDocument() { 201 return doc; 202 } 203 204 207 public String getId() { 208 return get(ICALENDAR.UID); 209 } 210 211 public void setId(String id) { 212 set(ICALENDAR.UID, id); 213 } 214 215 218 protected Element getParentElement() { 219 return parentElement; 220 } 221 222 public void setSummary(String s) { 223 set(ICALENDAR.SUMMARY, s); 224 } 225 226 public String getSummary() { 227 return get(ICALENDAR.SUMMARY); 228 } 229 230 public void setDescription(String s) { 231 set(ICALENDAR.DESCRIPTION, s); 232 } 233 234 public String getDescription() { 235 return get(ICALENDAR.DESCRIPTION); 236 } 237 238 public void setCalendar(String s) { 239 set(ICALENDAR.X_COLUMBA_CALENDAR, s); 240 } 241 242 public String getCalendar() { 243 return get(ICALENDAR.X_COLUMBA_CALENDAR); 244 } 245 246 public void setPriority(String s) { 247 set(ICALENDAR.PRIORITY, s); 248 } 249 250 public String getPriority() { 251 return get(ICALENDAR.PRIORITY); 252 } 253 254 public void setEventClass(String s) { 255 set(ICALENDAR.CLASS, s); 256 } 257 258 public String getEventClass() { 259 return get(ICALENDAR.CLASS); 260 } 261 262 public void setLocation(String s) { 263 set(ICALENDAR.LOCATION, s); 264 } 265 266 public String getLocation() { 267 return get(ICALENDAR.LOCATION); 268 } 269 270 public void setDTStart(Calendar c) { 271 set(ICALENDAR.DTSTART, DateParser.createStringFromCalendar(c)); 272 } 273 274 public Calendar getDTStart() { 275 String s = get(ICALENDAR.DTSTART); 276 Calendar c; 277 try { 278 c = DateParser.createCalendarFromString(s); 279 return c; 280 } catch (IllegalArgumentException e) { 281 LOG.severe("date parsing exception"); 282 283 e.printStackTrace(); 284 } 285 286 return Calendar.getInstance(); 287 } 288 289 public void setDTEnd(Calendar c) { 290 set(ICALENDAR.DTEND, DateParser.createStringFromCalendar(c)); 291 } 292 293 public Calendar getDTEnd() { 294 String s = get(ICALENDAR.DTEND); 295 Calendar c; 296 try { 297 c = DateParser.createCalendarFromString(s); 298 return c; 299 } catch (IllegalArgumentException e) { 300 LOG.severe("date parsing exception"); 301 302 e.printStackTrace(); 303 } 304 305 return Calendar.getInstance(); 306 } 307 308 public void setDTStamp(Calendar c) { 309 set(ICALENDAR.DTSTAMP, DateParser.createStringFromCalendar(c)); 310 } 311 312 public Calendar getDTStamp() { 313 String s = get(ICALENDAR.DTSTAMP); 314 315 Calendar c; 316 try { 317 c = DateParser.createCalendarFromString(s); 318 return c; 319 } catch (IllegalArgumentException e) { 320 LOG.severe("date parsing exception"); 321 322 e.printStackTrace(); 323 } 324 325 return Calendar.getInstance(); 326 } 327 328 public void addCategory(String category) { 329 set(ICALENDAR.CATEGORIES, ICALENDAR.ITEM, category); 330 } 331 332 public void removeCategory(String category) { 333 Element child = getParentElement().getChild(ICALENDAR.CATEGORIES); 334 List 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 child.removeContent(e); 340 } 341 } 342 } 343 344 public Enumeration getCategoryEnumeration() { 345 348 return null; 350 } 351 352 public RecurrenceRule getRecurrenceRule() { 353 String rrule = get(ICALENDAR.RRULE); 357 String freqS = getRValue(rrule, "FREQ"); 358 String intervalS = getRValue(rrule, "INTERVAL"); 359 String countS = getRValue(rrule, "COUNT"); 360 String untilS = getRValue(rrule, "UNTIL"); 361 int freq = -1; 362 int interval = -1; 363 int count = -1; 364 Calendar 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 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 e) { 392 LOG.severe("getRecurrenceRule: date parsing exception"); 393 } 394 } catch (NumberFormatException 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 getRValue(String rrule, String property) { 413 String irrule = rrule.toLowerCase(); 415 String iproperty = property.toLowerCase() + "="; 416 String 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 if (r == null) { 431 set(ICALENDAR.RRULE, ""); 432 return; 433 } 434 435 String 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 ("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 rruleString = "FREQ="+freqString; 461 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 rruleString += ";UNTIL="+dateString(r.getUntilDate()); 469 470 set(ICALENDAR.RRULE, rruleString); 471 } 472 473 private String dateString(Calendar d) { 474 String 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 categories) { 486 set(ICALENDAR.CATEGORIES, categories); 487 } 488 489 public String getCategories() { 490 return get(ICALENDAR.CATEGORIES); 491 } 492 493 } 494
| Popular Tags
|