Index: src/OFString+XMLUnescaping.h ================================================================== --- src/OFString+XMLUnescaping.h +++ src/OFString+XMLUnescaping.h @@ -25,11 +25,12 @@ * exception will be thrown. * * \param entity The name of the entity that is unknown * \return A substitution for the entity or nil */ -- (OFString*)didFindUnknownEntityNamed: (OFString*)entity; +- (OFString*)string: (OFString*)str + containsUnknownEntityNamed: (OFString*)entity; @end /** * \brief A category for unescaping XML in strings. */ @@ -43,8 +44,8 @@ * Unescapes XML in the string and uses the specified handler for unknown * entities. * * \param h An OFXMLUnescapingDelegate as a handler for unknown entities */ -- (OFString*)stringByXMLUnescapingWithHandler: - (OFObject *)h; +- (OFString*)stringByXMLUnescapingWithDelegate: + (OFObject *)delegate; @end Index: src/OFString+XMLUnescaping.m ================================================================== --- src/OFString+XMLUnescaping.m +++ src/OFString+XMLUnescaping.m @@ -69,15 +69,15 @@ } @implementation OFString (XMLUnescaping) - (OFString*)stringByXMLUnescaping { - return [self stringByXMLUnescapingWithHandler: nil]; + return [self stringByXMLUnescapingWithDelegate: nil]; } -- (OFString*)stringByXMLUnescapingWithHandler: - (OFObject *)h +- (OFString*)stringByXMLUnescapingWithDelegate: + (OFObject *)delegate { size_t i, last; BOOL in_entity; OFMutableString *ret; @@ -130,19 +130,20 @@ @throw [OFInvalidEncodingException newWithClass: isa]; [ret appendString: tmp]; [pool release]; - } else if (h != nil) { + } else if (delegate != nil) { OFAutoreleasePool *pool; OFString *n, *tmp; pool = [[OFAutoreleasePool alloc] init]; n = [OFString stringWithCString: entity length: len]; - tmp = [h didFindUnknownEntityNamed: n]; + tmp = [delegate string: self + containsUnknownEntityNamed: n]; if (tmp == nil) @throw [OFInvalidEncodingException newWithClass: isa]; Index: src/OFXMLParser.h ================================================================== --- src/OFXMLParser.h +++ src/OFXMLParser.h @@ -28,49 +28,58 @@ * \param name The name of the tag which just started * \param prefix The prefix of the tag which just started or nil * \param ns The namespace of the tag which just started or nil * \param attrs The attributes included in the tag which just started or nil */ -- (void)xmlParser: (OFXMLParser*)parser - didStartTagWithName: (OFString*)name - prefix: (OFString*)prefix - namespace: (OFString*)ns - attributes: (OFArray*)attrs; +- (void)parser: (OFXMLParser*)parser + didStartElement: (OFString*)name + withPrefix: (OFString*)prefix + namespace: (OFString*)ns + attributes: (OFArray*)attrs; /** * This callback is called when the XML parser found the end of a tag. * * \param parser The parser which found the end of a tag * \param name The name of the tag which just ended * \param prefix The prefix of the tag which just ended or nil * \param ns The namespace of the tag which just ended or nil */ -- (void)xmlParser: (OFXMLParser*)parser - didEndTagWithName: (OFString*)name - prefix: (OFString*)prefix - namespace: (OFString*)ns; +- (void)parser: (OFXMLParser*)parser + didEndElement: (OFString*)name + withPrefix: (OFString*)prefix + namespace: (OFString*)ns; /** - * This callback is called when the XML parser found a string. + * This callback is called when the XML parser found characters. * * In case there are comments or CDATA, it is possible that this callback is * called multiple times in a row. * * \param parser The parser which found a string * \param string The string the XML parser found */ -- (void)xmlParser: (OFXMLParser*)parser - didFindString: (OFString*)string; +- (void)parser: (OFXMLParser*)parser + foundCharacters: (OFString*)string; + +/** + * This callback is called when the XML parser found CDATA. + * + * \param parser The parser which found a string + * \param string The string the XML parser found + */ +- (void)parser: (OFXMLParser*)parser + foundCDATA: (OFString*)cdata; /** * This callback is called when the XML parser found a comment. * * \param parser The parser which found a comment * \param comment The comment the XML parser found */ -- (void)xmlParser: (OFXMLParser*)parser - didFindComment: (OFString*)comment; +- (void)parser: (OFXMLParser*)parser + foundComment: (OFString*)comment; /** * This callback is called when the XML parser found an entity it doesn't know. * The callback is supposed to return a substitution for the entity or nil if * it is not known to the callback as well, in which case an exception will be @@ -78,12 +87,12 @@ * * \param parser The parser which found an unknown entity * \param entity The name of the entity the XML parser didn't know * \return A substitution for the entity or nil */ -- (OFString*)xmlParser: (OFXMLParser*)parser - didFindUnknownEntityNamed: (OFString*)entity; +- (OFString*)parser: (OFXMLParser*)parser + foundUnknownEntityNamed: (OFString*)entity; @end /** * \brief An event-based XML parser. * Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -23,17 +23,17 @@ #import "OFExceptions.h" #import "macros.h" static OF_INLINE OFString* transform_string(OFMutableString *cache, - OFObject *handler) + OFObject *delegate) { [cache replaceOccurrencesOfString: @"\r\n" withString: @"\n"]; [cache replaceOccurrencesOfString: @"\r" withString: @"\n"]; - return [cache stringByXMLUnescapingWithHandler: handler]; + return [cache stringByXMLUnescapingWithDelegate: delegate]; } static OF_INLINE OFString* namespace_for_prefix(OFString *prefix, OFArray *namespaces) { @@ -136,12 +136,12 @@ if ([cache cStringLength] > 0) { OFString *str; pool = [[OFAutoreleasePool alloc] init]; str = transform_string(cache, self); - [delegate xmlParser: self - didFindString: str]; + [delegate parser: self + foundCharacters: str]; [pool release]; } [cache setToCString: ""]; @@ -204,21 +204,21 @@ @throw [OFUnboundNamespaceException newWithClass: isa prefix: prefix]; - [delegate xmlParser: self - didStartTagWithName: name - prefix: prefix - namespace: ns - attributes: nil]; + [delegate parser: self + didStartElement: name + withPrefix: prefix + namespace: ns + attributes: nil]; if (buf[i] == '/') - [delegate xmlParser: self - didEndTagWithName: name - prefix: prefix - namespace: ns]; + [delegate parser: self + didEndElement: name + withPrefix: prefix + namespace: ns]; else [previous addObject: [[cache copy] autorelease]]; [pool release]; @@ -289,14 +289,14 @@ @throw [OFUnboundNamespaceException newWithClass: isa prefix: prefix]; [namespaces removeNObjects: 1]; - [delegate xmlParser: self - didEndTagWithName: name - prefix: prefix - namespace: ns]; + [delegate parser: self + didEndElement: name + withPrefix: prefix + namespace: ns]; [pool release]; [name release]; [prefix release]; @@ -320,21 +320,21 @@ if (prefix != nil && ns == nil) @throw [OFUnboundNamespaceException newWithClass: isa prefix: prefix]; - [delegate xmlParser: self - didStartTagWithName: name - prefix: prefix - namespace: ns - attributes: attrs]; + [delegate parser: self + didStartElement: name + withPrefix: prefix + namespace: ns + attributes: attrs]; if (buf[i] == '/') { - [delegate xmlParser: self - didEndTagWithName: name - prefix: prefix - namespace: ns]; + [delegate parser: self + didEndElement: name + withPrefix: prefix + namespace: ns]; [namespaces removeNObjects: 1]; } else if (prefix != nil) { OFString *str = [OFString stringWithFormat: @"%s:%s", [prefix cString], @@ -566,12 +566,12 @@ cdata = [[cache mutableCopy] autorelease]; len = [cdata length]; [cdata removeCharactersFromIndex: len - 2 toIndex: len]; - [delegate xmlParser: self - didFindString: cdata]; + [delegate parser: self + foundCDATA: cdata]; [pool release]; [cache setToCString: ""]; last = i + 1; @@ -609,12 +609,12 @@ comment = [[cache mutableCopy] autorelease]; len = [comment length]; [comment removeCharactersFromIndex: len - 2 toIndex: len]; - [delegate xmlParser: self - didFindComment: comment]; + [delegate parser: self + foundComment: comment]; [pool release]; [cache setToCString: ""]; last = i + 1; @@ -632,44 +632,50 @@ if (len > 0 && state != OF_XMLPARSER_IN_TAG) [cache appendCStringWithoutUTF8Checking: buf + last length: len]; } -- (OFString*)didFindUnknownEntityNamed: (OFString*)entity +- (OFString*)string: (OFString*)string + containsUnknownEntityNamed: (OFString*)entity { - return [delegate xmlParser: self - didFindUnknownEntityNamed: entity]; + return [delegate parser: self + foundUnknownEntityNamed: entity]; } @end @implementation OFObject (OFXMLParserDelegate) -- (void)xmlParser: (OFXMLParser*)parser - didStartTagWithName: (OFString*)name - prefix: (OFString*)prefix - namespace: (OFString*)ns - attributes: (OFArray*)attrs -{ -} - -- (void)xmlParser: (OFXMLParser*)parser - didEndTagWithName: (OFString*)name - prefix: (OFString*)prefix - namespace: (OFString*)ns -{ -} - -- (void)xmlParser: (OFXMLParser*)parser - didFindString: (OFString*)string -{ -} - -- (void)xmlParser: (OFXMLParser*)parser - didFindComment: (OFString*)comment -{ -} - -- (OFString*)xmlParser: (OFXMLParser*)parser - didFindUnknownEntityNamed: (OFString*)entity +- (void)parser: (OFXMLParser*)parser + didStartElement: (OFString*)name + withPrefix: (OFString*)prefix + namespace: (OFString*)ns + attributes: (OFArray*)attrs +{ +} + +- (void)parser: (OFXMLParser*)parser + didEndElement: (OFString*)name + withPrefix: (OFString*)prefix + namespace: (OFString*)ns +{ +} + +- (void)parser: (OFXMLParser*)parser + foundCharacters: (OFString*)string +{ +} + +- (void)parser: (OFXMLParser*)parser + foundCDATA: (OFString*)cdata +{ +} + +- (void)parser: (OFXMLParser*)parser + foundComment: (OFString*)comment +{ +} + +- (OFString*)parser: (OFXMLParser*)parser + foundUnknownEntityNamed: (OFString*)entity { return nil; } @end Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -26,11 +26,12 @@ @interface EntityHandler: OFObject @end @implementation EntityHandler -- (OFString*)didFindUnknownEntityNamed: (OFString*)entity +- (OFString*)string: (OFString*)string + containsUnknownEntityNamed: (OFString*)entity { if ([entity isEqual: @"foo"]) return @"bar"; return nil; @@ -355,11 +356,11 @@ EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " @"#6", OFInvalidEncodingException, [@"&#xg;" stringByXMLUnescaping]) TEST(@"-[stringByXMLUnescapingWithHandler:]", (h = [[[EntityHandler alloc] init] autorelease]) && - [[@"x&foo;y" stringByXMLUnescapingWithHandler: h] + [[@"x&foo;y" stringByXMLUnescapingWithDelegate: h] isEqual: @"xbary"]) [pool drain]; } @end Index: tests/OFXMLParserTests.m ================================================================== --- tests/OFXMLParserTests.m +++ tests/OFXMLParserTests.m @@ -27,21 +27,22 @@ enum event_type { TAG_START, TAG_END, STRING, + CDATA, COMMENT }; @implementation TestsAppDelegate (OFXMLParser) -- (void)xmlParserCallbackWithEventType: (enum event_type)et - name: (OFString*)name - prefix: (OFString*)prefix - namespace: (OFString*)ns - attributes: (OFArray*)attrs - string: (OFString*)string - comment: (OFString*)comment +- (void)parserCallbackWithEventType: (enum event_type)et + name: (OFString*)name + prefix: (OFString*)prefix + namespace: (OFString*)ns + attributes: (OFArray*)attrs + string: (OFString*)string + comment: (OFString*)comment { OFString *msg; i++; msg = [OFString stringWithFormat: @"Parsing part #%d", i]; @@ -53,11 +54,11 @@ break; case 2: TEST(msg, et == STRING && [string isEqual: @"\n "]) break; case 3: - TEST(msg, et == STRING && [string isEqual: @"f