-
Notifications
You must be signed in to change notification settings - Fork 12
/
APElement.m
332 lines (284 loc) · 8.12 KB
/
APElement.m
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright 2009, Arash Payan (http://arashpayan.com)
This library is distributed under the terms of the GNU Lesser GPL.
This file is part of APXML.
APXML is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
APXML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with APXML. If not, see <http://www.gnu.org/licenses/>.
*/
#import "APElement.h"
#import "APAttribute.h"
@implementation APElement
@synthesize name;
@synthesize parent;
/*
Returns a new element with the specified tag name
*/
+ (id)elementWithName:(NSString*)aName {
return [[[APElement alloc] initWithName:aName] autorelease];
}
/*
Returns a new element with the specified tag name and attributes
*/
+ (id)elementWithName:(NSString*)aName attributes:(NSDictionary*)someAttributes {
APElement *anElement = [[[APElement alloc] initWithName:aName] autorelease];
[anElement addAttributes:someAttributes];
return anElement;
}
/*
Initializes the element with the specified tag name
*/
- (id)initWithName:(NSString*)aName {
if (self = [super init])
{
name = [[NSString alloc] initWithString:aName];
//value = [[NSMutableString alloc] init];
parent = nil;
attributes = [[NSMutableDictionary alloc] init];
childElements = [[NSMutableArray alloc] init];
}
return self;
}
/*
Adds the specified attribute to the element
*/
- (void)addAttribute:(APAttribute*)anAttribute {
[attributes setObject:anAttribute.value forKey:anAttribute.name];
}
/*
Adds the specified name and value as an attribute for the element
*/
- (void)addAttributeNamed:(NSString*)aName withValue:(NSString*)aValue {
[attributes setObject:aValue forKey:aName];
}
/*
Adds a dictionary of name/values to the element.
All keys and values in the supplied dictionary must be of type NSString*
*/
- (void)addAttributes:(NSDictionary*)someAttributes {
if (someAttributes != nil)
{
[attributes addEntriesFromDictionary:someAttributes];
}
}
/*
Adds the specified element as a child of the receiver.
*/
- (void)addChild:(APElement*)anElement {
[childElements addObject:anElement];
}
/*
Appends the specified string to the element's text value
*/
- (void)appendValue:(NSString*)aValue {
if (value == nil)
value = [[NSMutableString alloc] init];
[value appendString:aValue];
}
/*
Returns the number of attributes on this element
*/
- (NSInteger)attributeCount {
return [attributes count];
}
/*
Returns the names of attributes on this element
*/
- (NSArray *)attributeNames {
return [attributes allKeys];
}
/*
Returns the number of child elements
*/
- (NSInteger)childCount {
return [childElements count];
}
/*
Returns an array of APElements that are direct descendants of this element.
Returns an empty array if the element has no children.
*/
- (NSArray*)childElements {
return [NSArray arrayWithArray:childElements];
}
/*
Returns an array of APElements that are direct descendants of this element
and have the specified tag name.
Returns an empty array if the element has no children.
*/
- (NSMutableArray*)childElements:(NSString*)aName {
NSMutableArray *result = [[[NSMutableArray alloc] init] autorelease];
NSInteger numElements = [childElements count];
for (int i=0; i<numElements; i++)
{
APElement *currElement = [childElements objectAtIndex:i];
if ([currElement.name isEqual:aName])
[result addObject:currElement];
}
return result;
}
/*
Returns the first direct descendant of this element.
Returns nil if the element has no children.
*/
- (APElement*)firstChildElement {
if ([childElements count] > 0)
return [childElements objectAtIndex:0];
else
return nil;
}
/*
Returns the first direct descendant with the specified tag name.
Returns nil if the element has no matching child.
*/
- (APElement*)firstChildElementNamed:(NSString*)aName {
NSInteger numElements = [childElements count];
for (int i=0; i<numElements; i++)
{
APElement *currElement = [childElements objectAtIndex:i];
if ([currElement.name isEqual:aName])
return currElement;
}
return nil;
}
/*
Returns the text content of the element, or nil if there is none.
*/
- (NSString*)value {
if (value == nil)
return nil;
else
return [NSString stringWithString:value];
}
/*
Returns the value for the specified attribute name.
Returns nil if no such attribute exists
*/
- (NSString*)valueForAttributeNamed:(NSString*)aName {
return [attributes objectForKey:aName];
}
/*
Returns a human readable description of this element
*/
- (NSString*)description {
return name;
}
/*
Returns an xml string of the element, its attributes and children.
Useful for debugging.
Simply specify 0 for the tabs argument.
*/
- (NSString*)prettyXML:(int)tabs {
NSMutableString *xmlResult = [[[NSMutableString alloc] init] autorelease];
// append open bracket and element name
for (int i=0; i<tabs; i++)
[xmlResult appendFormat:@"\t"];
[xmlResult appendFormat:@"<%@", name];
for (NSString *key in attributes)
{
[xmlResult appendFormat:@" %@=\"%@\"", key, [attributes objectForKey:key]];
}
NSInteger numChildren = [childElements count];
if (numChildren == 0 && value == nil)
{
[xmlResult appendFormat:@" />\n"];
return xmlResult;
}
if (numChildren != 0)
{
[xmlResult appendString:@">\n"];
for (int i=0; i<numChildren; i++)
[xmlResult appendString:[[childElements objectAtIndex:i] prettyXML:(tabs+1)]];
for (int i=0; i<tabs; i++)
[xmlResult appendFormat:@"\t"];
[xmlResult appendFormat:@"</%@>\n", name];
return xmlResult;
}
else // there must be a value
{
[xmlResult appendFormat:@">%@</%@>\n", [self encodeEntities:value], name];
return xmlResult;
}
}
/*
Returns an xml string containing a compact representation of this element, its attributes
and children.
*/
- (NSString*)xml {
NSMutableString *xmlResult = [[[NSMutableString alloc] init] autorelease];
// append open bracket and element name
[xmlResult appendFormat:@"<%@", name];
for (NSString *key in attributes)
{
[xmlResult appendFormat:@" %@=\"%@\"", key, [attributes objectForKey:key]];
}
// append closing bracket and value
NSInteger numChildren = [childElements count];
if (numChildren == 0 && value == nil)
{
[xmlResult appendFormat:@"/>"];
return xmlResult;
}
if (numChildren != 0)
{
[xmlResult appendString:@">"];
for (int i=0; i<numChildren; i++)
[xmlResult appendString:[[childElements objectAtIndex:i] xml]];
[xmlResult appendFormat:@"</%@>", name];
return xmlResult;
}
else // there must be a value
{
[xmlResult appendFormat:@"><![CDATA[%@]]></%@>", value, name];
//[xmlResult appendFormat:@">%@</%@>", [self encodeEntities:value], name];
return xmlResult;
}
}
/*
Encodes the predeclared entities in the specified string, and returns a new encoded
string.
*/
- (NSString*)encodeEntities:(NSMutableString*)aString {
if (aString == nil || [aString length] == 0)
return @"";
if ([aString hasPrefix:@"<![CDATA["]) {
return aString;
}
NSMutableString *result = [[NSMutableString alloc] init];
[result appendString:aString];
[result replaceOccurrencesOfString:@"&"
withString:@"&"
options:0
range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@"<"
withString:@"<"
options:0
range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@">"
withString:@">"
options:0
range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@"'"
withString:@"'"
options:0
range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@"\""
withString:@"""
options:0
range:NSMakeRange(0, [result length])];
return [result autorelease];
}
- (void)dealloc {
[name release];
[value release];
[attributes release];
[childElements release];
[super dealloc];
}
@end