-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpalXMLParser.m
297 lines (246 loc) · 7.35 KB
/
OpalXMLParser.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
//
// OpalXMLParser.m
// Opal
//
// Created by Christian Niles on 5/4/10.
// Copyright 2010 Christian Niles. All rights reserved.
//
#import "OpalXMLParser.h"
#import "OpalXMLScanner.h"
#import "OpalXMLEvent.h"
@interface OpalXMLParser ()
- (OpalXMLEvent *)consumeNextEvent;
- (OpalXMLEvent *)readNextEvent;
@end
@implementation OpalXMLParser
// ===== INIT / DEALLOC ================================================================================================
+ (OpalXMLParser *)parserWithString:(NSString *)xmlString
{
return [[[OpalXMLParser alloc] initWithString:xmlString] autorelease];
}
- (id)initWithString:(NSString *)xmlString
{
if (self = [super init]) {
eventCount = 0;
elementCount = 0;
currentEvent = nil;
eventBuffer = [[NSMutableArray alloc] initWithCapacity:4];
stack = [[NSMutableArray alloc] initWithCapacity:8];
scanner = [[OpalXMLScanner scannerWithString:xmlString] retain];
}
return self;
}
- (void)dealloc
{
[eventBuffer release];
[stack release];
[scanner release];
[currentEvent release];
[super dealloc];
}
// ===== SCANNER =======================================================================================================
@synthesize scanner;
@synthesize eventCount;
@synthesize elementCount;
- (BOOL)isInProlog
{
return elementCount == 0;
}
- (BOOL)isInElement
{
return [stack count] > 1;
}
- (NSUInteger)characterPosition
{
return [scanner scanLocation];
}
// ===== CURRENT EVENT =================================================================================================
@synthesize currentEvent;
- (NSUInteger)currentDepth
{
return [stack count];
}
// ===== NEXT EVENT ====================================================================================================
- (OpalXMLEvent *)nextEvent
{
// return nil after we have seen the end of the document
if (currentEvent && [currentEvent isEndDocument]) return nil;
// consume the next event
OpalXMLEvent *theNextEvent = [self consumeNextEvent];
// skip whitespace outside of the document element
if (![self isInElement]) {
while (theNextEvent != nil && [theNextEvent isWhitespace]) {
theNextEvent = [self consumeNextEvent];
}
}
if (theNextEvent == nil) {
theNextEvent = [OpalXMLEvent endDocumentEvent];
// FIXME: theNextEvent should only be nil when an unparsable document is encountered. Riase error?
}
// insert a default start document event when an XML declaration isn't found
if (eventCount == 0 && ![theNextEvent isStartDocument]) {
[eventBuffer addObject:theNextEvent];
theNextEvent = [OpalXMLEvent startDocumentEvent];
}
// clear the current event
[currentEvent release];
currentEvent = [theNextEvent retain];
if ([theNextEvent isStartTag]) {
elementCount += 1;
[stack addObject:currentEvent];
// VALIDATION: What if multiple roots exist in the document?
} else if ([theNextEvent isEndTag]) {
OpalXMLEvent *startTag = [stack lastObject];
if (startTag != nil && [startTag isStartTag]) {
[stack removeLastObject];
// VALIDATION: Does end tag match start tag?
} else {
// VALIDATION: Unmatched end tag?
}
} else if ([theNextEvent isEndDocument]) {
// pop start document off of stack
OpalXMLEvent *startDocument = [stack lastObject];
if (startDocument != nil && [startDocument isStartDocument]) {
[stack removeLastObject];
} else {
// VALIDATION: Unmatched start tag?
}
} else if ([theNextEvent isStartDocument]) {
// add it to the stack
if ([stack count] == 0) {
[stack addObject:currentEvent];
} else {
// VALIDATION: XML Declaration inside document?
}
}
eventCount += 1;
return currentEvent;
}
- (OpalXMLEvent *)peek
{
// return nil after we have seen the end of the document
if (currentEvent && [currentEvent isEndDocument]) return nil;
OpalXMLEvent *peekedEvent = nil;
if ([eventBuffer count] > 0) {
peekedEvent = [eventBuffer objectAtIndex:0];
} else {
peekedEvent = [self readNextEvent];
if (peekedEvent != nil) {
[eventBuffer addObject:peekedEvent];
}
}
return peekedEvent;
}
- (OpalXMLEvent *)consumeNextEvent
{
if ([eventBuffer count] != 0) {
OpalXMLEvent *e = [eventBuffer objectAtIndex:0];
[eventBuffer removeObjectAtIndex:0];
return e;
} else {
return [self readNextEvent];
}
}
- (OpalXMLEvent *)readNextEvent
{
OpalXMLEvent *nextEvent = nil;
[self readNextEventInto:&nextEvent];
return nextEvent;
}
- (BOOL)readNextEventInto:(OpalXMLEvent **)theNextEvent
{
NSString *tagName;
NSDictionary *attributes;
if ([scanner isAtEnd]) {
if (theNextEvent != NULL) {
*theNextEvent = [OpalXMLEvent endDocumentEvent];
}
} else if ([scanner isAtStartTag]) {
[scanner scanStartTagBeginToken];
tagName = [scanner scanName];
[scanner scanWhitespace];
attributes = [scanner scanAttributes];
[scanner scanWhitespace];
[scanner scanTagEndToken];
if (theNextEvent != NULL) {
*theNextEvent = [OpalXMLEvent startTagEventWithTagName:tagName andAttributes:attributes];
}
} else if ([scanner isAtEndTag]) {
[scanner scanEndTagBeginToken];
tagName = [scanner scanName];
[scanner scanWhitespace];
[scanner scanTagEndToken];
if (theNextEvent != NULL) {
*theNextEvent = [OpalXMLEvent endTagEventWithTagName:tagName];
}
} else if ([scanner isAtText]) {
NSString *content = [scanner scanText];
if (theNextEvent != NULL) {
*theNextEvent = [OpalXMLEvent textEventWithContent:content];
}
} else if ([scanner isAtComment]) {
NSString *comment = [scanner scanComment];
if (theNextEvent != NULL) {
*theNextEvent = [OpalXMLEvent commentEventWithContent:comment];
}
} else if ([scanner isAtXMLDeclaration]) {
attributes = [scanner scanXMLDeclaration];
if (theNextEvent != NULL) {
NSString *encoding = [attributes objectForKey:@"encoding"];
NSString *version = [attributes objectForKey:@"version"];
NSString *standaloneValue = [attributes objectForKey:@"standalone"];
BOOL standalone = YES;
if (standaloneValue != nil) {
standalone = [standaloneValue isEqualToString:@"yes"];
}
*theNextEvent = [OpalXMLEvent startDocumentEventWithVersion:version encoding:encoding standalone:standalone];
}
} else {
return NO;
}
return YES;
}
- (OpalXMLEvent *)nextEventOfType:(OpalXMLEventType)eventType
{
OpalXMLEvent *nextEvent;
do {
nextEvent = [self nextEvent];
} while (nextEvent != nil && nextEvent.type != eventType);
return nextEvent;
}
// ===== TAGS ==========================================================================================================
- (OpalXMLEvent *)nextStartTag
{
return [self nextEventOfType:OPAL_START_TAG_EVENT];
}
- (OpalXMLEvent *)skipElementContent
{
if ([self isInElement]) {
NSUInteger startDepth = [self currentDepth];
do {
[self nextEvent];
} while ([self currentDepth] >= startDepth);
return currentEvent;
} else {
return nil;
}
}
// ===== CHARACTER DATA ================================================================================================
- (OpalXMLEvent *)nextContentEvent
{
do { [self nextEvent]; } while (![currentEvent isContent]);
return currentEvent;
}
- (NSMutableString *)readElementText
{
if (![self isInElement]) return nil;
NSMutableString *elementText = [NSMutableString stringWithCapacity:0];
NSUInteger startDepth = self.currentDepth;
OpalXMLEvent *event;
do {
event = [self nextEvent];
if ([event isText]) [elementText appendString:[event content]];
} while (self.currentDepth >= startDepth);
return elementText;
}
@end