ObjFW  Diff

Differences From Artifact [5e664c8a15]:

To Artifact [177671f0ee]:


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
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







-
+





-
+

-
-
+
+




-
+








static Class charactersClass = Nil;
static Class CDATAClass = Nil;

@interface OFXMLElement_OFXMLElementBuilderDelegate: OFObject
{
@public
	OFXMLElement *element;
	OFXMLElement *_element;
}
@end

@implementation OFXMLElement_OFXMLElementBuilderDelegate
- (void)elementBuilder: (OFXMLElementBuilder*)builder
       didBuildElement: (OFXMLElement*)element_
       didBuildElement: (OFXMLElement*)element
{
	if (element == nil)
		element = [element_ retain];
	if (_element == nil)
		_element = [element retain];
}

- (void)dealloc
{
	[element release];
	[_element release];

	[super dealloc];
}
@end

@implementation OFXMLElement
+ (void)initialize
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
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







-
+

-
+




-
+


-
+




-
-
+
+

-
-
+
+



-
-
+
+





-
+




-
-
+
+

-
+







		[self release];
		@throw e;
	}

	abort();
}

- initWithName: (OFString*)name_
- initWithName: (OFString*)name
{
	return [self initWithName: name_
	return [self initWithName: name
			namespace: nil
		      stringValue: nil];
}

- initWithName: (OFString*)name_
- initWithName: (OFString*)name
   stringValue: (OFString*)stringValue
{
	return [self initWithName: name_
	return [self initWithName: name
			namespace: nil
		      stringValue: stringValue];
}

- initWithName: (OFString*)name_
     namespace: (OFString*)ns_
- initWithName: (OFString*)name
     namespace: (OFString*)namespace
{
	return [self initWithName: name_
			namespace: ns_
	return [self initWithName: name
			namespace: namespace
		      stringValue: nil];
}

- initWithName: (OFString*)name_
     namespace: (OFString*)ns_
- initWithName: (OFString*)name
     namespace: (OFString*)namespace
   stringValue: (OFString*)stringValue
{
	self = [super init];

	@try {
		if (name_ == nil)
		if (name == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		name = [name_ copy];
		ns = [ns_ copy];
		_name = [name copy];
		_namespace = [namespace copy];

		namespaces = [[OFMutableDictionary alloc]
		_namespaces = [[OFMutableDictionary alloc]
		    initWithKeysAndObjects:
		    @"http://www.w3.org/XML/1998/namespace", @"xml",
		    @"http://www.w3.org/2000/xmlns/", @"xmlns", nil];

		if (stringValue != nil)
			[self setStringValue: stringValue];
	} @catch (id e) {
195
196
197
198
199
200
201
202
203
204
205
206
207






208
209
210
211
212
213
214
195
196
197
198
199
200
201






202
203
204
205
206
207
208
209
210
211
212
213
214







-
-
-
-
-
-
+
+
+
+
+
+








	@try {
		if (element == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		name = [element->name copy];
		ns = [element->ns copy];
		defaultNamespace = [element->defaultNamespace copy];
		attributes = [element->attributes mutableCopy];
		namespaces = [element->namespaces mutableCopy];
		children = [element->children mutableCopy];
		_name = [element->_name copy];
		_namespace = [element->_namespace copy];
		_defaultNamespace = [element->_defaultNamespace copy];
		_attributes = [element->_attributes mutableCopy];
		_namespaces = [element->_namespaces mutableCopy];
		_children = [element->_children mutableCopy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
240
241
242
243
244
245
246
247

248
249
250
251
252
253
254
240
241
242
243
244
245
246

247
248
249
250
251
252
253
254







-
+








	[parser parseString: string];

	if (![parser finishedParsing])
		@throw [OFMalformedXMLException exceptionWithClass: c
							    parser: parser];

	self = [delegate->element retain];
	self = [delegate->_element retain];

	objc_autoreleasePoolPop(pool);

	return self;
}

- initWithFile: (OFString*)path
274
275
276
277
278
279
280
281

282
283
284
285
286
287
288
274
275
276
277
278
279
280

281
282
283
284
285
286
287
288







-
+








	[parser parseFile: path];

	if (![parser finishedParsing])
		@throw [OFMalformedXMLException exceptionWithClass: c
							    parser: parser];

	self = [delegate->element retain];
	self = [delegate->_element retain];

	objc_autoreleasePoolPop(pool);

	return self;
}

- initWithSerialization: (OFXMLElement*)element
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
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







-
-
-
-
+
+
+
+
+















-
+

-
+

-
+



-
+
-
-
-
-
-
+
+
+
+




-
+






-
-
+
+








-
+






-
-
+
+

+
-
-
-
-
+
+
+
+

-
+













+
+
+
+
+
+
+
+
+
+
+
+
-
+

-
+




-
+




-
+


-
+

-
+




-
+




-
+


-
+

-
+




-
+
















-
+





-
+








		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		name = [[[element attributeForName: @"name"] stringValue] copy];
		ns = [[[element attributeForName: @"namespace"] stringValue]
		    copy];
		defaultNamespace = [[[element attributeForName:
		_name = [[[element attributeForName: @"name"]
		    stringValue] copy];
		_namespace = [[[element attributeForName: @"namespace"]
		    stringValue] copy];
		_defaultNamespace = [[[element attributeForName:
		    @"defaultNamespace"] stringValue] copy];

		attributesElement = [[[element
		    elementForName: @"attributes"
			 namespace: OF_SERIALIZATION_NS] elementsForNamespace:
		    OF_SERIALIZATION_NS] firstObject];
		namespacesElement = [[[element
		    elementForName: @"namespaces"
			 namespace: OF_SERIALIZATION_NS] elementsForNamespace:
		    OF_SERIALIZATION_NS] firstObject];
		childrenElement = [[[element
		    elementForName: @"children"
			 namespace: OF_SERIALIZATION_NS] elementsForNamespace:
		    OF_SERIALIZATION_NS] firstObject];

		attributes = [[attributesElement objectByDeserializing]
		_attributes = [[attributesElement objectByDeserializing]
		    mutableCopy];
		namespaces = [[namespacesElement objectByDeserializing]
		_namespaces = [[namespacesElement objectByDeserializing]
		    mutableCopy];
		children = [[childrenElement objectByDeserializing]
		_children = [[childrenElement objectByDeserializing]
		    mutableCopy];

		/* Sanity checks */
		if ((attributes != nil &&
		if ((_attributes != nil && ![_attributes isKindOfClass:
		    ![attributes isKindOfClass: [OFMutableArray class]]) ||
		    (namespaces != nil &&
		    ![namespaces isKindOfClass: [OFMutableDictionary class]]) ||
		    (children != nil &&
		    ![children isKindOfClass: [OFMutableArray class]]))
		    [OFMutableArray class]]) || (_namespaces != nil &&
		    ![_namespaces isKindOfClass:
		    [OFMutableDictionary class]]) || (_children != nil &&
		    ![_children isKindOfClass: [OFMutableArray class]]))
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		objectEnumerator = [attributes objectEnumerator];
		objectEnumerator = [_attributes objectEnumerator];
		while ((object = [objectEnumerator nextObject]) != nil)
			if (![object isKindOfClass: [OFXMLAttribute class]])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];

		keyEnumerator = [namespaces keyEnumerator];
		objectEnumerator = [namespaces objectEnumerator];
		keyEnumerator = [_namespaces keyEnumerator];
		objectEnumerator = [_namespaces objectEnumerator];
		while ((key = [keyEnumerator nextObject]) != nil &&
		    (object = [objectEnumerator nextObject]) != nil)
			if (![key isKindOfClass: [OFString class]] ||
			    ![object isKindOfClass: [OFString class]])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];

		objectEnumerator = [children objectEnumerator];
		objectEnumerator = [_children objectEnumerator];
		while ((object = [objectEnumerator nextObject]) != nil)
			if (![object isKindOfClass: [OFXMLNode class]])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];

		if (namespaces == nil)
			namespaces = [[OFMutableDictionary alloc] init];
		if (_namespaces == nil)
			_namespaces = [[OFMutableDictionary alloc] init];

		[_namespaces
		[namespaces setObject: @"xml"
			       forKey: @"http://www.w3.org/XML/1998/namespace"];
		[namespaces setObject: @"xmlns"
			       forKey: @"http://www.w3.org/2000/xmlns/"];
		    setObject: @"xml"
		       forKey: @"http://www.w3.org/XML/1998/namespace"];
		[_namespaces setObject: @"xmlns"
				forKey: @"http://www.w3.org/2000/xmlns/"];

		if (name == nil)
		if (_name == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_name release];
	[_namespace release];
	[_defaultNamespace release];
	[_attributes release];
	[_namespaces release];
	[_children release];

	[super dealloc];
}

- (void)setName: (OFString*)name_
- (void)setName: (OFString*)name
{
	if (name_ == nil)
	if (name == nil)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	OF_SETTER(name, name_, YES, 1)
	OF_SETTER(_name, name, YES, 1)
}

- (OFString*)name
{
	OF_GETTER(name, YES)
	OF_GETTER(_name, YES)
}

- (void)setNamespace: (OFString*)ns_
- (void)setNamespace: (OFString*)namespace
{
	OF_SETTER(ns, ns_, YES, 1)
	OF_SETTER(_namespace, namespace, YES, 1)
}

- (OFString*)namespace
{
	OF_GETTER(ns, YES)
	OF_GETTER(_namespace, YES)
}

- (OFArray*)attributes
{
	OF_GETTER(attributes, YES)
	OF_GETTER(_attributes, YES)
}

- (void)setChildren: (OFArray*)children_
- (void)setChildren: (OFArray*)children
{
	OF_SETTER(children, children_, YES, 2)
	OF_SETTER(_children, children, YES, 2)
}

- (OFArray*)children
{
	OF_GETTER(children, YES)
	OF_GETTER(_children, YES)
}

- (void)setStringValue: (OFString*)stringValue
{
	void *pool = objc_autoreleasePoolPush();

	[self setChildren: [OFArray arrayWithObject:
	    [OFXMLCharacters charactersWithString: stringValue]]];

	objc_autoreleasePoolPop(pool);
}

- (OFString*)stringValue
{
	OFMutableString *ret;
	OFXMLElement **objects;
	size_t i, count = [children count];
	size_t i, count = [_children count];

	if (count == 0)
		return @"";

	ret = [OFMutableString string];
	objects = [children objects];
	objects = [_children objects];

	for (i = 0; i < count; i++) {
		void *pool = objc_autoreleasePoolPush();

		[ret appendString: [objects[i] stringValue]];

		objc_autoreleasePoolPop(pool);
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
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







-
+
+



-
-
+
+












-
+


-
+

-
-
-
-
+
+
+
+

-
+


-
+








-
+















-
-
+
+


-
-
-
+
+
+
+










+
-
-
+
+




-
-
+
+







	OFXMLAttribute **attributesObjects;
	OFString *ret;
	OFString *defaultNS;

	pool = objc_autoreleasePoolPush();

	parentPrefix = [allNamespaces objectForKey:
	    (parent != nil && parent->ns != nil ? parent->ns : (OFString*)@"")];
	    (parent != nil && parent->_namespace != nil
	    ? parent->_namespace : (OFString*)@"")];

	/* Add the namespaces of the current element */
	if (allNamespaces != nil) {
		OFEnumerator *keyEnumerator = [namespaces keyEnumerator];
		OFEnumerator *objectEnumerator = [namespaces objectEnumerator];
		OFEnumerator *keyEnumerator = [_namespaces keyEnumerator];
		OFEnumerator *objectEnumerator = [_namespaces objectEnumerator];
		OFMutableDictionary *tmp;
		id key, object;

		tmp = [[allNamespaces mutableCopy] autorelease];

		while ((key = [keyEnumerator nextObject]) != nil &&
		    (object = [objectEnumerator nextObject]) != nil)
			[tmp setObject: object
				forKey: key];

		allNamespaces = tmp;
	} else
		allNamespaces = namespaces;
		allNamespaces = _namespaces;

	prefix = [allNamespaces objectForKey:
	    (ns != nil ? ns : (OFString*)@"")];
	    (_namespace != nil ? _namespace : (OFString*)@"")];

	if (parent != nil && parent->ns != nil && parentPrefix == nil)
		defaultNS = parent->ns;
	else if (parent != nil && parent->defaultNamespace != nil)
		defaultNS = parent->defaultNamespace;
	if (parent != nil && parent->_namespace != nil && parentPrefix == nil)
		defaultNS = parent->_namespace;
	else if (parent != nil && parent->_defaultNamespace != nil)
		defaultNS = parent->_defaultNamespace;
	else
		defaultNS = defaultNamespace;
		defaultNS = _defaultNamespace;

	i = 0;
	length = [name UTF8StringLength] + 3 + (level * indentation);
	length = [_name UTF8StringLength] + 3 + (level * indentation);
	cString = [self allocMemoryWithSize: length];

	memset(cString + i, ' ', level * indentation);
	i += level * indentation;

	/* Start of tag */
	cString[i++] = '<';

	if (prefix != nil && ![ns isEqual: defaultNS]) {
	if (prefix != nil && ![_namespace isEqual: defaultNS]) {
		length += [prefix UTF8StringLength] + 1;
		@try {
			cString = [self resizeMemory: cString
						size: length];
		} @catch (id e) {
			[self freeMemory: cString];
			@throw e;
		}

		memcpy(cString + i, [prefix UTF8String],
		    [prefix UTF8StringLength]);
		i += [prefix UTF8StringLength];
		cString[i++] = ':';
	}

	memcpy(cString + i, [name UTF8String], [name UTF8StringLength]);
	i += [name UTF8StringLength];
	memcpy(cString + i, [_name UTF8String], [_name UTF8StringLength]);
	i += [_name UTF8StringLength];

	/* xmlns if necessary */
	if (prefix == nil && ((ns != nil && ![ns isEqual: defaultNS]) ||
	    (ns == nil && defaultNS != nil))) {
		length += [ns UTF8StringLength] + 9;
	if (prefix == nil && ((_namespace != nil &&
	    ![_namespace isEqual: defaultNS]) ||
	    (_namespace == nil && defaultNS != nil))) {
		length += [_namespace UTF8StringLength] + 9;
		@try {
			cString = [self resizeMemory: cString
						size: length];
		} @catch (id e) {
			[self freeMemory: cString];
			@throw e;
		}

		memcpy(cString + i, " xmlns='", 8);
		i += 8;
		memcpy(cString + i, [_namespace UTF8String],
		memcpy(cString + i, [ns UTF8String], [ns UTF8StringLength]);
		i += [ns UTF8StringLength];
		    [_namespace UTF8StringLength]);
		i += [_namespace UTF8StringLength];
		cString[i++] = '\'';
	}

	/* Attributes */
	attributesObjects = [attributes objects];
	attributesCount = [attributes count];
	attributesObjects = [_attributes objects];
	attributesCount = [_attributes count];

	for (j = 0; j < attributesCount; j++) {
		void *pool2 = objc_autoreleasePoolPush();
		OFString *attributeName = [attributesObjects[j] name];
		OFString *attributePrefix = nil;
		OFString *tmp =
		    [[attributesObjects[j] stringValue] stringByXMLEscaping];
602
603
604
605
606
607
608
609
610
611



612
613
614
615
616
617
618
618
619
620
621
622
623
624



625
626
627
628
629
630
631
632
633
634







-
-
-
+
+
+







		i += [tmp UTF8StringLength];
		cString[i++] = '\'';

		objc_autoreleasePoolPop(pool2);
	}

	/* Childen */
	if (children != nil) {
		OFXMLElement **childrenObjects = [children objects];
		size_t childrenCount = [children count];
	if (_children != nil) {
		OFXMLElement **childrenObjects = [_children objects];
		size_t childrenCount = [_children count];
		OFDataArray *tmp = [OFDataArray dataArray];
		BOOL indent;

		if (indentation > 0) {
			indent = YES;

			for (j = 0; j < childrenCount; j++) {
648
649
650
651
652
653
654
655

656
657
658
659
660
661
662
664
665
666
667
668
669
670

671
672
673
674
675
676
677
678







-
+







			[tmp addItems: [child UTF8String]
				count: [child UTF8StringLength]];
		}

		if (indent)
			[tmp addItem: "\n"];

		length += [tmp count] + [name UTF8StringLength] + 2 +
		length += [tmp count] + [_name UTF8StringLength] + 2 +
		    (indent ? level * indentation : 0);
		@try {
			cString = [self resizeMemory: cString
						size: length];
		} @catch (id e) {
			[self freeMemory: cString];
			@throw e;
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







-
-
+
+
+







			}

			memcpy(cString + i, [prefix UTF8String],
			    [prefix UTF8StringLength]);
			i += [prefix UTF8StringLength];
			cString[i++] = ':';
		}
		memcpy(cString + i, [name UTF8String], [name UTF8StringLength]);
		i += [name UTF8StringLength];
		memcpy(cString + i, [_name UTF8String],
		    [_name UTF8StringLength]);
		i += [_name UTF8StringLength];
	} else
		cString[i++] = '/';

	cString[i++] = '>';
	assert(i == length);

	objc_autoreleasePoolPop(pool);
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
958
959
960


961
962
963


964
965
966

967
968
969
970

971
972
973
974
975
976
977
978
979
980


981
982
983
984
985
986
987
988
989
990
991
992

993
994
995
996
997

998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010


1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022


1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041


1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056


1057
1058
1059
1060
1061


1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073


1074
1075
1076
1077
1078


1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098


1099
1100
1101
1102
1103


1104
1105
1106
1107
1108
1109
1110
1111
1112
1113

1114
1115
1116
1117
1118

1119
1120

1121

1122

1123
1124
1125


1126
1127
1128


1129
1130
1131


1132
1133
1134


1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151






1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
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
958
959
960
961


962
963
964

965
966
967
968
969
970
971
972
973
974
975


976
977
978


979
980
981
982

983
984
985
986

987
988
989
990
991
992
993
994
995


996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008

1009
1010
1011
1012
1013

1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025


1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037


1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056


1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071


1072
1073
1074
1075
1076


1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088


1089
1090
1091
1092
1093


1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113


1114
1115
1116
1117
1118


1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129

1130
1131
1132
1133
1134

1135
1136

1137
1138
1139

1140
1141


1142
1143
1144


1145
1146
1147


1148
1149
1150


1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163






1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179












1180







-
+

-
+

-
+

-
+

-
+

-
+

-
+






-
+



-
+


-
+
















-
+





-
+












-
-
+
+

-
-
-
+
+
+


-
+


-
+




-
-
+
+




-
-
+
+







-
-
+
+


-
-
+
+














-
-
+
+


-
-
+
+







-
-
+
+


-
-
-
+
+
+















-
-
+
+


-
-
-
+
+
+






-
+





-
-
+
+

-
-
+
+



-
+


-
+


-
+




-
+


-
+

-
+









-
-
+
+

-
+










-
-
+
+

-
-
+
+


-
+



-
+








-
-
+
+











-
+




-
+











-
-
+
+










-
-
+
+

















-
-
+
+













-
-
+
+



-
-
+
+










-
-
+
+



-
-
+
+


















-
-
+
+



-
-
+
+









-
+




-
+

-
+

+
-
+

-
-
+
+

-
-
+
+

-
-
+
+

-
-
+
+











-
-
-
-
-
-
+
+
+
+
+
+










-
-
-
-
-
-
-
-
-
-
-
-

{
	void *pool = objc_autoreleasePoolPush();
	OFXMLElement *element;

	element = [OFXMLElement elementWithName: [self className]
				      namespace: OF_SERIALIZATION_NS];

	if (name != nil)
	if (_name != nil)
		[element addAttributeWithName: @"name"
				  stringValue: name];
				  stringValue: _name];

	if (ns != nil)
	if (_namespace != nil)
		[element addAttributeWithName: @"namespace"
				  stringValue: ns];
				  stringValue: _namespace];

	if (defaultNamespace != nil)
	if (_defaultNamespace != nil)
		[element addAttributeWithName: @"defaultNamespace"
				  stringValue: defaultNamespace];
				  stringValue: _defaultNamespace];

	if (attributes != nil) {
	if (_attributes != nil) {
		OFXMLElement *attributesElement;

		attributesElement =
		    [OFXMLElement elementWithName: @"attributes"
					namespace: OF_SERIALIZATION_NS];
		[attributesElement addChild:
		    [attributes XMLElementBySerializing]];
		    [_attributes XMLElementBySerializing]];
		[element addChild: attributesElement];
	}

	if (namespaces != nil) {
	if (_namespaces != nil) {
		OFXMLElement *namespacesElement;
		OFMutableDictionary *namespacesCopy =
		    [[namespaces mutableCopy] autorelease];
		    [[_namespaces mutableCopy] autorelease];

		[namespacesCopy removeObjectForKey:
		    @"http://www.w3.org/XML/1998/namespace"];
		[namespacesCopy removeObjectForKey:
		    @"http://www.w3.org/2000/xmlns/"];

		if ([namespacesCopy count] > 0) {
			namespacesElement =
			    [OFXMLElement elementWithName: @"namespaces"
						namespace: OF_SERIALIZATION_NS];
			[namespacesElement addChild:
			    [namespacesCopy XMLElementBySerializing]];
			[element addChild: namespacesElement];
		}
	}

	if (children != nil) {
	if (_children != nil) {
		OFXMLElement *childrenElement;

		childrenElement =
		    [OFXMLElement elementWithName: @"children"
					namespace: OF_SERIALIZATION_NS];
		[childrenElement addChild: [children XMLElementBySerializing]];
		[childrenElement addChild: [_children XMLElementBySerializing]];
		[element addChild: childrenElement];
	}

	[element retain];

	objc_autoreleasePoolPop(pool);

	return [element autorelease];
}

- (void)addAttribute: (OFXMLAttribute*)attribute
{
	if (attributes == nil)
		attributes = [[OFMutableArray alloc] init];
	if (_attributes == nil)
		_attributes = [[OFMutableArray alloc] init];

	if ([self attributeForName: attribute->name
			 namespace: attribute->ns] == nil)
		[attributes addObject: attribute];
	if ([self attributeForName: attribute->_name
			 namespace: attribute->_namespace] == nil)
		[_attributes addObject: attribute];
}

- (void)addAttributeWithName: (OFString*)name_
- (void)addAttributeWithName: (OFString*)name
		 stringValue: (OFString*)stringValue
{
	[self addAttributeWithName: name_
	[self addAttributeWithName: name
			 namespace: nil
		       stringValue: stringValue];
}

- (void)addAttributeWithName: (OFString*)name_
		   namespace: (OFString*)ns_
- (void)addAttributeWithName: (OFString*)name
		   namespace: (OFString*)namespace
		 stringValue: (OFString*)stringValue
{
	void *pool = objc_autoreleasePoolPush();

	[self addAttribute: [OFXMLAttribute attributeWithName: name_
						    namespace: ns_
	[self addAttribute: [OFXMLAttribute attributeWithName: name
						    namespace: namespace
						  stringValue: stringValue]];

	objc_autoreleasePoolPop(pool);
}

- (OFXMLAttribute*)attributeForName: (OFString*)attributeName
{
	OFXMLAttribute **objects = [attributes objects];
	size_t i, count = [attributes count];
	OFXMLAttribute **objects = [_attributes objects];
	size_t i, count = [_attributes count];

	for (i = 0; i < count; i++)
		if (objects[i]->ns == nil &&
		    [objects[i]->name isEqual: attributeName])
		if (objects[i]->_namespace == nil &&
		    [objects[i]->_name isEqual: attributeName])
			return [[objects[i] retain] autorelease];

	return nil;
}

- (OFXMLAttribute*)attributeForName: (OFString*)attributeName
			  namespace: (OFString*)attributeNS
{
	OFXMLAttribute **objects;
	size_t i, count;

	if (attributeNS == nil)
		return [self attributeForName: attributeName];

	objects = [attributes objects];
	count = [attributes count];
	objects = [_attributes objects];
	count = [_attributes count];

	for (i = 0; i < count; i++)
		if ([objects[i]->ns isEqual: attributeNS] &&
		    [objects[i]->name isEqual: attributeName])
		if ([objects[i]->_namespace isEqual: attributeNS] &&
		    [objects[i]->_name isEqual: attributeName])
			return [[objects[i] retain] autorelease];

	return nil;
}

- (void)removeAttributeForName: (OFString*)attributeName
{
	OFXMLAttribute **objects = [attributes objects];
	size_t i, count = [attributes count];
	OFXMLAttribute **objects = [_attributes objects];
	size_t i, count = [_attributes count];

	for (i = 0; i < count; i++) {
		if (objects[i]->ns == nil &&
		    [objects[i]->name isEqual: attributeName]) {
			[attributes removeObjectAtIndex: i];
		if (objects[i]->_namespace == nil &&
		    [objects[i]->_name isEqual: attributeName]) {
			[_attributes removeObjectAtIndex: i];

			return;
		}
	}
}

- (void)removeAttributeForName: (OFString*)attributeName
		     namespace: (OFString*)attributeNS
{
	OFXMLAttribute **objects;
	size_t i, count;

	if (attributeNS == nil)
		return [self removeAttributeForName: attributeName];

	objects = [attributes objects];
	count = [attributes count];
	objects = [_attributes objects];
	count = [_attributes count];

	for (i = 0; i < count; i++) {
		if ([objects[i]->ns isEqual: attributeNS] &&
		    [objects[i]->name isEqual: attributeName]) {
			[attributes removeObjectAtIndex: i];
		if ([objects[i]->_namespace isEqual: attributeNS] &&
		    [objects[i]->_name isEqual: attributeName]) {
			[_attributes removeObjectAtIndex: i];
				return;
		}
	}
}

- (void)setPrefix: (OFString*)prefix
     forNamespace: (OFString*)ns_
     forNamespace: (OFString*)namespace
{
	if ([prefix length] == 0)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];
	if (ns_ == nil)
		ns_ = @"";
	if (namespace == nil)
		namespace = @"";

	[namespaces setObject: prefix
		       forKey: ns_];
	[_namespaces setObject: prefix
			forKey: namespace];
}

- (void)bindPrefix: (OFString*)prefix
      forNamespace: (OFString*)ns_
      forNamespace: (OFString*)namespace
{
	[self setPrefix: prefix
	   forNamespace: ns_];
	   forNamespace: namespace];
	[self addAttributeWithName: prefix
			 namespace: @"http://www.w3.org/2000/xmlns/"
		       stringValue: ns_];
		       stringValue: namespace];
}

- (OFString*)defaultNamespace
{
	OF_GETTER(defaultNamespace, YES)
	OF_GETTER(_defaultNamespace, YES)
}

- (void)setDefaultNamespace: (OFString*)ns_
- (void)setDefaultNamespace: (OFString*)defaultNamespace
{
	OF_SETTER(defaultNamespace, ns_, YES, 1)
	OF_SETTER(_defaultNamespace, defaultNamespace, YES, 1)
}

- (void)addChild: (OFXMLNode*)child
{
	if ([child isKindOfClass: [OFXMLAttribute class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	if (children == nil)
		children = [[OFMutableArray alloc] init];
	if (_children == nil)
		_children = [[OFMutableArray alloc] init];

	[children addObject: child];
	[_children addObject: child];
}

- (void)insertChild: (OFXMLNode*)child
	    atIndex: (size_t)index
{
	if ([child isKindOfClass: [OFXMLAttribute class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	if (children == nil)
		children = [[OFMutableArray alloc] init];
	if (_children == nil)
		_children = [[OFMutableArray alloc] init];

	[children insertObject: child
		       atIndex: index];
	[_children insertObject: child
			atIndex: index];
}

- (void)insertChildren: (OFArray*)children_
- (void)insertChildren: (OFArray*)children
	       atIndex: (size_t)index
{
	void *pool = objc_autoreleasePoolPush();
	OFEnumerator *enumerator = [children_ objectEnumerator];
	OFEnumerator *enumerator = [children objectEnumerator];
	OFXMLNode *node;

	while ((node = [enumerator nextObject]) != nil)
		if ([node isKindOfClass: [OFXMLAttribute class]])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

	[children insertObjectsFromArray: children_
				 atIndex: index];
	[_children insertObjectsFromArray: children
				  atIndex: index];

	objc_autoreleasePoolPop(pool);
}

- (void)removeChild: (OFXMLNode*)child
{
	if ([child isKindOfClass: [OFXMLAttribute class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	[children removeObject: child];
	[_children removeObject: child];
}

- (void)removeChildAtIndex: (size_t)index
{
	[children removeObjectAtIndex: index];
	[_children removeObjectAtIndex: index];
}

- (void)replaceChild: (OFXMLNode*)child
	    withNode: (OFXMLNode*)node
{
	if ([node isKindOfClass: [OFXMLAttribute class]] ||
	    [child isKindOfClass: [OFXMLAttribute class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	[children replaceObject: child
		     withObject: node];
	[_children replaceObject: child
		      withObject: node];
}

- (void)replaceChildAtIndex: (size_t)index
		   withNode: (OFXMLNode*)node
{
	if ([node isKindOfClass: [OFXMLAttribute class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	[children replaceObjectAtIndex: index
			    withObject: node];
	[_children replaceObjectAtIndex: index
			     withObject: node];
}

- (OFXMLElement*)elementForName: (OFString*)elementName
{
	return [[self elementsForName: elementName] firstObject];
}

- (OFXMLElement*)elementForName: (OFString*)elementName
		      namespace: (OFString*)elementNS
{
	return [[self elementsForName: elementName
			    namespace: elementNS] firstObject];
}

- (OFArray*)elements
{
	OFMutableArray *ret = [OFMutableArray array];
	OFXMLElement **objects = [children objects];
	size_t i, count = [children count];
	OFXMLElement **objects = [_children objects];
	size_t i, count = [_children count];

	for (i = 0; i < count; i++)
		if ([objects[i] isKindOfClass: [OFXMLElement class]])
			[ret addObject: objects[i]];

	[ret makeImmutable];

	return ret;
}

- (OFArray*)elementsForName: (OFString*)elementName
{
	OFMutableArray *ret = [OFMutableArray array];
	OFXMLElement **objects = [children objects];
	size_t i, count = [children count];
	OFXMLElement **objects = [_children objects];
	size_t i, count = [_children count];

	for (i = 0; i < count; i++)
		if ([objects[i] isKindOfClass: [OFXMLElement class]] &&
		    objects[i]->ns == nil &&
		    [objects[i]->name isEqual: elementName])
		    objects[i]->_namespace == nil &&
		    [objects[i]->_name isEqual: elementName])
			[ret addObject: objects[i]];

	[ret makeImmutable];

	return ret;
}

- (OFArray*)elementsForNamespace: (OFString*)elementNS
{
	OFMutableArray *ret = [OFMutableArray array];
	OFXMLElement **objects = [children objects];
	size_t i, count = [children count];
	OFXMLElement **objects = [_children objects];
	size_t i, count = [_children count];

	for (i = 0; i < count; i++)
		if ([objects[i] isKindOfClass: [OFXMLElement class]] &&
		    objects[i]->name != nil &&
		    [objects[i]->ns isEqual: elementNS])
		    objects[i]->_name != nil &&
		    [objects[i]->_namespace isEqual: elementNS])
			[ret addObject: objects[i]];

	[ret makeImmutable];

	return ret;
}

- (OFArray*)elementsForName: (OFString*)elementName
		  namespace: (OFString*)elementNS
{
	OFMutableArray *ret;
	OFXMLElement **objects;
	size_t i, count;

	if (elementNS == nil)
		return [self elementsForName: elementName];

	ret = [OFMutableArray array];
	objects = [children objects];
	count = [children count];
	objects = [_children objects];
	count = [_children count];

	for (i = 0; i < count; i++)
		if ([objects[i] isKindOfClass: [OFXMLElement class]] &&
		    [objects[i]->ns isEqual: elementNS] &&
		    [objects[i]->name isEqual: elementName])
		    [objects[i]->_namespace isEqual: elementNS] &&
		    [objects[i]->_name isEqual: elementName])
			[ret addObject: objects[i]];

	[ret makeImmutable];

	return ret;
}

- (BOOL)isEqual: (id)object
{
	OFXMLElement *otherElement;
	OFXMLElement *element;

	if (![object isKindOfClass: [OFXMLElement class]])
		return NO;

	otherElement = object;
	element = object;

	if (otherElement->name != name && ![otherElement->name isEqual: name])
	if (element->_name != _name && ![element->_name isEqual: _name])
		return NO;
	if (element->_namespace != _namespace &&
	if (otherElement->ns != ns && ![otherElement->ns isEqual: ns])
	    ![element->_namespace isEqual: _namespace])
		return NO;
	if (otherElement->defaultNamespace != defaultNamespace &&
	    ![otherElement->defaultNamespace isEqual: defaultNamespace])
	if (element->_defaultNamespace != _defaultNamespace &&
	    ![element->_defaultNamespace isEqual: _defaultNamespace])
		return NO;
	if (otherElement->attributes != attributes &&
	    ![otherElement->attributes isEqual: attributes])
	if (element->_attributes != _attributes &&
	    ![element->_attributes isEqual: _attributes])
		return NO;
	if (otherElement->namespaces != namespaces &&
	    ![otherElement->namespaces isEqual: namespaces])
	if (element->_namespaces != _namespaces &&
	    ![element->_namespaces isEqual: _namespaces])
		return NO;
	if (otherElement->children != children &&
	    ![otherElement->children isEqual: children])
	if (element->_children != _children &&
	    ![element->_children isEqual: _children])
		return NO;

	return YES;
}

- (uint32_t)hash
{
	uint32_t hash;

	OF_HASH_INIT(hash);

	OF_HASH_ADD_HASH(hash, [name hash]);
	OF_HASH_ADD_HASH(hash, [ns hash]);
	OF_HASH_ADD_HASH(hash, [defaultNamespace hash]);
	OF_HASH_ADD_HASH(hash, [attributes hash]);
	OF_HASH_ADD_HASH(hash, [namespaces hash]);
	OF_HASH_ADD_HASH(hash, [children hash]);
	OF_HASH_ADD_HASH(hash, [_name hash]);
	OF_HASH_ADD_HASH(hash, [_namespace hash]);
	OF_HASH_ADD_HASH(hash, [_defaultNamespace hash]);
	OF_HASH_ADD_HASH(hash, [_attributes hash]);
	OF_HASH_ADD_HASH(hash, [_namespaces hash]);
	OF_HASH_ADD_HASH(hash, [_children hash]);

	OF_HASH_FINALIZE(hash);

	return hash;
}

- copy
{
	return [[[self class] alloc] initWithElement: self];
}

- (void)dealloc
{
	[name release];
	[ns release];
	[defaultNamespace release];
	[attributes release];
	[namespaces release];
	[children release];

	[super dealloc];
}
@end