2 Copyright (C) 2003-2004 Max Berger
3 Copyright (C) 2004 OpenGroupware.org
5 This file is part of versitSaxDriver, written for the OpenGroupware.org
8 SOPE is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with SOPE; see the file COPYING. If not, write to the
20 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "VSSaxDriver.h"
25 #include "VSStringFormatter.h"
28 @implementation VSSaxDriver
30 static BOOL debugOn = NO;
32 static NSCharacterSet *dotCharSet = nil;
33 static NSCharacterSet *equalSignCharSet = nil;
34 static NSCharacterSet *commaCharSet = nil;
35 static NSCharacterSet *colonAndSemicolonCharSet = nil;
36 static NSCharacterSet *colonSemicolonAndDquoteCharSet = nil;
37 static NSCharacterSet *whitespaceCharSet = nil;
39 static VSStringFormatter *stringFormatter = nil;
42 static BOOL didInit = NO;
49 ud = [NSUserDefaults standardUserDefaults];
50 debugOn = [ud boolForKey:@"VSSaxDriverDebugEnabled"];
53 [[NSCharacterSet characterSetWithCharactersInString:@"."] retain];
55 [[NSCharacterSet characterSetWithCharactersInString:@"="] retain];
57 [[NSCharacterSet characterSetWithCharactersInString:@","] retain];
58 colonAndSemicolonCharSet =
59 [[NSCharacterSet characterSetWithCharactersInString:@":;"] retain];
60 colonSemicolonAndDquoteCharSet =
61 [[NSCharacterSet characterSetWithCharactersInString:@":;\""] retain];
63 [[NSCharacterSet whitespaceCharacterSet] retain];
65 stringFormatter = [VSStringFormatter sharedFormatter];
70 if ((self = [super init])) {
71 self->prefixURI = @"";
72 self->cardStack = [[NSMutableArray alloc] init];
73 self->elementList = [[NSMutableArray alloc] init];
74 self->attributeMapping = [[NSMutableDictionary alloc] init];
75 self->subItemMapping = [[NSMutableDictionary alloc] init];
81 [self->contentHandler release];
82 [self->prefixURI release];
83 [self->cardStack release];
84 [self->elementList release];
85 [self->attributeElements release];
86 [self->elementMapping release];
87 [self->attributeMapping release];
88 [self->subItemMapping release];
94 - (void)setFeature:(NSString *)_name to:(BOOL)_value {
96 - (BOOL)feature:(NSString *)_name {
100 - (void)setProperty:(NSString *)_name to:(id)_value {
102 - (id)property:(NSString *)_name {
108 - (void)setContentHandler:(id<NSObject,SaxContentHandler>)_handler {
109 ASSIGN(self->contentHandler,_handler);
112 - (void)setDTDHandler:(id<NSObject,SaxDTDHandler>)_handler {
116 - (void)setErrorHandler:(id<NSObject,SaxErrorHandler>)_handler {
119 - (void)setEntityResolver:(id<NSObject,SaxEntityResolver>)_handler {
123 - (id<NSObject,SaxContentHandler>)contentHandler {
124 return self->contentHandler;
127 - (id<NSObject,SaxDTDHandler>)dtdHandler {
132 - (id<NSObject,SaxErrorHandler>)errorHandler {
136 - (id<NSObject,SaxEntityResolver>)entityResolver {
141 - (void)setPrefixURI:(NSString *)_uri {
142 ASSIGNCOPY(self->prefixURI, _uri);
144 - (NSString *)prefixURI {
145 return self->prefixURI;
148 - (void)setAttributeElements:(NSSet *)_elements {
149 ASSIGNCOPY(self->attributeElements, _elements);
151 - (NSSet *)attributeElements {
152 return self->attributeElements;
155 - (void)setElementMapping:(NSDictionary *)_mapping {
156 ASSIGNCOPY(self->elementMapping, _mapping);
158 - (NSDictionary *)elementMapping {
159 return self->elementMapping;
162 - (void)setAttributeMapping:(NSDictionary *)_mapping {
163 [self setAttributeMapping:_mapping forElement:@""];
166 - (void)setAttributeMapping:(NSDictionary *)_mapping
167 forElement:(NSString *)_element
171 [attributeMapping setObject:_mapping forKey:_element];
174 - (void)setSubItemMapping:(NSArray *)_mapping
175 forElement:(NSString *)_element
177 [subItemMapping setObject:_mapping forKey:_element];
184 - (NSString *)_mapTagName:(NSString *)_tagName {
188 if ((ret = [self->elementMapping objectForKey:_tagName]) == nil) {
189 //NSLog(@"Unknown Key: %@ in %@",_tagName,self->elementMapping);
192 /* This is to allow parsing of vCards produced by Apple
193 Addressbook. AFAIK the .dot notation is a non-standard
195 r = [_tagName rangeOfCharacterFromSet:dotCharSet];
197 ret = [self _mapTagName:[_tagName substringFromIndex:(r.location + 1)]];
203 - (void)_addAttribute:(NSString *)_attribute
204 value:(NSString *)_value
205 toAttrs:(SaxAttributes *)_attrs
207 [_attrs addAttribute:_attribute
214 - (void)_addAttribute:(NSString *)_attribute value:(NSString *)_value {
215 NSArray *element = [cardStack lastObject];
216 SaxAttributes *attrs = [element objectAtIndex:2];
217 [self _addAttribute:_attribute value:_value toAttrs:attrs];
220 - (NSString *)_mapAttrName:(NSString *)_attrName forTag:(NSString *)_tagName {
221 NSString *mappedName;
223 mappedName = [(NSDictionary *)[self->attributeMapping objectForKey:_tagName]
224 objectForKey:_attrName];
225 if (mappedName == nil) {
226 mappedName = [(NSDictionary *)[self->attributeMapping objectForKey:
227 [self _mapTagName:_tagName]]
228 objectForKey:_attrName];
230 if (mappedName == nil) {
231 mappedName = [(NSDictionary *)[self->attributeMapping objectForKey:@""]
232 objectForKey:_attrName];
234 if (mappedName == nil)
235 mappedName = _attrName;
240 - (void)_parseAttr:(NSString *)_attr
241 forTag:(NSString *)_tagName
242 intoAttr:(NSString **)attr_
243 intoValue:(NSString **)value_
246 NSString *attrName, *attrValue, *mappedName;
248 r = [_attr rangeOfCharacterFromSet:equalSignCharSet];
250 unsigned left, right;
252 attrName = [[_attr substringToIndex:r.location] uppercaseString];
253 left = NSMaxRange(r);
254 right = [_attr length] - 1;
256 if(([_attr characterAtIndex:left] == '"') &&
257 ([_attr characterAtIndex:right] == '"'))
260 r = NSMakeRange(left, right - left);
261 attrValue = [_attr substringWithRange:r];
264 attrValue = [_attr substringFromIndex:left];
267 else if(left == right) {
268 attrValue = [_attr substringFromIndex:left];
280 // ZNeK: what's this for?
281 r = [attrValue rangeOfCharacterFromSet:commaCharSet];
282 while (r.length > 0) {
283 [attrValue replaceCharactersInRange:r withString:@" "];
284 r = [attrValue rangeOfCharacterFromSet:commaCharSet];
288 mappedName = [self _mapAttrName:attrName forTag:_tagName];
290 *value_ = [stringFormatter stringByUnescapingRFC2445Text:attrValue];
293 - (id<NSObject,SaxAttributes>)_mapAttrs:(NSArray *)_attrs
294 forTag:(NSString *)_tagName
296 SaxAttributes *retAttrs;
297 NSEnumerator *attrEnum;
298 NSString *curAttr, *mappedAttr, *mappedValue, *oldValue;
299 NSMutableDictionary *attributes;
301 if (!_attrs || [_attrs count] == 0)
304 attributes = [[NSMutableDictionary alloc] init];
305 retAttrs = [[[SaxAttributes alloc] init] autorelease];
306 attrEnum = [_attrs objectEnumerator];
307 while ((curAttr = [attrEnum nextObject])) {
308 [self _parseAttr:curAttr
311 intoValue:&mappedValue];
312 if ((oldValue = [attributes objectForKey:mappedAttr])) {
315 val = [NSString stringWithFormat:@"%@ %@",oldValue, mappedValue];
316 [attributes setObject:val forKey:mappedAttr];
319 [attributes setObject:mappedValue forKey:mappedAttr];
322 attrEnum = [attributes keyEnumerator];
323 while ((curAttr = [attrEnum nextObject])) {
324 [self _addAttribute:curAttr
325 value:[attributes objectForKey:curAttr]
329 [attributes release];
334 - (NSArray *)_beginTag:(NSString *)_tagName
335 withAttrs:(id<NSObject,SaxAttributes>)_attrs
337 NSArray *tag = [NSArray arrayWithObjects:@"BEGIN",_tagName,_attrs,NULL];
338 [self->elementList addObject:tag];
342 - (void)_endTag:(NSString *)_tagName {
343 [self->elementList addObject:
344 [NSArray arrayWithObjects:@"END",_tagName,NULL]];
347 - (void)_addSubItems:(NSArray *)_items withData:(NSString *)_content {
348 NSEnumerator *itemEnum, *contentEnum;
350 NSString *subContent;
352 itemEnum = [_items objectEnumerator];
353 contentEnum = [[_content componentsSeparatedByString:@";"] objectEnumerator];
355 while ((subTag=[itemEnum nextObject])) {
356 subContent = [contentEnum nextObject];
358 [self _beginTag:subTag withAttrs:nil];
359 if ([subContent length]>0)
360 [self->elementList addObject:
361 [NSArray arrayWithObjects:@"DATA", subContent, nil]];
362 [self _endTag:subTag];
366 - (void)_dataTag:(NSString *)_tagName
367 withAttrs:(id<NSObject,SaxAttributes>)_attrs
368 andContent:(NSString *)_content
372 _content = [stringFormatter stringByUnescapingRFC2445Text:_content];
373 if ([self->attributeElements containsObject:_tagName]) {
374 [self _addAttribute:_tagName value:_content];
377 [self _beginTag:_tagName withAttrs:_attrs];
378 if ([_content length] > 0) {
379 if ((subItems = [self->subItemMapping objectForKey:_tagName])) {
380 [self _addSubItems:subItems withData:_content];
383 [self->elementList addObject:
384 [NSArray arrayWithObjects:@"DATA", _content, nil]];
387 [self _endTag:_tagName];
391 - (void)_eventsForElements {
397 id<NSObject,SaxAttributes> attrs;
399 enu = [elementList objectEnumerator];
400 while ((obj = [enu nextObject])) {
401 type = [obj objectAtIndex:0];
402 name = [obj objectAtIndex:1];
405 attrs = [obj objectAtIndex:2];
409 if ([type isEqualToString:@"BEGIN"]) {
410 [self->contentHandler startElement:name
411 namespace:self->prefixURI
415 else if ([type isEqualToString:@"END"]) {
416 [self->contentHandler endElement:name
417 namespace:self->prefixURI
421 unsigned len = [name length];
422 chardata = malloc(len * sizeof(unichar));
423 [name getCharacters:chardata range:NSMakeRange(0, len)];
424 [self->contentHandler characters:chardata length:len];
429 [elementList removeAllObjects];
432 - (void)_parseLine:(NSString *)_line {
433 NSString *tagName, *tagValue;
434 NSMutableArray *tagAttributes;
435 NSRange r, todoRange;
438 length = [_line length];
439 todoRange = NSMakeRange(0, length);
440 r = [_line rangeOfCharacterFromSet:colonAndSemicolonCharSet
443 /* is line well-formed? */
446 NSLog(@"%s got an improper content line! ->\n%@",
453 tagName = [[_line substringToIndex:r.location] uppercaseString];
454 tagAttributes = [[NSMutableArray alloc] init];
456 /* possible shortcut: if we spotted a ':', we don't have to do "expensive"
457 argument scanning/processing.
459 if([_line characterAtIndex:r.location] != ':') {
460 BOOL isAtEnd = NO, isInDquote = NO;
461 unsigned start = NSMaxRange(r);
463 todoRange = NSMakeRange(start, length - start);
467 /* scan for parameters */
468 r = [_line rangeOfCharacterFromSet:colonSemicolonAndDquoteCharSet
471 /* is line well-formed? */
472 if(r.length == 0 || r.location == 0) {
474 NSLog(@"%s got an improper content line! ->\n%@",
478 [tagAttributes release];
481 /* first check if delimiter candidate is escaped */
482 if([_line characterAtIndex:(r.location - 1)] != '\\') {
486 delimiter = [_line characterAtIndex:r.location];
487 if(delimiter == '\"') {
488 /* not a real delimiter - toggle isInDquote for proper escaping */
489 isInDquote = !isInDquote;
493 /* is a delimiter, which one? */
495 if(delimiter == ':') {
498 copyRange = NSMakeRange(start, r.location - start);
499 [tagAttributes addObject:[_line substringWithRange:copyRange]];
501 /* adjust start, todoRange */
502 start = NSMaxRange(r);
503 todoRange = NSMakeRange(start, length - start);
509 /* adjust todoRange */
510 unsigned offset = NSMaxRange(r);
511 todoRange = NSMakeRange(offset, length - offset);
515 tagValue = [_line substringFromIndex:NSMaxRange(r)];
517 if ([tagName isEqualToString:@"BEGIN"]) {
520 tag = [self _beginTag:[self _mapTagName:tagValue]
521 withAttrs:[[[SaxAttributes alloc] init] autorelease]];
522 [self->cardStack addObject:tag];
524 else if ([tagName isEqualToString:@"END"]) {
527 mtName = [self _mapTagName:tagValue];
528 if([self->cardStack count] > 0) {
529 NSString *expectedName;
531 expectedName = [[self->cardStack lastObject] objectAtIndex:1];
532 if(![expectedName isEqualToString:mtName]) {
534 NSLog(@"%s found end tag '%@' which doesn't match expected name "
535 @"'%@'! Tag '%@' hasn't been closed properly. Given iCal "
536 @"document contains errors!",
542 /* probably futile attempt to parse anyways */
544 NSLog(@"%s trying to fix previous error by inserting bogus end "
546 __PRETTY_FUNCTION__);
548 [self _endTag:expectedName];
549 [self->cardStack removeLastObject];
554 NSLog(@"%s found end tag '%@' without any open tags left?!",
559 [self _endTag:mtName];
560 [self->cardStack removeLastObject];
561 if ([self->cardStack count] == 0)
562 [self _eventsForElements];
565 [self _dataTag:[self _mapTagName:tagName]
566 withAttrs:[self _mapAttrs:tagAttributes forTag:tagName]
567 andContent:tagValue];
569 [tagAttributes release];
572 - (void)_parseString:(NSString *)_rawString {
573 unsigned pos, length;
574 NSMutableString *line;
577 [self->contentHandler startDocument];
578 [self->contentHandler startPrefixMapping:@"" uri:self->prefixURI];
580 length = [_rawString length];
582 contentline = name *(";" param ) ":" value CRLF
583 ; When parsing a content line, folded lines MUST first
586 r = NSMakeRange(0, 0);
587 /* probably too optimistic */
588 line = [[NSMutableString alloc] initWithCapacity:75 + 2];
590 for(pos = 0; pos < length; pos++) {
591 unichar c = [_rawString characterAtIndex:pos];
594 if(((length - 1) - pos) >= 1) {
595 if([_rawString characterAtIndex:pos + 1] == '\n') {
596 BOOL isAtEndOfLine = YES;
597 /* test for folding first */
598 if(((length - 1) - pos) >= 2) {
599 unichar ws = [_rawString characterAtIndex:pos + 2];
600 isAtEndOfLine = [whitespaceCharSet characterIsMember:ws] ? NO
603 /* assemble part of line up to pos */
605 [line appendString:[_rawString substringWithRange:r]];
609 r = NSMakeRange(pos + 1, 0); /* begin new range */
613 /* assemble part of line up to pos */
615 [line appendString:[_rawString substringWithRange:r]];
617 [self _parseLine:line];
619 [line deleteCharactersInRange:NSMakeRange(0, [line length])];
621 r = NSMakeRange(pos + 1, 0); /* begin new range */
626 /* garbled last line! */
628 NSLog(@"%s Last line is truncated, trying to parse anyways!",
629 __PRETTY_FUNCTION__);
633 else if(c == '\n') { /* broken, non-standard */
634 BOOL isAtEndOfLine = YES;
635 /* test for folding first */
636 if(((length - 1) - pos) >= 1) {
637 unichar ws = [_rawString characterAtIndex:pos + 1];
638 isAtEndOfLine = [whitespaceCharSet characterIsMember:ws] ? NO
641 /* assemble part of line up to pos */
643 [line appendString:[_rawString substringWithRange:r]];
647 r = NSMakeRange(pos + 1, 0); /* begin new range */
651 /* assemble part of line up to pos */
653 [line appendString:[_rawString substringWithRange:r]];
655 [self _parseLine:line];
657 [line deleteCharactersInRange:NSMakeRange(0, [line length])];
658 r = NSMakeRange(pos + 1, 0); /* begin new range */
667 NSLog(@"%s Last line of iCal string is not properly terminated!",
668 __PRETTY_FUNCTION__);
670 [line appendString:[_rawString substringWithRange:r]];
671 [self _parseLine:line];
674 if([self->cardStack count] != 0) {
676 NSLog(@"%s found elements on cardStack. This indicates an improper "
677 @"iCal structure! Not all required events will have been "
678 @"generated, leading to unpredictable results!",
679 __PRETTY_FUNCTION__);
684 [self->contentHandler endPrefixMapping:@""];
685 [self->contentHandler endDocument];
688 - (void)parseFromSource:(id)_source {
690 NSLog(@"%s: parse: %@", __PRETTY_FUNCTION__, _source);
692 if ([_source isKindOfClass:[NSURL class]]) {
694 NSLog(@"%s: trying to load URL...",__PRETTY_FUNCTION__);
695 _source = [_source resourceDataUsingCache:NO];
698 if ([_source isKindOfClass:[NSData class]]) {
699 // FIXME: Data is not always utf-8.....
701 NSLog(@"%s: trying to decode data...",__PRETTY_FUNCTION__);
702 _source = [[[NSString alloc]
703 initWithData:_source encoding:NSUTF8StringEncoding]
707 if ([_source isKindOfClass:[NSString class]]) {
709 NSLog(@"%s: trying to parse string...",__PRETTY_FUNCTION__);
710 [self _parseString:_source];
714 NSLog(@"%s: unrecognizable source: %@", __PRETTY_FUNCTION__,_source);
715 // FIXME: Return Error
719 - (void)parseFromSource:(id)_source systemId:(NSString *)_sysId {
720 [self parseFromSource:_source];
723 - (void)parseFromSystemId:(NSString *)_sysId {
726 if ((url = [NSURL URLWithString:_sysId]))
727 [self parseFromSource:url systemId:_sysId];
732 - (BOOL)isDebuggingEnabled {
736 @end /* VersitSaxDriver */