ObjFW  Diff

Differences From Artifact [19e272e9d6]:

To Artifact [ada192cadd]:


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

	[super dealloc];
}
@end

@implementation OFXMLElement
@synthesize name = _name, namespace = _namespace;
@synthesize defaultNamespace = _defaultNamespace;

+ (instancetype)elementWithName: (OFString *)name
{
	return [[[self alloc] initWithName: name] autorelease];
}

+ (instancetype)elementWithName: (OFString *)name







<







69
70
71
72
73
74
75

76
77
78
79
80
81
82

	[super dealloc];
}
@end

@implementation OFXMLElement
@synthesize name = _name, namespace = _namespace;


+ (instancetype)elementWithName: (OFString *)name
{
	return [[[self alloc] initWithName: name] autorelease];
}

+ (instancetype)elementWithName: (OFString *)name
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
		@throw e;
	}

	self = [self initWithName: element->_name
			namespace: element->_namespace];

	@try {
		[_defaultNamespace release];
		_defaultNamespace = [element->_defaultNamespace retain];
		[_attributes release];
		_attributes = [element->_attributes retain];
		[_namespaces release];
		_namespaces = [element->_namespaces retain];
		[_children release];
		_children = [element->_children retain];








<
<







206
207
208
209
210
211
212


213
214
215
216
217
218
219
		@throw e;
	}

	self = [self initWithName: element->_name
			namespace: element->_namespace];

	@try {


		[_attributes release];
		_attributes = [element->_attributes retain];
		[_namespaces release];
		_namespaces = [element->_namespaces retain];
		[_children release];
		_children = [element->_children retain];

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
		@throw e;
	}

	self = [self initWithName: element->_name
			namespace: element->_namespace];

	@try {
		[_defaultNamespace release];
		_defaultNamespace = [element->_defaultNamespace retain];
		[_attributes release];
		_attributes = [element->_attributes retain];
		[_namespaces release];
		_namespaces = [element->_namespaces retain];
		[_children release];
		_children = [element->_children retain];








<
<







258
259
260
261
262
263
264


265
266
267
268
269
270
271
		@throw e;
	}

	self = [self initWithName: element->_name
			namespace: element->_namespace];

	@try {


		[_attributes release];
		_attributes = [element->_attributes retain];
		[_namespaces release];
		_namespaces = [element->_namespaces retain];
		[_children release];
		_children = [element->_children retain];

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

	@try {
		OFXMLElement *attributesElement, *namespacesElement;
		OFXMLElement *childrenElement;
		OFEnumerator *keyEnumerator, *objectEnumerator;
		OFString *key, *object;

		[_defaultNamespace release];
		_defaultNamespace = nil;
		_defaultNamespace = [[element attributeForName:
		    @"defaultNamespace"].stringValue copy];

		attributesElement = [[element
		    elementForName: @"attributes"
			 namespace: OFSerializationNS]
		    elementsForNamespace: OFSerializationNS].firstObject;
		namespacesElement = [[element
		    elementForName: @"namespaces"
			 namespace: OFSerializationNS]







<
<
<
<
<







302
303
304
305
306
307
308





309
310
311
312
313
314
315

	@try {
		OFXMLElement *attributesElement, *namespacesElement;
		OFXMLElement *childrenElement;
		OFEnumerator *keyEnumerator, *objectEnumerator;
		OFString *key, *object;






		attributesElement = [[element
		    elementForName: @"attributes"
			 namespace: OFSerializationNS]
		    elementsForNamespace: OFSerializationNS].firstObject;
		namespacesElement = [[element
		    elementForName: @"namespaces"
			 namespace: OFSerializationNS]
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
	return self;
}

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

	[super dealloc];
}








<







376
377
378
379
380
381
382

383
384
385
386
387
388
389
	return self;
}

- (void)dealloc
{
	[_name release];
	[_namespace release];

	[_attributes release];
	[_namespaces release];
	[_children release];

	[super dealloc];
}

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
	}

	[ret makeImmutable];

	return ret;
}

