ObjFW  Check-in [14991e7035]

Overview
Comment:Add -[XMLStringWithIndentation:] to OFXMLElement.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 14991e703540a409bc368a78b7224d8fee5eb5f99efd6e3836036551e622b450
User & Date: js on 2011-06-05 23:59:38
Other Links: manifest | tags
Context
2011-06-06
00:02
Add serialization.xml to Xcode project. check-in: 26da62682d user: js tags: trunk
2011-06-05
23:59
Add -[XMLStringWithIndentation:] to OFXMLElement. check-in: 14991e7035 user: js tags: trunk
22:26
Nicer API for serialization. check-in: 29988c434b user: js tags: trunk
Changes

Modified src/OFObject+Serialization.m from [8fb7c383c5] to [3d621a2dfd].

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	element = [(id)self XMLElementBySerializing];

	root = [OFXMLElement elementWithName: @"serialization"
				   namespace: OF_SERIALIZATION_NS];
	[root addChild: element];

	ret = [@"<?xml version='1.0' encoding='UTF-8'?>\n"
	    stringByAppendingString: [root XMLString]];
	[ret retain];

	@try {
		[pool release];
	} @catch (id e) {
		[ret release];
	}

	return [ret autorelease];
}
@end







|











44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	element = [(id)self XMLElementBySerializing];

	root = [OFXMLElement elementWithName: @"serialization"
				   namespace: OF_SERIALIZATION_NS];
	[root addChild: element];

	ret = [@"<?xml version='1.0' encoding='UTF-8'?>\n"
	    stringByAppendingString: [root XMLStringWithIndentation: 2]];
	[ret retain];

	@try {
		[pool release];
	} @catch (id e) {
		[ret release];
	}

	return [ret autorelease];
}
@end

Modified src/OFXMLElement.h from [d808ad9d11] to [6238b06167].

252
253
254
255
256
257
258
259
260
261
262







263
264
265
266
267
268
269
/**
 * \return A string with the string value of all children concatenated
 */
- (OFString*)stringValue;

/**
 * \return A new autoreleased OFString representing the OFXMLElement as an
 * XML string
 */
- (OFString*)XMLString;








/**
 * Adds the specified attribute.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param attribute The attribute to add







|



>
>
>
>
>
>
>







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 * \return A string with the string value of all children concatenated
 */
- (OFString*)stringValue;

/**
 * \return A new autoreleased OFString representing the OFXMLElement as an
 *	   XML string
 */
- (OFString*)XMLString;

/**
 * \param indentation The indentation for the XML string
 * \return A new autoreleased OFString representing the OFXMLElement as an
 *	   XML string with indentation.
 */
- (OFString*)XMLStringWithIndentation: (unsigned int)indentation;

