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
|
@public
OFXMLElement *element;
}
@end
@implementation OFXMLElement_OFXMLElementBuilderDelegate
- (void)elementBuilder: (OFXMLElementBuilder*)builder
didBuildElement: (OFXMLElement*)elem
{
element = [elem retain];
}
- (void)dealloc
{
[element release];
[super dealloc];
}
@end
@implementation OFXMLElement
+ elementWithName: (OFString*)name_
{
return [[[self alloc] initWithName: name_] autorelease];
}
+ elementWithName: (OFString*)name
stringValue: (OFString*)stringval
{
return [[[self alloc] initWithName: name
stringValue: stringval] autorelease];
}
+ elementWithName: (OFString*)name
namespace: (OFString*)ns
{
return [[[self alloc] initWithName: name
namespace: ns] autorelease];
}
+ elementWithName: (OFString*)name
namespace: (OFString*)ns
stringValue: (OFString*)stringval
{
return [[[self alloc] initWithName: name
namespace: ns
stringValue: stringval] autorelease];
}
+ elementWithCharacters: (OFString*)chars
{
return [[[self alloc] initWithCharacters: chars] autorelease];
}
+ elementWithCDATA: (OFString*)cdata
{
return [[[self alloc] initWithCDATA: cdata] autorelease];
}
+ elementWithComment: (OFString*)comment
{
return [[[self alloc] initWithComment: comment] autorelease];
}
+ elementWithXMLString: (OFString*)str
{
return [[[self alloc] initWithXMLString: str] autorelease];
}
- init
{
Class c = isa;
[self release];
@throw [OFNotImplementedException newWithClass: c
selector: _cmd];
}
- initWithName: (OFString*)name_
{
return [self initWithName: name_
namespace: nil
stringValue: nil];
}
- initWithName: (OFString*)name_
stringValue: (OFString*)stringval
{
return [self initWithName: name_
namespace: nil
stringValue: stringval];
}
- initWithName: (OFString*)name_
namespace: (OFString*)ns_
{
return [self initWithName: name_
namespace: ns_
stringValue: nil];
}
- initWithName: (OFString*)name_
namespace: (OFString*)ns_
stringValue: (OFString*)stringval
{
self = [super init];
@try {
name = [name_ copy];
ns = [ns_ copy];
if (stringval != nil) {
OFAutoreleasePool *pool;
pool = [[OFAutoreleasePool alloc] init];
[self addChild:
[OFXMLElement elementWithCharacters: stringval]];
[pool release];
}
namespaces = [[OFMutableDictionary alloc]
initWithKeysAndObjects:
@"http://www.w3.org/XML/1998/namespace", @"xml",
@"http://www.w3.org/2000/xmlns/", @"xmlns", nil];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithCharacters: (OFString*)chars
{
self = [super init];
@try {
characters = [chars copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithCDATA: (OFString*)cdata_
{
self = [super init];
@try {
cdata = [cdata_ copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
@public
OFXMLElement *element;
}
@end
@implementation OFXMLElement_OFXMLElementBuilderDelegate
- (void)elementBuilder: (OFXMLElementBuilder*)builder
didBuildElement: (OFXMLElement*)element_
{
element = [element_ retain];
}
- (void)dealloc
{
[element release];
[super dealloc];
}
@end
@implementation OFXMLElement
+ elementWithName: (OFString*)name
{
return [[[self alloc] initWithName: name] autorelease];
}
+ elementWithName: (OFString*)name
stringValue: (OFString*)stringValue
{
return [[[self alloc] initWithName: name
stringValue: stringValue] autorelease];
}
+ elementWithName: (OFString*)name
namespace: (OFString*)ns
{
return [[[self alloc] initWithName: name
namespace: ns] autorelease];
}
+ elementWithName: (OFString*)name
namespace: (OFString*)ns
stringValue: (OFString*)stringValue
{
return [[[self alloc] initWithName: name
namespace: ns
stringValue: stringValue] autorelease];
}
+ elementWithCharacters: (OFString*)characters
{
return [[[self alloc] initWithCharacters: characters] autorelease];
}
+ elementWithCDATA: (OFString*)CDATA
{
return [[[self alloc] initWithCDATA: CDATA] autorelease];
}
+ elementWithComment: (OFString*)comment
{
return [[[self alloc] initWithComment: comment] autorelease];
}
+ elementWithXMLString: (OFString*)string
{
return [[[self alloc] initWithXMLString: string] autorelease];
}
- init
{
Class c = isa;
[self release];
@throw [OFNotImplementedException newWithClass: c
selector: _cmd];
}
- initWithName: (OFString*)name_
{
return [self initWithName: name_
namespace: nil
stringValue: nil];
}
- initWithName: (OFString*)name_
stringValue: (OFString*)stringValue
{
return [self initWithName: name_
namespace: nil
stringValue: stringValue];
}
- initWithName: (OFString*)name_
namespace: (OFString*)ns_
{
return [self initWithName: name_
namespace: ns_
stringValue: nil];
}
- initWithName: (OFString*)name_
namespace: (OFString*)ns_
stringValue: (OFString*)stringValue
{
self = [super init];
@try {
name = [name_ copy];
ns = [ns_ copy];
if (stringValue != nil) {
OFAutoreleasePool *pool;
pool = [[OFAutoreleasePool alloc] init];
[self addChild:
[OFXMLElement elementWithCharacters: stringValue]];
[pool release];
}
namespaces = [[OFMutableDictionary alloc]
initWithKeysAndObjects:
@"http://www.w3.org/XML/1998/namespace", @"xml",
@"http://www.w3.org/2000/xmlns/", @"xmlns", nil];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithCharacters: (OFString*)characters_
{
self = [super init];
@try {
characters = [characters_ copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithCDATA: (OFString*)CDATA_
{
self = [super init];
@try {
CDATA = [CDATA_ copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
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
|
}
- (OFArray*)children
{
return [[children copy] autorelease];
}
- (void)setStringValue: (OFString*)value
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
[self setChildren: [OFArray arrayWithObject:
[OFXMLElement elementWithCharacters: value]]];
[pool release];
}
- (OFString*)stringValue
{
OFAutoreleasePool *pool;
OFMutableString *ret;
OFXMLElement **children_c;
size_t i, count = [children count];
if (count == 0)
return @"";
ret = [OFMutableString string];
children_c = [children cArray];
pool = [[OFAutoreleasePool alloc] init];
for (i = 0; i < count; i++) {
if (children_c[i]->characters != nil)
[ret appendString: children_c[i]->characters];
else if (children_c[i]->cdata != nil)
[ret appendString: children_c[i]->cdata];
else if (children_c[i]->comment == nil) {
[ret appendString: [children_c[i] stringValue]];
[pool releaseObjects];
}
}
[pool release];
/*
* Class swizzle the string to be immutable. We declared the return type
* to be OFString*, so it can't be modified anyway. But not swizzling it
* would create a real copy each time -[copy] is called.
*/
ret->isa = [OFString class];
return ret;
}
- (OFString*)_XMLStringWithParent: (OFXMLElement*)parent
{
OFAutoreleasePool *pool, *pool2;
char *str_c;
size_t len, i, j, attrs_count;
OFString *prefix, *parent_prefix;
OFXMLAttribute **attrs_carray;
OFString *ret, *tmp;
OFMutableDictionary *all_namespaces;
OFString *def_ns;
if (characters != nil)
return [characters stringByXMLEscaping];
if (cdata != nil)
return [OFString stringWithFormat: @"<![CDATA[%@]]>", cdata];
if (comment != nil) {
OFMutableString *str;
str = [OFMutableString stringWithString: @"<!--"];
[str appendString: comment];
[str appendString: @"-->"];
/*
* Class swizzle the string to be immutable. We declared the
* return type to be OFString*, so it can't be modified anyway.
* But not swizzling it would create a real copy each time
* -[copy] is called.
*/
str->isa = [OFString class];
return str;
}
pool = [[OFAutoreleasePool alloc] init];
def_ns = (defaultNamespace != nil
? defaultNamespace
: (parent != nil ? parent->defaultNamespace : (OFString*)nil));
if (parent != nil && parent->namespaces != nil) {
OFEnumerator *key_enum = [namespaces keyEnumerator];
OFEnumerator *obj_enum = [namespaces objectEnumerator];
id key, obj;
all_namespaces = [[parent->namespaces mutableCopy] autorelease];
while ((key = [key_enum nextObject]) != nil &&
(obj = [obj_enum nextObject]) != nil)
[all_namespaces setObject: obj
forKey: key];
} else
all_namespaces = namespaces;
prefix = [all_namespaces objectForKey:
(ns != nil ? ns : (OFString*)@"")];
parent_prefix = [all_namespaces objectForKey:
(parent != nil && parent->ns != nil ? parent->ns : (OFString*)@"")];
i = 0;
len = [name cStringLength] + 3;
str_c = [self allocMemoryWithSize: len];
/* Start of tag */
str_c[i++] = '<';
if (prefix != nil && ![ns isEqual: def_ns] &&
(![ns isEqual: (parent != nil ? parent->ns : (OFString*)nil)] ||
parent_prefix != nil)) {
len += [prefix cStringLength] + 1;
@try {
str_c = [self resizeMemory: str_c
toSize: len];
} @catch (id e) {
[self freeMemory: str_c];
@throw e;
}
memcpy(str_c + i, [prefix cString],
[prefix cStringLength]);
i += [prefix cStringLength];
str_c[i++] = ':';
}
memcpy(str_c + i, [name cString], [name cStringLength]);
i += [name cStringLength];
/* xmlns if necessary */
if (ns != nil && prefix == nil && ![ns isEqual: def_ns] &&
(![ns isEqual: (parent != nil ? parent->ns : (OFString*)nil)] ||
parent_prefix != nil)) {
len += [ns cStringLength] + 9;
@try {
str_c = [self resizeMemory: str_c
toSize: len];
} @catch (id e) {
[self freeMemory: str_c];
@throw e;
}
memcpy(str_c + i, " xmlns='", 8);
i += 8;
memcpy(str_c + i, [ns cString], [ns cStringLength]);
i += [ns cStringLength];
str_c[i++] = '\'';
def_ns = ns;
}
/* Attributes */
attrs_carray = [attributes cArray];
attrs_count = [attributes count];
pool2 = [[OFAutoreleasePool alloc] init];
for (j = 0; j < attrs_count; j++) {
OFString *attr_name = [attrs_carray[j] name];
OFString *attr_prefix = nil;
tmp = [[attrs_carray[j] stringValue] stringByXMLEscaping];
if ([attrs_carray[j] namespace] != nil &&
(attr_prefix = [all_namespaces objectForKey:
[attrs_carray[j] namespace]]) == nil)
@throw [OFUnboundNamespaceException
newWithClass: isa
namespace: [attrs_carray[j] namespace]];
len += [attr_name cStringLength] +
(attr_prefix != nil ? [attr_prefix cStringLength] + 1 : 0) +
[tmp cStringLength] + 4;
@try {
str_c = [self resizeMemory: str_c
toSize: len];
} @catch (id e) {
[self freeMemory: str_c];
@throw e;
}
str_c[i++] = ' ';
if (attr_prefix != nil) {
memcpy(str_c + i, [attr_prefix cString],
[attr_prefix cStringLength]);
i += [attr_prefix cStringLength];
str_c[i++] = ':';
}
memcpy(str_c + i, [attr_name cString],
[attr_name cStringLength]);
i += [attr_name cStringLength];
str_c[i++] = '=';
str_c[i++] = '\'';
memcpy(str_c + i, [tmp cString], [tmp cStringLength]);
i += [tmp cStringLength];
str_c[i++] = '\'';
[pool2 releaseObjects];
}
/* Childen */
if (children != nil) {
OFXMLElement **children_carray = [children cArray];
size_t children_count = [children count];
IMP append;
tmp = [OFMutableString string];
append = [tmp methodForSelector:
@selector(appendCStringWithoutUTF8Checking:)];
for (j = 0; j < children_count; j++)
append(tmp, @selector(
appendCStringWithoutUTF8Checking:),
[[children_carray[j]
_XMLStringWithParent: self] cString]);
len += [tmp cStringLength] + [name cStringLength] + 2;
@try {
str_c = [self resizeMemory: str_c
toSize: len];
} @catch (id e) {
[self freeMemory: str_c];
@throw e;
}
str_c[i++] = '>';
memcpy(str_c + i, [tmp cString], [tmp cStringLength]);
i += [tmp cStringLength];
str_c[i++] = '<';
str_c[i++] = '/';
if (prefix != nil) {
len += [prefix cStringLength] + 1;
@try {
str_c = [self resizeMemory: str_c
toSize: len];
} @catch (id e) {
[self freeMemory: str_c];
@throw e;
}
memcpy(str_c + i, [prefix cString],
[prefix cStringLength]);
i += [prefix cStringLength];
str_c[i++] = ':';
}
memcpy(str_c + i, [name cString], [name cStringLength]);
i += [name cStringLength];
} else
str_c[i++] = '/';
str_c[i++] = '>';
assert(i == len);
[pool release];
@try {
ret = [OFString stringWithCString: str_c
length: len];
} @finally {
[self freeMemory: str_c];
}
return ret;
}
- (OFString*)XMLString
{
return [self _XMLStringWithParent: nil];
}
- (OFString*)description
{
return [self XMLString];
}
- (void)addAttribute: (OFXMLAttribute*)attr
{
if (name == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
if (attributes == nil)
attributes = [[OFMutableArray alloc] init];
if ([self attributeForName: attr->name
namespace: attr->ns] == nil)
[attributes addObject: attr];
}
- (void)addAttributeWithName: (OFString*)name_
stringValue: (OFString*)value
{
[self addAttributeWithName: name_
namespace: nil
stringValue: value];
}
- (void)addAttributeWithName: (OFString*)name_
namespace: (OFString*)ns_
stringValue: (OFString*)value
{
OFAutoreleasePool *pool;
if (name == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
pool = [[OFAutoreleasePool alloc] init];
[self addAttribute: [OFXMLAttribute attributeWithName: name_
namespace: ns_
stringValue: value]];
[pool release];
}
- (OFXMLAttribute*)attributeForName: (OFString*)attrname
{
return [self attributeForName: attrname
namespace: nil];
}
- (OFXMLAttribute*)attributeForName: (OFString*)attrname
namespace: (OFString*)attrns
{
OFXMLAttribute **attrs_c = [attributes cArray];
size_t i, attrs_count = [attributes count];
if (attrns != nil) {
for (i = 0; i < attrs_count; i++)
if ([attrs_c[i]->ns isEqual: attrns] &&
[attrs_c[i]->name isEqual: attrname])
return attrs_c[i];
} else {
for (i = 0; i < attrs_count; i++)
if (attrs_c[i]->ns == nil &&
[attrs_c[i]->name isEqual: attrname])
return attrs_c[i];
}
return nil;
}
- (void)removeAttributeForName: (OFString*)attrname
{
[self removeAttributeForName: attrname
namespace: nil];
}
- (void)removeAttributeForName: (OFString*)attrname
namespace: (OFString*)attrns
{
OFXMLAttribute **attrs_c = [attributes cArray];
size_t i, attrs_count = [attributes count];
if (attrns != nil) {
for (i = 0; i < attrs_count; i++) {
if ([attrs_c[i]->ns isEqual: attrns] &&
[attrs_c[i]->name isEqual: attrname]) {
[attributes removeObjectAtIndex: i];
return;
}
}
} else {
for (i = 0; i < attrs_count; i++) {
if (attrs_c[i]->ns == nil &&
[attrs_c[i]->name isEqual: attrname]) {
[attributes removeObjectAtIndex: i];
return;
}
}
}
}
- (void)setPrefix: (OFString*)prefix
forNamespace: (OFString*)ns_
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
<
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
>
>
>
>
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
>
>
|
>
>
>
>
|
>
|
|
>
>
|
|
|
|
|
<
<
<
<
|
<
|
>
>
|
<
|
|
|
|
<
<
|
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
|
}
- (OFArray*)children
{
return [[children copy] autorelease];
}
- (void)setStringValue: (OFString*)stringValue
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
[self setChildren: [OFArray arrayWithObject:
[OFXMLElement elementWithCharacters: stringValue]]];
[pool release];
}
- (OFString*)stringValue
{
OFAutoreleasePool *pool;
OFMutableString *ret;
OFXMLElement **cArray;
size_t i, count = [children count];
if (count == 0)
return @"";
ret = [OFMutableString string];
cArray = [children cArray];
pool = [[OFAutoreleasePool alloc] init];
for (i = 0; i < count; i++) {
if (cArray[i]->characters != nil)
[ret appendString: cArray[i]->characters];
else if (cArray[i]->CDATA != nil)
[ret appendString: cArray[i]->CDATA];
else if (cArray[i]->comment == nil) {
[ret appendString: [cArray[i] stringValue]];
[pool releaseObjects];
}
}
[pool release];
/*
* Class swizzle the string to be immutable. We declared the return type
* to be OFString*, so it can't be modified anyway. But not swizzling it
* would create a real copy each time -[copy] is called.
*/
ret->isa = [OFString class];
return ret;
}
- (OFString*)_XMLStringWithParent: (OFXMLElement*)parent
{
OFAutoreleasePool *pool, *pool2;
char *cString;
size_t length, i, j, attributesCount;
OFString *prefix, *parentPrefix;
OFXMLAttribute **attributesCArray;
OFString *ret, *tmp;
OFMutableDictionary *allNamespaces;
OFString *defaultNS;
if (characters != nil)
return [characters stringByXMLEscaping];
if (CDATA != nil)
return [OFString stringWithFormat: @"<![CDATA[%@]]>", CDATA];
if (comment != nil)
return [OFString stringWithFormat: @"<!--%@-->", comment];
pool = [[OFAutoreleasePool alloc] init];
defaultNS = (defaultNamespace != nil
? defaultNamespace
: (parent != nil ? parent->defaultNamespace : (OFString*)nil));
if (parent != nil && parent->namespaces != nil) {
OFEnumerator *keyEnumerator = [namespaces keyEnumerator];
OFEnumerator *objectEnumerator = [namespaces objectEnumerator];
id key, object;
allNamespaces = [[parent->namespaces mutableCopy] autorelease];
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil)
[allNamespaces setObject: object
forKey: key];
} else
allNamespaces = namespaces;
prefix = [allNamespaces objectForKey:
(ns != nil ? ns : (OFString*)@"")];
parentPrefix = [allNamespaces objectForKey:
(parent != nil && parent->ns != nil ? parent->ns : (OFString*)@"")];
i = 0;
length = [name cStringLength] + 3;
cString = [self allocMemoryWithSize: length];
/* Start of tag */
cString[i++] = '<';
if (prefix != nil && ![ns isEqual: defaultNS] &&
(![ns isEqual: (parent != nil ? parent->ns : (OFString*)nil)] ||
parentPrefix != nil)) {
length += [prefix cStringLength] + 1;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, [prefix cString], [prefix cStringLength]);
i += [prefix cStringLength];
cString[i++] = ':';
}
memcpy(cString + i, [name cString], [name cStringLength]);
i += [name cStringLength];
/* xmlns if necessary */
if (ns != nil && prefix == nil && ![ns isEqual: defaultNS] &&
(![ns isEqual: (parent != nil ? parent->ns : (OFString*)nil)] ||
parentPrefix != nil)) {
length += [ns cStringLength] + 9;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, " xmlns='", 8);
i += 8;
memcpy(cString + i, [ns cString], [ns cStringLength]);
i += [ns cStringLength];
cString[i++] = '\'';
defaultNS = ns;
}
/* Attributes */
attributesCArray = [attributes cArray];
attributesCount = [attributes count];
pool2 = [[OFAutoreleasePool alloc] init];
for (j = 0; j < attributesCount; j++) {
OFString *attributeName = [attributesCArray[j] name];
OFString *attributePrefix = nil;
tmp = [[attributesCArray[j] stringValue] stringByXMLEscaping];
if ([attributesCArray[j] namespace] != nil &&
(attributePrefix = [allNamespaces objectForKey:
[attributesCArray[j] namespace]]) == nil)
@throw [OFUnboundNamespaceException
newWithClass: isa
namespace: [attributesCArray[j] namespace]];
length += [attributeName cStringLength] +
(attributePrefix != nil ?
[attributePrefix cStringLength] + 1 : 0) +
[tmp cStringLength] + 4;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
cString[i++] = ' ';
if (attributePrefix != nil) {
memcpy(cString + i, [attributePrefix cString],
[attributePrefix cStringLength]);
i += [attributePrefix cStringLength];
cString[i++] = ':';
}
memcpy(cString + i, [attributeName cString],
[attributeName cStringLength]);
i += [attributeName cStringLength];
cString[i++] = '=';
cString[i++] = '\'';
memcpy(cString + i, [tmp cString], [tmp cStringLength]);
i += [tmp cStringLength];
cString[i++] = '\'';
[pool2 releaseObjects];
}
/* Childen */
if (children != nil) {
OFXMLElement **childrenCArray = [children cArray];
size_t childrenCount = [children count];
IMP append;
SEL appendSel = @selector(appendCStringWithoutUTF8Checking:);
tmp = [OFMutableString string];
append = [tmp methodForSelector: appendSel];
for (j = 0; j < childrenCount; j++)
append(tmp, appendSel,
[[childrenCArray[j] _XMLStringWithParent: self]
cString]);
length += [tmp cStringLength] + [name cStringLength] + 2;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
cString[i++] = '>';
memcpy(cString + i, [tmp cString], [tmp cStringLength]);
i += [tmp cStringLength];
cString[i++] = '<';
cString[i++] = '/';
if (prefix != nil) {
length += [prefix cStringLength] + 1;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, [prefix cString],
[prefix cStringLength]);
i += [prefix cStringLength];
cString[i++] = ':';
}
memcpy(cString + i, [name cString], [name cStringLength]);
i += [name cStringLength];
} else
cString[i++] = '/';
cString[i++] = '>';
assert(i == length);
[pool release];
@try {
ret = [OFString stringWithCString: cString
length: length];
} @finally {
[self freeMemory: cString];
}
return ret;
}
- (OFString*)XMLString
{
return [self _XMLStringWithParent: nil];
}
- (OFString*)description
{
return [self XMLString];
}
- (void)addAttribute: (OFXMLAttribute*)attribute
{
if (name == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
if (attributes == nil)
attributes = [[OFMutableArray alloc] init];
if ([self attributeForName: attribute->name
namespace: attribute->ns] == nil)
[attributes addObject: attribute];
}
- (void)addAttributeWithName: (OFString*)name_
stringValue: (OFString*)stringValue
{
[self addAttributeWithName: name_
namespace: nil
stringValue: stringValue];
}
- (void)addAttributeWithName: (OFString*)name_
namespace: (OFString*)ns_
stringValue: (OFString*)stringValue
{
OFAutoreleasePool *pool;
if (name == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
pool = [[OFAutoreleasePool alloc] init];
[self addAttribute: [OFXMLAttribute attributeWithName: name_
namespace: ns_
stringValue: stringValue]];
[pool release];
}
- (OFXMLAttribute*)attributeForName: (OFString*)attributeName
{
OFXMLAttribute **cArray = [attributes cArray];
size_t i, count = [attributes count];
for (i = 0; i < count; i++)
if (cArray[i]->ns == nil &&
[cArray[i]->name isEqual: attributeName])
return cArray[i];
return nil;
}
- (OFXMLAttribute*)attributeForName: (OFString*)attributeName
namespace: (OFString*)attributeNS
{
OFXMLAttribute **cArray;
size_t i, count;
if (attributeNS == nil)
return [self attributeForName: attributeName];
cArray = [attributes cArray];
count = [attributes count];
for (i = 0; i < count; i++)
if ([cArray[i]->ns isEqual: attributeNS] &&
[cArray[i]->name isEqual: attributeName])
return cArray[i];
return nil;
}
- (void)removeAttributeForName: (OFString*)attributeName
{
OFXMLAttribute **cArray = [attributes cArray];
size_t i, count = [attributes count];
for (i = 0; i < count; i++) {
if (cArray[i]->ns == nil &&
[cArray[i]->name isEqual: attributeName]) {
[attributes removeObjectAtIndex: i];
return;
}
}
}
- (void)removeAttributeForName: (OFString*)attributeName
namespace: (OFString*)attributeNS
{
OFXMLAttribute **cArray;
size_t i, count;
if (attributeNS == nil)
return [self removeAttributeForName: attributeName];
cArray = [attributes cArray];
count = [attributes count];
for (i = 0; i < count; i++) {
if ([cArray[i]->ns isEqual: attributeNS] &&
[cArray[i]->name isEqual: attributeName]) {
[attributes removeObjectAtIndex: i];
return;
}
}
}
- (void)setPrefix: (OFString*)prefix
forNamespace: (OFString*)ns_
{
|
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
|
if (children == nil)
children = [[OFMutableArray alloc] init];
[children addObject: child];
}
- (OFXMLElement*)elementForName: (OFString*)elemname
{
return [self elementForName: elemname
namespace: nil];
}
- (OFArray*)elementsForName: (OFString*)elemname
{
return [self elementsForName: elemname
namespace: nil];
}
- (OFXMLElement*)elementForName: (OFString*)elemname
namespace: (OFString*)elemns
{
return [[self elementsForName: elemname
namespace: elemns] firstObject];
}
- (OFArray*)elementsForName: (OFString*)elemname
namespace: (OFString*)elemns
{
OFMutableArray *ret = [OFMutableArray array];
OFXMLElement **children_c = [children cArray];
size_t i, children_count = [children count];
if (elemns != nil) {
for (i = 0; i < children_count; i++)
if ([children_c[i]->ns isEqual: elemns] &&
[children_c[i]->name isEqual: elemname])
[ret addObject: children_c[i]];
} else {
for (i = 0; i < children_count; i++)
if (children_c[i]->ns == nil &&
[children_c[i]->name isEqual: elemname])
[ret addObject: children_c[i]];
}
return ret;
}
- (void)dealloc
{
[name release];
[ns release];
[attributes release];
[namespaces release];
[children release];
[characters release];
[cdata release];
[comment release];
[super dealloc];
}
@end
|
|
|
<
|
>
|
|
|
<
>
|
|
|
>
>
>
>
>
>
>
|
|
|
|
|
|
>
|
>
|
|
|
<
|
|
|
|
<
|
|
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
|
if (children == nil)
children = [[OFMutableArray alloc] init];
[children addObject: child];
}
- (OFXMLElement*)elementForName: (OFString*)elementName
{
return [[self elementsForName: elementName] firstObject];
}
- (OFXMLElement*)elementForName: (OFString*)elementName
namespace: (OFString*)elementNS
{
return [[self elementsForName: elementName
namespace: elementNS] firstObject];
}
- (OFArray*)elementsForName: (OFString*)elementName
{
OFMutableArray *ret = [OFMutableArray array];
OFXMLElement **cArray = [children cArray];
size_t i, count = [children count];
for (i = 0; i < count; i++)
if (cArray[i]->ns == nil &&
[cArray[i]->name isEqual: elementName])
[ret addObject: cArray[i]];
return ret;
}
- (OFArray*)elementsForName: (OFString*)elementName
namespace: (OFString*)elementNS
{
OFMutableArray *ret;
OFXMLElement **cArray;
size_t i, count;
if (elementNS == nil)
return [self elementsForName: elementName];
ret = [OFMutableArray array];
cArray = [children cArray];
count = [children count];
for (i = 0; i < count; i++)
if ([cArray[i]->ns isEqual: elementNS] &&
[cArray[i]->name isEqual: elementName])
[ret addObject: cArray[i]];
return ret;
}
- (void)dealloc
{
[name release];
[ns release];
[attributes release];
[namespaces release];
[children release];
[characters release];
[CDATA release];
[comment release];
[super dealloc];
}
@end
|