ObjFW  Check-in [e2499be5c1]

Overview
Comment:Add support for CDATA to OFXMLElement.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e2499be5c1ed138822d40e3d888089bd45315429bab06bdfd1aaeaec3139973d
User & Date: js on 2010-07-02 20:56:35
Other Links: manifest | tags
Context
2010-07-02
21:05
Rename +[xmlParser] to +[parser]. check-in: c1f392e534 user: js tags: trunk
20:56
Add support for CDATA to OFXMLElement. check-in: e2499be5c1 user: js tags: trunk
20:45
Rename +[elementWithText:] to +[elementWithCharacters:]. check-in: 4ea5f3f7fd user: js tags: trunk
Changes

Modified src/OFXMLElement.h from [c4cba39561] to [e29f433914].

22
23
24
25
26
27
28

29
30
31
32
33
34
35
	OFString *name;
	OFString *namespace;
	OFString *defaultNamespace;
	OFMutableArray *attributes;
	OFMutableDictionary *namespaces;
	OFMutableArray *children;
	OFString *characters;

	OFMutableString *comment;
}

/**
 * \param name The name for the element
 * \return A new autoreleased OFXMLElement with the specified element name
 */







>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	OFString *name;
	OFString *namespace;
	OFString *defaultNamespace;
	OFMutableArray *attributes;
	OFMutableDictionary *namespaces;
	OFMutableArray *children;
	OFString *characters;
	OFString *cdata;
	OFMutableString *comment;
}

/**
 * \param name The name for the element
 * \return A new autoreleased OFXMLElement with the specified element name
 */
69
70
71
72
73
74
75








76
77
78
79
80
81
82
 *
 * \param chars The characters the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified
 *	   characters
 */
+ elementWithCharacters: (OFString*)chars;









/**
 * Creates a new element, only consisting of the specified comment.
 *
 * \param comment The comment the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified comment
 */
+ elementWithComment: (OFString*)comment;







>
>
>
>
>
>
>
>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 *
 * \param chars The characters the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified
 *	   characters
 */
+ elementWithCharacters: (OFString*)chars;

/**
 * Creates a new element, only consisting of the specified CDATA.
 *
 * \param cdata The CDATA the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified CDATA
 */
+ elementWithCDATA: (OFString*)cdata;

/**
 * Creates a new element, only consisting of the specified comment.
 *
 * \param comment The comment the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified comment
 */
+ elementWithComment: (OFString*)comment;
133
134
135
136
137
138
139









140
141
142
143
144
145
146
 * specified characters.
 *
 * \param chars The characters the element represents
 * \return An initialized OFXMLElement consisting of the specified characters
 */
- initWithCharacters: (OFString*)chars;










/**
 * Initializes an already allocated OFXMLElement so that it only consists of the
 * specified comment.
 *
 * \param comment The comment the element represents
 * \return An initialized OFXMLElement consisting of the specified comment
 */







>
>
>
>
>
>
>
>
>







142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
 * specified characters.
 *
 * \param chars The characters the element represents
 * \return An initialized OFXMLElement consisting of the specified characters
 */
- initWithCharacters: (OFString*)chars;

/**
 * Initializes an already allocated OFXMLElement so that it only consists of the
 * specified CDATA.
 *
 * \param cdata The CDATA the element represents
 * \return An initialized OFXMLElement consisting of the specified CDATA
 */
- initWithCDATA: (OFString*)cdata;

/**
 * Initializes an already allocated OFXMLElement so that it only consists of the
 * specified comment.
 *
 * \param comment The comment the element represents
 * \return An initialized OFXMLElement consisting of the specified comment
 */

Modified src/OFXMLElement.m from [05eb8e5213] to [917291b8e9].

51
52
53
54
55
56
57





58
59
60
61
62
63
64
			       stringValue: stringval] autorelease];
}

+ elementWithCharacters: (OFString*)chars
{
	return [[[self alloc] initWithCharacters: chars] autorelease];
}






+ elementWithComment: (OFString*)comment
{
	return [[[self alloc] initWithComment: comment] autorelease];
}

- init







>
>
>
>
>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
			       stringValue: stringval] autorelease];
}

