/*
- Copyright (C) 2000-2006 SKYRIX Software AG
- Copyright (C) 2006 Helge Hess
+ Copyright (C) 2000-2007 SKYRIX Software AG
+ Copyright (C) 2007 Helge Hess
This file is part of SOPE.
return YES;
}
-- (NSString *)stringForResourceType:(id)_value ofProperty:(NSString *)_prop
+- (NSString *)stringForValue:(id)_value ofProperty:(NSString *)_prop
prefixes:(NSDictionary *)_prefixes
+ requireTagValue:(BOOL)_requireTagValue
{
NSString *davNS;
- davNS = [_prefixes objectForKey:@"DAV:"];
+ davNS = [_prefixes objectForKey:XMLNS_WEBDAV];
if ([_value isKindOfClass:[NSArray class]]) {
/*
<collection/>
<C:todos xmlns:C="urn:ietf:params:xml:ns:caldav"/>
Item Format:
- ( TAG ) => tag in DAV: namespace
- ( TAG, NS ) => tag in NS namespace
- ( TAG, NS, PREFIX ) => tag in NS namespace with PREFIX
+ ( TAG ) => tag in DAV: namespace
+ ( TAG, NS ) => tag in NS namespace
+ ( TAG, NS, PREFIX ) => tag in NS namespace with PREFIX
+ ( TAG, NS, PREFIX, val ) => tag in NS namespace with PREFIX and value
*/
NSMutableString *ms;
NSEnumerator *e;
id item;
- if ([_value count] == 0)
+ if (![_value isNotEmpty])
return nil;
-
+
ms = [NSMutableString stringWithCapacity:16];
e = [_value objectEnumerator];
while ((item = [e nextObject]) != nil) {
+ NSString *tag, *ns, *pre;
unsigned count;
if (![item isKindOfClass:[NSArray class]]) {
item = [item stringValue];
- if ([item length] == 0) continue;
+ if (![item isNotEmpty]) continue;
[ms appendFormat:@"<%@:%@ />", davNS, item];
continue;
}
if ((count = [item count]) == 0)
continue;
-
- if (count == 1)
- [ms appendFormat:@"<%@:%@ />", davNS, [item objectAtIndex:0]];
- else if (count == 2) {
- /* 0=tag, 1=nsuri */
- [ms appendFormat:@"<%@ xmlns=\"%@\" />",
- [item objectAtIndex:0], [item objectAtIndex:1]];
+
+ tag = [[item objectAtIndex:0] stringValue];
+ ns = (count > 1) ? [[item objectAtIndex:1] stringValue] : nil;
+ pre = (count > 2) ? [[item objectAtIndex:2] stringValue] : davNS;
+
+ [ms appendString:@"<"];
+ if (count != 2) {
+ [ms appendString:pre];
+ [ms appendString:@":"];
+ }
+ [ms appendString:tag];
+
+ if (count == 2) {
+ [ms appendString:@" xmlns=\""];
+ [ms appendString:ns];
+ [ms appendString:@"\""];
+ }
+ else if (count > 2) {
+ [ms appendString:@" xmlns:"];
+ [ms appendString:pre];
+ [ms appendString:@"=\""];
+ [ms appendString:ns];
+ [ms appendString:@"\""];
+ }
+
+ if (count > 3) {
+ id value = [item objectAtIndex:3];
+ [ms appendString:@">"];
+
+ if ([value isNotEmpty]) {
+ /* nested tag */
+ [ms appendString:[self stringForValue:value ofProperty:_prop
+ prefixes:_prefixes requireTagValue:NO]];
+ }
+
+ [ms appendString:@"</"];
+ if (count != 2) {
+ [ms appendString:pre];
+ [ms appendString:@":"];
+ }
+ [ms appendString:tag];
+ [ms appendString:@">"];
}
else {
- /* 0=tag, 1=nsuri, 2=nsprefix */
- [ms appendFormat:@"<%@:%@ xmlns:%@=\"%@\" />",
- [item objectAtIndex:2], [item objectAtIndex:0],
- [item objectAtIndex:2], [item objectAtIndex:1]];
+ /* no value, close tag */
+ [ms appendString:@" />"];
}
}
return ms;
}
_value = [_value stringValue];
- if ([_value length] == 0) return nil;
+ if (![_value isNotEmpty]) return nil;
+
+ if (_requireTagValue) {
+ /*
+ This is for properties like 'resourcetype'. davResourceType just
+ returns 'collection' but gets rendered as '<D:collection/>'
+ */
+ return [NSString stringWithFormat:@"<%@:%@/>", davNS, _value];
+ }
- return [NSString stringWithFormat:@"<%@:%@/>", davNS, _value];
+ return _value;
}
- (NSString *)stringForValue:(id)_value ofProperty:(NSString *)_prop
prefixes:(NSDictionary *)_prefixes
/* special processing for some properties */
if ([_prop isEqualToString:@"{DAV:}resourcetype"]) {
- return [self stringForResourceType:_value ofProperty:_prop
- prefixes:_prefixes];
+ return [self stringForValue:_value ofProperty:_prop
+ prefixes:_prefixes requireTagValue:YES];
}
- else if ([_prop isEqualToString:@"{DAV:}creationdate"])
+
+ if ([_prop isEqualToString:@"{DAV:}creationdate"])
datefmt = @"%Y-%m-%dT%H:%M:%S%zZ";
/* special processing for some properties */
locale:nil];
}
+ if ([_value isKindOfClass:[NSArray class]]) {
+ return [self stringForValue:_value ofProperty:_prop
+ prefixes:_prefixes requireTagValue:NO];
+ }
+
return [[_value stringValue] stringByEscapingXMLString];
}