70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
prefix: attr_prefix];
[attr->ns release];
attr->ns = [attr_ns retain];
}
@implementation OFXMLParser
+ (void)initialize
{
size_t i;
const SEL sels[] = {
@selector(_parseOutsideTagWithBuffer:i:last:),
@selector(_parseTagOpenedWithBuffer:i:last:),
@selector(_parseInPrologWithBuffer:i:last:),
@selector(_parseInTagNameWithBuffer:i:last:),
@selector(_parseInCloseTagNameWithBuffer:i:last:),
@selector(_parseInTagWithBuffer:i:last:),
@selector(_parseInAttributeNameWithBuffer:i:last:),
@selector(_parseExpectDelimiterWithBuffer:i:last:),
@selector(_parseInAttributeValueWithBuffer:i:last:),
@selector(_parseExpectCloseWithBuffer:i:last:),
@selector(_parseExpectSpaceOrCloseWithBuffer:i:last:),
@selector(_parseInCDATAOrCommentWithBuffer:i:last:),
@selector(_parseInCDATAOpening1WithBuffer:i:last:),
@selector(_parseInCDATAOpening2WithBuffer:i:last:),
@selector(_parseInCDATAOpening3WithBuffer:i:last:),
@selector(_parseInCDATAOpening4WithBuffer:i:last:),
@selector(_parseInCDATAOpening5WithBuffer:i:last:),
@selector(_parseInCDATAOpening6WithBuffer:i:last:),
@selector(_parseInCDATA1WithBuffer:i:last:),
@selector(_parseInCDATA2WithBuffer:i:last:),
@selector(_parseInCDATA3WithBuffer:i:last:),
@selector(_parseInCommentOpeningWithBuffer:i:last:),
@selector(_parseInComment1WithBuffer:i:last:),
@selector(_parseInComment2WithBuffer:i:last:),
@selector(_parseInComment3WithBuffer:i:last:)
};
memcpy(selectors, sels, sizeof(sels));
for (i = 0; i < OF_XMLPARSER_NUM_STATES; i++) {
if (![self instancesRespondToSelector: selectors[i]])
@throw [OFInitializationFailedException
newWithClass: self];
lookup_table[i] = (state_function)
[self instanceMethodForSelector: selectors[i]];
}
}
+ parser
{
return [[[self alloc] init] autorelease];
}
- init
{
|
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
|
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
|
-
-
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
-
+
-
-
+
+
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
-
+
+
-
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
+
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
-
-
+
+
+
-
+
-
-
-
-
+
+
+
-
-
+
-
+
-
-
+
+
-
-
+
-
-
-
-
-
+
+
+
+
+
-
+
-
-
+
+
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
+
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
-
+
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
-
+
-
-
-
+
+
+
-
-
+
-
+
-
-
+
+
-
-
+
-
-
-
-
+
+
+
+
-
+
-
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
-
+
-
-
+
+
-
-
+
-
-
+
+
-
-
+
-
-
-
-
-
+
+
+
+
+
-
+
-
-
+
+
-
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
+
-
-
+
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
+
-
-
+
+
-
+
-
-
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
-
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
-
-
+
-
-
+
+
-
+
-
-
+
-
-
-
-
+
+
+
-
-
+
-
-
+
+
-
+
-
-
-
+
+
-
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
+
+
+
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
|
unknownEntityHandler = block;
}
#endif
- (void)parseBuffer: (const char*)buf
withSize: (size_t)size
{
OFAutoreleasePool *pool;
size_t i, last, len;
size_t i, last = 0;
for (i = 0; i < size; i++)
lookup_table[state](self, selectors[state], buf, &i, &last);
last = 0;
for (i = 0; i < size; i++) {
switch (state) {
/* Not in a tag */
case OF_XMLPARSER_OUTSIDE_TAG:
if (buf[i] == '<') {
/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
if (size - last > 0 && state != OF_XMLPARSER_IN_TAG)
[cache appendCStringWithoutUTF8Checking: buf + last
length: size - last];
}
/*
* The following methods handle the different states of the parser. They are
* lookup up in +[initialize] and put in a lookup table to speed things up.
* One dispatch for every character would be way too slow!
*/
/* Not in a tag */
- (void)_parseOutsideTagWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
size_t len;
if (buf[*i] != '<')
len = i - last;
return;
if (len > 0)
[cache appendCStringWithoutUTF8Checking:
if ((len = *i - *last) > 0)
[cache appendCStringWithoutUTF8Checking: buf + *last
buf + last
length: len];
length: len];
if ([cache cStringLength] > 0) {
OFString *str;
if ([cache cStringLength] > 0) {
OFString *str;
OFAutoreleasePool *pool;
pool = [[OFAutoreleasePool alloc] init];
str = transform_string(cache, self);
pool = [[OFAutoreleasePool alloc] init];
str = transform_string(cache, self);
#ifdef OF_HAVE_BLOCKS
if (charactersHandler != nil)
charactersHandler(self, str);
else
if (charactersHandler != nil)
charactersHandler(self, str);
else
#endif
[delegate parser: self
foundCharacters: str];
[delegate parser: self
foundCharacters: str];
[pool release];
}
[pool release];
}
[cache setToCString: ""];
[cache setToCString: ""];
last = i + 1;
state = OF_XMLPARSER_TAG_OPENED;
}
*last = *i + 1;
state = OF_XMLPARSER_TAG_OPENED;
}
break;
/* Tag was just opened */
case OF_XMLPARSER_TAG_OPENED:
switch (buf[i]) {
case '?':
last = i + 1;
state = OF_XMLPARSER_IN_PROLOG;
break;
case '/':
last = i + 1;
state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
break;
case '!':
last = i + 1;
state = OF_XMLPARSER_IN_CDATA_OR_COMMENT;
break;
default:
state = OF_XMLPARSER_IN_TAG_NAME;
i--;
break;
}
break;
/* Tag was just opened */
- (void)_parseTagOpenedWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
switch (buf[*i]) {
case '?':
*last = *i + 1;
state = OF_XMLPARSER_IN_PROLOG;
break;
case '/':
*last = *i + 1;
state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
break;
case '!':
*last = *i + 1;
state = OF_XMLPARSER_IN_CDATA_OR_COMMENT;
break;
default:
state = OF_XMLPARSER_IN_TAG_NAME;
(*i)--;
break;
}
}
/* Inside prolog */
- (void)_parseInPrologWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
/* Inside prolog */
case OF_XMLPARSER_IN_PROLOG:
last = i + 1;
if (buf[i] == '?')
state = OF_XMLPARSER_EXPECT_CLOSE;
*last = *i + 1;
if (buf[*i] == '?')
state = OF_XMLPARSER_EXPECT_CLOSE;
break;
/* Inside a tag, no name yet */
case OF_XMLPARSER_IN_TAG_NAME:
if (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\r' ||
buf[i] == '>' || buf[i] == '/') {
const char *cache_c, *tmp;
size_t cache_len;
}
/* Inside a tag, no name yet */
- (void)_parseInTagNameWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
const char *cache_c, *tmp;
size_t len, cache_len;
if (buf[*i] != ' ' && buf[*i] != '\n' && buf[*i] != '\r' &&
buf[*i] != '>' && buf[*i] != '/')
return;
len = i - last;
if (len > 0)
[cache appendCStringWithoutUTF8Checking:
if ((len = *i - *last) > 0)
[cache appendCStringWithoutUTF8Checking: buf + *last
buf + last
length: len];
cache_c = [cache cString];
cache_len = [cache cStringLength];
length: len];
cache_c = [cache cString];
cache_len = [cache cStringLength];
if ((tmp = memchr(cache_c, ':',
if ((tmp = memchr(cache_c, ':', cache_len)) != NULL) {
cache_len)) != NULL) {
name = [[OFString alloc]
name = [[OFString alloc] initWithCString: tmp + 1
initWithCString: tmp + 1
length: cache_len - (tmp -
cache_c) - 1];
prefix = [[OFString alloc]
length: cache_len -
(tmp - cache_c) - 1];
prefix = [[OFString alloc] initWithCString: cache_c
initWithCString: cache_c
length: tmp - cache_c];
} else {
name = [cache copy];
prefix = nil;
}
length: tmp - cache_c];
} else {
name = [cache copy];
prefix = nil;
}
if (buf[i] == '>' || buf[i] == '/') {
OFString *ns;
if (buf[*i] == '>' || buf[*i] == '/') {
OFAutoreleasePool *pool;
OFString *ns;
ns = namespace_for_prefix(prefix,
ns = namespace_for_prefix(prefix, namespaces);
namespaces);
if (prefix != nil && ns == nil)
@throw
[OFUnboundNamespaceException
if (prefix != nil && ns == nil)
@throw
[OFUnboundNamespaceException newWithClass: isa
newWithClass: isa
prefix: prefix];
prefix: prefix];
pool = [[OFAutoreleasePool alloc] init];
pool = [[OFAutoreleasePool alloc] init];
#ifdef OF_HAVE_BLOCKS
if (elementStartHandler != nil)
elementStartHandler(self, name,
if (elementStartHandler != nil)
elementStartHandler(self, name, prefix, ns, nil);
prefix, ns, nil);
else
else
#endif
[delegate parser: self
didStartElement: name
withPrefix: prefix
namespace: ns
attributes: nil];
[delegate parser: self
didStartElement: name
withPrefix: prefix
namespace: ns
attributes: nil];
if (buf[i] == '/') {
if (buf[*i] == '/') {
#ifdef OF_HAVE_BLOCKS
if (elementEndHandler != nil)
elementEndHandler(self,
if (elementEndHandler != nil)
elementEndHandler(self, name, prefix, ns);
name, prefix, ns);
else
else
#endif
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
} else
[previous addObject:
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
} else
[previous addObject: [[cache copy] autorelease]];
[[cache copy] autorelease]];
[pool release];
[pool release];
[name release];
[prefix release];
name = prefix = nil;
[name release];
[prefix release];
name = prefix = nil;
state = (buf[i] == '/'
? OF_XMLPARSER_EXPECT_CLOSE
: OF_XMLPARSER_OUTSIDE_TAG);
} else
state = OF_XMLPARSER_IN_TAG;
state = (buf[*i] == '/'
? OF_XMLPARSER_EXPECT_CLOSE
: OF_XMLPARSER_OUTSIDE_TAG);
} else
state = OF_XMLPARSER_IN_TAG;
if (buf[i] != '/') {
pool = [[OFAutoreleasePool alloc] init];
[namespaces addObject:
if (buf[*i] != '/') {
OFAutoreleasePool *pool;
pool = [[OFAutoreleasePool alloc] init];
[namespaces addObject: [OFMutableDictionary dictionary]];
[OFMutableDictionary dictionary]];
[pool release];
}
[pool release];
}
[cache setToCString: ""];
last = i + 1;
}
[cache setToCString: ""];
*last = *i + 1;
}
break;
/* Inside a close tag, no name yet */
case OF_XMLPARSER_IN_CLOSE_TAG_NAME:
if (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\r' ||
buf[i] == '>') {
const char *cache_c, *tmp;
size_t cache_len;
OFString *ns;
/* Inside a close tag, no name yet */
- (void)_parseInCloseTagNameWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
OFAutoreleasePool *pool;
const char *cache_c, *tmp;
size_t len, cache_len;
OFString *ns;
if (buf[*i] != ' ' && buf[*i] != '\n' && buf[*i] != '\r' &&
buf[*i] != '>')
return;
len = i - last;
if (len > 0)
[cache appendCStringWithoutUTF8Checking:
if ((len = *i - *last) > 0)
[cache appendCStringWithoutUTF8Checking: buf + *last
buf + last
length: len];
cache_c = [cache cString];
cache_len = [cache cStringLength];
length: len];
cache_c = [cache cString];
cache_len = [cache cStringLength];
if ((tmp = memchr(cache_c, ':',
if ((tmp = memchr(cache_c, ':', cache_len)) != NULL) {
cache_len)) != NULL) {
name = [[OFString alloc]
name = [[OFString alloc] initWithCString: tmp + 1
initWithCString: tmp + 1
length: cache_len - (tmp -
cache_c) - 1];
prefix = [[OFString alloc]
length: cache_len -
(tmp - cache_c) - 1];
prefix = [[OFString alloc] initWithCString: cache_c
initWithCString: cache_c
length: tmp - cache_c];
} else {
name = [cache copy];
prefix = nil;
}
length: tmp - cache_c];
} else {
name = [cache copy];
prefix = nil;
}
if (![[previous lastObject] isEqual: cache])
@throw [OFMalformedXMLException
newWithClass: isa];
[previous removeNObjects: 1];
if (![[previous lastObject] isEqual: cache])
@throw [OFMalformedXMLException newWithClass: isa];
[previous removeNObjects: 1];
[cache setToCString: ""];
[cache setToCString: ""];
ns = namespace_for_prefix(prefix, namespaces);
if (prefix != nil && ns == nil)
@throw [OFUnboundNamespaceException
ns = namespace_for_prefix(prefix, namespaces);
if (prefix != nil && ns == nil)
@throw [OFUnboundNamespaceException newWithClass: isa
newWithClass: isa
prefix: prefix];
prefix: prefix];
pool = [[OFAutoreleasePool alloc] init];
pool = [[OFAutoreleasePool alloc] init];
#ifdef OF_HAVE_BLOCKS
if (elementEndHandler != nil)
elementEndHandler(self, name, prefix,
if (elementEndHandler != nil)
elementEndHandler(self, name, prefix, ns);
ns);
else
else
#endif
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
[pool release];
[pool release];
[namespaces removeNObjects: 1];
[name release];
[prefix release];
name = prefix = nil;
[namespaces removeNObjects: 1];
[name release];
[prefix release];
name = prefix = nil;
last = i + 1;
state = (buf[i] == '>'
? OF_XMLPARSER_OUTSIDE_TAG
: OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE);
}
*last = *i + 1;
state = (buf[*i] == '>'
? OF_XMLPARSER_OUTSIDE_TAG
: OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE);
}
break;
/* Inside a tag, name found */
case OF_XMLPARSER_IN_TAG:
if (buf[i] == '>' || buf[i] == '/') {
OFString *ns;
OFXMLAttribute **attrs_c = [attrs cArray];
size_t j, attrs_cnt = [attrs count];
/* Inside a tag, name found */
- (void)_parseInTagWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
OFAutoreleasePool *pool;
OFString *ns;
OFXMLAttribute **attrs_c;
size_t j, attrs_cnt;
if (buf[*i] != '>' && buf[*i] != '/') {
if (buf[*i] != ' ' && buf[*i] != '\n' && buf[*i] != '\r') {
*last = *i;
state = OF_XMLPARSER_IN_ATTR_NAME;
(*i)--;
}
return;
}
attrs_c = [attrs cArray];
attrs_cnt = [attrs count];
ns = namespace_for_prefix(prefix, namespaces);
ns = namespace_for_prefix(prefix, namespaces);
if (prefix != nil && ns == nil)
@throw [OFUnboundNamespaceException
if (prefix != nil && ns == nil)
@throw [OFUnboundNamespaceException newWithClass: isa
newWithClass: isa
prefix: prefix];
prefix: prefix];
for (j = 0; j < attrs_cnt; j++)
resolve_attr_namespace(attrs_c[j],
for (j = 0; j < attrs_cnt; j++)
resolve_attr_namespace(attrs_c[j], prefix, ns, namespaces, isa);
prefix, ns, namespaces, isa);
pool = [[OFAutoreleasePool alloc] init];
pool = [[OFAutoreleasePool alloc] init];
#ifdef OF_HAVE_BLOCKS
if (elementStartHandler != nil)
elementStartHandler(self, name, prefix,
if (elementStartHandler != nil)
elementStartHandler(self, name, prefix, ns, attrs);
ns, attrs);
else
else
#endif
[delegate parser: self
didStartElement: name
withPrefix: prefix
namespace: ns
attributes: attrs];
[delegate parser: self
didStartElement: name
withPrefix: prefix
namespace: ns
attributes: attrs];
if (buf[i] == '/') {
if (buf[*i] == '/') {
#ifdef OF_HAVE_BLOCKS
if (elementEndHandler != nil)
elementEndHandler(self, name,
if (elementEndHandler != nil)
elementEndHandler(self, name, prefix, ns);
prefix, ns);
else
else
#endif
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
[namespaces removeNObjects: 1];
} else if (prefix != nil) {
OFString *str = [OFString
[delegate parser: self
didEndElement: name
withPrefix: prefix
namespace: ns];
[namespaces removeNObjects: 1];
} else if (prefix != nil) {
OFString *str = [OFString stringWithFormat: @"%s:%s",
stringWithFormat: @"%s:%s",
[prefix cString],
[name cString]];
[previous addObject: str];
} else
[previous addObject: name];
[prefix cString],
[name cString]];
[previous addObject: str];
} else
[previous addObject: name];
[pool release];
[pool release];
[name release];
[prefix release];
[attrs release];
name = prefix = nil;
attrs = nil;
[name release];
[prefix release];
[attrs release];
name = prefix = nil;
attrs = nil;
last = i + 1;
state = (buf[i] == '/'
? OF_XMLPARSER_EXPECT_CLOSE
: OF_XMLPARSER_OUTSIDE_TAG);
*last = *i + 1;
state = (buf[*i] == '/'
? OF_XMLPARSER_EXPECT_CLOSE
: OF_XMLPARSER_OUTSIDE_TAG);
} else if (buf[i] != ' ' && buf[i] != '\n' &&
buf[i] != '\r') {
last = i;
state = OF_XMLPARSER_IN_ATTR_NAME;
i--;
}
}
break;
/* Looking for attribute name */
/* Looking for attribute name */
case OF_XMLPARSER_IN_ATTR_NAME:
if (buf[i] == '=') {
const char *cache_c, *tmp;
size_t cache_len;
- (void)_parseInAttributeNameWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
const char *cache_c, *tmp;
size_t len, cache_len;
if (buf[*i] != '=')
return;
len = i - last;
if (len > 0)
[cache appendCStringWithoutUTF8Checking:
if ((len = *i - *last) > 0)
[cache appendCStringWithoutUTF8Checking: buf + *last
buf + last
length: len];
length: len];
cache_c = [cache cString];
cache_len = [cache cStringLength];
cache_c = [cache cString];
cache_len = [cache cStringLength];
if ((tmp = memchr(cache_c, ':',
if ((tmp = memchr(cache_c, ':', cache_len)) != NULL ) {
cache_len)) != NULL ) {
attrName = [[OFString alloc]
attrName = [[OFString alloc] initWithCString: tmp + 1
initWithCString: tmp + 1
length: cache_len - (tmp -
cache_c) - 1];
attrPrefix = [[OFString alloc]
length: cache_len -
(tmp - cache_c) -
1];
attrPrefix = [[OFString alloc] initWithCString: cache_c
initWithCString: cache_c
length: tmp - cache_c];
} else {
attrName = [cache copy];
attrPrefix = nil;
}
length: tmp - cache_c];
} else {
attrName = [cache copy];
attrPrefix = nil;
}
[cache setToCString: ""];
[cache setToCString: ""];
last = i + 1;
state = OF_XMLPARSER_EXPECT_DELIM;
}
*last = *i + 1;
state = OF_XMLPARSER_EXPECT_DELIM;
}
break;
/* Expecting delimiter */
case OF_XMLPARSER_EXPECT_DELIM:
if (buf[i] != '\'' && buf[i] != '"')
@throw [OFMalformedXMLException
/* Expecting delimiter */
- (void)_parseExpectDelimiterWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] != '\'' && buf[*i] != '"')
@throw [OFMalformedXMLException newWithClass: isa];
newWithClass: isa];
delim = buf[i];
last = i + 1;
state = OF_XMLPARSER_IN_ATTR_VALUE;
delim = buf[*i];
*last = *i + 1;
state = OF_XMLPARSER_IN_ATTR_VALUE;
break;
/* Looking for attribute value */
}
/* Looking for attribute value */
case OF_XMLPARSER_IN_ATTR_VALUE:
if (buf[i] == delim) {
OFString *attr_val;
- (void)_parseInAttributeValueWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
OFAutoreleasePool *pool;
OFString *attr_val;
size_t len;
if (buf[*i] != delim)
return;
len = i - last;
if (len > 0)
[cache appendCStringWithoutUTF8Checking:
if ((len = *i - *last) > 0)
[cache appendCStringWithoutUTF8Checking: buf + *last
buf + last
length: len];
length: len];
pool = [[OFAutoreleasePool alloc] init];
attr_val = transform_string(cache, self);
pool = [[OFAutoreleasePool alloc] init];
attr_val = transform_string(cache, self);
if (attrPrefix == nil &&
if (attrPrefix == nil && [attrName isEqual: @"xmlns"])
[attrName isEqual: @"xmlns"])
[[namespaces lastObject]
[[namespaces lastObject] setObject: attr_val
setObject: attr_val
forKey: @""];
if ([attrPrefix isEqual: @"xmlns"])
[[namespaces lastObject]
forKey: @""];
if ([attrPrefix isEqual: @"xmlns"])
[[namespaces lastObject] setObject: attr_val
setObject: attr_val
forKey: attrName];
forKey: attrName];
if (attrs == nil)
attrs = [[OFMutableArray alloc] init];
if (attrs == nil)
attrs = [[OFMutableArray alloc] init];
[attrs addObject: [OFXMLAttribute
[attrs addObject: [OFXMLAttribute attributeWithName: attrName
attributeWithName: attrName
namespace: attrPrefix
stringValue: attr_val]];
namespace: attrPrefix
stringValue: attr_val]];
[pool release];
[pool release];
[cache setToCString: ""];
[attrName release];
[attrPrefix release];
attrName = attrPrefix = nil;
[cache setToCString: ""];
[attrName release];
[attrPrefix release];
attrName = attrPrefix = nil;
last = i + 1;
state = OF_XMLPARSER_IN_TAG;
}
*last = *i + 1;
state = OF_XMLPARSER_IN_TAG;
}
break;
/* Expecting closing '>' */
case OF_XMLPARSER_EXPECT_CLOSE:
if (buf[i] == '>') {
last = i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else
@throw [OFMalformedXMLException
/* Expecting closing '>' */
- (void)_parseExpectCloseWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == '>') {
*last = *i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else
@throw [OFMalformedXMLException newWithClass: isa];
newWithClass: isa];
break;
/* Expecting closing '>' or space */
}
/* Expecting closing '>' or space */
case OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE:
if (buf[i] == '>') {
last = i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else if (buf[i] != ' ' && buf[i] != '\n' &&
buf[i] != '\r')
@throw [OFMalformedXMLException
newWithClass: isa];
break;
/* CDATA or comment */
case OF_XMLPARSER_IN_CDATA_OR_COMMENT:
if (buf[i] == '-')
- (void)_parseExpectSpaceOrCloseWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == '>') {
*last = *i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else if (buf[*i] != ' ' && buf[*i] != '\n' && buf[*i] != '\r')
@throw [OFMalformedXMLException newWithClass: isa];
}
/* CDATA or comment */
- (void)_parseInCDATAOrCommentWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == '-')
state = OF_XMLPARSER_IN_COMMENT_OPENING;
else if (buf[*i] == '[')
state = OF_XMLPARSER_IN_CDATA_OPENING_1;
else
@throw [OFMalformedXMLException newWithClass: isa];
*last = *i + 1;
}
/* CDATA */
- (void)_parseInCDATAOpening1WithBuffer: (const char*)buf
i: (size_t*)i
state = OF_XMLPARSER_IN_COMMENT_OPENING;
else if (buf[i] == '[')
state = OF_XMLPARSER_IN_CDATA_OPENING_1;
else
@throw [OFMalformedXMLException
last: (size_t*)last
{
if (buf[*i] == 'C')
state = OF_XMLPARSER_IN_CDATA_OPENING_2;
else
@throw [OFMalformedXMLException newWithClass: isa];
newWithClass: isa];
last = i + 1;
break;
*last = *i + 1;
}
- (void)_parseInCDATAOpening2WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
/* CDATA */
case OF_XMLPARSER_IN_CDATA_OPENING_1:
if (buf[i] == 'C')
state = OF_XMLPARSER_IN_CDATA_OPENING_2;
else
@throw [OFMalformedXMLException
newWithClass: isa];
last = i + 1;
break;
case OF_XMLPARSER_IN_CDATA_OPENING_2:
if (buf[i] == 'D')
state = OF_XMLPARSER_IN_CDATA_OPENING_3;
else
@throw [OFMalformedXMLException
newWithClass: isa];
last = i + 1;
break;
case OF_XMLPARSER_IN_CDATA_OPENING_3:
if (buf[i] == 'A')
if (buf[*i] == 'D')
state = OF_XMLPARSER_IN_CDATA_OPENING_3;
else
@throw [OFMalformedXMLException newWithClass: isa];
*last = *i + 1;
}
- (void)_parseInCDATAOpening3WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == 'A')
state = OF_XMLPARSER_IN_CDATA_OPENING_4;
else
@throw [OFMalformedXMLException newWithClass: isa];
*last = *i + 1;
}
- (void)_parseInCDATAOpening4WithBuffer: (const char*)buf
i: (size_t*)i
state = OF_XMLPARSER_IN_CDATA_OPENING_4;
else
@throw [OFMalformedXMLException
newWithClass: isa];
last = i + 1;
last: (size_t*)last
break;
case OF_XMLPARSER_IN_CDATA_OPENING_4:
if (buf[i] == 'T')
state = OF_XMLPARSER_IN_CDATA_OPENING_5;
else
@throw [OFMalformedXMLException
newWithClass: isa];
last = i + 1;
break;
case OF_XMLPARSER_IN_CDATA_OPENING_5:
if (buf[i] == 'A')
state = OF_XMLPARSER_IN_CDATA_OPENING_6;
else
@throw [OFMalformedXMLException
newWithClass: isa];
last = i + 1;
break;
case OF_XMLPARSER_IN_CDATA_OPENING_6:
if (buf[i] == '[')
state = OF_XMLPARSER_IN_CDATA_1;
else
@throw [OFMalformedXMLException
newWithClass: isa];
last = i + 1;
{
if (buf[*i] == 'T')
state = OF_XMLPARSER_IN_CDATA_OPENING_5;
else
@throw [OFMalformedXMLException newWithClass: isa];
*last = *i + 1;
}
- (void)_parseInCDATAOpening5WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == 'A')
state = OF_XMLPARSER_IN_CDATA_OPENING_6;
else
@throw [OFMalformedXMLException newWithClass: isa];
*last = *i + 1;
}
- (void)_parseInCDATAOpening6WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == '[')
state = OF_XMLPARSER_IN_CDATA_1;
else
@throw [OFMalformedXMLException newWithClass: isa];
*last = *i + 1;
break;
case OF_XMLPARSER_IN_CDATA_1:
if (buf[i] == ']')
state = OF_XMLPARSER_IN_CDATA_2;
break;
case OF_XMLPARSER_IN_CDATA_2:
if (buf[i] == ']')
state = OF_XMLPARSER_IN_CDATA_3;
else
state = OF_XMLPARSER_IN_CDATA_1;
break;
case OF_XMLPARSER_IN_CDATA_3:
if (buf[i] == '>') {
OFMutableString *cdata;
size_t len;
}
- (void)_parseInCDATA1WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == ']')
state = OF_XMLPARSER_IN_CDATA_2;
}
- (void)_parseInCDATA2WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == ']')
state = OF_XMLPARSER_IN_CDATA_3;
else
state = OF_XMLPARSER_IN_CDATA_1;
}
- (void)_parseInCDATA3WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
OFAutoreleasePool *pool;
OFMutableString *cdata;
size_t len;
if (buf[*i] != '>') {
if (buf[*i] != ']')
state = OF_XMLPARSER_IN_CDATA_1;
return;
}
pool = [[OFAutoreleasePool alloc] init];
pool = [[OFAutoreleasePool alloc] init];
[cache
appendCStringWithoutUTF8Checking: buf + last
length: i - last];
cdata = [[cache mutableCopy] autorelease];
len = [cdata length];
[cache appendCStringWithoutUTF8Checking: buf + *last
length: *i - *last];
cdata = [[cache mutableCopy] autorelease];
len = [cdata length];
[cdata removeCharactersFromIndex: len - 2
toIndex: len];
[cdata removeCharactersFromIndex: len - 2
toIndex: len];
#ifdef OF_HAVE_BLOCKS
if (CDATAHandler != nil)
CDATAHandler(self, cdata);
else
if (CDATAHandler != nil)
CDATAHandler(self, cdata);
else
#endif
[delegate parser: self
foundCDATA: cdata];
[delegate parser: self
foundCDATA: cdata];
[pool release];
[pool release];
[cache setToCString: ""];
[cache setToCString: ""];
last = i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
*last = *i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else if (buf[i] != ']')
state = OF_XMLPARSER_IN_CDATA_1;
break;
/* Comment */
case OF_XMLPARSER_IN_COMMENT_OPENING:
if (buf[i] != '-')
@throw [OFMalformedXMLException
newWithClass: isa];
last = i + 1;
state = OF_XMLPARSER_IN_COMMENT_1;
break;
}
/* Comment */
- (void)_parseInCommentOpeningWithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] != '-')
@throw [OFMalformedXMLException newWithClass: isa];
*last = *i + 1;
state = OF_XMLPARSER_IN_COMMENT_1;
}
case OF_XMLPARSER_IN_COMMENT_1:
if (buf[i] == '-')
state = OF_XMLPARSER_IN_COMMENT_2;
break;
- (void)_parseInComment1WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
if (buf[*i] == '-')
state = OF_XMLPARSER_IN_COMMENT_2;
}
case OF_XMLPARSER_IN_COMMENT_2:
state = (buf[i] == '-' ? OF_XMLPARSER_IN_COMMENT_3 :
OF_XMLPARSER_IN_COMMENT_1);
break;
case OF_XMLPARSER_IN_COMMENT_3:
if (buf[i] == '>') {
OFMutableString *comment;
size_t len;
pool = [[OFAutoreleasePool alloc] init];
- (void)_parseInComment2WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
state = (buf[*i] == '-' ?
OF_XMLPARSER_IN_COMMENT_3 :
OF_XMLPARSER_IN_COMMENT_1);
}
- (void)_parseInComment3WithBuffer: (const char*)buf
i: (size_t*)i
last: (size_t*)last
{
OFAutoreleasePool *pool;
OFMutableString *comment;
size_t len;
if (buf[*i] != '>')
@throw [OFMalformedXMLException newWithClass: isa];
pool = [[OFAutoreleasePool alloc] init];
[cache
appendCStringWithoutUTF8Checking: buf + last
length: i - last];
comment = [[cache mutableCopy] autorelease];
len = [comment length];
[cache appendCStringWithoutUTF8Checking: buf + *last
length: *i - *last];
comment = [[cache mutableCopy] autorelease];
len = [comment length];
[comment removeCharactersFromIndex: len - 2
toIndex: len];
[comment removeCharactersFromIndex: len - 2
toIndex: len];
#ifdef OF_HAVE_BLOCKS
if (commentHandler != nil)
commentHandler(self, comment);
else
if (commentHandler != nil)
commentHandler(self, comment);
else
#endif
[delegate parser: self
foundComment: comment];
[delegate parser: self
foundComment: comment];
[pool release];
[pool release];
[cache setToCString: ""];
[cache setToCString: ""];
last = i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
*last = *i + 1;
state = OF_XMLPARSER_OUTSIDE_TAG;
} else
@throw [OFMalformedXMLException
newWithClass: isa];
break;
}
}
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
containsUnknownEntityNamed: (OFString*)entity
{
#ifdef OF_HAVE_BLOCKS
if (unknownEntityHandler != nil)
|