- (OFString *)of_XMLStringWithParent: (OFXMLElement *)parent
			  namespaces: (OFDictionary *)allNS
			 indentation: (unsigned int)indentation
			       level: (unsigned int)level OF_DIRECT
{
	void *pool;
	char *cString;
	size_t length, i;
	OFString *prefix, *parentPrefix;
	OFString *ret;
	OFString *defaultNS;

	pool = objc_autoreleasePoolPush();

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

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

		tmp = [[allNS mutableCopy] autorelease];

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

		allNS = tmp;
	} else
		allNS = _namespaces;

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

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

	i = 0;
	length = _name.UTF8StringLength + 3 + (level * indentation);
	cString = OFAllocMemory(length, 1);

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

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

		if (prefix != nil && ![_namespace isEqual: defaultNS]) {
			length += prefix.UTF8StringLength + 1;
			cString = OFResizeMemory(cString, length, 1);

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

		memcpy(cString + i, _name.UTF8String, _name.UTF8StringLength);
		i += _name.UTF8StringLength;

		/* xmlns if necessary */
		if (prefix == nil && ((_namespace != nil &&
		    ![_namespace isEqual: defaultNS]) ||
		    (_namespace == nil && defaultNS != nil))) {
			length += _namespace.UTF8StringLength + 9;
			cString = OFResizeMemory(cString, length, 1);

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


		}

		/* Attributes */
		for (OFXMLAttribute *attribute in _attributes) {
			void *pool2 = objc_autoreleasePoolPush();
			const char *attributeNameCString =
			    attribute->_name.UTF8String;
			size_t attributeNameLength =
			    attribute->_name.UTF8StringLength;
			OFString *attributePrefix = nil;
			OFString *tmp =
			    attribute.stringValue.stringByXMLEscaping;
			char delimiter = (attribute->_useDoubleQuotes
			    ? '"' : '\'');

			if (attribute->_namespace != nil &&
			    (attributePrefix = [allNS objectForKey:
			    attribute->_namespace]) == nil)
				@throw [OFUnboundNamespaceException
				    exceptionWithNamespace: attribute.namespace
						   element: self];

			length += attributeNameLength + (attributePrefix != nil
			    ? attributePrefix.UTF8StringLength + 1 : 0) +
			    tmp.UTF8StringLength + 4;







|
|
|
|




|
<
<



<
<
<
<




















<
<
<
<
<
<
<











|













|
|
<









>
>
















|
|







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
	}

	[ret makeImmutable];

	return ret;
}

