Index: src/OFXMLElement.h ================================================================== --- src/OFXMLElement.h +++ src/OFXMLElement.h @@ -105,10 +105,18 @@ * \param comment The comment the element represents * \return A new autoreleased OFXMLElement consisting of the specified comment */ + elementWithComment: (OFString*)comment; +/** + * Parses the string and returns an OFXMLElement for it. + * + * \param str The string to parse + * \return A new autoreleased OFXMLElement with the contents of the string + */ ++ elementWithString: (OFString*)str; + /** * Initializes an already allocated OFXMLElement with the specified element * name. * * \param name The name for the element @@ -179,10 +187,18 @@ * \param comment The comment the element represents * \return An initialized OFXMLElement consisting of the specified comment */ - initWithComment: (OFString*)comment; +/** + * Parses the string and initializes an already allocated OFXMLElement with it. + * + * \param str The string to parse + * \return An initialized OFXMLElement with the contents of the string + */ +- initWithString: (OFString*)str; + /** * \return The name of the element */ - (OFString*)name; Index: src/OFXMLElement.m ================================================================== --- src/OFXMLElement.m +++ src/OFXMLElement.m @@ -22,15 +22,39 @@ #import "OFXMLElement.h" #import "OFString.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFXMLAttribute.h" +#import "OFXMLParser.h" +#import "OFXMLElementBuilder.h" #import "OFAutoreleasePool.h" #import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" #import "OFUnboundNamespaceException.h" + +@interface OFXMLElement_OFXMLElementBuilderDelegate: OFObject +{ +@public + OFXMLElement *element; +} +@end + +@implementation OFXMLElement_OFXMLElementBuilderDelegate +- (void)elementBuilder: (OFXMLElementBuilder*)builder + didBuildElement: (OFXMLElement*)elem +{ + element = [elem retain]; +} + +- (void)dealloc +{ + [element release]; + + [super dealloc]; +} +@end @implementation OFXMLElement + elementWithName: (OFString*)name_ { return [[[self alloc] initWithName: name_] autorelease]; @@ -71,10 +95,15 @@ + elementWithComment: (OFString*)comment { return [[[self alloc] initWithComment: comment] autorelease]; } + ++ elementWithString: (OFString*)str +{ + return [[[self alloc] initWithString: str] autorelease]; +} - init { Class c = isa; [self release]; @@ -168,10 +197,43 @@ { self = [super init]; @try { comment = [comment_ copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithString: (OFString*)str +{ + OFAutoreleasePool *pool; + OFXMLParser *parser; + OFXMLElementBuilder *builder; + OFXMLElement_OFXMLElementBuilderDelegate *delegate; + + [self release]; + + pool = [[OFAutoreleasePool alloc] init]; + + parser = [OFXMLParser parser]; + builder = [OFXMLElementBuilder elementBuilder]; + delegate = [[[OFXMLElement_OFXMLElementBuilderDelegate alloc] init] + autorelease]; + + [parser setDelegate: builder]; + [builder setDelegate: delegate]; + + [parser parseString: str]; + + self = [delegate->element retain]; + + @try { + [pool release]; } @catch (id e) { [self release]; @throw e; }