Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subclassing support #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions KissXML/Additions/DDXMLElementAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

@implementation DDXMLElement (DDAdditions)

+ (Class)replacementClassForClass:(Class)currentClass {
return currentClass;
}

/**
* Quick method to create an element
**/
+ (DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns
{
DDXMLElement *element = [DDXMLElement elementWithName:name];
Class type = [[self class] replacementClassForClass:[DDXMLElement class]];
DDXMLElement *element = [type elementWithName:name];
[element setXmlns:ns];
return element;
}
Expand Down Expand Up @@ -83,7 +88,8 @@ - (void)setXmlns:(NSString *)ns
//
// This applies to both Apple's NSXML and DDXML.

[self addNamespace:[DDXMLNode namespaceWithName:@"" stringValue:ns]];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[self addNamespace:[type namespaceWithName:@"" stringValue:ns]];
}

/**
Expand All @@ -107,7 +113,8 @@ - (NSString *)compactXMLString
**/
- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string
{
[self addAttribute:[DDXMLNode attributeWithName:name stringValue:string]];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[self addAttribute:[type attributeWithName:name stringValue:string]];
}

/**
Expand Down
13 changes: 10 additions & 3 deletions KissXML/DDXMLDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@

@implementation DDXMLDocument

+ (Class)replacementClassForClass:(Class)currentClass {
return currentClass;
}

/**
* Returns a DDXML wrapper object for the given primitive node.
* The given node MUST be non-NULL and of the proper type.
**/
+ (id)nodeWithDocPrimitive:(xmlDocPtr)doc owner:(DDXMLNode *)owner
{
return [[[DDXMLDocument alloc] initWithDocPrimitive:doc owner:owner] autorelease];
Class type = [[self class] replacementClassForClass:[DDXMLDocument class]];
return [[[type alloc] initWithDocPrimitive:doc owner:owner] autorelease];
}

- (id)initWithDocPrimitive:(xmlDocPtr)doc owner:(DDXMLNode *)inOwner
Expand Down Expand Up @@ -116,8 +121,10 @@ - (DDXMLElement *)rootElement

xmlNodePtr rootNode = xmlDocGetRootElement(doc);

if (rootNode != NULL)
return [DDXMLElement nodeWithElementPrimitive:rootNode owner:self];
if (rootNode != NULL){
Class type = [[self class] replacementClassForClass:[DDXMLElement class]];
return [type nodeWithElementPrimitive:rootNode owner:self];
}
else
return nil;
}
Expand Down
40 changes: 27 additions & 13 deletions KissXML/DDXMLElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@

@implementation DDXMLElement

+ (Class)replacementClassForClass:(Class)currentClass {
return currentClass;
}

/**
* Returns a DDXML wrapper object for the given primitive node.
* The given node MUST be non-NULL and of the proper type.
**/
+ (id)nodeWithElementPrimitive:(xmlNodePtr)node owner:(DDXMLNode *)owner
{
return [[[DDXMLElement alloc] initWithElementPrimitive:node owner:owner] autorelease];
Class type = [[self class] replacementClassForClass:[DDXMLElement class]];
return [[[type alloc] initWithElementPrimitive:node owner:owner] autorelease];
}

- (id)initWithElementPrimitive:(xmlNodePtr)node owner:(DDXMLNode *)inOwner
Expand Down Expand Up @@ -103,7 +108,8 @@ - (id)initWithName:(NSString *)name stringValue:(NSString *)string

- (id)initWithXMLString:(NSString *)string error:(NSError **)error
{
DDXMLDocument *doc = [[DDXMLDocument alloc] initWithXMLString:string options:0 error:error];
Class type = [[self class] replacementClassForClass:[DDXMLDocument class]];
DDXMLDocument *doc = [[type alloc] initWithXMLString:string options:0 error:error];
if (doc == nil)
{
[self release];
Expand Down Expand Up @@ -177,7 +183,8 @@ - (NSArray *)_elementsForName:(NSString *)name

if (match)
{
[result addObject:[DDXMLElement nodeWithElementPrimitive:child owner:self]];
Class type = [[self class] replacementClassForClass:[DDXMLElement class]];
[result addObject:[type nodeWithElementPrimitive:child owner:self]];
}
}

Expand Down Expand Up @@ -210,7 +217,8 @@ - (NSArray *)elementsForName:(NSString *)name
NSString *prefix;
NSString *localName;

[DDXMLNode getPrefix:&prefix localName:&localName forName:name];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type getPrefix:&prefix localName:&localName forName:name];

if ([prefix length] > 0)
{
Expand Down Expand Up @@ -253,7 +261,8 @@ - (NSArray *)elementsForLocalName:(NSString *)localName URI:(NSString *)uri
NSString *prefix;
NSString *realLocalName;

[DDXMLNode getPrefix:&prefix localName:&realLocalName forName:localName];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type getPrefix:&prefix localName:&realLocalName forName:localName];

return [self _elementsForName:localName localName:realLocalName prefix:prefix uri:uri];
}
Expand Down Expand Up @@ -297,7 +306,8 @@ - (void)_removeAttributeForName:(NSString *)name
{
if (xmlStrEqual(attr->name, xmlName))
{
[DDXMLNode removeAttribute:attr];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type removeAttribute:attr];
return;
}
attr = attr->next;
Expand Down Expand Up @@ -407,8 +417,8 @@ - (void)setAttributes:(NSArray *)attributes
#if DDXML_DEBUG_MEMORY_ISSUES
DDXMLNotZombieAssert();
#endif

[DDXMLNode removeAllAttributesFromNode:(xmlNodePtr)genericPtr];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type removeAllAttributesFromNode:(xmlNodePtr)genericPtr];

NSUInteger i;
for (i = 0; i < [attributes count]; i++)
Expand Down Expand Up @@ -436,7 +446,8 @@ - (void)_removeNamespaceForPrefix:(NSString *)name
{
if (xmlStrEqual(ns->prefix, xmlName))
{
[DDXMLNode removeNamespace:ns fromNode:node];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type removeNamespace:ns fromNode:node];
break;
}
ns = ns->next;
Expand Down Expand Up @@ -577,7 +588,8 @@ - (void)setNamespaces:(NSArray *)namespaces
DDXMLNotZombieAssert();
#endif

[DDXMLNode removeAllNamespacesFromNode:(xmlNodePtr)genericPtr];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type removeAllNamespacesFromNode:(xmlNodePtr)genericPtr];

NSUInteger i;
for (i = 0; i < [namespaces count]; i++)
Expand Down Expand Up @@ -778,7 +790,8 @@ - (void)removeChildAtIndex:(NSUInteger)index
{
if (i == index)
{
[DDXMLNode removeChild:child];
Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type removeChild:child];
return;
}

Expand All @@ -793,8 +806,9 @@ - (void)setChildren:(NSArray *)children
#if DDXML_DEBUG_MEMORY_ISSUES
DDXMLNotZombieAssert();
#endif

[DDXMLNode removeAllChildrenFromNode:(xmlNodePtr)genericPtr];

Class type = [[self class] replacementClassForClass:[DDXMLNode class]];
[type removeAllChildrenFromNode:(xmlNodePtr)genericPtr];

NSUInteger i;
for (i = 0; i < [children count]; i++)
Expand Down
Loading