ObjFW  Diff

Differences From Artifact [7faef87abe]:

To Artifact [5fc577399d]:


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







-
+


-
+



-
+



-
+







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

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

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

+ (instancetype)elementWithElement: (OFXMLElement *)element
{
	return [[[self alloc] initWithElement: element] autorelease];
}
175
176
177
178
179
180
181
182

183
184
185
186
187
188
189
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189







-
+








		_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];
			self.stringValue = stringValue;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
226
227
228
229
230
231
232
233
234


235
236
237
238

239
240
241
242
243
244
245
226
227
228
229
230
231
232


233
234
235
236
237

238
239
240
241
242
243
244
245







-
-
+
+



-
+







	pool = objc_autoreleasePoolPush();

	parser = [OFXMLParser parser];
	builder = [OFXMLElementBuilder elementBuilder];
	delegate = [[[OFXMLElement_OFXMLElementBuilderDelegate alloc] init]
	    autorelease];

	[parser setDelegate: builder];
	[builder setDelegate: delegate];
	parser.delegate = builder;
	builder.delegate = delegate;

	[parser parseString: string];

	if (![parser hasFinishedParsing])
	if (!parser.hasFinishedParsing)
		@throw [OFMalformedXMLException exceptionWithParser: parser];

	self = [delegate->_element retain];

	objc_autoreleasePoolPop(pool);

	return self;
258
259
260
261
262
263
264
265
266


267
268
269
270

271
272
273
274
275
276
277
258
259
260
261
262
263
264


265
266
267
268
269

270
271
272
273
274
275
276
277







-
-
+
+



-
+







	pool = objc_autoreleasePoolPush();

	parser = [OFXMLParser parser];
	builder = [OFXMLElementBuilder elementBuilder];
	delegate = [[[OFXMLElement_OFXMLElementBuilderDelegate alloc] init]
	    autorelease];

	[parser setDelegate: builder];
	[builder setDelegate: delegate];
	parser.delegate = builder;
	builder.delegate = delegate;

	[parser parseFile: path];

	if (![parser hasFinishedParsing])
	if (!parser.hasFinishedParsing)
		@throw [OFMalformedXMLException exceptionWithParser: parser];

	self = [delegate->_element retain];

	objc_autoreleasePoolPop(pool);

	return self;
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
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







-
-
+
+


-
+
-
-
-
-
-
+
+
+
+

-
+


-
-
+
+


-
-
+
+


-
+

-
+

-
+

