Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Green tests for all CXMLElement namespace methods

  • Loading branch information...
commit 43d5621f972f8dac061eee9e08746dc4a24bb18f 1 parent 44ac4d8
@sophistifunk sophistifunk authored schwa committed
View
6 Common/Source/CXMLElement.h
@@ -42,9 +42,9 @@
- (CXMLNode *)attributeForLocalName:(NSString *)localName URI:(NSString *)URI;
- (NSArray *)namespaces;
-//- (CXMLNode *)namespaceForPrefix:(NSString *)name;
-//- (CXMLNode *)resolveNamespaceForName:(NSString *)name;
-//- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
+- (CXMLNode *)namespaceForPrefix:(NSString *)name;
+- (CXMLNode *)resolveNamespaceForName:(NSString *)name;
+- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
//- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str;
@end
View
64 Common/Source/CXMLElement.m
@@ -174,9 +174,67 @@ - (NSArray *)namespaces
return theNamespaces;
}
-//- (CXMLNode *)namespaceForPrefix:(NSString *)name;
-//- (CXMLNode *)resolveNamespaceForName:(NSString *)name;
-//- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
+- (CXMLNode *)namespaceForPrefix:(NSString *)name
+{
+ const xmlChar *thePrefix = (const xmlChar *)[name UTF8String];
+ xmlNsPtr theCurrentNamespace = _node->nsDef;
+
+ while (theCurrentNamespace != NULL)
+ {
+ if (xmlStrcmp(theCurrentNamespace->prefix, thePrefix) == 0)
+ {
+ NSString *thePrefix = theCurrentNamespace->prefix ? [NSString stringWithUTF8String:(const char *)theCurrentNamespace->prefix] : @"";
+ NSString *theURI = [NSString stringWithUTF8String:(const char *)theCurrentNamespace->href];
+ return [[[CXMLNamespaceNode alloc] initWithPrefix:thePrefix URI:theURI parentElement:self] autorelease];
+ }
+ theCurrentNamespace = theCurrentNamespace->next;
+ }
+ return nil;
+}
+
+- (CXMLNode *)resolveNamespaceForName:(NSString *)name
+{
+ NSRange split = [name rangeOfString:@":"];
+
+ if (split.length > 0)
+ return [self namespaceForPrefix:[name substringToIndex:split.location]];
+
+ xmlNsPtr theCurrentNamespace = _node->nsDef;
+
+ while (theCurrentNamespace != NULL)
+ {
+ if (theCurrentNamespace->prefix == 0
+ || (theCurrentNamespace->prefix)[0] == 0)
+ {
+ NSString *thePrefix = theCurrentNamespace->prefix ? [NSString stringWithUTF8String:(const char *)theCurrentNamespace->prefix] : @"";
+ NSString *theURI = [NSString stringWithUTF8String:(const char *)theCurrentNamespace->href];
+ return [[[CXMLNamespaceNode alloc] initWithPrefix:thePrefix URI:theURI parentElement:self] autorelease];
+ }
+ theCurrentNamespace = theCurrentNamespace->next;
+ }
+
+ return nil;
+}
+
+- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI
+{
+ const xmlChar *theXMLURI = (const xmlChar *)[namespaceURI UTF8String];
+
+ xmlNsPtr theCurrentNamespace = _node->nsDef;
+
+ while (theCurrentNamespace != NULL)
+ {
+ if (xmlStrcmp(theCurrentNamespace->href, theXMLURI) == 0)
+ {
+ if(theCurrentNamespace->prefix)
+ return [NSString stringWithUTF8String:(const char *)theCurrentNamespace->prefix];
+
+ return @"";
+ }
+ theCurrentNamespace = theCurrentNamespace->next;
+ }
+ return nil;
+}
//- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str
//{
View
163 UnitTests/Source/NamespaceTests.m
@@ -212,13 +212,40 @@ - (void) assertTouchElement:(CXMLElement *)txElement matchesNSXMLElement:(NSXMLE
@"Running \"%@\" - For [namespaces count], touch gave me %d, nsxml gave me %d", desc,
[txNamespaces count],
[nsNamespaces count]);
-
+
+ CXMLNode *txNamespaceNode;
+ NSXMLNode *nsNamespaceNode;
+
for (int i = 0; i < [txNamespaces count]; i++)
{
- [self assertTouchNode:[txNamespaces objectAtIndex:i]
- matchesNSXMLNode:[nsNamespaces objectAtIndex:i]
+ txNamespaceNode = [txNamespaces objectAtIndex:i];
+ nsNamespaceNode = [nsNamespaces objectAtIndex:i];
+
+ [self assertTouchNode:txNamespaceNode
+ matchesNSXMLNode:nsNamespaceNode
describedAs:@"Comparing namespaces"];
}
+
+ txNamespaceNode = [txElement namespaceForPrefix:@"ns1"];
+ nsNamespaceNode = [nsElement namespaceForPrefix:@"ns1"];
+
+ [self assertTouchNode:txNamespaceNode
+ matchesNSXMLNode:nsNamespaceNode
+ describedAs:@"Comparing namespaceForPrefix \"ns1\""];
+
+ txNamespaceNode = [txElement namespaceForPrefix:@"ns2"];
+ nsNamespaceNode = [nsElement namespaceForPrefix:@"ns2"];
+
+ [self assertTouchNode:txNamespaceNode
+ matchesNSXMLNode:nsNamespaceNode
+ describedAs:@"Comparing namespaceForPrefix \"ns2\""];
+
+ txNamespaceNode = [txElement namespaceForPrefix:@"unused"];
+ nsNamespaceNode = [nsElement namespaceForPrefix:@"unused"];
+
+ [self assertTouchNode:txNamespaceNode
+ matchesNSXMLNode:nsNamespaceNode
+ describedAs:@"Comparing namespaceForPrefix \"unused\""];
}
@@ -451,4 +478,134 @@ - (void) test_getElementsFullyQualified
}
+- (void) test_resolveNamespaceForName
+{
+ CXMLDocument *txDoc = nil;
+ NSXMLDocument *nsDoc = nil;
+
+ // Setup
+
+ [self buildTouchDocument:&txDoc
+ andNSXMLDocument:&nsDoc
+ withXMLString:simpleDocument()];
+
+ CXMLElement *txRootElement = [txDoc rootElement];
+ NSXMLElement *nsRootElement = [nsDoc rootElement];
+
+ // Asserts
+
+ [self assertTouchNode:[txRootElement resolveNamespaceForName:@"ns1:childElement"]
+ matchesNSXMLNode:[nsRootElement resolveNamespaceForName:@"ns1:childElement"]
+ describedAs:@"Resolving namespace for ns1(colon)childElement"];
+
+ [self assertTouchNode:[txRootElement resolveNamespaceForName:@"ns2:childElement"]
+ matchesNSXMLNode:[nsRootElement resolveNamespaceForName:@"ns2:childElement"]
+ describedAs:@"Resolving namespace for ns2(colon)childElement"];
+
+ [self assertTouchNode:[txRootElement resolveNamespaceForName:@"ns1:unusedElementName"]
+ matchesNSXMLNode:[nsRootElement resolveNamespaceForName:@"ns1:unusedElementName"]
+ describedAs:@"Resolving namespace for ns1(colon)unusedElementName"];
+
+ [self assertTouchNode:[txRootElement resolveNamespaceForName:@"childElement"]
+ matchesNSXMLNode:[nsRootElement resolveNamespaceForName:@"childElement"]
+ describedAs:@"Resolving namespace for childElement"];
+
+ [self assertTouchNode:[txRootElement resolveNamespaceForName:@"unused:childElement"]
+ matchesNSXMLNode:[nsRootElement resolveNamespaceForName:@"unused:childElement"]
+ describedAs:@"Resolving namespace for unused(colon)childElement"];
+}
+
+- (void) test_resolvePrefixForNamespaceURI
+{
+ CXMLDocument *txDoc = nil;
+ NSXMLDocument *nsDoc = nil;
+
+ // Setup
+
+ [self buildTouchDocument:&txDoc
+ andNSXMLDocument:&nsDoc
+ withXMLString:simpleDocument()];
+
+ CXMLElement *txRootElement = [txDoc rootElement];
+ NSXMLElement *nsRootElement = [nsDoc rootElement];
+
+ // Asserts
+
+ NSString *txURI = [txRootElement resolvePrefixForNamespaceURI:(NSString *)NS1];
+ NSString *nsURI = [nsRootElement resolvePrefixForNamespaceURI:(NSString *)NS1];
+
+ STAssertEqualObjects(txURI,
+ nsURI,
+ @"Resolving prefix for \"%@\", touch gave me %@ but nsxml gave me %@", NS1,
+ txURI,
+ nsURI);
+
+ txURI = [txRootElement resolvePrefixForNamespaceURI:(NSString *)NS2];
+ nsURI = [nsRootElement resolvePrefixForNamespaceURI:(NSString *)NS2];
+
+ STAssertEqualObjects(txURI,
+ nsURI,
+ @"Resolving prefix for \"%@\", touch gave me %@ but nsxml gave me %@", NS2,
+ txURI,
+ nsURI);
+
+ NSString *unusedNamespace = @"www.example.org/this/namespace/unused";
+
+ txURI = [txRootElement resolvePrefixForNamespaceURI:unusedNamespace];
+ nsURI = [nsRootElement resolvePrefixForNamespaceURI:unusedNamespace];
+
+ STAssertEqualObjects(txURI,
+ nsURI,
+ @"Resolving prefix for \"%@\", touch gave me %@ but nsxml gave me %@", unusedNamespace,
+ txURI,
+ nsURI);
+
+}
+- (void) test_resolvePrefixForNamespaceURI2
+{
+ CXMLDocument *txDoc = nil;
+ NSXMLDocument *nsDoc = nil;
+
+ // Setup
+
+ [self buildTouchDocument:&txDoc
+ andNSXMLDocument:&nsDoc
+ withXMLString:emptyDocumentNoPrefix()];
+
+ CXMLElement *txRootElement = [txDoc rootElement];
+ NSXMLElement *nsRootElement = [nsDoc rootElement];
+
+ // Asserts
+
+ NSString *txURI = [txRootElement resolvePrefixForNamespaceURI:(NSString *)NS1];
+ NSString *nsURI = [nsRootElement resolvePrefixForNamespaceURI:(NSString *)NS1];
+
+ STAssertEqualObjects(txURI,
+ nsURI,
+ @"Resolving prefix for \"%@\", touch gave me %@ but nsxml gave me %@", NS1,
+ txURI,
+ nsURI);
+
+ txURI = [txRootElement resolvePrefixForNamespaceURI:(NSString *)NS2];
+ nsURI = [nsRootElement resolvePrefixForNamespaceURI:(NSString *)NS2];
+
+ STAssertEqualObjects(txURI,
+ nsURI,
+ @"Resolving prefix for \"%@\", touch gave me %@ but nsxml gave me %@", NS2,
+ txURI,
+ nsURI);
+
+ NSString *unusedNamespace = @"www.example.org/this/namespace/unused";
+
+ txURI = [txRootElement resolvePrefixForNamespaceURI:unusedNamespace];
+ nsURI = [nsRootElement resolvePrefixForNamespaceURI:unusedNamespace];
+
+ STAssertEqualObjects(txURI,
+ nsURI,
+ @"Resolving prefix for \"%@\", touch gave me %@ but nsxml gave me %@", unusedNamespace,
+ txURI,
+ nsURI);
+}
+
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.