Merge pull request #405 from gnustep/small_xml_fixes

Two small xml fixes
This commit is contained in:
Doug Simons 2024-05-15 09:26:57 -06:00 committed by GitHub
commit 0935f77d8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 83 additions and 3 deletions

View file

@ -1,3 +1,13 @@
2024-04-29 Doug Simons <douglas.simons@keysight.com>
* NSXMLNode.m:
Fix execute_xpath() to return an NSError when xpath expression can't
be evaluated instead of just calling NSLog.
* NSXMLElement.m:
Fix setAttributes: and setAttributesWithDictionary: to remove previous
attributes.
2024-05-12 ethanc8R (github user)
* Headers/Foundation/NSRegularExpression.h:

View file

@ -362,7 +362,16 @@ extern void ensure_oldNs(xmlNodePtr node);
NSEnumerator *enumerator = [attributes objectEnumerator];
NSXMLNode *attribute;
// FIXME: Remove all previous attributes
// Remove all previous attributes
NSArray *currentAttributes = [self attributes];
int index;
for (index = [currentAttributes count]-1; index >= 0; index--)
{
NSXMLNode *attrNode = [currentAttributes objectAtIndex:index];
NSString *name = [attrNode name];
[self removeAttributeForName:name];
}
while ((attribute = [enumerator nextObject]) != nil)
{
[self addAttribute: attribute];
@ -379,7 +388,16 @@ extern void ensure_oldNs(xmlNodePtr node);
NSEnumerator *en = [attributes keyEnumerator];
NSString *key;
// FIXME: Remove all previous attributes
// Remove all previous attributes
NSArray *currentAttributes = [self attributes];
int index;
for (index = [currentAttributes count]-1; index >= 0; index--)
{
NSXMLNode *attrNode = [currentAttributes objectAtIndex:index];
NSString *name = [attrNode name];
[self removeAttributeForName:name];
}
while ((key = [en nextObject]) != nil)
{
NSString *val = [[attributes objectForKey: key] stringValue];

View file

@ -29,6 +29,7 @@
#define GSInternal NSXMLNodeInternal
#import "Foundation/NSCharacterSet.h"
#import "Foundation/NSError.h"
#import "NSXMLPrivate.h"
#import "GSInternal.h"
GS_PRIVATE_INTERNAL(NSXMLNode)
@ -826,6 +827,14 @@ execute_xpath(xmlNodePtr node, NSString *xpath_exp, NSDictionary *constants,
if (xpathObj == NULL)
{
NSLog(@"Error: unable to evaluate xpath expression \"%s\"", xpathExpr);
if (error != 0)
{
xmlError xmlError = xpathCtx->lastError;
NSString *message = [NSString stringWithFormat:@"Error: unable to evaluate xpath expression \"%s\" (%d)", xpathExpr, xmlError.code];
*error = [NSError errorWithDomain: @"LibXMLErrorDomain"
code: xmlError.code
userInfo: [NSDictionary dictionaryWithObject:message forKey:NSLocalizedDescriptionKey]];
}
xmlXPathFreeContext(xpathCtx);
return nil;
}

View file

@ -43,7 +43,15 @@ int main()
NSInternalInconsistencyException,
"cannot add attributes to multiple parents");
[root1 setAttributes:@[attr2]];
PASS_EQUAL([root1 attributeForName: @"attr2"], attr2,
"setAttributes: added the new attribute");
PASS_EQUAL([root1 attributeForName: @"attr1"], nil,
"setAttributes: removed the old attribute");
PASS_EQUAL([root1 attributes], @[attr2], "attributes are just as set");
[root1 setAttributes:@[]];
PASS_EQUAL([root1 attributes], @[], "setAttributes:@[] removes all attributes");
[root1 release];
[root2 release];

View file

@ -0,0 +1,35 @@
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSXMLElement.h>
#import <Foundation/NSXMLDocument.h>
#import <Foundation/NSXMLNode.h>
#import <Foundation/NSError.h>
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSString *sourceXML = @"<parent>"
"<chyld>buzz</chyld>"
"<otherchyld>woody</otherchyld>"
"<zorgtree>gollyfoo</zorgtree>"
"<ln:loner xmlns:ln=\"http://loner.ns\">POW</ln:loner>"
"</parent>";
NSXMLDocument *doc = [[[NSXMLDocument alloc] initWithXMLString:sourceXML options:0 error:NULL] autorelease];
PASS(doc != nil, "created a document from an XML string");
NSError *anError = nil;
NSXMLNode *node = [[doc nodesForXPath:@"//chyld" error:&anError] lastObject];
PASS(node != nil, "access chyld node");
PASS(anError == nil, "no error accessing chyld node");
PASS_EQUAL([node stringValue], @"buzz", "retrieve chyld node");
node = [[doc nodesForXPath:@"//ln:loner" error:&anError] lastObject];
PASS(node == nil, "can't access ln:loner node if namespace not defined at top");
PASS(anError != nil, "should get error when fail to access node");
[arp release];
arp = nil;
return 0;
}