- (OFString *)of_XMLStringWithDefaultNS: (OFString *)defaultNS
			     namespaces: (OFDictionary *)allNS
			    indentation: (unsigned int)indentation
				  level: (unsigned int)level OF_DIRECT
{
	void *pool;
	char *cString;
	size_t length, i;
	OFString *prefix, *ret;



	pool = objc_autoreleasePoolPush();





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

		tmp = [[allNS mutableCopy] autorelease];

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

		allNS = tmp;
	} else
		allNS = _namespaces;

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








	i = 0;
	length = _name.UTF8StringLength + 3 + (level * indentation);
	cString = OFAllocMemory(length, 1);

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

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

		if (prefix.length > 0) {
			length += prefix.UTF8StringLength + 1;
			cString = OFResizeMemory(cString, length, 1);

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

		memcpy(cString + i, _name.UTF8String, _name.UTF8StringLength);
		i += _name.UTF8StringLength;

		/* xmlns if necessary */
		if (prefix.length == 0 && defaultNS != _namespace &&
		    ![defaultNS isEqual: _namespace]) {

			length += _namespace.UTF8StringLength + 9;
			cString = OFResizeMemory(cString, length, 1);

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

			defaultNS = _namespace;
		}

		/* Attributes */
		for (OFXMLAttribute *attribute in _attributes) {
			void *pool2 = objc_autoreleasePoolPush();
			const char *attributeNameCString =
			    attribute->_name.UTF8String;
			size_t attributeNameLength =
			    attribute->_name.UTF8StringLength;
			OFString *attributePrefix = nil;
			OFString *tmp =
			    attribute.stringValue.stringByXMLEscaping;
			char delimiter = (attribute->_useDoubleQuotes
			    ? '"' : '\'');

			if (attribute->_namespace != nil &&
			    [(attributePrefix = [allNS objectForKey:
			    attribute->_namespace]) length] == 0)
				@throw [OFUnboundNamespaceException
				    exceptionWithNamespace: attribute.namespace
						   element: self];

			length += attributeNameLength + (attributePrefix != nil
			    ? attributePrefix.UTF8StringLength + 1 : 0) +
			    tmp.UTF8StringLength + 4;
601
602
603
604
605
606
607
608
609
610
611

612
613
614

615
616

617
618
619
620
621
622
623
				unsigned int ind = (indent ? indentation : 0);

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

				if ([child isKindOfClass: [OFXMLElement class]])
					childString = [(OFXMLElement *)child
					    of_XMLStringWithParent: self
							namespaces: allNS
						       indentation: ind
							     level: level + 1];

				else
					childString = [child
					    XMLStringWithIndentation: ind

							       level: level +
								      1];


				[tmp addItems: childString.UTF8String
					count: childString.UTF8StringLength];
			}

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







|
|
|
|
>
|
|
<
>
|
|
>







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
				unsigned int ind = (indent ? indentation : 0);

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

				if ([child isKindOfClass: [OFXMLElement class]])
					childString = [(OFXMLElement *)child
					    of_XMLStringWithDefaultNS: defaultNS
							   namespaces: allNS
							  indentation: ind
								level: level +
									   1];
				else {
					childString = child.XMLString;

					for (unsigned int j = 0;
					    j < ind * (level + 1); j++)
						[tmp addItem: " "];
				}

				[tmp addItems: childString.UTF8String
					count: childString.UTF8StringLength];
			}

			if (indent)
				[tmp addItem: "\n"];
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
			if (indent) {
				memset(cString + i, ' ', level * indentation);
				i += level * indentation;
			}

			cString[i++] = '<';
			cString[i++] = '/';
			if (prefix != nil) {
				length += prefix.UTF8StringLength + 1;
				cString = OFResizeMemory(cString, length, 1);

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







|







613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
			if (indent) {
				memset(cString + i, ' ', level * indentation);
				i += level * indentation;
			}

			cString[i++] = '<';
			cString[i++] = '/';
			if (prefix.length > 0) {
				length += prefix.UTF8StringLength + 1;
				cString = OFResizeMemory(cString, length, 1);

				memcpy(cString + i, prefix.UTF8String,
				    prefix.UTF8StringLength);
				i += prefix.UTF8StringLength;
				cString[i++] = ':';
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
		OFFreeMemory(cString);
	}
	return ret;
}

- (OFString *)XMLString
{
	return [self of_XMLStringWithParent: nil
				 namespaces: nil
				indentation: 0
				      level: 0];
}

- (OFString *)XMLStringWithIndentation: (unsigned int)indentation
{
	return [self of_XMLStringWithParent: nil
				 namespaces: nil
				indentation: indentation
				      level: 0];
}

- (OFString *)XMLStringWithIndentation: (unsigned int)indentation
				 level: (unsigned int)level
{
	return [self of_XMLStringWithParent: nil
				 namespaces: nil
				indentation: indentation
				      level: level];
}

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

	element = [OFXMLElement elementWithName: self.className
				      namespace: OFSerializationNS];

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

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

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

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

		attributesElement =
		    [OFXMLElement elementWithName: @"attributes"
					namespace: OFSerializationNS];
		[attributesElement addChild:







|
|
|
|




|
|
|
|


|
|

|
|
|
|

















<
<
<
<







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
		OFFreeMemory(cString);
	}
	return ret;
}

- (OFString *)XMLString
{
	return [self of_XMLStringWithDefaultNS: nil
				    namespaces: nil
				   indentation: 0
					 level: 0];
}

- (OFString *)XMLStringWithIndentation: (unsigned int)indentation
{
	return [self of_XMLStringWithDefaultNS: nil
				    namespaces: nil
				   indentation: indentation
					 level: 0];
}

- (OFString *)XMLStringWithDefaultNamespace: (OFString *)defaultNS
				indentation: (unsigned int)indentation
{
	return [self of_XMLStringWithDefaultNS: defaultNS
				    namespaces: nil
				   indentation: indentation
					 level: 0];
}

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

	element = [OFXMLElement elementWithName: self.className
				      namespace: OFSerializationNS];

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

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





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

		attributesElement =
		    [OFXMLElement elementWithName: @"attributes"
					namespace: OFSerializationNS];
		[attributesElement addChild:
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
	}
}

