summaryrefslogtreecommitdiff
path: root/tests/xml_node_tests.m
blob: bd169e207fa04039ea4b23005a8ee32c46f5d05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#import <Foundation/Foundation.h>

NSArray *getXMLNodesNamed(NSString *nodeName, NSData *responseData)
{
	NSError *err = nil;
	id responseDoc = [[NSClassFromString(@"NSXMLDocument") alloc] initWithData: responseData options: 0 error: &err];

	NSMutableArray *matches = [NSMutableArray array];
	NSArray *nodes = [responseDoc children];
	NSEnumerator *chain = [nodes objectEnumerator];
	NSXMLNode *node = nil;

	while ((node = [chain nextObject])) {
		if (![[node name] isEqualToString: nodeName]) {
			NSLog(@"Node name: %@ is not %@, recursing into children", [node name], nodeName);
			nodes = [[nodes lastObject] children];
			chain = [nodes objectEnumerator];
			continue;
		}

		[matches addObject: node];
	}

	return [NSArray arrayWithArray: matches];
}

NSDictionary *getXMLNodesAndAttributesFromResponse(NSData *responseData)
{
	NSError *err = nil;
	id responseDoc = [[NSClassFromString(@"NSXMLDocument") alloc] initWithData: responseData options: 0 error: &err];

	NSMutableDictionary *nodesWithAttributes = [NSMutableDictionary dictionary];
	NSArray *nodes = [responseDoc children];
	NSEnumerator *chain = [nodes objectEnumerator];
	NSXMLNode *node = nil;

	while ((node = [chain nextObject])) {
		id element = [[NSClassFromString(@"NSXMLElement") alloc] initWithXMLString: [node XMLString] error: &err];
		if ([[element attributes] count] > 0) {
			NSEnumerator *attributeChain = [[element attributes] objectEnumerator];
			id attribute = nil;
			while ((attribute = [attributeChain nextObject]))
				[nodesWithAttributes setObject: [attribute stringValue] forKey: [NSString stringWithFormat: @"%@%@", [node name], [attribute name]]];
		}

		[nodesWithAttributes setObject: [node stringValue] forKey: [node name]];

		if ([[node children] count] > 0 && [[[[node children] objectAtIndex: 0] name] length] > 0) {
			nodes = [node children];
			chain = [nodes objectEnumerator];
		}

		[element release];
	}
	
	[responseDoc release];

	return [NSDictionary dictionaryWithDictionary: nodesWithAttributes];
}

int main(int a, char **b)
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	NSData *frobXML = [NSData dataWithContentsOfFile: @"get_frob_response.xml"];
	NSString *frob = [[getXMLNodesNamed(@"frob", frobXML) lastObject] stringValue];
	NSLog(@"Result of getXMLNodes (frob): %@", frob);

	NSData *tagsXML = [NSData dataWithContentsOfFile: @"get_tags_response.xml"];
	NSArray *tags = getXMLNodesNamed(@"tag", tagsXML);
	NSEnumerator *tagChain = [tags objectEnumerator];
	id currentTag = nil;
	while ((currentTag = [tagChain nextObject]))
		NSLog(@"Tag found: %@", [currentTag stringValue]);

	NSData *tokenXML = [NSData dataWithContentsOfFile: @"full_auth_token_response.xml"];
	NSDictionary *token = getXMLNodesAndAttributesFromResponse(tokenXML);
	NSLog(@"Token is: \n---\n%@\n---\n", token);

	[pool release];
	return 0;
}