-
+







	@try {
		void *pool = objc_autoreleasePoolPush();
		OFXMLElement *attributesElement, *namespacesElement;
		OFXMLElement *childrenElement;
		OFEnumerator *keyEnumerator, *objectEnumerator;
		OFString *key, *object;

		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
		if (![element.name isEqual: self.className] ||
		    ![element.namespace isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

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

		attributesElement = [[[element
		attributesElement = [[element
		    elementForName: @"attributes"
			 namespace: OF_SERIALIZATION_NS] elementsForNamespace:
		    OF_SERIALIZATION_NS] firstObject];
		namespacesElement = [[[element
		    OF_SERIALIZATION_NS].firstObject;
		namespacesElement = [[element
		    elementForName: @"namespaces"
			 namespace: OF_SERIALIZATION_NS] elementsForNamespace:
		    OF_SERIALIZATION_NS] firstObject];
		childrenElement = [[[element
		    OF_SERIALIZATION_NS].firstObject;
		childrenElement = [[element
		    elementForName: @"children"
			 namespace: OF_SERIALIZATION_NS] elementsForNamespace:
		    OF_SERIALIZATION_NS] firstObject];
		    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 && ![_attributes isKindOfClass:
		    [OFMutableArray class]]) || (_namespaces != nil &&
		    ![_namespaces isKindOfClass:
		    [OFMutableDictionary class]]) || (_children != nil &&
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
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







-
-
+
+








-
+







-
+







	return [[_children copy] autorelease];
}

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

	[self setChildren: [OFArray arrayWithObject:
	    [OFXMLCharacters charactersWithString: stringValue]]];
	self.children = [OFArray arrayWithObject:
	    [OFXMLCharacters charactersWithString: stringValue]];

	objc_autoreleasePoolPop(pool);
}

- (OFString *)stringValue
{
	OFMutableString *ret;

	if ([_children count] == 0)
	if (_children.count == 0)
		return @"";

	ret = [OFMutableString string];

	for (OFXMLNode *child in _children) {
		void *pool = objc_autoreleasePoolPush();

		[ret appendString: [child stringValue]];
		[ret appendString: child.stringValue];

		objc_autoreleasePoolPop(pool);
	}

	[ret makeImmutable];

	return ret;
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
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







-
+









-
+








-
-
+
+
-



-
-
+
+





-
+










-
-
-
+
+
+






-
+
-
-
+
-

-
+











-
-
+
+











-
-
-
+
+
+






-
-
+
+







		defaultNS = parent->_namespace;
	else if (parent != nil && parent->_defaultNamespace != nil)
		defaultNS = parent->_defaultNamespace;
	else
		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 && ![_namespace isEqual: defaultNS]) {
		length += [prefix UTF8StringLength] + 1;
		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]);
		memcpy(cString + i, prefix.UTF8String, prefix.UTF8StringLength);
		i += 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 && ((_namespace != nil &&
	    ![_namespace isEqual: defaultNS]) ||
	    (_namespace == nil && defaultNS != nil))) {
		length += [_namespace UTF8StringLength] + 9;
		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],
		    [_namespace UTF8StringLength]);
		i += [_namespace UTF8StringLength];
		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 =
		const char *attributeNameCString = attribute->_name.UTF8String;
		    [attribute->_name UTF8String];
		size_t attributeNameLength =
		size_t attributeNameLength = attribute->_name.UTF8StringLength;
		    [attribute->_name UTF8StringLength];
		OFString *attributePrefix = nil;
		OFString *tmp = [[attribute stringValue] stringByXMLEscaping];
		OFString *tmp = attribute.stringValue.stringByXMLEscaping;
		char delimiter = (attribute->_useDoubleQuotes ? '"' : '\'');

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

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

		@try {
			cString = [self resizeMemory: cString
						size: length];
		} @catch (id e) {
			[self freeMemory: cString];
			@throw e;
		}

		cString[i++] = ' ';
		if (attributePrefix != nil) {
			memcpy(cString + i, [attributePrefix UTF8String],
			    [attributePrefix UTF8StringLength]);
			i += [attributePrefix UTF8StringLength];
			memcpy(cString + i, attributePrefix.UTF8String,
			    attributePrefix.UTF8StringLength);
			i += attributePrefix.UTF8StringLength;
			cString[i++] = ':';
		}
		memcpy(cString + i, attributeNameCString, attributeNameLength);
		i += attributeNameLength;
		cString[i++] = '=';
		cString[i++] = delimiter;
		memcpy(cString + i, [tmp UTF8String], [tmp UTF8StringLength]);
		i += [tmp UTF8StringLength];
		memcpy(cString + i, tmp.UTF8String, tmp.UTF8StringLength);
		i += tmp.UTF8StringLength;
		cString[i++] = delimiter;

		objc_autoreleasePoolPop(pool2);
	}

	/* Children */
	if (_children != nil) {
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
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







-
-
+
+





-
+











-
-
+
+









-
+








-
-
-
+
+
+


-
-
+
+
-







					       indentation: ind
						     level: level + 1];
			else
				childString = [child
				    XMLStringWithIndentation: ind
						       level: level + 1];

			[tmp addItems: [childString UTF8String]
				count: [childString UTF8StringLength]];
			[tmp addItems: childString.UTF8String
				count: childString.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;
		}

		cString[i++] = '>';

		memcpy(cString + i, [tmp items], [tmp count]);
		i += [tmp count];
		memcpy(cString + i, tmp.items, tmp.count);
		i += tmp.count;

		if (indent) {
			memset(cString + i, ' ', level * indentation);
			i += level * indentation;
		}

		cString[i++] = '<';
		cString[i++] = '/';
		if (prefix != nil) {
			length += [prefix UTF8StringLength] + 1;
			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];
			memcpy(cString + i, prefix.UTF8String,
			    prefix.UTF8StringLength);
			i += prefix.UTF8StringLength;
			cString[i++] = ':';
		}
		memcpy(cString + i, [_name UTF8String],
		    [_name UTF8StringLength]);
		memcpy(cString + i, _name.UTF8String, _name.UTF8StringLength);
		i += _name.UTF8StringLength;
		i += [_name UTF8StringLength];
	} else
		cString[i++] = '/';

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

	objc_autoreleasePoolPop(pool);
697
698
699
700
701
702
703
704

705
706
707
708
709
710
711
692
693
694
695
696
697
698

699
700
701
702
703
704
705
706







-
+







}

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

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

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

	if (_namespace != nil)
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
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







-
+













-
+




-
+










-
+







	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) {
		OFXMLElement *namespacesElement;
		OFMutableDictionary *namespacesCopy =
		    [[_namespaces mutableCopy] autorelease];

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

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

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

820
821
822
823
824
825
826
827
828


829
830
831
832
833
834
835
815
816
817
818
819
820
821


822
823
824
825
826
827
828
829
830







-
-
+
+







			return attribute;

	return nil;
}

- (void)removeAttributeForName: (OFString *)attributeName
{
	OFXMLAttribute *const *objects = [_attributes objects];
	size_t count = [_attributes count];
	OFXMLAttribute *const *objects = _attributes.objects;
	size_t count = _attributes.count;

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

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







-
-
+
+













-
+







	size_t count;

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

	objects = [_attributes objects];
	count = [_attributes count];
	objects = _attributes.objects;
	count = _attributes.count;

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

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

	[_namespaces setObject: prefix
			forKey: namespace];
}
949
950
951
952
953
954
955
956

957
958
959
960
961
962
963


964
965
966
967
968
969
970
944
945
946
947
948
949
950

951
952
953
954
955
956


957
958
959
960
961
962
963
964
965







-
+





-
-
+
+








	[_children replaceObjectAtIndex: idx
			     withObject: node];
}

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

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

- (OFArray *)elements
{
	OFMutableArray OF_GENERIC(OFXMLElement *) *ret = [OFMutableArray array];

	for (OFXMLNode *child in _children)
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086






1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1069
1070
1071
1072
1073
1074
1075






1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092







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












- (uint32_t)hash
{
	uint32_t hash;

	OF_HASH_INIT(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_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;
}

- (id)copy
{
	return [[[self class] alloc] initWithElement: self];
}
@end