-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAPDocument.m
217 lines (170 loc) · 4.76 KB
/
APDocument.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
/*
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 "APDocument.h"
#import "APElement.h"
/*
You probably only need to care about this class if you want to make
changes to the way APXML builds APDocuments.
This class is used as the NSXMLParser delegate that's responsible for
building APDocuments.
*/
@interface APXMLBuilder : NSObject <NSXMLParserDelegate>
{
APElement *rootElement;
APElement *openElement;
APElement *parentElement;
}
- (APElement*)rootElement;
@end
@implementation APXMLBuilder
- (id)init {
if (self = [super init])
{
rootElement = nil;
openElement = nil;
parentElement = nil;
}
return self;
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
APElement *newElement = [APElement elementWithName:elementName attributes:attributeDict];
if (rootElement == nil)
rootElement = [newElement retain];
if (openElement != nil)
{
newElement.parent = openElement;
parentElement = openElement;
openElement = newElement;
}
else
{
newElement.parent = parentElement;
openElement = newElement;
}
if (parentElement != nil)
[parentElement addChild:openElement];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if (openElement != nil)
{
openElement = parentElement;
parentElement = parentElement.parent;
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
}
- (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validError {
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[openElement appendValue:string];
}
/*
Returns the root element of the document that was parsed in.
*/
- (APElement*)rootElement {
return rootElement;
}
- (void) dealloc
{
[rootElement release];
[super dealloc];
}
@end
/*
DOM representation of an XML document
*/
@implementation APDocument
/*
Returns an APDocument representation of the specified xml. Should be used
if you want to read in some XML as a document and want to manipulate it as
such.
*/
+ (id)documentWithXMLString:(NSString*)anXMLString {
return [[[APDocument alloc] initWithString:anXMLString] autorelease];
}
/*
Initializes the document with a root element. This should be used
if you're creating a new document programmatically.
*/
- (id)initWithRootElement:(APElement*)aRootElement {
if (self = [super init])
{
rootElement = [aRootElement retain];
}
return self;
}
/*
Initializes the document with an XML string.
*/
- (id)initWithString:(NSString*)anXMLString {
if (self = [super init])
{
APXMLBuilder *builder = [[APXMLBuilder alloc] init];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[anXMLString dataUsingEncoding:NSUTF8StringEncoding]];
[parser setDelegate:builder];
[parser parse];
rootElement = [[builder rootElement] retain];
[builder release];
[parser release];
}
return self;
}
/*
Returns the document's root element
*/
- (APElement*)rootElement {
return rootElement;
}
/*
Returns a pretty string representation of the document with newlines
and tabs.
*/
- (NSString*)prettyXML {
if (rootElement != nil)
{
NSMutableString *result = [[[NSMutableString alloc] init] autorelease];
[result appendString:@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"];
[result appendString:[rootElement prettyXML:0]];
return result;
}
else
return nil;
}
/*
Returns a compact string representation of the document without newlines
or tabs.
*/
- (NSString*)xml {
if (rootElement != nil)
{
NSMutableString *result = [[[NSMutableString alloc] init] autorelease];
[result appendString:@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"];
[result appendString:[rootElement xml]];
return result;
}
else
return nil;
}
- (void) dealloc
{
[rootElement release];
[super dealloc];
}
@end