Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 89 lines (70 sloc) 3.616 kb
9f27f1f @pointlesscrap Changed license back to FreeBSD
pointlesscrap authored
1 //
2 // NSXMLElementAdditions.m
3 // MKAbeFook
4 //
5 // Created by Mike Kinney on 11/27/07.
c4e4e9f @pointlesscrap Initial 0.7.3 import.
pointlesscrap authored
6 /*
2b9122d @pointlesscrap Updates copyright to 2009, fixes issue with MKFacebookRequest not set…
pointlesscrap authored
7 Copyright (c) 2009, Mike Kinney
9f27f1f @pointlesscrap Changed license back to FreeBSD
pointlesscrap authored
8 All rights reserved.
9e3d61d @pointlesscrap Changed license to Creative Commons. Updated version to 0.7.6
pointlesscrap authored
9
9f27f1f @pointlesscrap Changed license back to FreeBSD
pointlesscrap authored
10 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
13
14 Neither the name of MKAbeFook nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
17 */
c4e4e9f @pointlesscrap Initial 0.7.3 import.
pointlesscrap authored
18
19 #import "NSXMLElementAdditions.h"
20
21 //Parsing methods originally written by Josh Wiseman (Facebook, Inc.) and distributed with the iPhoto plugin. modifications made by Mike Kinney
22
23 @implementation NSXMLElement (NSXMLElementAdditions)
24 -(NSArray *)arrayFromXMLElement
25 {
26 NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
27 NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
28
29 BOOL isList = NO;
30 NSString *isListString = [[self attributeForName:@"list"] stringValue];
31 if (isListString && [isListString caseInsensitiveCompare:@"true"] == NSOrderedSame)
32 isList = YES;
33
34 NSEnumerator *e = [[self children] objectEnumerator];
35 NSXMLElement *childElement;
36 NSString *key;
37 id value;
38 while (childElement = [e nextObject]) {
39 key = [childElement name];
40 value = nil;
41
42 // if it's a pure text value
43 if ([childElement childCount] == 1 && [[childElement childAtIndex:0] kind] == NSXMLTextKind) {
44 value = [childElement stringValue];
45 }
46 else if ([childElement childCount] > 0) {
47 self = childElement;
48 // assume the child is a list
49 value = [self arrayFromXMLElement];
50 // if the returned array only has one object, turn it into a structure
51 if ([(NSArray *)value count] == 1) {
52 value = (NSDictionary *)[(NSArray *)value objectAtIndex:0];
53 }
54 }
55
56 // as long as it wasn't an empty element
57 if (value) {
58 // save the value into a dictionary as well, just in case this isn't really a list
59 [dictionary setValue:value forKey:key];
60 [array addObject:value];
61 }
62 }
63
64 // if we didn't really have a list, populate the array with the dictionary we've been saving in parallel.
65 if (isList == NO) {
66 [array removeAllObjects];
67 [array addObject: dictionary];
68 }
69
70 return array;
71 }
72
73
74 -(NSDictionary *)dictionaryFromXMLElement
75 {
76 NSArray *array = [self arrayFromXMLElement];
77 if ([array count] != 1)
78 return nil;
79
80 id firstValue = [array objectAtIndex:0];
81 if ([[firstValue class] isSubclassOfClass: [NSDictionary class]] == NO)
82 return nil;
83 return firstValue;
84 }
85
86
87
88 @end
Something went wrong with that request. Please try again.