/**
 * Adds the specified attribute.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param attribute The attribute to add

Modified src/OFXMLElement.m from [07cdc30ecf] to [c30a5b5e0c].

433
434
435
436
437
438
439


440
441
442
443
444
445
446
447
448

449
450
451
452
453
454
455
	 * 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];








>
>









>







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
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFString class];
	return ret;
}

- (OFString*)_XMLStringWithParent: (OFXMLElement*)parent
		      indentation: (unsigned int)indentation
			    level: (size_t)level
{
	OFAutoreleasePool *pool, *pool2;
	char *cString;
	size_t length, i, j, attributesCount;
	OFString *prefix, *parentPrefix;
	OFXMLAttribute **attributesCArray;
	OFString *ret, *tmp;
	OFMutableDictionary *allNamespaces;
	OFString *defaultNS;
	BOOL indentAfter = YES;

	if (characters != nil)
		return [characters stringByXMLEscaping];

	if (CDATA != nil)
		return [OFString stringWithFormat: @"<![CDATA[%@]]>", CDATA];

481
482
483
484
485
486
487
488
489



490
491
492
493
494
495
496
		defaultNS = parent->ns;
	else if (parent != nil && parent->defaultNamespace != nil)
		defaultNS = parent->defaultNamespace;
	else
		defaultNS = defaultNamespace;

	i = 0;
	length = [name cStringLength] + 3;
	cString = [self allocMemoryWithSize: length];




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

	if (prefix != nil && ![ns isEqual: defaultNS]) {
		length += [prefix cStringLength] + 1;
		@try {







|

>
>
>







484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
		defaultNS = parent->ns;
	else if (parent != nil && parent->defaultNamespace != nil)
		defaultNS = parent->defaultNamespace;
	else
		defaultNS = defaultNamespace;

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

	for (j = 0; j < level * indentation; j++)
		cString[i++] = ' ';

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

	if (prefix != nil && ![ns isEqual: defaultNS]) {
		length += [prefix cStringLength] + 1;
		@try {
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
		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];







|
>
>
>

|
>
>
>

|
>
>
>
>
>
>
>
|
>









>


>
>
>
>
>







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
		size_t childrenCount = [children count];
		IMP append;
		SEL appendSel = @selector(appendCStringWithoutUTF8Checking:);

		tmp = [OFMutableString string];
		append = [tmp methodForSelector: appendSel];

		for (j = 0; j < childrenCount; j++) {
			if (indentation > 0 && childrenCArray[j]->name != nil)
				append(tmp, appendSel, "\n");

			append(tmp, appendSel,
			    [[childrenCArray[j]
			    _XMLStringWithParent: self
				     indentation: indentation
					   level: level + 1]
			    cString]);
		}

		if (indentation > 0 && childrenCount > 0 &&
		    childrenCArray[j - 1]->name != nil)
			append(tmp, appendSel, "\n");
		else
			indentAfter = NO;

		length += [tmp cStringLength] + [name cStringLength] + 2 +
		    (indentAfter ? level * indentation : 0);
		@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];

		if (indentAfter)
			for (j = 0; j < level * indentation; j++)
				cString[i++] = ' ';

		cString[i++] = '<';
		cString[i++] = '/';
		if (prefix != nil) {
			length += [prefix cStringLength] + 1;
			@try {
				cString = [self resizeMemory: cString
						      toSize: length];
638
639
640
641
642
643
644
645









646
647
648
649
650
651
652
653
654
655
656
657
		[self freeMemory: cString];
	}
	return ret;
}

- (OFString*)XMLString
{
	return [self _XMLStringWithParent: nil];









}

- (OFString*)description
{
	return [self XMLString];
}

- (OFXMLElement*)XMLElementBySerializing
{
	OFAutoreleasePool *pool;
	OFXMLElement *element;








|
>
>
>
>
>
>
>
>
>




|







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
		[self freeMemory: cString];
	}
	return ret;
}

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

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

- (OFString*)description
{
	return [self XMLStringWithIndentation: 2];
}

- (OFXMLElement*)XMLElementBySerializing
{
	OFAutoreleasePool *pool;
	OFXMLElement *element;

Modified tests/OFSerializationTests.m from [f8cccb7ff5] to [527ae62e46].

26
27
28
29
30
31
32
33
34
35
36
37
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
#import "OFURL.h"
#import "OFAutoreleasePool.h"
#import "OFXMLElement.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFSerialization";
static const OFString *expected = @"<?xml version='1.0' encoding='UTF-8'?>\n"
    @"<serialization xmlns='https://webkeks.org/objfw/serialization'>"
    @"<object class='OFMutableDictionary'><pair><key><object class='OFArray'>"
    @"<object class='OFString'>Qu&quot;xbar\ntest</object>"
    @"<object class='OFNumber' type='signed'>1234</object>"
    @"<object class='OFMutableString'>asd</object>"
    @"<object class='OFDate'><seconds>1234</seconds>"
    @"<microseconds>5678</microseconds></object></object></key><value>"
    @"<object class='OFString'>Hello</object></value></pair><pair><key>"
    @"<object class='OFString'>Blub</object></key><value>"
    @"<object class='OFString'>B&quot;la</object></value></pair><pair><key>"
    @"<object class='OFList'><object class='OFString'>Hello</object>"
    @"<object class='OFString'>Wo&#xD;ld!\nHow are you?</object>"
    @"<object class='OFURL'>https://webkeks.org/</object>"
    @"<object class='OFXMLElement'><name>x</name><namespaces>"
    @"<object class='OFMutableDictionary'><pair><key>"
    @"<object class='OFString'>http://www.w3.org/2000/xmlns/</object></key>"
    @"<value><object class='OFString'>xmlns</object></value></pair><pair><key>"
    @"<object class='OFString'>http://www.w3.org/XML/1998/namespace</object>"
    @"</key><value><object class='OFString'>xml</object></value></pair>"
    @"</object></namespaces><children><object class='OFMutableArray'>"
    @"<object class='OFXMLElement'><name>y</name><namespaces>"
    @"<object class='OFMutableDictionary'><pair><key><object class='OFString'>"
    @"http://www.w3.org/2000/xmlns/</object></key><value>"
    @"<object class='OFString'>xmlns</object></value></pair><pair><key>"
    @"<object class='OFString'>http://www.w3.org/XML/1998/namespace</object>"
    @"</key><value><object class='OFString'>xml</object></value></pair>"
    @"</object></namespaces></object></object></children></object></object>"
    @"</key><value><object class='OFString'>list</object></value></pair>"
    @"</object></serialization>";

@implementation TestsAppDelegate (SerializationTests)
- (void)serializationTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableDictionary *d = [OFMutableDictionary dictionary];
	OFMutableArray *a = [OFMutableArray array];







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







26
27
28
29
30
31
32






























33
34
35
36
37
38
39
#import "OFURL.h"
#import "OFAutoreleasePool.h"
#import "OFXMLElement.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFSerialization";































@implementation TestsAppDelegate (SerializationTests)
- (void)serializationTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableDictionary *d = [OFMutableDictionary dictionary];
	OFMutableArray *a = [OFMutableArray array];
86
87
88
89
90
91
92
93

94
95
96
97
98
99
100
	[l appendObject: [OFURL URLWithString: @"https://webkeks.org/"]];
	[l appendObject: [OFXMLElement elementWithXMLString: @"<x><y/></x>"]];

	[d setObject: @"list"
	      forKey: l];

	TEST(@"-[stringBySerializing]",
	    (s = [d stringBySerializing]) && [s isEqual: expected])


	TEST(@"-[objectByDeserializing]",
	    [[s objectByDeserializing] isEqual: d])

	[pool drain];
}
@end







|
>







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	[l appendObject: [OFURL URLWithString: @"https://webkeks.org/"]];
	[l appendObject: [OFXMLElement elementWithXMLString: @"<x><y/></x>"]];

	[d setObject: @"list"
	      forKey: l];

	TEST(@"-[stringBySerializing]",
	    (s = [d stringBySerializing]) && [s isEqual:
	    [OFString stringWithContentsOfFile: @"serialization.xml"]])

	TEST(@"-[objectByDeserializing]",
	    [[s objectByDeserializing] isEqual: d])

	[pool drain];
}
@end

Modified tests/OFXMLElementTests.m from [9cc1013df2] to [0c7d40f6d8].

131
132
133
134
135
136
137





138
139
140

	TEST(@"-[isEqual:]",
	    [[OFXMLElement elementWithXMLString: @"<foo bar='asd'/>"] isEqual:
	    [OFXMLElement elementWithXMLString: @"<foo bar='asd'></foo>"]] &&
	    [[OFXMLElement elementWithXMLString: @"<x><y/></x>"] isEqual:
	    [OFXMLElement elementWithXMLString: @"<x><y></y></x>"]])






	[pool drain];
}
@end







>
>
>
>
>



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

	TEST(@"-[isEqual:]",
	    [[OFXMLElement elementWithXMLString: @"<foo bar='asd'/>"] isEqual:
	    [OFXMLElement elementWithXMLString: @"<foo bar='asd'></foo>"]] &&
	    [[OFXMLElement elementWithXMLString: @"<x><y/></x>"] isEqual:
	    [OFXMLElement elementWithXMLString: @"<x><y></y></x>"]])

	TEST(@"-[XMLStringWithIndentation:]",
	    [[[OFXMLElement elementWithXMLString: @"<x><y><z>a\nb</z></y></x>"]
	    XMLStringWithIndentation: 2] isEqual:
	    @"<x>\n  <y>\n    <z>a\nb</z>\n  </y>\n</x>"])

	[pool drain];
}
@end

Added tests/serialization.xml version [df9f9a2c48].



























































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
<?xml version='1.0' encoding='UTF-8'?>
<serialization xmlns='https://webkeks.org/objfw/serialization'>
  <object class='OFMutableDictionary'>
    <pair>
      <key>
        <object class='OFArray'>
          <object class='OFString'>Qu&quot;xbar
test</object>
          <object class='OFNumber' type='signed'>1234</object>
          <object class='OFMutableString'>asd</object>
          <object class='OFDate'>
            <seconds>1234</seconds>
            <microseconds>5678</microseconds>
          </object>
        </object>
      </key>
      <value>
        <object class='OFString'>Hello</object>
      </value>
    </pair>
    <pair>
      <key>
        <object class='OFString'>Blub</object>
      </key>
      <value>
        <object class='OFString'>B&quot;la</object>
      </value>
    </pair>
    <pair>
      <key>
        <object class='OFList'>
          <object class='OFString'>Hello</object>
          <object class='OFString'>Wo&#xD;ld!
How are you?</object>
          <object class='OFURL'>https://webkeks.org/</object>
          <object class='OFXMLElement'>
            <name>x</name>
            <namespaces>
              <object class='OFMutableDictionary'>
                <pair>
                  <key>
                    <object class='OFString'>http://www.w3.org/2000/xmlns/</object>
                  </key>
                  <value>
                    <object class='OFString'>xmlns</object>
                  </value>
                </pair>
                <pair>
                  <key>
                    <object class='OFString'>http://www.w3.org/XML/1998/namespace</object>
                  </key>
                  <value>
                    <object class='OFString'>xml</object>
                  </value>
                </pair>
              </object>
            </namespaces>
            <children>
              <object class='OFMutableArray'>
                <object class='OFXMLElement'>
                  <name>y</name>
                  <namespaces>
                    <object class='OFMutableDictionary'>
                      <pair>
                        <key>
                          <object class='OFString'>http://www.w3.org/2000/xmlns/</object>
                        </key>
                        <value>
                          <object class='OFString'>xmlns</object>
                        </value>
                      </pair>
                      <pair>
                        <key>
                          <object class='OFString'>http://www.w3.org/XML/1998/namespace</object>
                        </key>
                        <value>
                          <object class='OFString'>xml</object>
                        </value>
                      </pair>
                    </object>
                  </namespaces>
                </object>
              </object>
            </children>
          </object>
        </object>
      </key>
      <value>
        <object class='OFString'>list</object>
      </value>
    </pair>
  </object>
</serialization>