- (void)setPrefix: (OFString *)prefix forNamespace: (OFString *)namespace
{
	if (prefix.length == 0)
		@throw [OFInvalidArgumentException exception];
	if (namespace == nil)
		namespace = @"";

	[_namespaces setObject: prefix forKey: namespace];
}

- (void)bindPrefix: (OFString *)prefix forNamespace: (OFString *)namespace
{
	[self setPrefix: prefix forNamespace: namespace];







<
<







829
830
831
832
833
834
835


836
837
838
839
840
841
842
	}
}

- (void)setPrefix: (OFString *)prefix forNamespace: (OFString *)namespace
{
	if (prefix.length == 0)
		@throw [OFInvalidArgumentException exception];



	[_namespaces setObject: prefix forKey: namespace];
}

- (void)bindPrefix: (OFString *)prefix forNamespace: (OFString *)namespace
{
	[self setPrefix: prefix forNamespace: namespace];
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
	element = object;

	if (element->_name != _name && ![element->_name isEqual: _name])
		return false;
	if (element->_namespace != _namespace &&
	    ![element->_namespace isEqual: _namespace])
		return false;
	if (element->_defaultNamespace != _defaultNamespace &&
	    ![element->_defaultNamespace isEqual: _defaultNamespace])
		return false;
	if (element->_attributes != _attributes &&
	    ![element->_attributes isEqual: _attributes])
		return false;
	if (element->_namespaces != _namespaces &&
	    ![element->_namespaces isEqual: _namespaces])
		return false;
	if (element->_children != _children &&







<
<
<







1007
1008
1009
1010
1011
1012
1013



1014
1015
1016
1017
1018
1019
1020
	element = object;

	if (element->_name != _name && ![element->_name isEqual: _name])
		return false;
	if (element->_namespace != _namespace &&
	    ![element->_namespace isEqual: _namespace])
		return false;



	if (element->_attributes != _attributes &&
	    ![element->_attributes isEqual: _attributes])
		return false;
	if (element->_namespaces != _namespaces &&
	    ![element->_namespaces isEqual: _namespaces])
		return false;
	if (element->_children != _children &&
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
{
	unsigned long hash;

	OFHashInit(&hash);

	OFHashAddHash(&hash, _name.hash);
	OFHashAddHash(&hash, _namespace.hash);
	OFHashAddHash(&hash, _defaultNamespace.hash);
	OFHashAddHash(&hash, _attributes.hash);
	OFHashAddHash(&hash, _namespaces.hash);
	OFHashAddHash(&hash, _children.hash);

	OFHashFinalize(&hash);

	return hash;
}

- (id)copy
{
	OFXMLElement *copy = [[OFXMLElement alloc] of_init];
	@try {
		copy->_name = [_name copy];
		copy->_namespace = [_namespace copy];
		copy->_defaultNamespace = [_defaultNamespace copy];
		copy->_attributes = [_attributes mutableCopy];
		copy->_namespaces = [_namespaces mutableCopy];
		copy->_children = [_children mutableCopy];
	} @catch (id e) {
		[copy release];
		@throw e;
	}

	return copy;
}
@end







<















<











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
{
	unsigned long hash;

	OFHashInit(&hash);

	OFHashAddHash(&hash, _name.hash);
	OFHashAddHash(&hash, _namespace.hash);

	OFHashAddHash(&hash, _attributes.hash);
	OFHashAddHash(&hash, _namespaces.hash);
	OFHashAddHash(&hash, _children.hash);

	OFHashFinalize(&hash);

	return hash;
}

- (id)copy
{
	OFXMLElement *copy = [[OFXMLElement alloc] of_init];
	@try {
		copy->_name = [_name copy];
		copy->_namespace = [_namespace copy];

		copy->_attributes = [_attributes mutableCopy];
		copy->_namespaces = [_namespaces mutableCopy];
		copy->_children = [_children mutableCopy];
	} @catch (id e) {
		[copy release];
		@throw e;
	}

	return copy;
}
@end