mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
* Source/NSXMLElement.m: Correct used namespace field.
* Source/NSXMLNode.m: Implement special handling for namespace nodes. * Tests/base/NSXMLNode/basic.m: Correct method name for namespace node. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34926 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
d5c5a0bf87
commit
f2ffb6dab9
4 changed files with 134 additions and 80 deletions
|
@ -1,3 +1,9 @@
|
|||
2012-03-12 Fred Kiefer <FredKiefer@gmx.de>
|
||||
|
||||
* Source/NSXMLElement.m: Correct used namespace field.
|
||||
* Source/NSXMLNode.m: Implement special handling for namespace nodes.
|
||||
* Tests/base/NSXMLNode/basic.m: Correct method name for namespace node.
|
||||
|
||||
2012-03-11 Richard Frith-Macdonald & Fred Kiefer
|
||||
|
||||
* Source/NSXMLNamespace.m:
|
||||
|
|
|
@ -196,16 +196,8 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
|
|||
|
||||
- (void) removeAttributeForName: (NSString*)name
|
||||
{
|
||||
xmlNodePtr node = internal->node;
|
||||
xmlAttrPtr attr = xmlHasProp(node, XMLSTRING(name));
|
||||
NSXMLNode *attrNode = nil;
|
||||
NSXMLNode *attrNode = [self attributeForName: name];
|
||||
|
||||
if (NULL == attr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
attrNode = [NSXMLNode _objectForNode: (xmlNodePtr)attr];
|
||||
[attrNode detach];
|
||||
}
|
||||
|
||||
|
@ -260,8 +252,9 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
|
|||
|
||||
- (NSXMLNode*) attributeForName: (NSString*)name
|
||||
{
|
||||
NSXMLNode *result = nil;
|
||||
xmlAttrPtr attributeNode = xmlHasProp(internal->node, XMLSTRING(name));
|
||||
NSXMLNode *result = nil;
|
||||
xmlNodePtr node = internal->node;
|
||||
xmlAttrPtr attributeNode = xmlHasProp(node, XMLSTRING(name));
|
||||
|
||||
if (NULL != attributeNode)
|
||||
{
|
||||
|
@ -282,13 +275,13 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
|
|||
{
|
||||
xmlNsPtr ns = (xmlNsPtr)[aNamespace _node];
|
||||
|
||||
if (internal->node->ns == NULL)
|
||||
if (internal->node->nsDef == NULL)
|
||||
{
|
||||
internal->node->ns = ns;
|
||||
internal->node->nsDef = ns;
|
||||
}
|
||||
else
|
||||
{
|
||||
xmlNsPtr cur = internal->node->ns;
|
||||
xmlNsPtr cur = internal->node->nsDef;
|
||||
const xmlChar *prefix = ns->prefix;
|
||||
|
||||
while (xmlStrcmp(prefix, cur->prefix) != 0)
|
||||
|
@ -315,14 +308,14 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
|
|||
xmlNsPtr cur = NULL;
|
||||
|
||||
// FIXME: Remove old namespaces
|
||||
// xmlFreeNsList(internal->node->ns);
|
||||
// internal->node->ns = NULL;
|
||||
// xmlFreeNsList(internal->node->nsDef);
|
||||
// internal->node->nsDef = NULL;
|
||||
while ((namespace = (NSXMLNode *)[en nextObject]) != nil)
|
||||
{
|
||||
xmlNsPtr ns = (xmlNsPtr)[namespace _node];
|
||||
if (internal->node->ns == NULL)
|
||||
if (internal->node->nsDef == NULL)
|
||||
{
|
||||
internal->node->ns = ns;
|
||||
internal->node->nsDef = ns;
|
||||
cur = ns;
|
||||
}
|
||||
else
|
||||
|
@ -337,7 +330,7 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
|
|||
{
|
||||
// FIXME: Should use xmlGetNsList()
|
||||
NSMutableArray *result = nil;
|
||||
xmlNsPtr ns = internal->node->ns;
|
||||
xmlNsPtr ns = internal->node->nsDef;
|
||||
|
||||
if (ns)
|
||||
{
|
||||
|
@ -355,7 +348,8 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
|
|||
- (NSXMLNode*) namespaceForPrefix: (NSString*)name
|
||||
{
|
||||
// FIXME: Should use xmlSearchNs()
|
||||
xmlNsPtr ns = internal->node->ns;
|
||||
xmlNsPtr ns = internal->node->nsDef;
|
||||
|
||||
if (ns)
|
||||
{
|
||||
const xmlChar *prefix = XMLSTRING(name);
|
||||
|
@ -387,7 +381,7 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
|
|||
- (NSString*) resolvePrefixForNamespaceURI: (NSString*)namespaceURI
|
||||
{
|
||||
// FIXME Should use xmlSearchNsByHref()
|
||||
xmlNsPtr ns = internal->node->ns;
|
||||
xmlNsPtr ns = internal->node->nsDef;
|
||||
|
||||
if (ns)
|
||||
{
|
||||
|
|
|
@ -33,9 +33,6 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
|
|||
|
||||
#if defined(HAVE_LIBXML)
|
||||
|
||||
@interface NSXMLNamespaceNode : NSXMLNode
|
||||
@end
|
||||
|
||||
static int
|
||||
countAttributes(xmlNodePtr node)
|
||||
{
|
||||
|
@ -141,6 +138,23 @@ isEqualNode(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
|||
if (nodeA->type != nodeB->type)
|
||||
return NO;
|
||||
|
||||
if (nodeA->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
xmlNsPtr nsA = (xmlNsPtr)nodeA;
|
||||
xmlNsPtr nsB = (xmlNsPtr)nodeB;
|
||||
|
||||
if (xmlStrcmp(nsA->href, nsB->href) != 0)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
if (xmlStrcmp(nsA->prefix, nsB->prefix) != 0)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
if (xmlStrcmp(nodeA->name, nodeB->name) != 0)
|
||||
return NO;
|
||||
|
||||
|
@ -192,6 +206,11 @@ isEqualTree(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
|||
return NO;
|
||||
}
|
||||
|
||||
if (nodeA->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
// Check children
|
||||
childA = nodeA->children;
|
||||
childB = nodeB->children;
|
||||
|
@ -213,20 +232,9 @@ isEqualTree(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
|||
|
||||
/* FIXME ... the libxml2 data structure representing a namespace has a
|
||||
* completely different layout from that of almost all other nodes, so
|
||||
* the generix NSXMLNode code won't work and we need to override every
|
||||
* method we use!
|
||||
* the generic xmlNode code won't work and we need to check the type
|
||||
* in every method we use!
|
||||
*/
|
||||
@implementation NSXMLNamespaceNode
|
||||
- (NSUInteger) level
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
- (NSXMLNode*) parent
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation NSXMLNode (Private)
|
||||
- (void *) _node
|
||||
{
|
||||
|
@ -336,7 +344,7 @@ isEqualTree(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
|||
}
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
docNode = NULL;
|
||||
docNode = ((xmlNsPtr)node)->context;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -462,6 +470,10 @@ isEqualTree(xmlNodePtr nodeA, xmlNodePtr nodeB)
|
|||
child = [NSXMLNode _objectForNode: addedNode];
|
||||
}
|
||||
}
|
||||
else if (childNode->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
// FIXME
|
||||
}
|
||||
else
|
||||
{
|
||||
/* here we avoid merging adjacent text nodes by linking
|
||||
|
@ -1009,9 +1021,9 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
[internal->subNodes release];
|
||||
if (node)
|
||||
{
|
||||
if (internal->node->type == XML_NAMESPACE_DECL)
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
((xmlNsPtr)internal->node)->_private = NULL;
|
||||
((xmlNsPtr)node)->_private = NULL;
|
||||
xmlFreeNode(node);
|
||||
}
|
||||
else
|
||||
|
@ -1038,8 +1050,7 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
|
||||
if (node)
|
||||
{
|
||||
xmlNodePtr parentNode = node->parent;
|
||||
NSXMLNode *parent = (parentNode ? parentNode->_private : nil);
|
||||
NSXMLNode *parent = [self parent];
|
||||
|
||||
// separate our node from its parent and siblings
|
||||
xmlUnlinkNode(node);
|
||||
|
@ -1061,7 +1072,7 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
xmlNodePtr node = internal->node;
|
||||
int count = 0;
|
||||
|
||||
if (internal->node->type == XML_NAMESPACE_DECL)
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
// FIXME: Could try to go to document an loop over the namespaces
|
||||
return 0;
|
||||
|
@ -1118,7 +1129,7 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
break;
|
||||
|
||||
case NSXMLNamespaceKind:
|
||||
theSubclass = [NSXMLNamespaceNode class];
|
||||
theSubclass = [NSXMLNode class];
|
||||
break;
|
||||
|
||||
case NSXMLAttributeKind:
|
||||
|
@ -1227,26 +1238,16 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
|
||||
- (NSUInteger) level
|
||||
{
|
||||
NSUInteger level = 0;
|
||||
xmlNodePtr tmp;
|
||||
|
||||
if (!internal->node)
|
||||
NSXMLNode *parent = [self parent];
|
||||
|
||||
if (nil == parent)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (internal->node->type == XML_NAMESPACE_DECL)
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
return [parent level] + 1;
|
||||
}
|
||||
|
||||
tmp = internal->node->parent;
|
||||
while (tmp != NULL)
|
||||
{
|
||||
level++;
|
||||
tmp = tmp->parent;
|
||||
}
|
||||
return level;
|
||||
}
|
||||
|
||||
- (NSString*) localName
|
||||
|
@ -1256,11 +1257,21 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
|
||||
- (NSString*) name
|
||||
{
|
||||
xmlNodePtr node = internal->node;
|
||||
|
||||
if (NSXMLInvalidKind == internal->kind)
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
return StringFromXMLStringPtr(internal->node->name);
|
||||
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
return StringFromXMLStringPtr(((xmlNs *)node)->prefix);
|
||||
}
|
||||
else
|
||||
{
|
||||
return StringFromXMLStringPtr(node->name);
|
||||
}
|
||||
}
|
||||
|
||||
- (NSXMLNode*) _nodeFollowingInNaturalDirection: (BOOL)forward
|
||||
|
@ -1329,7 +1340,23 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
|
||||
- (NSXMLNode*) parent
|
||||
{
|
||||
return [NSXMLNode _objectForNode: internal->node->parent];
|
||||
xmlNodePtr parent = NULL;
|
||||
xmlNodePtr node = internal->node;
|
||||
|
||||
if (NULL == node)
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
parent = (xmlNodePtr)(((xmlNs *)node)->context);
|
||||
}
|
||||
else
|
||||
{
|
||||
parent = node->parent;
|
||||
}
|
||||
return [NSXMLNode _objectForNode: parent];
|
||||
}
|
||||
|
||||
- (NSString*) prefix
|
||||
|
@ -1359,14 +1386,6 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
xmlChar *content = xmlNodeGetContent(node);
|
||||
NSString *result = nil;
|
||||
|
||||
/*
|
||||
if (node->type == XML_ATTRIBUTE_NODE
|
||||
|| node->type == XML_ELEMENT_NODE)
|
||||
{
|
||||
node = node->children;
|
||||
}
|
||||
*/
|
||||
|
||||
if (NULL != content)
|
||||
{
|
||||
result = StringFromXMLStringPtr(content);
|
||||
|
@ -1393,12 +1412,27 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
|
||||
- (void) setName: (NSString *)name
|
||||
{
|
||||
xmlNodePtr node = internal->node;
|
||||
|
||||
if (NSXMLInvalidKind == internal->kind)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
xmlNodeSetName(internal->node, XMLSTRING(name));
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
xmlNsPtr ns = (xmlNsPtr)node;
|
||||
|
||||
if (ns->prefix != NULL)
|
||||
{
|
||||
xmlFree((xmlChar *)ns->prefix);
|
||||
}
|
||||
ns->prefix = XMLStringCopy(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
xmlNodeSetName(node, XMLSTRING(name));
|
||||
}
|
||||
}
|
||||
|
||||
- (void) setStringValue: (NSString*)string
|
||||
|
@ -1410,17 +1444,29 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
{
|
||||
xmlNodePtr node = internal->node;
|
||||
|
||||
if (resolve == NO)
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
xmlNodeSetContent(node, XMLSTRING(string));
|
||||
xmlNsPtr ns = (xmlNsPtr)node;
|
||||
if (ns->href != NULL)
|
||||
{
|
||||
xmlFree((xmlChar *)ns->href);
|
||||
}
|
||||
ns->href = XMLStringCopy(string);
|
||||
}
|
||||
else
|
||||
{
|
||||
// need to actually resolve entities...
|
||||
// is this the right functionality?? xmlEncodeSpecialChars()
|
||||
xmlChar *newstr = xmlEncodeEntitiesReentrant(node->doc, XMLSTRING(string));
|
||||
xmlNodeSetContent(node, newstr);
|
||||
xmlMemFree(newstr);
|
||||
if (resolve == NO)
|
||||
{
|
||||
xmlNodeSetContent(node, XMLSTRING(string));
|
||||
}
|
||||
else
|
||||
{
|
||||
// need to actually resolve entities...
|
||||
// is this the right functionality?? xmlEncodeSpecialChars()
|
||||
xmlChar *newstr = xmlEncodeEntitiesReentrant(node->doc, XMLSTRING(string));
|
||||
xmlNodeSetContent(node, newstr);
|
||||
xmlMemFree(newstr);
|
||||
}
|
||||
}
|
||||
ASSIGN(internal->objectValue, string);
|
||||
}
|
||||
|
@ -1455,11 +1501,19 @@ execute_xpath(NSXMLNode *xmlNode, NSString *xpath_exp, NSString *nmspaces)
|
|||
NSString *string = nil;
|
||||
xmlNodePtr node = (xmlNodePtr)[self _node];
|
||||
xmlChar *buf = NULL;
|
||||
xmlDocPtr doc = node->doc;
|
||||
xmlDocPtr doc;
|
||||
xmlBufferPtr buffer = xmlBufferCreate();
|
||||
int error = 0;
|
||||
int len = 0;
|
||||
|
||||
if (node->type == XML_NAMESPACE_DECL)
|
||||
{
|
||||
doc = ((xmlNs *)node)->context;
|
||||
}
|
||||
else
|
||||
{
|
||||
doc = node->doc;
|
||||
}
|
||||
error = xmlNodeDump(buffer, doc, node, 1, 1);
|
||||
if (-1 == error)
|
||||
{
|
||||
|
|
|
@ -25,8 +25,8 @@ int main()
|
|||
text = [NSXMLNode textWithStringValue: @"Text node"];
|
||||
pi = [NSXMLNode processingInstructionWithName: @"PI name"
|
||||
stringValue: @"PI string"];
|
||||
ns = [NSXMLNode processingInstructionWithName: @"name space name"
|
||||
stringValue: @"name space string"];
|
||||
ns = [NSXMLNode namespaceWithName: @"name space name"
|
||||
stringValue: @"name space string"];
|
||||
comment = [NSXMLNode commentWithStringValue: @"Comment node"];
|
||||
attr = [NSXMLNode attributeWithName: @"key"
|
||||
stringValue: @"value"];
|
||||
|
|
Loading…
Reference in a new issue