@@ -14,10 +14,11 @@ #import "OFXMLElementBuilder.h" #import "OFXMLElement.h" #import "OFXMLParser.h" #import "OFMutableArray.h" #import "OFAutoreleasePool.h" +#import "OFExceptions.h" @implementation OFXMLElementBuilder + elementBuilder { return [[[self alloc] init] autorelease]; @@ -97,10 +98,18 @@ - (void)parser: (OFXMLParser*)parser didEndElement: (OFString*)name withPrefix: (OFString*)prefix namespace: (OFString*)ns { + if ([stack count] == 0) { + [delegate elementBuilder: self + didNotExpectCloseTag: name + withPrefix: prefix + namespace: ns]; + return; + } + if ([stack count] == 1) [delegate elementBuilder: self didBuildElement: [stack firstObject]]; [stack removeNObjects: 1]; @@ -108,29 +117,61 @@ - (void)parser: (OFXMLParser*)parser foundCharacters: (OFString*)str { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - [[stack lastObject] - addChild: [OFXMLElement elementWithCharacters: str]]; + OFXMLElement *elem = [OFXMLElement elementWithCharacters: str]; + + if ([stack count] == 0) + [delegate elementBuilder: self + didBuildElement: elem]; + else + [[stack lastObject] addChild: elem]; + [pool release]; } - (void)parser: (OFXMLParser*)parser foundCDATA: (OFString*)cdata { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - [[stack lastObject] addChild: [OFXMLElement elementWithCDATA: cdata]]; + OFXMLElement *elem = [OFXMLElement elementWithCDATA: cdata]; + + if ([stack count] == 0) + [delegate elementBuilder: self + didBuildElement: elem]; + else + [[stack lastObject] addChild: elem]; + [pool release]; } - (void)parser: (OFXMLParser*)parser foundComment: (OFString*)comment { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFXMLElement *last = [stack lastObject]; + OFXMLElement *elem = [OFXMLElement elementWithComment: comment]; - [last addChild: [OFXMLElement elementWithComment: comment]]; + if ([stack count] == 0) + [delegate elementBuilder: self + didBuildElement: elem]; + else + [[stack lastObject] addChild: elem]; [pool release]; } @end + +@implementation OFObject (OFXMLElementBuilderDelegate) +- (void)elementBuilder: (OFXMLElementBuilder*)builder + didBuildElement: (OFXMLElement*)elem +{ +} + +- (void)elementBuilder: (OFXMLElementBuilder*)builder + didNotExpectCloseTag: (OFString*)name + withPrefix: (OFString*)prefix + namespace: (OFString*)ns +{ + @throw [OFMalformedXMLException newWithClass: [builder class]]; +} +@end