Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 117 lines (95 sloc) 3.472 kb
d45292a @topfunky First import
authored
1 //
2 // TFHppleElement.m
3 // Hpple
4 //
5 // Created by Geoffrey Grosenbach on 1/31/09.
6 //
7 // Copyright (c) 2009 Topfunky Corporation, http://topfunky.com
8 //
9 // MIT LICENSE
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30
31 #import "TFHppleElement.h"
32
9a475c8 Add static qualifier to NSString key's
Zach Drayer authored
33 static NSString * const TFHppleNodeContentKey = @"nodeContent";
34 static NSString * const TFHppleNodeNameKey = @"nodeName";
a49d5fe Add -children to TFHppleElement to get a node's children
Zach Drayer authored
35 static NSString * const TFHppleNodeChildrenKey = @"nodeChildArray";
9a475c8 Add static qualifier to NSString key's
Zach Drayer authored
36 static NSString * const TFHppleNodeAttributeArrayKey = @"nodeAttributeArray";
37 static NSString * const TFHppleNodeAttributeNameKey = @"attributeName";
d45292a @topfunky First import
authored
38
977617b Properties, add a way to get the parent node of a child
Zach Drayer authored
39 @interface TFHppleElement ()
77cf5e7 @mattjgalloway ARC-ify
mattjgalloway authored
40 @property (nonatomic, unsafe_unretained, readwrite) TFHppleElement *parent;
977617b Properties, add a way to get the parent node of a child
Zach Drayer authored
41 @end
42
d45292a @topfunky First import
authored
43 @implementation TFHppleElement
977617b Properties, add a way to get the parent node of a child
Zach Drayer authored
44 @synthesize parent;
d45292a @topfunky First import
authored
45
46
47 - (id) initWithNode:(NSDictionary *) theNode
48 {
49 if (!(self = [super init]))
50 return nil;
51
52 node = theNode;
d35a6b6 @topfunky Minor test cleanup for iPhone OS 3.0. Still crashes after tests run.
authored
53
d45292a @topfunky First import
authored
54 return self;
55 }
56
3c5cbad autoreleased TFHppleElement as well
Zach Drayer authored
57 + (TFHppleElement *) hppleElementWithNode:(NSDictionary *) theNode {
77cf5e7 @mattjgalloway ARC-ify
mattjgalloway authored
58 return [[[self class] alloc] initWithNode:theNode];
3c5cbad autoreleased TFHppleElement as well
Zach Drayer authored
59 }
60
61 #pragma mark -
d45292a @topfunky First import
authored
62
63 - (NSString *) content
64 {
65 return [node objectForKey:TFHppleNodeContentKey];
66 }
67
68
69 - (NSString *) tagName
70 {
71 return [node objectForKey:TFHppleNodeNameKey];
72 }
73
a49d5fe Add -children to TFHppleElement to get a node's children
Zach Drayer authored
74 - (NSArray *) children
75 {
76 NSMutableArray *children = [NSMutableArray array];
77 for (NSDictionary *child in [node objectForKey:TFHppleNodeChildrenKey]) {
977617b Properties, add a way to get the parent node of a child
Zach Drayer authored
78 TFHppleElement *element = [TFHppleElement hppleElementWithNode:child];
79 element.parent = self;
80 [children addObject:element];
a49d5fe Add -children to TFHppleElement to get a node's children
Zach Drayer authored
81 }
82 return children;
83 }
84
c1cb1d9 Make formatting consistent everywhere, add a firstChild propert to TFHpp...
Zach Drayer authored
85 - (TFHppleElement *) firstChild
86 {
87 NSArray * children = self.children;
88 if (children.count)
89 return [children objectAtIndex:0];
90 return nil;
91 }
92
93
d45292a @topfunky First import
authored
94 - (NSDictionary *) attributes
95 {
96 NSMutableDictionary * translatedAttributes = [NSMutableDictionary dictionary];
d35a6b6 @topfunky Minor test cleanup for iPhone OS 3.0. Still crashes after tests run.
authored
97 for (NSDictionary * attributeDict in [node objectForKey:TFHppleNodeAttributeArrayKey]) {
07d5b4b @3lvis Added validators that crash the app when a kye is not found.
3lvis authored
98 if ([attributeDict objectForKey:TFHppleNodeContentKey] && [attributeDict objectForKey:TFHppleNodeAttributeNameKey]) {
99 [translatedAttributes setObject:[attributeDict objectForKey:TFHppleNodeContentKey]
100 forKey:[attributeDict objectForKey:TFHppleNodeAttributeNameKey]];
101 }
d45292a @topfunky First import
authored
102 }
103 return translatedAttributes;
104 }
105
106 - (NSString *) objectForKey:(NSString *) theKey
107 {
108 return [[self attributes] objectForKey:theKey];
109 }
110
111 - (id) description
112 {
113 return [node description];
114 }
115
116 @end
Something went wrong with that request. Please try again.