Permalink
Browse files

Bug fix for xml documents with DTDs

  • Loading branch information...
1 parent 6bfb4f5 commit d46d915f859ce1e894e94d5b14d3735a6cbbdbd3 @robbiehanson committed Feb 16, 2009
Showing with 55 additions and 16 deletions.
  1. +11 −7 DDXMLDocument.m
  2. +1 −1 DDXMLElement.m
  3. +42 −7 DDXMLNode.m
  4. +1 −1 DDXMLPrivate.h
View
@@ -14,7 +14,7 @@ - (id)initWithPrimitive:(xmlKindPtr)nodePtr
{
if(nodePtr == NULL || nodePtr->type != XML_DOCUMENT_NODE)
{
- [super dealloc];
+ [self release];
return nil;
}
@@ -45,7 +45,7 @@ - (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)err
{
if(error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:0 userInfo:nil];
- [super dealloc];
+ [self release];
return nil;
}
@@ -54,7 +54,7 @@ - (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)err
{
if(error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:1 userInfo:nil];
- [super dealloc];
+ [self release];
return nil;
}
@@ -66,12 +66,16 @@ - (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)err
**/
- (DDXMLElement *)rootElement
{
- xmlDocPtr docPtr = (xmlDocPtr)genericPtr;
+ xmlDocPtr doc = (xmlDocPtr)genericPtr;
- if(docPtr->children == NULL)
- return nil;
+ // doc->children is a list containing possibly comments, DTDs, etc...
+
+ xmlNodePtr rootNode = xmlDocGetRootElement(doc);
+
+ if(rootNode != NULL)
+ return [DDXMLElement nodeWithPrimitive:(xmlKindPtr)(rootNode)];
else
- return [DDXMLElement nodeWithPrimitive:(xmlKindPtr)(docPtr->children)];
+ return nil;
}
- (NSData *)XMLData
View
@@ -47,7 +47,7 @@ - (id)initWithPrimitive:(xmlKindPtr)nodePtr
{
if(nodePtr == NULL || nodePtr->type != XML_ELEMENT_NODE)
{
- [super dealloc];
+ [self release];
return nil;
}
View
@@ -115,7 +115,7 @@ - (id)initWithPrimitive:(xmlKindPtr)nodePtr
{
if(nodePtr == NULL)
{
- [super dealloc];
+ [self release];
return nil;
}
@@ -152,7 +152,7 @@ - (id)initWithPrimitive:(xmlKindPtr)nodePtr nsParent:(xmlNodePtr)parentPtr
{
if(nodePtr == NULL)
{
- [super dealloc];
+ [self release];
return nil;
}
@@ -183,7 +183,7 @@ - (id)initWithPrimitive:(xmlKindPtr)nodePtr nsParent:(xmlNodePtr)parentPtr
- (void)dealloc
{
// Check if genericPtr is NULL
- // This may be the case if, eg, DDXMLElement calls [super dealloc] from it's init method
+ // This may be the case if, eg, DDXMLElement calls [self release] from it's init method
if(genericPtr != NULL)
{
[self nodeRelease];
@@ -1615,10 +1615,45 @@ + (void)removeAllChildrenFromNode:(xmlNodePtr)node
/**
* Removes the root element from the given document.
**/
-+ (void)removeRootElementFromDoc:(xmlDocPtr)doc
++ (void)removeAllChildrenFromDoc:(xmlDocPtr)doc
{
- // It's safe to cast a xmlDocPtr to a xmlNodePtr in this particular case
- [self removeAllChildrenFromNode:(xmlNodePtr)doc];
+ xmlNodePtr child = doc->children;
+
+ while(child != NULL)
+ {
+ xmlNodePtr nextChild = child->next;
+
+ if(child->type == XML_ELEMENT_NODE)
+ {
+ // Remove child from list of children
+ if(child->prev != NULL)
+ {
+ child->prev->next = child->next;
+ }
+ if(child->next != NULL)
+ {
+ child->next->prev = child->prev;
+ }
+ if(doc->children == child)
+ {
+ doc->children = child->next;
+ }
+ if(doc->last == child)
+ {
+ doc->last = child->prev;
+ }
+
+ // Free the child recursively if it's no longer in use
+ [self nodeFree:child];
+ }
+ else
+ {
+ // Leave comments and DTD's embedded in the doc's child list.
+ // They will get freed in xmlFreeDoc.
+ }
+
+ child = nextChild;
+ }
}
/**
@@ -1734,7 +1769,7 @@ - (void)nodeRelease
}
else if([self isXmlDocPtr])
{
- [[self class] removeRootElementFromDoc:(xmlDocPtr)genericPtr];
+ [[self class] removeAllChildrenFromDoc:(xmlDocPtr)genericPtr];
xmlFreeDoc((xmlDocPtr)genericPtr);
}
else
View
@@ -53,7 +53,7 @@ typedef struct _xmlRetain *xmlRetainPtr;
+ (void)removeChild:(xmlNodePtr)child fromNode:(xmlNodePtr)node;
+ (void)removeAllChildrenFromNode:(xmlNodePtr)node;
-+ (void)removeRootElementFromDoc:(xmlDocPtr)doc;
++ (void)removeAllChildrenFromDoc:(xmlDocPtr)doc;
- (void)nodeRetain;
- (void)nodeRelease;

0 comments on commit d46d915

Please sign in to comment.