+ elementWithCharacters: (OFString*)chars
{
	return [[[self alloc] initWithCharacters: chars] autorelease];
}

+ elementWithCDATA: (OFString*)cdata
{
	return [[[self alloc] initWithCDATA: cdata] autorelease];
}

+ elementWithComment: (OFString*)comment
{
	return [[[self alloc] initWithComment: comment] autorelease];
}

- init
117
118
119
120
121
122
123









124
125
126
127
128
129
130
{
	self = [super init];

	characters = [chars copy];

	return self;
}










- initWithComment: (OFString*)comment_
{
	self = [super init];

	comment = [comment_ copy];








>
>
>
>
>
>
>
>
>







122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
	self = [super init];

	characters = [chars copy];

	return self;
}

- initWithCDATA: (OFString*)cdata_
{
	self = [super init];

	cdata = [cdata_ copy];

	return self;
}

- initWithComment: (OFString*)comment_
{
	self = [super init];

	comment = [comment_ copy];

142
143
144
145
146
147
148




149
150
151
152
153
154
155
	OFString *ret, *tmp;
	OFMutableDictionary *all_namespaces;
	OFString *def_ns;

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





	if (comment != nil) {
		OFMutableString *str;

		str = [OFMutableString stringWithString: @"<!--"];
		[str appendString: comment];
		[str appendString: @"-->"];








>
>
>
>







156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	OFString *ret, *tmp;
	OFMutableDictionary *all_namespaces;
	OFString *def_ns;

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

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

	if (comment != nil) {
		OFMutableString *str;

		str = [OFMutableString stringWithString: @"<!--"];
		[str appendString: comment];
		[str appendString: @"-->"];

429
430
431
432
433
434
435

436
437
438
439
440
{
	[name release];
	[namespace release];
	[attributes release];
	[namespaces release];
	[children release];
	[characters release];

	[comment release];

	[super dealloc];
}
@end







>





447
448
449
450
451
452
453
454
455
456
457
458
459
{
	[name release];
	[namespace release];
	[attributes release];
	[namespaces release];
	[children release];
	[characters release];
	[cdata release];
	[comment release];

	[super dealloc];
}
@end

Modified tests/OFXMLElementTests.m from [3f6cbf99e7] to [f1b03ff45a].

55
56
57
58
59
60
61




62
63
64
65
66
67
68
		    forNamespace: @"urn:objfw:test"]) &&
	    [[elem[3] string] isEqual: @"<objfw-test:foo>x</objfw-test:foo>"])

	TEST(@"+[elementWithCharacters:]",
	    (elem[3] = [OFXMLElement elementWithCharacters: @"<foo>"]) &&
	    [[elem[3] string] isEqual: @"&lt;foo&gt;"])





	TEST(@"+[elementWithComment:]",
	    (elem[3] = [OFXMLElement elementWithComment: @" comment "]) &&
	    [[elem[3] string] isEqual: @"<!-- comment -->"])

	TEST(@"-[addAttributeWithName:stringValue:]",
	    R([elem[0] addAttributeWithName: @"foo"
				stringValue: @"b&ar"]) &&







>
>
>
>







55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
		    forNamespace: @"urn:objfw:test"]) &&
	    [[elem[3] string] isEqual: @"<objfw-test:foo>x</objfw-test:foo>"])

	TEST(@"+[elementWithCharacters:]",
	    (elem[3] = [OFXMLElement elementWithCharacters: @"<foo>"]) &&
	    [[elem[3] string] isEqual: @"&lt;foo&gt;"])

	TEST(@"+[elementWithCDATA:]",
	    (elem[3] = [OFXMLElement elementWithCDATA: @"<foo>"]) &&
	    [[elem[3] string] isEqual: @"<![CDATA[<foo>]]>"]);

	TEST(@"+[elementWithComment:]",
	    (elem[3] = [OFXMLElement elementWithComment: @" comment "]) &&
	    [[elem[3] string] isEqual: @"<!-- comment -->"])

	TEST(@"-[addAttributeWithName:stringValue:]",
	    R([elem[0] addAttributeWithName: @"foo"
				stringValue: @"b&ar"]) &&