Permalink
Browse files

KissXML now recycles existing DDXML wrapper nodes. This means faster …

…operation if your code accesses the same element over and over again.
  • Loading branch information...
1 parent b54de0e commit 63cb57b37f3fee5f7a493a6a4cc717b6fc4b7e79 @robbiehanson committed May 18, 2009
Showing with 680 additions and 391 deletions.
  1. +27 −4 DDXMLDocument.m
  2. +50 −20 DDXMLElement.m
  3. +156 −147 DDXMLNode.m
  4. +8 −9 DDXMLPrivate.h
  5. +7 −2 DDXMLTesting.m
  6. +73 −48 KissXML.xcodeproj/robbie.mode1v3
  7. +359 −161 KissXML.xcodeproj/robbie.pbxuser
View
@@ -7,18 +7,41 @@ @implementation DDXMLDocument
+ (id)nodeWithPrimitive:(xmlKindPtr)nodePtr
{
- return [[[DDXMLDocument alloc] initWithPrimitive:nodePtr] autorelease];
+ if(nodePtr == NULL || nodePtr->type != XML_DOCUMENT_NODE)
+ {
+ return nil;
+ }
+
+ xmlDocPtr doc = (xmlDocPtr)nodePtr;
+ if(doc->_private == NULL)
+ return [[[DDXMLDocument alloc] initWithCheckedPrimitive:nodePtr] autorelease];
+ else
+ return [[((DDXMLDocument *)(doc->_private)) retain] autorelease];
}
-- (id)initWithPrimitive:(xmlKindPtr)nodePtr
+- (id)initWithUncheckedPrimitive:(xmlKindPtr)nodePtr
{
if(nodePtr == NULL || nodePtr->type != XML_DOCUMENT_NODE)
{
[self release];
return nil;
}
- self = [super initWithPrimitive:nodePtr];
+ xmlDocPtr doc = (xmlDocPtr)nodePtr;
+ if(doc->_private == NULL)
+ {
+ return [self initWithCheckedPrimitive:nodePtr];
+ }
+ else
+ {
+ [self release];
+ return [((DDXMLDocument *)(doc->_private)) retain];
+ }
+}
+
+- (id)initWithCheckedPrimitive:(xmlKindPtr)nodePtr
+{
+ self = [super initWithCheckedPrimitive:nodePtr];
return self;
}
@@ -65,7 +88,7 @@ - (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)err
return nil;
}
- return [self initWithPrimitive:(xmlKindPtr)doc];
+ return [self initWithCheckedPrimitive:(xmlKindPtr)doc];
}
/**
View
@@ -10,17 +10,27 @@ - (id)initWithName:(NSString *)name
// Note: Make every guarantee that genericPtr is not null
xmlNodePtr node = xmlNewNode(NULL, [name xmlChar]);
+ if(node == NULL)
+ {
+ [self release];
+ return nil;
+ }
- return [self initWithPrimitive:(xmlKindPtr)node];
+ return [self initWithCheckedPrimitive:(xmlKindPtr)node];
}
- (id)initWithName:(NSString *)name URI:(NSString *)URI
{
// Note: Make every guarantee that genericPtr is not null
xmlNodePtr node = xmlNewNode(NULL, [name xmlChar]);
+ if(node == NULL)
+ {
+ [self release];
+ return nil;
+ }
- id result = [self initWithPrimitive:(xmlKindPtr)node];
+ DDXMLElement *result = [self initWithCheckedPrimitive:(xmlKindPtr)node];
[result setURI:URI];
return result;
@@ -31,8 +41,13 @@ - (id)initWithName:(NSString *)name stringValue:(NSString *)string
// Note: Make every guarantee that genericPtr is not null
xmlNodePtr node = xmlNewNode(NULL, [name xmlChar]);
+ if(node == NULL)
+ {
+ [self release];
+ return nil;
+ }
- id result = [self initWithPrimitive:(xmlKindPtr)node];
+ DDXMLElement *result = [self initWithCheckedPrimitive:(xmlKindPtr)node];
[result setStringValue:string];
return result;
@@ -41,33 +56,57 @@ - (id)initWithName:(NSString *)name stringValue:(NSString *)string
- (id)initWithXMLString:(NSString *)string error:(NSError **)error
{
DDXMLDocument *doc = [[DDXMLDocument alloc] initWithXMLString:string options:0 error:error];
-
if(doc == nil)
{
+ [self release];
return nil;
}
- DDXMLNode *result = [doc rootElement];
+ DDXMLElement *result = [doc rootElement];
[result detach];
[doc release];
+ [self release];
return [result retain];
}
+ (id)nodeWithPrimitive:(xmlKindPtr)nodePtr
{
- return [[[DDXMLElement alloc] initWithPrimitive:nodePtr] autorelease];
+ if(nodePtr == NULL || nodePtr->type != XML_ELEMENT_NODE)
+ {
+ return nil;
+ }
+
+ xmlNodePtr node = (xmlNodePtr)nodePtr;
+ if(node->_private == NULL)
+ return [[[DDXMLElement alloc] initWithCheckedPrimitive:nodePtr] autorelease];
+ else
+ return [[((DDXMLElement *)(node->_private)) retain] autorelease];
}
-- (id)initWithPrimitive:(xmlKindPtr)nodePtr
+- (id)initWithUncheckedPrimitive:(xmlKindPtr)nodePtr
{
if(nodePtr == NULL || nodePtr->type != XML_ELEMENT_NODE)
{
[self release];
return nil;
}
- self = [super initWithPrimitive:nodePtr];
+ xmlNodePtr node = (xmlNodePtr)nodePtr;
+ if(node->_private == NULL)
+ {
+ return [self initWithCheckedPrimitive:nodePtr];
+ }
+ else
+ {
+ [self release];
+ return [((DDXMLElement *)(node->_private)) retain];
+ }
+}
+
+- (id)initWithCheckedPrimitive:(xmlKindPtr)nodePtr
+{
+ self = [super initWithCheckedPrimitive:nodePtr];
return self;
}
@@ -174,10 +213,7 @@ - (NSArray *)elementsWithName:(NSString *)name uri:(NSString *)uri
if(match)
{
- DDXMLElement *childElement = [[DDXMLElement alloc] initWithPrimitive:(xmlKindPtr)child];
-
- [result addObject:childElement];
- [childElement release];
+ [result addObject:[DDXMLElement nodeWithPrimitive:(xmlKindPtr)child]];
}
}
@@ -241,10 +277,7 @@ - (NSArray *)attributes
xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties;
while(attr != NULL)
{
- DDXMLNode *attrNode = [[DDXMLNode alloc] initWithPrimitive:(xmlKindPtr)attr];
-
- [result addObject:attrNode];
- [attrNode release];
+ [result addObject:[DDXMLNode nodeWithPrimitive:(xmlKindPtr)attr]];
attr = attr->next;
}
@@ -362,10 +395,7 @@ - (NSArray *)namespaces
xmlNsPtr ns = ((xmlNodePtr)genericPtr)->nsDef;
while(ns != NULL)
{
- DDXMLNode *nsNode = [[DDXMLNode alloc] initWithPrimitive:(xmlKindPtr)ns nsParent:(xmlNodePtr)genericPtr];
-
- [result addObject:nsNode];
- [nsNode release];
+ [result addObject:[DDXMLNode nodeWithPrimitive:(xmlKindPtr)ns nsParent:(xmlNodePtr)genericPtr]];
ns = ns->next;
}
Oops, something went wrong.

0 comments on commit 63cb57b

Please sign in to comment.