︙ | | |
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
-
+
-
-
+
+
|
return [OFString stringWithCString: buf
length: i];
}
@implementation OFString (XMLUnescaping)
- (OFString*)stringByXMLUnescaping
{
return [self stringByXMLUnescapingWithHandler: nil];
return [self stringByXMLUnescapingWithDelegate: nil];
}
- (OFString*)stringByXMLUnescapingWithHandler:
(OFObject <OFStringXMLUnescapingDelegate>*)h
- (OFString*)stringByXMLUnescapingWithDelegate:
(OFObject <OFStringXMLUnescapingDelegate>*)delegate
{
size_t i, last;
BOOL in_entity;
OFMutableString *ret;
last = 0;
in_entity = NO;
|
︙ | | |
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
-
+
+
-
+
|
if (tmp == nil)
@throw [OFInvalidEncodingException
newWithClass: isa];
[ret appendString: tmp];
[pool release];
} else if (h != nil) {
} else if (delegate != nil) {
OFAutoreleasePool *pool;
OFString *n, *tmp;
pool = [[OFAutoreleasePool alloc] init];
n = [OFString stringWithCString: entity
length: len];
tmp = [delegate string: self
tmp = [h didFindUnknownEntityNamed: n];
containsUnknownEntityNamed: n];
if (tmp == nil)
@throw [OFInvalidEncodingException
newWithClass: isa];
[ret appendString: tmp];
[pool release];
|
︙ | | |
︙ | | |
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
-
+
-
+
|
#import "OFXMLAttribute.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"
static OF_INLINE OFString*
transform_string(OFMutableString *cache,
OFObject <OFStringXMLUnescapingDelegate> *handler)
OFObject <OFStringXMLUnescapingDelegate> *delegate)
{
[cache replaceOccurrencesOfString: @"\r\n"
withString: @"\n"];
[cache replaceOccurrencesOfString: @"\r"
withString: @"\n"];
return [cache stringByXMLUnescapingWithHandler: handler];
return [cache stringByXMLUnescapingWithDelegate: delegate];
}
static OF_INLINE OFString*
namespace_for_prefix(OFString *prefix, OFArray *namespaces)
{
OFDictionary **carray = [namespaces cArray];
ssize_t i;
|
︙ | | |
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
-
-
+
+
|
length: len];
if ([cache cStringLength] > 0) {
OFString *str;
pool = [[OFAutoreleasePool alloc] init];
str = transform_string(cache, self);
[delegate xmlParser: self
didFindString: str];
[delegate parser: self
foundCharacters: str];
[pool release];
}
[cache setToCString: ""];
last = i + 1;
state = OF_XMLPARSER_TAG_OPENED;
|
︙ | | |
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
|
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
|
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
|
if (prefix != nil && ns == nil)
@throw
[OFUnboundNamespaceException
newWithClass: isa
prefix: prefix];
[delegate xmlParser: self
didStartTagWithName: name
prefix: prefix
namespace: ns
attributes: nil];
[delegate parser: self
didStartElement: name
withPrefix: prefix
namespace: ns
attributes: nil];
if (buf[i] == '/')
[delegate xmlParser: self
didEndTagWithName: name
prefix: prefix
namespace: ns];
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
else
[previous addObject:
[[cache copy] autorelease]];
[pool release];
[name release];
|
︙ | | |
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
-
-
-
-
+
+
+
+
|
ns = namespace_for_prefix(prefix, namespaces);
if (prefix != nil && ns == nil)
@throw [OFUnboundNamespaceException
newWithClass: isa
prefix: prefix];
[namespaces removeNObjects: 1];
[delegate xmlParser: self
didEndTagWithName: name
prefix: prefix
namespace: ns];
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
[pool release];
[name release];
[prefix release];
name = prefix = nil;
|
︙ | | |
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
|
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
|
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
|
ns = namespace_for_prefix(prefix, namespaces);
if (prefix != nil && ns == nil)
@throw [OFUnboundNamespaceException
newWithClass: isa
prefix: prefix];
[delegate xmlParser: self
didStartTagWithName: name
prefix: prefix
namespace: ns
attributes: attrs];
[delegate parser: self
didStartElement: name
withPrefix: prefix
namespace: ns
attributes: attrs];
if (buf[i] == '/') {
[delegate xmlParser: self
didEndTagWithName: name
prefix: prefix
namespace: ns];
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
[namespaces removeNObjects: 1];
} else if (prefix != nil) {
OFString *str = [OFString
stringWithFormat: @"%s:%s",
[prefix cString],
[name cString]];
[previous addObject: str];
|
︙ | | |
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
-
-
+
+
|
appendCStringWithoutUTF8Checking: buf + last
length: i - last];
cdata = [[cache mutableCopy] autorelease];
len = [cdata length];
[cdata removeCharactersFromIndex: len - 2
toIndex: len];
[delegate xmlParser: self
didFindString: cdata];
[delegate parser: self
foundCDATA: cdata];
[pool release];
[cache setToCString: ""];
last = i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else if (buf[i] != ']')
|
︙ | | |
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
-
-
+
+
|
appendCStringWithoutUTF8Checking: buf + last
length: i - last];
comment = [[cache mutableCopy] autorelease];
len = [comment length];
[comment removeCharactersFromIndex: len - 2
toIndex: len];
[delegate xmlParser: self
didFindComment: comment];
[delegate parser: self
foundComment: comment];
[pool release];
[cache setToCString: ""];
last = i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else
|
︙ | | |
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
|
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
|
+
-
+
-
-
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
-
-
+
+
|
len = size - last;
/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
if (len > 0 && state != OF_XMLPARSER_IN_TAG)
[cache appendCStringWithoutUTF8Checking: buf + last
length: len];
}
- (OFString*)string: (OFString*)string
- (OFString*)didFindUnknownEntityNamed: (OFString*)entity
containsUnknownEntityNamed: (OFString*)entity
{
return [delegate xmlParser: self
didFindUnknownEntityNamed: entity];
return [delegate parser: self
foundUnknownEntityNamed: entity];
}
@end
@implementation OFObject (OFXMLParserDelegate)
- (void)xmlParser: (OFXMLParser*)parser
didStartTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
- (void)parser: (OFXMLParser*)parser
didStartElement: (OFString*)name
withPrefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
{
}
- (void)xmlParser: (OFXMLParser*)parser
didEndTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
- (void)parser: (OFXMLParser*)parser
didEndElement: (OFString*)name
withPrefix: (OFString*)prefix
namespace: (OFString*)ns
{
}
- (void)xmlParser: (OFXMLParser*)parser
didFindString: (OFString*)string
- (void)parser: (OFXMLParser*)parser
foundCharacters: (OFString*)string
{
}
- (void)xmlParser: (OFXMLParser*)parser
didFindComment: (OFString*)comment
- (void)parser: (OFXMLParser*)parser
foundCDATA: (OFString*)cdata
{
}
- (void)parser: (OFXMLParser*)parser
foundComment: (OFString*)comment
{
}
- (OFString*)xmlParser: (OFXMLParser*)parser
didFindUnknownEntityNamed: (OFString*)entity
- (OFString*)parser: (OFXMLParser*)parser
foundUnknownEntityNamed: (OFString*)entity
{
return nil;
}
@end
|
︙ | | |
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
+
-
+
|
@" \t\t \t\t \t \t"
};
@interface EntityHandler: OFObject <OFStringXMLUnescapingDelegate>
@end
@implementation EntityHandler
- (OFString*)string: (OFString*)string
- (OFString*)didFindUnknownEntityNamed: (OFString*)entity
containsUnknownEntityNamed: (OFString*)entity
{
if ([entity isEqual: @"foo"])
return @"bar";
return nil;
}
@end
|
︙ | | |
353
354
355
356
357
358
359
360
361
362
363
364
365
|
354
355
356
357
358
359
360
361
362
363
364
365
366
|
-
+
|
EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] "
@"#5", OFInvalidEncodingException, [@"&#g;" stringByXMLUnescaping])
EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] "
@"#6", OFInvalidEncodingException, [@"&#xg;" stringByXMLUnescaping])
TEST(@"-[stringByXMLUnescapingWithHandler:]",
(h = [[[EntityHandler alloc] init] autorelease]) &&
[[@"x&foo;y" stringByXMLUnescapingWithHandler: h]
[[@"x&foo;y" stringByXMLUnescapingWithDelegate: h]
isEqual: @"xbary"])
[pool drain];
}
@end
|
︙ | | |
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
|
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
|
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
+
|
static OFString *module = @"OFXMLParser";
static int i = 0;
enum event_type {
TAG_START,
TAG_END,
STRING,
CDATA,
COMMENT
};
@implementation TestsAppDelegate (OFXMLParser)
- (void)xmlParserCallbackWithEventType: (enum event_type)et
name: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
string: (OFString*)string
comment: (OFString*)comment
- (void)parserCallbackWithEventType: (enum event_type)et
name: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
string: (OFString*)string
comment: (OFString*)comment
{
OFString *msg;
i++;
msg = [OFString stringWithFormat: @"Parsing part #%d", i];
switch (i) {
case 1:
TEST(msg, et == TAG_START && [name isEqual: @"root"] &&
prefix == nil && ns == nil && [attrs count] == 0)
break;
case 2:
TEST(msg, et == STRING && [string isEqual: @"\n "])
break;
case 3:
TEST(msg, et == STRING && [string isEqual: @"f<oo"])
TEST(msg, et == CDATA && [string isEqual: @"f<oo"])
break;
case 4:
TEST(msg, et == TAG_START && [name isEqual: @"bar"] &&
prefix == nil && ns == nil && attrs == nil)
break;
case 5:
TEST(msg, et == TAG_END && [name isEqual: @"bar"] &&
|
︙ | | |
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
|
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
|
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
+
|
case 30:
TEST(msg, et == TAG_END && [name isEqual: @"root"] &&
prefix == nil && ns == nil);
break;
}
}
- (void)xmlParser: (OFXMLParser*)parser
didStartTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
- (void)parser: (OFXMLParser*)parser
didStartElement: (OFString*)name
withPrefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
{
[self xmlParserCallbackWithEventType: TAG_START
name: name
prefix: prefix
namespace: ns
attributes: attrs
string: nil
comment: nil];
[self parserCallbackWithEventType: TAG_START
name: name
prefix: prefix
namespace: ns
attributes: attrs
string: nil
comment: nil];
}
- (void)xmlParser: (OFXMLParser*)parser
didEndTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
- (void)parser: (OFXMLParser*)parser
didEndElement: (OFString*)name
withPrefix: (OFString*)prefix
namespace: (OFString*)ns
{
[self xmlParserCallbackWithEventType: TAG_END
name: name
prefix: prefix
namespace: ns
attributes: nil
string: nil
comment: nil];
[self parserCallbackWithEventType: TAG_END
name: name
prefix: prefix
namespace: ns
attributes: nil
string: nil
comment: nil];
}
- (void)xmlParser: (OFXMLParser*)parser
didFindString: (OFString*)string
- (void)parser: (OFXMLParser*)parser
foundCharacters: (OFString*)string
{
[self xmlParserCallbackWithEventType: STRING
name: nil
prefix: nil
namespace: nil
attributes: nil
string: string
comment: nil];
[self parserCallbackWithEventType: STRING
name: nil
prefix: nil
namespace: nil
attributes: nil
string: string
comment: nil];
}
- (void)xmlParser: (OFXMLParser*)parser
didFindComment: (OFString*)comment
- (void)parser: (OFXMLParser*)parser
foundCDATA: (OFString*)cdata
{
[self parserCallbackWithEventType: CDATA
name: nil
prefix: nil
namespace: nil
attributes: nil
string: cdata
comment: nil];
}
- (void)parser: (OFXMLParser*)parser
foundComment: (OFString*)comment
{
[self xmlParserCallbackWithEventType: COMMENT
name: nil
prefix: nil
namespace: nil
attributes: nil
string: nil
comment: comment];
[self parserCallbackWithEventType: COMMENT
name: nil
prefix: nil
namespace: nil
attributes: nil
string: nil
comment: comment];
}
- (OFString*)xmlParser: (OFXMLParser*)parser
didFindUnknownEntityNamed: (OFString*)entity
- (OFString*)parser: (OFXMLParser*)parser
foundUnknownEntityNamed: (OFString*)entity
{
if ([entity isEqual: @"foo"])
return @"foobar";
return nil;
}
|
︙ | | |