2009-02-09 16:16:11 +00:00
|
|
|
/* Implementation for NSXMLNode for GNUStep
|
|
|
|
Copyright (C) 2008 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Created: September 2008
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
2011-10-01 18:43:29 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
|
|
|
|
2012-01-04 09:20:32 +00:00
|
|
|
#define GSInternal NSXMLNodeInternal
|
2012-01-05 20:40:12 +00:00
|
|
|
#import "NSXMLPrivate.h"
|
|
|
|
#import "GSInternal.h"
|
2012-01-04 09:20:32 +00:00
|
|
|
GS_PRIVATE_INTERNAL(NSXMLNode)
|
2011-10-01 18:43:29 +00:00
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
#if defined(HAVE_LIBXML)
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static int
|
|
|
|
countAttributes(xmlNodePtr node)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
xmlAttrPtr attr = node->properties;
|
|
|
|
|
|
|
|
while (attr)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
attr = attr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static BOOL
|
|
|
|
isEqualAttr(const xmlAttrPtr attrA, const xmlAttrPtr attrB)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
xmlChar *contentA;
|
|
|
|
xmlChar *contentB;
|
|
|
|
const xmlChar *nameA;
|
|
|
|
const xmlChar *nameB;
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
/* what has to be the same for two attributes to be equal --
|
|
|
|
* just their values??
|
|
|
|
*/
|
2012-02-21 08:21:12 +00:00
|
|
|
if (attrB == attrA)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2012-02-21 08:21:12 +00:00
|
|
|
if (attrA == NULL || attrB == NULL)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2012-02-21 08:21:12 +00:00
|
|
|
nameA = attrA->name;
|
|
|
|
nameB = attrB->name;
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-02-21 08:21:12 +00:00
|
|
|
if (xmlStrcmp(nameA, nameB) == 0)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-21 08:21:12 +00:00
|
|
|
// get the content...
|
|
|
|
contentA = xmlNodeGetContent((const xmlNodePtr)attrA);
|
|
|
|
contentB = xmlNodeGetContent((const xmlNodePtr)attrB);
|
|
|
|
|
|
|
|
if (xmlStrcmp(contentA, contentB) == 0)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-21 08:21:12 +00:00
|
|
|
xmlFree(contentA);
|
|
|
|
xmlFree(contentB);
|
2012-02-20 03:40:15 +00:00
|
|
|
return YES;
|
|
|
|
}
|
2012-02-21 08:21:12 +00:00
|
|
|
xmlFree(contentA);
|
|
|
|
xmlFree(contentB);
|
2012-02-20 03:40:15 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static xmlAttrPtr
|
|
|
|
findAttrWithName(xmlNodePtr node, const xmlChar* targetName)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
xmlAttrPtr attr = node->properties;
|
|
|
|
|
|
|
|
// find an attr in node with the given name, and return it, else NULL
|
2012-02-21 08:21:12 +00:00
|
|
|
while ((attr != NULL) && xmlStrcmp(attr->name, targetName) != 0)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
attr = attr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static BOOL
|
|
|
|
isEqualAttributes(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
xmlAttrPtr attrA = NULL;
|
|
|
|
|
|
|
|
if (countAttributes(nodeA) != countAttributes(nodeB))
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
attrA = nodeA->properties;
|
2012-02-21 08:21:12 +00:00
|
|
|
while (attrA)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-21 08:21:12 +00:00
|
|
|
xmlAttrPtr attrB = findAttrWithName(nodeB, attrA->name);
|
2012-02-20 03:40:15 +00:00
|
|
|
if (!isEqualAttr(attrA, attrB))
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
attrA = attrA->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static BOOL
|
|
|
|
isEqualNode(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
if (nodeA == nodeB)
|
|
|
|
return YES;
|
|
|
|
|
|
|
|
if (nodeA->type != nodeB->type)
|
|
|
|
return NO;
|
|
|
|
|
2012-02-21 08:21:12 +00:00
|
|
|
if (xmlStrcmp(nodeA->name, nodeB->name) != 0)
|
2012-02-20 03:40:15 +00:00
|
|
|
return NO;
|
|
|
|
|
|
|
|
if (nodeA->type == XML_ELEMENT_NODE)
|
|
|
|
{
|
|
|
|
xmlChar *contentA = NULL;
|
|
|
|
xmlChar *contentB = NULL;
|
|
|
|
|
|
|
|
if (!isEqualAttributes(nodeA, nodeB))
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value of any text node underneath the current element.
|
|
|
|
contentA = xmlNodeGetContent((const xmlNodePtr)nodeA);
|
|
|
|
contentB = xmlNodeGetContent((const xmlNodePtr)nodeB);
|
2012-02-21 08:21:12 +00:00
|
|
|
if (xmlStrcmp(contentA, contentB) != 0)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-21 08:21:12 +00:00
|
|
|
xmlFree(contentA);
|
|
|
|
xmlFree(contentB);
|
2012-02-20 03:40:15 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2012-02-21 08:21:12 +00:00
|
|
|
xmlFree(contentA);
|
|
|
|
xmlFree(contentB);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static BOOL
|
|
|
|
isEqualTree(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-26 19:58:25 +00:00
|
|
|
xmlNodePtr childA;
|
|
|
|
xmlNodePtr childB;
|
|
|
|
|
2012-02-21 08:21:12 +00:00
|
|
|
if (nodeA == nodeB)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nodeA == NULL || nodeB == NULL)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEqualNode(nodeA, nodeB))
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2012-02-26 19:58:25 +00:00
|
|
|
// Check children
|
|
|
|
childA = nodeA->children;
|
|
|
|
childB = nodeB->children;
|
|
|
|
while (isEqualTree(childA, childB))
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-26 19:58:25 +00:00
|
|
|
if (childA == NULL)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
childA = childA->next;
|
|
|
|
childB = childB->next;
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
2012-02-26 19:58:25 +00:00
|
|
|
return NO;
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private methods to manage libxml pointers...
|
|
|
|
@interface NSXMLNode (Private)
|
|
|
|
- (void *) _node;
|
|
|
|
- (void) _setNode: (void *)_anode;
|
|
|
|
+ (NSXMLNode *) _objectForNode: (xmlNodePtr)node;
|
2012-02-23 17:57:50 +00:00
|
|
|
- (void) _addSubNode: (NSXMLNode *)subNode;
|
|
|
|
- (void) _removeSubNode: (NSXMLNode *)subNode;
|
|
|
|
- (id) _initWithNode: (xmlNodePtr)node kind: (NSXMLNodeKind)kind;
|
2012-02-22 10:55:12 +00:00
|
|
|
- (xmlNodePtr) _childNodeAtIndex: (NSUInteger)index;
|
|
|
|
- (void) _insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index;
|
2012-02-20 03:40:15 +00:00
|
|
|
- (void) _updateExternalRetains;
|
|
|
|
- (void) _invalidate;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSXMLNode (Private)
|
|
|
|
- (void *) _node
|
|
|
|
{
|
|
|
|
return internal->node;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _setNode: (void *)_anode
|
|
|
|
{
|
|
|
|
if (_anode)
|
|
|
|
((xmlNodePtr)_anode)->_private = self;
|
|
|
|
internal->node = _anode;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSXMLNode *) _objectForNode: (xmlNodePtr)node
|
|
|
|
{
|
|
|
|
NSXMLNode *result = nil;
|
|
|
|
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
result = node->_private;
|
2012-02-22 10:55:12 +00:00
|
|
|
if (result == nil)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-27 12:53:25 +00:00
|
|
|
Class cls;
|
|
|
|
NSXMLNodeKind kind;
|
2012-02-22 10:55:12 +00:00
|
|
|
xmlElementType type = node->type;
|
|
|
|
|
2012-02-27 12:53:25 +00:00
|
|
|
switch (type)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
case XML_DOCUMENT_NODE:
|
|
|
|
cls = [NSXMLDocument class];
|
|
|
|
kind = NSXMLDocumentKind;
|
|
|
|
break;
|
|
|
|
case XML_ELEMENT_NODE:
|
|
|
|
cls = [NSXMLElement class];
|
|
|
|
kind = NSXMLElementKind;
|
|
|
|
break;
|
|
|
|
case XML_TEXT_NODE:
|
|
|
|
cls = [NSXMLNode class];
|
|
|
|
kind = NSXMLTextKind;
|
|
|
|
break;
|
|
|
|
case XML_PI_NODE:
|
|
|
|
cls = [NSXMLNode class];
|
|
|
|
kind = NSXMLProcessingInstructionKind;
|
|
|
|
break;
|
|
|
|
case XML_COMMENT_NODE:
|
|
|
|
cls = [NSXMLNode class];
|
|
|
|
kind = NSXMLCommentKind;
|
|
|
|
break;
|
|
|
|
case XML_ATTRIBUTE_NODE:
|
|
|
|
cls = [NSXMLNode class];
|
|
|
|
kind = NSXMLAttributeKind;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NSLog(@"ERROR: _objectForNode: called with a node of type %d",
|
|
|
|
type);
|
|
|
|
return nil;
|
|
|
|
break;
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
2012-02-27 12:53:25 +00:00
|
|
|
if ((node->doc != NULL) && ((xmlNodePtr)(node->doc) != node))
|
|
|
|
{
|
|
|
|
NSXMLDocument *doc;
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
doc = (NSXMLDocument*)[self
|
|
|
|
_objectForNode: (xmlNodePtr)node->doc];
|
2012-02-27 12:53:25 +00:00
|
|
|
if (doc != nil)
|
|
|
|
{
|
|
|
|
cls = [[doc class] replacementClassForClass: cls];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = [[cls alloc] _initWithNode: node kind: kind];
|
2012-02-20 03:40:15 +00:00
|
|
|
AUTORELEASE(result);
|
|
|
|
if (node->parent)
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *parent = [self _objectForNode: node->parent];
|
|
|
|
[parent _addSubNode: result];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
/* FIXME ... the whole following section working with externalRetains
|
|
|
|
* depends on unsafe assumptions about the functioning of the (deprecated)
|
|
|
|
* -retainCount method).
|
|
|
|
* There's no documented policy on object ownership ... so it's not clear
|
|
|
|
* what this code *should* do.
|
|
|
|
* The actual OSX policy should be determined and documented, then we
|
|
|
|
* need to produce code which implements the same behavior in a manner
|
|
|
|
* consistent with ARC and CR as well as old fashioned retain counts
|
|
|
|
* (though we may not want to replicate OSX bugs).
|
|
|
|
* It seems to me that there should be no need for the externalRetains
|
|
|
|
* counter and definitely no overriding of -retain or -release or use
|
|
|
|
* of -retainCount
|
|
|
|
* A naive (simple/obvious) model would be to say that parents own their
|
|
|
|
* children, so when the root is deallocated, all the children get detached
|
|
|
|
* and released. In this case retaining a node within a document would cause
|
|
|
|
* that node and its children to persist when the document was released.
|
|
|
|
* But maybe Apple did something different?
|
|
|
|
*/
|
2012-02-20 03:40:15 +00:00
|
|
|
- (int) _externalRetains
|
|
|
|
{
|
|
|
|
return internal->externalRetains;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) verifyExternalRetains
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
// start with 1 or 0 for ourself
|
|
|
|
int extraRetains = ([self retainCount] > 1 ? 1 : 0);
|
2012-02-20 03:40:15 +00:00
|
|
|
int index;
|
|
|
|
for (index = 0; index < [internal->subNodes count]; index++)
|
2012-03-01 06:11:30 +00:00
|
|
|
extraRetains
|
|
|
|
+= [[internal->subNodes objectAtIndex: index] _externalRetains];
|
2012-02-20 03:40:15 +00:00
|
|
|
return extraRetains;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _updateExternalRetains
|
|
|
|
{
|
|
|
|
xmlNodePtr pnode = (MY_NODE ? MY_NODE->parent : NULL);
|
|
|
|
NSXMLNode *parent = (NSXMLNode *)(pnode ? pnode->_private : nil);
|
|
|
|
int oldCount = internal->externalRetains;
|
2012-03-01 06:11:30 +00:00
|
|
|
// start with 1 or 0 for ourself
|
|
|
|
int extraRetains = ([self retainCount] > 1 ? 1 : 0);
|
2012-02-20 03:40:15 +00:00
|
|
|
int index;
|
|
|
|
for (index = 0; index < [internal->subNodes count]; index++)
|
2012-03-01 06:11:30 +00:00
|
|
|
extraRetains
|
|
|
|
+= [[internal->subNodes objectAtIndex: index] _externalRetains];
|
2012-02-20 03:40:15 +00:00
|
|
|
internal->externalRetains = extraRetains;
|
|
|
|
if (extraRetains != oldCount)
|
|
|
|
{
|
|
|
|
if (parent)
|
2012-03-01 06:11:30 +00:00
|
|
|
{
|
|
|
|
// tell our parent (if any) since our count has changed
|
|
|
|
[parent _updateExternalRetains];
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
else
|
2012-03-01 06:11:30 +00:00
|
|
|
{
|
|
|
|
/* we're the root node of this tree, so retain or
|
|
|
|
* release ourself as needed
|
|
|
|
*/
|
2012-02-20 03:40:15 +00:00
|
|
|
if (oldCount == 0 && extraRetains > 0)
|
|
|
|
{
|
|
|
|
[super retain];
|
|
|
|
internal->retainedSelf++;
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode", @"RETAINED SELF %@ (%d)",
|
|
|
|
self, internal->retainedSelf);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
else if (oldCount > 0 && extraRetains == 0 && internal->retainedSelf)
|
|
|
|
{
|
|
|
|
internal->retainedSelf--;
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode", @"RELEASED SELF %@ (%d)",
|
|
|
|
self, internal->retainedSelf);
|
2012-02-20 03:40:15 +00:00
|
|
|
[super release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!parent)
|
|
|
|
{
|
|
|
|
if (extraRetains > 0 && internal->retainedSelf == 0)
|
|
|
|
{
|
|
|
|
[super retain];
|
|
|
|
internal->retainedSelf++;
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"RETAINED SELF AFTER STATUS CHANGED %@ (%d)",
|
|
|
|
self, internal->retainedSelf);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
else if (extraRetains == 0 && internal->retainedSelf > 0)
|
|
|
|
{
|
|
|
|
internal->retainedSelf--;
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"RELEASED SELF AFTER STATUS CHANGED %@ (%d)",
|
|
|
|
self, internal->retainedSelf);
|
2012-02-20 03:40:15 +00:00
|
|
|
[super release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
- (void) _passExternalRetainsTo: (NSXMLNode *)parent
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
/* this object just became a subNode, so pass knowledge of
|
|
|
|
* external retains up the line
|
|
|
|
*/
|
2012-02-20 03:40:15 +00:00
|
|
|
if (internal->externalRetains > 0)
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"_passExternalRetainsTo: %@ (%d,%d) from %@ Start: (%d,%d)",
|
|
|
|
parent, [parent _externalRetains], [parent verifyExternalRetains],
|
|
|
|
self, internal->externalRetains, [self verifyExternalRetains]);
|
2012-02-20 03:40:15 +00:00
|
|
|
// if ([self retainCount] == 2)
|
|
|
|
// {
|
|
|
|
// internal->externalRetains--;
|
2012-03-01 06:11:30 +00:00
|
|
|
// NSDebugLLog(@"NSXMLNode",
|
|
|
|
// @"RELEASING TRICKY EXTRA RETAIN WHILE ADDING TO PARENT "
|
|
|
|
// @"in %@ now: %d", self, internal->externalRetains);
|
2012-02-20 03:40:15 +00:00
|
|
|
// }
|
|
|
|
//[self _updateExternalRetains];
|
|
|
|
if (internal->retainedSelf)
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
// we're no longer the root of our branch, so stop retaining ourself
|
|
|
|
[super release];
|
|
|
|
internal->retainedSelf--;
|
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"RELEASED SELF %@ (%d) in _passExternal...",
|
|
|
|
self, internal->retainedSelf);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
[parent _updateExternalRetains];
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"DID _passExternalRetainsTo: %@ (%d,%d) from %@ End: (%d,%d)",
|
|
|
|
parent, [parent _externalRetains], [parent verifyExternalRetains],
|
|
|
|
self, internal->externalRetains, [self verifyExternalRetains]);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
- (void) _removeExternalRetainsFrom: (NSXMLNode *)parent
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
/* this object is no longer a subNode, so pass removal of
|
|
|
|
* external retains up the line
|
|
|
|
*/
|
2012-02-20 03:40:15 +00:00
|
|
|
if (internal->externalRetains > 0)
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"_removeExternalRetainsTo: %@ from %@ Start: %d",
|
|
|
|
parent, self, internal->externalRetains);
|
|
|
|
// [parent releaseExternalRetain: internal->externalRetains];
|
|
|
|
if ([self retainCount] == 1)
|
|
|
|
{
|
|
|
|
internal->externalRetains++;
|
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"ADDED TRICKY EXTRA COUNT WHILE REMOVING FROM PARENT in %@ "
|
|
|
|
@"now: %d subNodes(low): %d", self, internal->externalRetains,
|
|
|
|
[self verifyExternalRetains]);
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
// becoming detached, so retain ourself as the new root of our branch
|
|
|
|
[super retain];
|
2012-02-20 03:40:15 +00:00
|
|
|
internal->retainedSelf++;
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"RETAINED SELF %@ (%d) in _removeExternal...",
|
|
|
|
self, internal->retainedSelf);
|
2012-02-20 03:40:15 +00:00
|
|
|
[parent _updateExternalRetains];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
- (void) _addSubNode: (NSXMLNode *)subNode
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
if (!internal->subNodes)
|
|
|
|
internal->subNodes = [[NSMutableArray alloc] init];
|
2012-02-23 17:57:50 +00:00
|
|
|
if ([internal->subNodes indexOfObjectIdenticalTo: subNode] == NSNotFound)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
[internal->subNodes addObject: subNode];
|
|
|
|
[subNode _passExternalRetainsTo: self];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
- (void) _removeSubNode: (NSXMLNode *)subNode
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
// retain temporarily so we can safely remove from our subNodes list first
|
|
|
|
[subNode retain];
|
2012-02-23 17:57:50 +00:00
|
|
|
[internal->subNodes removeObjectIdenticalTo: subNode];
|
|
|
|
[subNode _removeExternalRetainsFrom: self];
|
2012-02-20 03:40:15 +00:00
|
|
|
[subNode release]; // release temporary hold
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _createInternal
|
|
|
|
{
|
|
|
|
GS_CREATE_INTERNAL(NSXMLNode);
|
|
|
|
}
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
- (id) _initWithNode: (xmlNodePtr)node kind: (NSXMLNodeKind)kind
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
[self _createInternal];
|
2012-02-22 10:55:12 +00:00
|
|
|
[self _setNode: node];
|
2012-02-20 03:40:15 +00:00
|
|
|
internal->kind = kind;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
- (xmlNodePtr) _childNodeAtIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
NSUInteger count = 0;
|
|
|
|
xmlNodePtr node = (xmlNodePtr)(internal->node);
|
|
|
|
xmlNodePtr children = node->children;
|
2012-03-01 06:11:30 +00:00
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
if (!children)
|
|
|
|
return NULL; // the Cocoa docs say it returns nil if there are no children
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
while (children != NULL && count++ < index)
|
2012-02-22 10:55:12 +00:00
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
children = children->next;
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
if (count < index)
|
2012-02-22 10:55:12 +00:00
|
|
|
[NSException raise: NSRangeException format: @"child index too large"];
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
/* this private method provides the common insertion
|
|
|
|
* implementation used by NSXMLElement and NSXMLDocument
|
|
|
|
*/
|
2012-02-22 10:55:12 +00:00
|
|
|
|
|
|
|
// Get all of the nodes...
|
|
|
|
xmlNodePtr parentNode = MY_NODE; // we are the parent
|
|
|
|
xmlNodePtr childNode = ((xmlNodePtr)[child _node]);
|
|
|
|
xmlNodePtr curNode = [self _childNodeAtIndex: index];
|
|
|
|
BOOL mergeTextNodes = NO; // is there a defined option for this?
|
|
|
|
|
|
|
|
if (mergeTextNodes || childNode->type == XML_ATTRIBUTE_NODE)
|
|
|
|
{
|
|
|
|
// this uses the built-in libxml functions which merge adjacent text nodes
|
|
|
|
xmlNodePtr addedNode = NULL;
|
|
|
|
//curNode = ((xmlNodePtr)[cur _node]);
|
|
|
|
|
|
|
|
if (curNode == NULL) //(0 == childCount || index == childCount)
|
|
|
|
{
|
|
|
|
addedNode = xmlAddChild(parentNode, childNode);
|
|
|
|
}
|
2012-02-23 17:57:50 +00:00
|
|
|
else //if (index < childCount)
|
2012-02-22 10:55:12 +00:00
|
|
|
{
|
|
|
|
addedNode = xmlAddPrevSibling(curNode, childNode);
|
|
|
|
}
|
|
|
|
if (addedNode != childNode)
|
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
[child _setNode: NULL];
|
|
|
|
child = [NSXMLNode _objectForNode: addedNode];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
/* here we avoid merging adjacent text nodes by linking
|
|
|
|
* the new node in "by hand"
|
|
|
|
*/
|
2012-02-22 10:55:12 +00:00
|
|
|
childNode->parent = parentNode;
|
|
|
|
if (curNode)
|
|
|
|
{
|
|
|
|
// insert childNode before an existing node curNode
|
|
|
|
xmlNodePtr prevNode = curNode->prev;
|
|
|
|
curNode->prev = childNode;
|
|
|
|
childNode->next = curNode;
|
|
|
|
if (prevNode)
|
|
|
|
{
|
|
|
|
childNode->prev = prevNode;
|
|
|
|
prevNode->next = childNode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
/* in this case, this is the new "first child",
|
|
|
|
* so update our parent to point to it
|
|
|
|
*/
|
2012-02-22 10:55:12 +00:00
|
|
|
parentNode->children = childNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// not inserting before an existing node... add as new "last child"
|
|
|
|
xmlNodePtr formerLastChild = parentNode->last;
|
|
|
|
if (formerLastChild)
|
|
|
|
{
|
|
|
|
formerLastChild->next = childNode;
|
|
|
|
childNode->prev = formerLastChild;
|
|
|
|
parentNode->last = childNode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no former children -- this is the first
|
|
|
|
parentNode->children = childNode;
|
|
|
|
parentNode->last = childNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
[self _addSubNode: child];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
- (void) _invalidate
|
|
|
|
{
|
|
|
|
internal->kind = NSXMLInvalidKind;
|
2012-02-22 10:55:12 +00:00
|
|
|
[self _setNode: NULL];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static void
|
|
|
|
clearPrivatePointers(xmlNodePtr aNode)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
if (!aNode)
|
|
|
|
return;
|
|
|
|
aNode->_private = NULL;
|
|
|
|
clearPrivatePointers(aNode->children);
|
|
|
|
clearPrivatePointers(aNode->next);
|
|
|
|
if (aNode->type == XML_ELEMENT_NODE)
|
|
|
|
clearPrivatePointers((xmlNodePtr)(aNode->properties));
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static int
|
|
|
|
register_namespaces(xmlXPathContextPtr xpathCtx, const xmlChar* nsList)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
xmlChar* nsListDup;
|
|
|
|
xmlChar* prefix;
|
|
|
|
xmlChar* href;
|
|
|
|
xmlChar* next;
|
|
|
|
|
|
|
|
assert(xpathCtx);
|
|
|
|
assert(nsList);
|
|
|
|
|
|
|
|
nsListDup = xmlStrdup(nsList);
|
2012-02-22 10:55:12 +00:00
|
|
|
if (nsListDup == NULL)
|
|
|
|
{
|
|
|
|
NSLog(@"Error: unable to strdup namespaces list");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
next = nsListDup;
|
2012-02-22 10:55:12 +00:00
|
|
|
while (next != NULL)
|
|
|
|
{
|
|
|
|
/* skip spaces */
|
2012-02-23 17:57:50 +00:00
|
|
|
while ((*next) == ' ') next++;
|
|
|
|
if ((*next) == '\0') break;
|
2012-02-22 10:55:12 +00:00
|
|
|
|
|
|
|
/* find prefix */
|
|
|
|
prefix = next;
|
|
|
|
next = (xmlChar*)xmlStrchr(next, '=');
|
|
|
|
if (next == NULL)
|
|
|
|
{
|
|
|
|
NSLog(@"Error: invalid namespaces list format");
|
|
|
|
xmlFree(nsListDup);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
*(next++) = '\0';
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
/* find href */
|
|
|
|
href = next;
|
|
|
|
next = (xmlChar*)xmlStrchr(next, ' ');
|
|
|
|
if (next != NULL)
|
|
|
|
{
|
|
|
|
*(next++) = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do register namespace */
|
|
|
|
if (xmlXPathRegisterNs(xpathCtx, prefix, href) != 0)
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
NSLog(@"Error: unable to register NS with prefix=\"%s\""
|
|
|
|
@" and href=\"%s\"", prefix, href);
|
2012-02-22 10:55:12 +00:00
|
|
|
xmlFree(nsListDup);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xmlFree(nsListDup);
|
2012-02-22 10:55:12 +00:00
|
|
|
return 0;
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
static NSArray *
|
|
|
|
execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-26 20:45:00 +00:00
|
|
|
xmlNodePtr node = [xmlNode _node];
|
|
|
|
xmlDocPtr doc = node->doc;
|
2012-02-20 03:40:15 +00:00
|
|
|
NSMutableArray *result = [NSMutableArray arrayWithCapacity: 10];
|
|
|
|
xmlChar* xpathExpr = (xmlChar *)XMLSTRING(xpath_exp);
|
|
|
|
xmlChar* nsList = (xmlChar *)XMLSTRING(nmspaces);
|
|
|
|
xmlXPathContextPtr xpathCtx = NULL;
|
|
|
|
xmlXPathObjectPtr xpathObj = NULL;
|
|
|
|
xmlNodeSetPtr nodeset = NULL;
|
|
|
|
xmlNodePtr cur = NULL;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
assert(xpathExpr);
|
|
|
|
|
|
|
|
/* Create xpath evaluation context */
|
|
|
|
xpathCtx = xmlXPathNewContext(doc);
|
2012-02-22 10:55:12 +00:00
|
|
|
if (!xpathCtx)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
NSLog(@"Error: unable to create new XPath context.");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register namespaces from list (if any) */
|
2012-02-22 10:55:12 +00:00
|
|
|
if ((nsList != NULL) && (register_namespaces(xpathCtx, nsList) < 0))
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
NSLog(@"Error: failed to register namespaces list \"%s\"", nsList);
|
|
|
|
xmlXPathFreeContext(xpathCtx);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
if (![xpath_exp hasPrefix: @"/"])
|
2012-02-26 20:45:00 +00:00
|
|
|
{
|
|
|
|
// provide a context for relative paths
|
|
|
|
xpathCtx->node = node;
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
/* Evaluate xpath expression */
|
|
|
|
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
|
2012-02-22 10:55:12 +00:00
|
|
|
if (xpathObj == NULL)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
NSLog(@"Error: unable to evaluate xpath expression \"%s\"", xpathExpr);
|
2012-02-29 21:13:13 +00:00
|
|
|
xmlXPathFreeContext(xpathCtx);
|
2012-02-20 03:40:15 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* results */
|
|
|
|
nodeset = xpathObj->nodesetval;
|
|
|
|
/*
|
|
|
|
if (nodeset == NULL || nodeset->nodeNr == 0)
|
|
|
|
{
|
|
|
|
xpathObj = xmlXPathEval(xpathExpr, xpathCtx);
|
|
|
|
if (xpathObj != NULL)
|
|
|
|
nodeset = xpathObj->nodesetval;
|
|
|
|
if (nodeset)
|
|
|
|
NSLog(@"Succeeded in evaluating as a path, using xmlXPathEval");
|
|
|
|
}
|
|
|
|
*/
|
2012-02-22 10:55:12 +00:00
|
|
|
if (nodeset)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
/* Collect results */
|
2012-02-22 10:55:12 +00:00
|
|
|
for (i = 0; i < nodeset->nodeNr; i++)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
id obj = nil;
|
|
|
|
cur = nodeset->nodeTab[i];
|
|
|
|
obj = [NSXMLNode _objectForNode: cur];
|
2012-02-22 10:55:12 +00:00
|
|
|
if (obj)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
[result addObject: obj];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
xmlXPathFreeObject(xpathObj);
|
|
|
|
xmlXPathFreeContext(xpathCtx);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2011-10-17 10:24:07 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
@implementation NSXMLNode
|
|
|
|
|
|
|
|
+ (id) attributeWithName: (NSString*)name
|
|
|
|
stringValue: (NSString*)stringValue
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
n = [[[self alloc] initWithKind: NSXMLAttributeKind] autorelease];
|
|
|
|
[n setStringValue: stringValue];
|
2011-12-30 21:40:12 +00:00
|
|
|
[n setName: name];
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) attributeWithName: (NSString*)name
|
|
|
|
URI: (NSString*)URI
|
|
|
|
stringValue: (NSString*)stringValue
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
n = [[[self alloc] initWithKind: NSXMLAttributeKind] autorelease];
|
|
|
|
[n setURI: URI];
|
|
|
|
[n setStringValue: stringValue];
|
2011-12-30 21:40:12 +00:00
|
|
|
[n setName: name];
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) commentWithStringValue: (NSString*)stringValue
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
n = [[[self alloc] initWithKind: NSXMLCommentKind] autorelease];
|
|
|
|
[n setStringValue: stringValue];
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) DTDNodeWithXMLString: (NSString*)string
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
n = [[[NSXMLDTDNode alloc] initWithXMLString: string] autorelease];
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) document
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
n = [[[NSXMLDocument alloc] initWithKind: NSXMLDocumentKind] autorelease];
|
2009-02-09 16:16:11 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) documentWithRootElement: (NSXMLElement*)element
|
|
|
|
{
|
|
|
|
NSXMLDocument *d;
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
d = [[[NSXMLDocument alloc] initWithRootElement: element] autorelease];
|
2009-02-09 16:16:11 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) elementWithName: (NSString*)name
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
n = [[[NSXMLElement alloc] initWithName: name] autorelease];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) elementWithName: (NSString*)name
|
|
|
|
children: (NSArray*)children
|
|
|
|
attributes: (NSArray*)attributes
|
|
|
|
{
|
|
|
|
NSXMLElement *e = [self elementWithName: name];
|
|
|
|
|
|
|
|
[e insertChildren: children atIndex: 0];
|
|
|
|
[e setAttributes: attributes];
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) elementWithName: (NSString*)name
|
|
|
|
URI: (NSString*)URI
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
n = [[[NSXMLElement alloc] initWithName: name URI: URI] autorelease];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) elementWithName: (NSString*)name
|
|
|
|
stringValue: (NSString*)string
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLElement *e;
|
|
|
|
|
2012-02-26 19:58:25 +00:00
|
|
|
e = [[NSXMLElement alloc] initWithName: name stringValue: string];
|
2009-02-09 16:16:11 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) localNameForName: (NSString*)name
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
const xmlChar *xmlName = XMLSTRING(name);
|
|
|
|
xmlChar *prefix = NULL;
|
|
|
|
xmlChar *localName;
|
|
|
|
|
|
|
|
if (NULL == xmlName)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
localName = xmlSplitQName2(xmlName, &prefix);
|
|
|
|
return StringFromXMLStringPtr(localName);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) namespaceWithName: (NSString*)name
|
|
|
|
stringValue: (NSString*)stringValue
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
n = [[[self alloc] initWithKind: NSXMLNamespaceKind] autorelease];
|
2012-02-22 10:55:12 +00:00
|
|
|
[n setName: name];
|
2009-02-09 16:16:11 +00:00
|
|
|
[n setStringValue: stringValue];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSXMLNode*) predefinedNamespaceForPrefix: (NSString*)name
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
// FIXME: We should cache these instances
|
|
|
|
if ([name isEqualToString: @"xml"])
|
|
|
|
{
|
|
|
|
return [self namespaceWithName: @"xml"
|
2012-03-01 06:11:30 +00:00
|
|
|
stringValue: @"http: //www.w3.org/XML/1998/namespace"];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
if ([name isEqualToString: @"xs"])
|
|
|
|
{
|
|
|
|
return [self namespaceWithName: @"xs"
|
2012-03-01 06:11:30 +00:00
|
|
|
stringValue: @"http: //www.w3.org/2001/XMLSchema"];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
if ([name isEqualToString: @"xsi"])
|
|
|
|
{
|
|
|
|
return [self namespaceWithName: @"xsi"
|
2012-03-01 06:11:30 +00:00
|
|
|
stringValue: @"http: //www.w3.org/2001/XMLSchema-instance"];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
if ([name isEqualToString: @"fn"])
|
|
|
|
{
|
|
|
|
return [self namespaceWithName: @"fn"
|
2012-03-01 06:11:30 +00:00
|
|
|
stringValue: @"http: //www.w3.org/2003/11/xpath-functions"];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
if ([name isEqualToString: @"local"])
|
|
|
|
{
|
|
|
|
return [self namespaceWithName: @"local"
|
2012-03-01 06:11:30 +00:00
|
|
|
stringValue: @"http: //www.w3.org/2003/11/xpath-local-functions"];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) prefixForName: (NSString*)name
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
const xmlChar *xmlName = XMLSTRING(name);
|
|
|
|
xmlChar *prefix = NULL;
|
|
|
|
|
|
|
|
if (NULL == xmlName)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
xmlSplitQName2(xmlName, &prefix);
|
|
|
|
|
|
|
|
if (NULL == prefix)
|
|
|
|
{
|
|
|
|
return @"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return StringFromXMLStringPtr(prefix);
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) processingInstructionWithName: (NSString*)name
|
|
|
|
stringValue: (NSString*)stringValue
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
n = [[[self alloc] initWithKind: NSXMLProcessingInstructionKind] autorelease];
|
|
|
|
[n setStringValue: stringValue];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) textWithStringValue: (NSString*)stringValue
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSXMLNode *n;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
n = [[[self alloc] initWithKind: NSXMLTextKind] autorelease];
|
|
|
|
[n setStringValue: stringValue];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) canonicalXMLStringPreservingComments: (BOOL)comments
|
|
|
|
{
|
|
|
|
return [self notImplemented: _cmd]; // FIXME ... generate from libxml
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLNode*) childAtIndex: (NSUInteger)index
|
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
xmlNodePtr childNode = [self _childNodeAtIndex: index];
|
2012-02-22 10:55:12 +00:00
|
|
|
return [NSXMLNode _objectForNode: childNode];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger) childCount
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
NSUInteger count = 0;
|
|
|
|
xmlNodePtr children = NULL;
|
|
|
|
xmlNodePtr node = MY_NODE;
|
|
|
|
|
|
|
|
for (children = node->children; children; children = children->next)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 12:22:30 +00:00
|
|
|
- (NSArray*) children
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
NSMutableArray *childrenArray = nil;
|
2012-02-22 10:55:12 +00:00
|
|
|
|
|
|
|
if (NSXMLInvalidKind == internal->kind)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xmlNodePtr children = NULL;
|
|
|
|
xmlNodePtr node = (xmlNodePtr)(internal->node);
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
if (node->children == NULL)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
childrenArray = [NSMutableArray array];
|
|
|
|
for (children = node->children; children; children = children->next)
|
|
|
|
{
|
|
|
|
NSXMLNode *n = [NSXMLNode _objectForNode: children];
|
|
|
|
[childrenArray addObject: n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return childrenArray;
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
|
|
|
{
|
2012-01-06 02:43:26 +00:00
|
|
|
id c = [[self class] allocWithZone: zone];
|
2012-02-29 21:13:13 +00:00
|
|
|
xmlNodePtr newNode = xmlCopyNode([self _node], 1); // make a deep copy
|
2012-02-20 03:40:15 +00:00
|
|
|
clearPrivatePointers(newNode);
|
|
|
|
|
2012-02-29 21:13:13 +00:00
|
|
|
//c = [c initWithKind: internal->kind options: internal->options];
|
|
|
|
//[c _setNode: newNode];
|
2012-02-22 10:55:12 +00:00
|
|
|
c = [c _initWithNode: newNode kind: internal->kind];
|
2012-01-04 12:41:45 +00:00
|
|
|
|
2012-02-29 21:13:13 +00:00
|
|
|
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
// [c setName: [self name]];
|
|
|
|
// [c setURI: [self URI]];
|
|
|
|
// [c setObjectValue: [self objectValue]];
|
|
|
|
// [c setStringValue: [self stringValue]];
|
2012-01-06 02:43:26 +00:00
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
return c;
|
2012-01-06 12:48:49 +00:00
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
|
2012-02-27 12:53:25 +00:00
|
|
|
- (NSString*) description
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"<%@ %@ %d>%@\n",
|
2012-03-01 06:11:30 +00:00
|
|
|
NSStringFromClass([self class]),
|
|
|
|
[self name], [self kind], [self stringValue]];
|
2012-02-27 12:53:25 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
/* FIXME ... this method should be deleted!
|
|
|
|
*/
|
2012-02-20 03:40:15 +00:00
|
|
|
- (id) retain
|
|
|
|
{
|
|
|
|
[super retain]; // do this first
|
|
|
|
if ([self retainCount] == 2)
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
[self _updateExternalRetains];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
/* FIXME ... this method should be deleted!
|
|
|
|
*/
|
2012-02-20 03:40:15 +00:00
|
|
|
- (void) release
|
|
|
|
{
|
|
|
|
if ([self retainCount] == 2)
|
|
|
|
{
|
|
|
|
[super release];
|
2012-02-22 10:55:12 +00:00
|
|
|
[self _updateExternalRetains];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
[super release];
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
if (GS_EXISTS_INTERNAL)
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodePtr node = (xmlNodePtr)(internal->node);
|
2012-01-04 12:41:45 +00:00
|
|
|
[internal->URI release];
|
|
|
|
[internal->objectValue release];
|
2012-02-20 03:40:15 +00:00
|
|
|
[internal->subNodes release];
|
|
|
|
if (node)
|
|
|
|
node->_private = NULL;
|
|
|
|
if (node && node->parent == NULL)
|
|
|
|
{
|
|
|
|
if (node->type == XML_DOCUMENT_NODE)
|
|
|
|
xmlFreeDoc((xmlDocPtr)node);
|
|
|
|
else
|
|
|
|
xmlFreeNode(node); // the top level node frees the entire tree
|
|
|
|
}
|
2012-01-04 12:41:45 +00:00
|
|
|
GS_DESTROY_INTERNAL(NSXMLNode);
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) detach
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodePtr node = (xmlNodePtr)(internal->node);
|
2012-02-22 10:55:12 +00:00
|
|
|
if (node)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
xmlNodePtr parentNode = node->parent;
|
2012-03-01 06:11:30 +00:00
|
|
|
NSXMLNode *parent = (parentNode ? parentNode->_private : nil);
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlUnlinkNode(node); // separate our node from its parent and siblings
|
|
|
|
if (parent)
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
int extraRetains = 0;
|
2012-02-20 03:40:15 +00:00
|
|
|
// transfer extra retains of this branch from our parent to ourself
|
2012-03-01 06:11:30 +00:00
|
|
|
extraRetains = internal->externalRetains;
|
|
|
|
//[self verifyExternalRetains];
|
2012-02-20 03:40:15 +00:00
|
|
|
if (extraRetains)
|
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
/// [parent releaseExternalRetain: extraRetains];
|
2012-03-01 06:11:30 +00:00
|
|
|
if ([self retainCount] == 1)
|
|
|
|
{
|
|
|
|
internal->externalRetains++;
|
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"ADDED TRICKY EXTRA COUNT WHILE DETACHING in %@ now: "
|
|
|
|
@"%d subNodes(low): %d", self, internal->externalRetains,
|
|
|
|
[self verifyExternalRetains]);
|
|
|
|
}
|
2012-02-23 17:57:50 +00:00
|
|
|
[super retain]; //[self recordExternalRetain: extraRetains];
|
2012-02-20 03:40:15 +00:00
|
|
|
internal->retainedSelf++;
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode", @"RETAINED SELF %@ (%d) in detach",
|
|
|
|
self, internal->retainedSelf);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
2012-02-23 17:57:50 +00:00
|
|
|
[parent _removeSubNode: self];
|
2012-03-01 06:11:30 +00:00
|
|
|
NSDebugLLog(@"NSXMLNode",
|
|
|
|
@"DETACHED %@ from %@ and transferred extra retains: %d",
|
|
|
|
self, parent, extraRetains);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 12:22:30 +00:00
|
|
|
- (NSUInteger) hash
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
return [[self name] hash];
|
2012-01-06 12:22:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (NSUInteger) index
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
int count = 0;
|
|
|
|
xmlNodePtr node = (xmlNodePtr)(internal->node);
|
|
|
|
while ((node = node->prev))
|
2012-01-07 13:08:03 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
count++; // count our earlier sibling nodes
|
2012-01-07 13:08:03 +00:00
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
return count;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 18:43:29 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithKind: NSXMLInvalidKind];
|
|
|
|
}
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
- (id) initWithKind: (NSXMLNodeKind) kind
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
return [self initWithKind: kind options: 0];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 18:43:29 +00:00
|
|
|
- (id) initWithKind: (NSXMLNodeKind)kind options: (NSUInteger)theOptions
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2011-10-01 18:43:29 +00:00
|
|
|
Class theSubclass = [NSXMLNode class];
|
2012-02-20 03:40:15 +00:00
|
|
|
void *node = NULL;
|
2012-01-04 12:41:45 +00:00
|
|
|
|
2011-10-01 18:43:29 +00:00
|
|
|
if (nil == (self = [super init]))
|
2012-01-01 07:38:53 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
2011-10-01 18:43:29 +00:00
|
|
|
|
|
|
|
/*
|
2012-02-23 17:57:50 +00:00
|
|
|
* We find the correct subclass for specific node kinds:
|
2011-10-01 18:43:29 +00:00
|
|
|
*/
|
|
|
|
switch (kind)
|
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
case NSXMLDocumentKind:
|
2012-02-20 03:40:15 +00:00
|
|
|
theSubclass = [NSXMLDocument class];
|
|
|
|
break;
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
case NSXMLInvalidKind:
|
2012-02-20 03:40:15 +00:00
|
|
|
theSubclass = [NSXMLNode class];
|
|
|
|
break;
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
case NSXMLElementKind:
|
2012-02-20 03:40:15 +00:00
|
|
|
theSubclass = [NSXMLElement class];
|
|
|
|
break;
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
case NSXMLDTDKind:
|
2012-02-20 03:40:15 +00:00
|
|
|
theSubclass = [NSXMLDTD class];
|
|
|
|
break;
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
case NSXMLEntityDeclarationKind:
|
|
|
|
case NSXMLElementDeclarationKind:
|
|
|
|
case NSXMLNotationDeclarationKind:
|
2012-02-20 03:40:15 +00:00
|
|
|
theSubclass = [NSXMLDTDNode class];
|
|
|
|
break;
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
case NSXMLAttributeDeclarationKind:
|
2012-02-20 03:40:15 +00:00
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
break;
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
case NSXMLProcessingInstructionKind:
|
|
|
|
case NSXMLCommentKind:
|
|
|
|
case NSXMLTextKind:
|
|
|
|
case NSXMLNamespaceKind:
|
|
|
|
case NSXMLAttributeKind:
|
2012-02-20 03:40:15 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-23 17:57:50 +00:00
|
|
|
default:
|
2012-02-20 03:40:15 +00:00
|
|
|
kind = NSXMLInvalidKind;
|
|
|
|
theSubclass = [NSXMLNode class];
|
|
|
|
break;
|
2011-10-01 18:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether we are already initializing an instance of the given
|
2012-03-01 06:11:30 +00:00
|
|
|
* subclass. If we are not, release ourselves and allocate a subclass
|
|
|
|
* instance instead.
|
2011-10-01 18:43:29 +00:00
|
|
|
*/
|
|
|
|
if (NO == [self isKindOfClass: theSubclass])
|
2012-01-01 07:38:53 +00:00
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return [[theSubclass alloc] initWithKind: kind
|
|
|
|
options: theOptions];
|
|
|
|
}
|
2011-10-01 18:43:29 +00:00
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
switch (kind)
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
case NSXMLDocumentKind:
|
|
|
|
node = xmlNewDoc((xmlChar *)"1.0");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSXMLInvalidKind:
|
|
|
|
case NSXMLElementKind:
|
|
|
|
node = xmlNewNode(NULL,(xmlChar *)"");
|
|
|
|
break;
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-03-01 06:11:30 +00:00
|
|
|
case NSXMLDTDKind:
|
|
|
|
node = xmlNewDtd(NULL, (xmlChar *)"", (xmlChar *)"",(xmlChar *)"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSXMLEntityDeclarationKind:
|
|
|
|
case NSXMLElementDeclarationKind:
|
|
|
|
case NSXMLNotationDeclarationKind:
|
|
|
|
node = xmlNewNode(NULL, (xmlChar *)"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSXMLProcessingInstructionKind:
|
|
|
|
node = xmlNewPI((xmlChar *)"", (xmlChar *)"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSXMLCommentKind:
|
|
|
|
node = xmlNewComment((xmlChar *)"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSXMLTextKind:
|
|
|
|
node = xmlNewText((xmlChar *)"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSXMLNamespaceKind:
|
|
|
|
node = xmlNewNs(NULL,(xmlChar *)"",(xmlChar *)"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSXMLAttributeKind:
|
|
|
|
node = xmlNewProp(NULL,(xmlChar *)"",(xmlChar *)"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
/* Create holder for internal instance variables if needed.
|
2011-10-01 18:43:29 +00:00
|
|
|
*/
|
2012-02-20 03:40:15 +00:00
|
|
|
[self _createInternal];
|
|
|
|
|
|
|
|
/* Create libxml object to go with it...
|
|
|
|
*/
|
|
|
|
[self _setNode: node];
|
2011-10-01 18:43:29 +00:00
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
/* If we are initializing for the correct class, we can actually perform
|
2012-02-23 17:57:50 +00:00
|
|
|
* initializations:
|
2012-01-04 12:41:45 +00:00
|
|
|
*/
|
|
|
|
internal->kind = kind;
|
2012-01-04 09:20:32 +00:00
|
|
|
internal->options = theOptions;
|
2009-02-09 16:16:11 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-01-06 12:22:30 +00:00
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
if ([self kind] != [other kind])
|
2012-01-06 12:22:30 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
return isEqualTree(MY_NODE,(xmlNodePtr)[other _node]);
|
2012-01-06 12:22:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (NSXMLNodeKind) kind
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->kind;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger) level
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
NSUInteger level = 0;
|
|
|
|
xmlNodePtr tmp = MY_NODE->parent;
|
2009-02-09 16:16:11 +00:00
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
while (tmp != NULL)
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
|
|
|
level++;
|
2012-02-20 03:40:15 +00:00
|
|
|
tmp = tmp->parent;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) localName
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
return [[self class] localNameForName: [self name]];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) name
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
if (NSXMLInvalidKind == internal->kind)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return StringFromXMLStringPtr(MY_NODE->name);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 18:43:29 +00:00
|
|
|
- (NSXMLNode*) _nodeFollowingInNaturalDirection: (BOOL)forward
|
|
|
|
{
|
2012-02-22 22:52:31 +00:00
|
|
|
NSXMLNode *ancestor = self;
|
2011-10-01 18:43:29 +00:00
|
|
|
NSXMLNode *candidate = nil;
|
2012-02-22 22:52:31 +00:00
|
|
|
NSXMLNodeKind kind;
|
2011-10-01 18:43:29 +00:00
|
|
|
|
|
|
|
/* Node walking is a depth-first thingy. Hence, we consider children first: */
|
2012-02-20 03:40:15 +00:00
|
|
|
if (0 != [self childCount])
|
2011-10-01 18:43:29 +00:00
|
|
|
{
|
2012-01-01 07:38:53 +00:00
|
|
|
NSUInteger theIndex = 0;
|
|
|
|
if (NO == forward)
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
theIndex = [self childCount] - 1;
|
2012-01-01 07:38:53 +00:00
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
candidate = [[self children] objectAtIndex: theIndex];
|
2011-10-01 18:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If there are no children, we move on to siblings: */
|
|
|
|
/* If there are no siblings left for the receiver, we recurse down to the root
|
|
|
|
* of the tree until we find an ancestor with further siblings: */
|
|
|
|
while ((nil == candidate) && (nil != ancestor))
|
|
|
|
{
|
2012-01-01 07:38:53 +00:00
|
|
|
if (forward)
|
|
|
|
{
|
|
|
|
candidate = [ancestor nextSibling];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
candidate = [ancestor previousSibling];
|
|
|
|
}
|
2012-02-22 22:52:31 +00:00
|
|
|
ancestor = [ancestor parent];
|
2011-10-01 18:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No children, no next siblings, no next siblings for any ancestor: We are
|
|
|
|
* the last node */
|
|
|
|
if (nil == candidate)
|
2012-01-01 07:38:53 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
2011-10-01 18:43:29 +00:00
|
|
|
|
|
|
|
/* Sanity check: Namespace and attribute nodes are skipped: */
|
2012-02-22 22:52:31 +00:00
|
|
|
kind = [candidate kind];
|
|
|
|
if ((NSXMLAttributeKind == kind) || (NSXMLNamespaceKind == kind))
|
2012-01-01 07:38:53 +00:00
|
|
|
{
|
|
|
|
return [candidate _nodeFollowingInNaturalDirection: forward];
|
|
|
|
}
|
2011-10-01 18:43:29 +00:00
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (NSXMLNode*) nextNode
|
|
|
|
{
|
2011-10-01 18:43:29 +00:00
|
|
|
return [self _nodeFollowingInNaturalDirection: YES];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLNode*) nextSibling
|
|
|
|
{
|
2012-02-22 22:52:31 +00:00
|
|
|
return [NSXMLNode _objectForNode: MY_NODE->next];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectValue
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->objectValue;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLNode*) parent
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
return [NSXMLNode _objectForNode: MY_NODE->parent];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) prefix
|
|
|
|
{
|
2012-02-22 10:55:12 +00:00
|
|
|
return [[self class] prefixForName: [self name]];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLNode*) previousNode
|
|
|
|
{
|
2011-10-01 18:43:29 +00:00
|
|
|
return [self _nodeFollowingInNaturalDirection: NO];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLNode*) previousSibling
|
|
|
|
{
|
2012-02-22 22:52:31 +00:00
|
|
|
return [NSXMLNode _objectForNode: MY_NODE->prev];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDocument*) rootDocument
|
|
|
|
{
|
2012-03-01 06:11:30 +00:00
|
|
|
return
|
|
|
|
(NSXMLDocument *)[NSXMLNode _objectForNode: (xmlNodePtr)(MY_NODE->doc)];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) stringValue
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodePtr node = MY_NODE;
|
|
|
|
xmlChar *content = xmlNodeGetContent(node);
|
|
|
|
NSString *result = nil;
|
|
|
|
|
|
|
|
/*
|
2012-03-01 06:11:30 +00:00
|
|
|
if (node->type == XML_ATTRIBUTE_NODE
|
|
|
|
|| node->type == XML_ELEMENT_NODE)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
node = node->children;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
result = StringFromXMLStringPtr(content);
|
2012-02-22 10:55:12 +00:00
|
|
|
xmlFree(content);
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
return result;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setObjectValue: (id)value
|
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
if (nil == value)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
ASSIGN(internal->objectValue, [NSString stringWithString: @""]);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-04 12:41:45 +00:00
|
|
|
ASSIGN(internal->objectValue, value);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 07:27:56 +00:00
|
|
|
- (void) setName: (NSString *)name
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
if (NSXMLInvalidKind != internal->kind)
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodePtr node = MY_NODE;
|
|
|
|
xmlNodeSetName(node, XMLSTRING(name));
|
2012-01-06 12:22:30 +00:00
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStringValue: (NSString*)string
|
|
|
|
{
|
|
|
|
[self setStringValue: string resolvingEntities: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStringValue: (NSString*)string resolvingEntities: (BOOL)resolve
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodePtr node = MY_NODE;
|
2012-02-22 22:52:31 +00:00
|
|
|
|
|
|
|
if (nil == string)
|
|
|
|
{
|
|
|
|
// string value may not be nil
|
|
|
|
string = @"";
|
|
|
|
}
|
2012-01-04 12:41:45 +00:00
|
|
|
if (resolve == NO)
|
2012-01-03 17:15:29 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodeSetContent(node, XMLSTRING(string));
|
2012-01-03 17:15:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
// need to actually resolve entities...
|
2012-02-22 22:52:31 +00:00
|
|
|
// is this the right functionality??
|
|
|
|
xmlChar *newstr = xmlEncodeSpecialChars(node->doc, XMLSTRING(string));
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodeSetContent(node, newstr);
|
|
|
|
xmlMemFree(newstr);
|
2012-01-06 12:22:30 +00:00
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
- (void) setURI: (NSString*)URI
|
|
|
|
{
|
|
|
|
if (NSXMLInvalidKind != internal->kind)
|
|
|
|
{
|
|
|
|
ASSIGNCOPY(internal->URI, URI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 22:52:31 +00:00
|
|
|
- (NSString*) URI
|
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
if (NSXMLInvalidKind == internal->kind)
|
2012-02-22 22:52:31 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return internal->URI; // FIXME ... fetch from libxml
|
|
|
|
}
|
|
|
|
|
2012-02-22 10:55:12 +00:00
|
|
|
- (NSString*) XMLString
|
|
|
|
{
|
2012-02-22 22:52:31 +00:00
|
|
|
return [self XMLStringWithOptions: NSXMLNodeOptionsNone];
|
2012-02-22 10:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) XMLStringWithOptions: (NSUInteger)options
|
|
|
|
{
|
|
|
|
NSString *string = nil;
|
|
|
|
xmlNodePtr node = (xmlNodePtr)[self _node];
|
|
|
|
xmlChar *buf = NULL;
|
|
|
|
xmlDocPtr doc = node->doc;
|
|
|
|
xmlBufferPtr buffer = xmlBufferCreate(); //NULL;
|
|
|
|
int error = 0;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
error = xmlNodeDump(buffer, doc, node, 1, 1);
|
2012-02-22 22:52:31 +00:00
|
|
|
if (-1 == error)
|
|
|
|
{
|
|
|
|
xmlBufferFree(buffer);
|
|
|
|
return nil;
|
|
|
|
}
|
2012-02-22 10:55:12 +00:00
|
|
|
buf = buffer->content;
|
|
|
|
len = buffer->use;
|
|
|
|
string = StringFromXMLString(buf,len);
|
|
|
|
xmlBufferFree(buffer);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (NSString*) XPath
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlNodePtr node = MY_NODE;
|
|
|
|
return StringFromXMLStringPtr(xmlGetNodePath(node));
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
- (NSArray*) nodesForXPath: (NSString*)anxpath error: (NSError**)error
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
if (error != NULL)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
*error = NULL;
|
|
|
|
}
|
|
|
|
return execute_xpath(self, anxpath, NULL);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*) objectsForXQuery: (NSString*)xquery
|
|
|
|
constants: (NSDictionary*)constants
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
return [self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*) objectsForXQuery: (NSString*)xquery error: (NSError**)error
|
|
|
|
{
|
|
|
|
return [self notImplemented: _cmd];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
#endif
|