forked from itod/parsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLReader.m
executable file
·425 lines (294 loc) · 10.2 KB
/
XMLReader.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#import "XMLReader.h"
//#import <libxml2/libxml/relaxng.h>
#import <libxml/relaxng.h>
@interface NSString (libxml2Support)
+ (id)stringWithXmlChar:(xmlChar *)xc;
- (xmlChar *)xmlChar;
@end
@implementation NSString (libxml2Support)
+ (id)stringWithXmlChar:(xmlChar *)xc {
if (!xc) {
return nil;
}
return [NSString stringWithUTF8String:(char *)xc];
}
- (xmlChar *)xmlChar {
return (unsigned char *)[self UTF8String];
}
@end
@interface XMLReader ()
@property (nonatomic, copy) NSString *path;
@end
@implementation XMLReader
// don't know what this handles. can't get it to fire
static void readerErr(XMLReader *self, const char *msg, xmlParserSeverities severity, xmlTextReaderLocatorPtr locator) {
NSString *str = [NSString stringWithUTF8String:msg];
int line = xmlTextReaderLocatorLineNumber(locator);
NSLog(@"some kinda error! %s, severity: %i, line: %i", msg, severity, line);
switch (severity) {
case XMLReaderSeverityValidityWarning:
[self.errorHandler validityWarning:str lineNumber:line];
break;
case XMLReaderSeverityValidityError:
[self.errorHandler validityError:str lineNumber:line];
break;
case XMLReaderSeverityWarning:
[self.errorHandler warning:str lineNumber:line];
break;
case XMLReaderSeverityError:
[self.errorHandler error:str lineNumber:line];
break;
}
}
// handles well-formedness errors in instance document
// and handles validity errors in instance doc
static void structErr(XMLReader *self, xmlErrorPtr error) {
const char *msg = error->message;
int line = error->line;
int level = error->level;
NSLog(@"Instance doc well-formedness or validity error, level: %i", level);
NSLog(@"message: = %s", msg);
NSLog(@"line: = %i", line);
NSString *str = [NSString stringWithUTF8String:msg];
switch (level) {
case XML_ERR_WARNING:
[self.errorHandler warning:str lineNumber:line];
break;
case XML_ERR_ERROR:
[self.errorHandler error:str lineNumber:line];
break;
case XML_ERR_FATAL:
[self.errorHandler fatalError:str lineNumber:line];
break;
}
}
+ (id)parserWithContentsOfFile:(NSString *)path {
return [[[XMLReader alloc] initWithContentsOfFile:path] autorelease];
}
/*
+ (id)parserWithXMLString:(NSString *)XMLString {
return [[[self alloc] initWithXMLString:XMLStirng] autorelease];
}
*/
- (id)initWithContentsOfFile:(NSString *)newPath {
if (self = [super init]) {
self.path = newPath;
_reader = xmlNewTextReaderFilename([path UTF8String]);
xmlTextReaderSetParserProp(_reader, XML_PARSE_RECOVER, 1);
xmlTextReaderSetParserProp(_reader, XML_PARSE_XINCLUDE, 1);
xmlTextReaderSetErrorHandler(_reader, (xmlTextReaderErrorFunc)readerErr, (void *)self);
xmlTextReaderSetStructuredErrorHandler(_reader, (xmlStructuredErrorFunc)structErr, (void *)self);
}
return self;
}
- (void)dealloc {
self.path = nil;
self.errorHandler = nil;
self.relaxNGSchemaPath = nil;
if (_reader) {
xmlFreeTextReader(_reader);
}
if (_schema) {
xmlRelaxNGFree(_schema);
}
[super dealloc];
}
#pragma mark -
#pragma mark Properties
- (NSInteger)attributeCount {
return xmlTextReaderAttributeCount(_reader);
}
- (NSString *)baseURI {
return [NSString stringWithXmlChar:xmlTextReaderBaseUri(_reader)];
}
//- (BOOL)canResolveEntity {
// return YES;
//}
- (NSInteger)depth {
return xmlTextReaderDepth(_reader);
}
- (BOOL)isEOF {
return XMLReaderReadStateEOF == [self readState];
}
- (BOOL)hasAttributes {
return xmlTextReaderHasAttributes(_reader);
}
- (BOOL)hasValue {
return xmlTextReaderHasValue(_reader);
}
- (BOOL)isDefault {
return xmlTextReaderIsDefault(_reader);
}
- (BOOL)isEmptyElement {
return xmlTextReaderIsEmptyElement(_reader);
}
- (NSString *)localName {
xmlChar *c = xmlTextReaderLocalName(_reader);
if (c) {
return [NSString stringWithXmlChar:c];
}
return nil;
}
- (NSString *)name {
xmlChar *c = xmlTextReaderName(_reader);
if (c) {
return [NSString stringWithXmlChar:c];
}
return nil;
}
- (NSString *)namespaceURI {
xmlChar *c = xmlTextReaderNamespaceUri(_reader);
if (c) {
return [NSString stringWithXmlChar:c];
}
return nil;
}
#pragma mark -
#pragma mark Methods
- (XMLReaderNodeType)nodeType {
return xmlTextReaderNodeType(_reader);
}
- (NSString *)prefix {
return [NSString stringWithXmlChar:xmlTextReaderPrefix(_reader)];
}
- (char)quoteChar {
return xmlTextReaderQuoteChar(_reader);
}
- (XMLReaderReadState)readState {
return xmlTextReaderReadState(_reader);
}
- (NSString *)value {
return [NSString stringWithXmlChar:xmlTextReaderValue(_reader)];
}
- (NSString *)XMLLang {
return [NSString stringWithXmlChar:xmlTextReaderXmlLang(_reader)];
}
- (void)close {
xmlTextReaderClose(_reader);
}
- (NSString *)attributeAtIndex:(NSInteger)index {
return [NSString stringWithXmlChar:xmlTextReaderGetAttributeNo(_reader, index)];
}
- (NSString *)attributeWithQName:(NSString *)qName {
return [NSString stringWithXmlChar:xmlTextReaderGetAttribute(_reader, [qName xmlChar])];
}
- (NSString *)attributeWithLocalName:(NSString *)localName namespaceURI:(NSString *)nsURI {
return [NSString stringWithXmlChar:xmlTextReaderGetAttributeNs(_reader, [localName xmlChar], [nsURI xmlChar])];
}
+ (BOOL)isName:(NSString *)str {
return YES;
}
+ (BOOL)isNameToken:(NSString *)str {
return YES;
}
- (NSString *)lookupNamespace:(NSString *)prefix {
return [NSString stringWithXmlChar:xmlTextReaderLookupNamespace(_reader, [prefix xmlChar])];
}
- (void)moveToAttributeAtIndex:(NSInteger)index {
xmlTextReaderMoveToAttributeNo(_reader, index);
}
- (BOOL)moveToAttributeWithQName:(NSString *)qName {
return xmlTextReaderMoveToAttribute(_reader, [qName xmlChar]);
}
- (BOOL)moveToAttributeWithLocalName:(NSString *)localName namespaceURI:(NSString *)nsURI {
return xmlTextReaderMoveToAttributeNs(_reader, [localName xmlChar], [nsURI xmlChar]);
}
- (BOOL)moveToElement {
return xmlTextReaderMoveToElement(_reader);
}
- (BOOL)moveToFirstAttribute {
return xmlTextReaderMoveToFirstAttribute(_reader);
}
- (BOOL)moveToNextAttribute {
return xmlTextReaderMoveToNextAttribute(_reader);
}
- (BOOL)read {
return xmlTextReaderRead(_reader);
}
- (BOOL)readAttributeValue {
return xmlTextReaderReadAttributeValue(_reader);
}
- (NSString *)readElementString {
return [NSString stringWithXmlChar:xmlTextReaderReadString(_reader)];
}
- (NSString *)readInnerXML {
return [NSString stringWithXmlChar:xmlTextReaderReadInnerXml(_reader)];
}
- (NSString *)readOuterXML {
return [NSString stringWithXmlChar:xmlTextReaderReadOuterXml(_reader)];
}
- (NSString *)readString {
return [NSString stringWithXmlChar:xmlTextReaderReadString(_reader)];
}
- (void)skip {
xmlTextReaderNextSibling(_reader);
}
// handles warnings encountered while parsing RNG schema
static void rngWarn(XMLReader *self, const char *msg, ...) {
va_list ap;
va_start(ap, msg);
NSMutableString *str = [NSMutableString stringWithFormat:[NSString stringWithUTF8String:msg], ap];
NSLog(@"RELAX NG warn: %s", msg);
va_end(ap);
[str replaceOccurrencesOfString:@"<"
withString:@"<"
options:0
range:NSMakeRange(0, [str length])];
str = [NSString stringWithFormat:@"Warning while parsing RELAX NG schema: %s",msg];
[self.errorHandler validityWarning:str lineNumber:-1];
}
// handles errors encountered while parsing RNG schema
static void rngErr(XMLReader *self, const char *msg, ...) {
va_list ap;
va_start(ap, msg);
NSMutableString *str = [NSMutableString stringWithFormat:[NSString stringWithUTF8String:msg], ap];
NSLog(@"RELAX NG err %@",str);
va_end(ap);
[str replaceOccurrencesOfString:@"<"
withString:@"<"
options:0
range:NSMakeRange(0, [str length])];
str = [NSString stringWithFormat:@"Error while parsing RELAX NG schema: <br/><pre>%@</pre>",str];
[self.errorHandler validityError:str lineNumber:-1];
}
- (NSString *)relaxNGSchemaPath {
return [[relaxNGSchemaPath retain] autorelease];
}
- (void)setRelaxNGSchemaPath:(NSString *)newPath {
if (relaxNGSchemaPath != newPath) {
[relaxNGSchemaPath autorelease];
relaxNGSchemaPath = [newPath retain];
const char *schemafurl = [relaxNGSchemaPath UTF8String];
// RELAX NG Parser Context
xmlRelaxNGParserCtxtPtr ctxt = xmlRelaxNGNewParserCtxt(schemafurl);
xmlRelaxNGSetParserErrors(ctxt,
(xmlRelaxNGValidityErrorFunc)rngErr,
(xmlRelaxNGValidityWarningFunc)rngWarn,
(void *)self);
// xmlRelaxNGSetParserStructuredErrors(ctxt, (xmlStructuredErrorFunc)structErr, NULL);
if (_schema) {
xmlRelaxNGFree(_schema);
_schema = NULL;
}
NSLog(@"gonna parse schema");
_schema = xmlRelaxNGParse(ctxt);
NSLog(@"did parse schema");
xmlRelaxNGFreeParserCtxt(ctxt);
if (_reader) {
xmlFreeTextReader(_reader);
}
_reader = xmlNewTextReaderFilename([path UTF8String]);
xmlTextReaderSetParserProp(_reader, XML_PARSE_RECOVER, 1);
xmlTextReaderSetParserProp(_reader, XML_PARSE_XINCLUDE, 1);
xmlTextReaderRelaxNGSetSchema(_reader, _schema);
xmlTextReaderSetErrorHandler(_reader, (xmlTextReaderErrorFunc)readerErr, (void *)self);
xmlTextReaderSetStructuredErrorHandler(_reader, (xmlStructuredErrorFunc)structErr, (void *)self);
}
}
- (BOOL)isValid {
return xmlTextReaderIsValid(_reader);
}
@synthesize path;
@synthesize errorHandler;
@synthesize relaxNGSchemaPath;
@end