ObjFW  Check-in [4e88d4192d]

Overview
Comment:Add -[stringValue] to OFXMLElement.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4e88d4192d5834767229d1ef308dab57f5cccc9a62797a27c5972069d4ed3c64
User & Date: js on 2011-03-31 12:14:07
Other Links: manifest | tags
Context
2011-03-31
12:32
Add -[setChildren:] and -[setStringValue:] to OFXMLElement. check-in: caef9fbb2e user: js tags: trunk
12:14
Add -[stringValue] to OFXMLElement. check-in: 4e88d4192d user: js tags: trunk
11:55
Rename +[elementWithString:] to +[elementWithXMLString:] for clarity. check-in: 253fe5d154 user: js tags: trunk
Changes

Modified src/OFXMLElement.h from [8018d72592] to [1dd54e0aa6].

213
214
215
216
217
218
219





220
221
222
223
224
225
226
- (OFArray*)attributes;

/**
 * \return An array with all children of the element
 */
- (OFArray*)children;






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

/**







>
>
>
>
>







213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
- (OFArray*)attributes;

/**
 * \return An array with all children of the element
 */
- (OFArray*)children;

/**
 * \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;

/**

Modified src/OFXMLElement.m from [d71bd6e00b] to [7f8c49938a].

260
261
262
263
264
265
266




































267
268
269
270
271
272
273
	return [[attributes copy] autorelease];
}

- (OFArray*)children
{
	return [[children copy] autorelease];
}





































- (OFString*)_XMLStringWithParent: (OFXMLElement*)parent
{
	OFAutoreleasePool *pool, *pool2;
	char *str_c;
	size_t len, i, j, attrs_count;
	OFString *prefix, *parent_prefix;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
	return [[attributes copy] autorelease];
}

- (OFArray*)children
{
	return [[children copy] autorelease];
}

- (OFString*)stringValue
{
	OFAutoreleasePool *pool;
	OFMutableString *ret;
	OFXMLElement **children_c;
	size_t i, count = [children count];

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

	ret = [OFMutableString string];
	children_c = [children cArray];
	pool = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < count; i++) {
		if (children_c[i]->characters != nil)
			[ret appendString: children_c[i]->characters];
		else if (children_c[i]->cdata != nil)
			[ret appendString: children_c[i]->cdata];
		else if (children_c[i]->comment == nil) {
			[ret appendString: [children_c[i] stringValue]];
			[pool releaseObjects];
		}
	}

	[pool release];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFString class];
	return ret;
}

- (OFString*)_XMLStringWithParent: (OFXMLElement*)parent
{
	OFAutoreleasePool *pool, *pool2;
	char *str_c;
	size_t len, i, j, attrs_count;
	OFString *prefix, *parent_prefix;

Modified tests/OFXMLElementTests.m from [878add25fb] to [7235d66d34].

112
113
114
115
116
117
118





119
120
121
122
123
124
125
126
127
128
	    R([elem[0] addChild: [OFXMLElement elementWithName: @"bar"]]) &&
	    [[elem[0] XMLString] isEqual:
	    @"<foo foo='b&amp;ar'><bar/></foo>"] &&
	    R([elem[2] addChild: [OFXMLElement elementWithName: @"bar"
		      namespace: @"urn:objfw:test"]]) &&
	    [[elem[2] XMLString] isEqual:
	    @"<objfw-test:foo test='test'><objfw-test:bar/></objfw-test:foo>"])






	TEST(@"-[elementsForName:namespace:]",
	    (a = [elem[2] elementsForName: @"bar"
				namespace: @"urn:objfw:test"]) &&
	    [a count] == 1 && [[[a firstObject] XMLString] isEqual:
	    @"<bar xmlns='urn:objfw:test'/>"])

	[pool drain];
}
@end







>
>
>
>
>










112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	    R([elem[0] addChild: [OFXMLElement elementWithName: @"bar"]]) &&
	    [[elem[0] XMLString] isEqual:
	    @"<foo foo='b&amp;ar'><bar/></foo>"] &&
	    R([elem[2] addChild: [OFXMLElement elementWithName: @"bar"
		      namespace: @"urn:objfw:test"]]) &&
	    [[elem[2] XMLString] isEqual:
	    @"<objfw-test:foo test='test'><objfw-test:bar/></objfw-test:foo>"])

	TEST(@"+[elementWithXMLString:] and -[stringValue]",
	    [[[OFXMLElement elementWithXMLString:
	    @"<x>foo<![CDATA[bar]]><y>baz</y>qux</x>"] stringValue] isEqual:
	    @"foobarbazqux"])

	TEST(@"-[elementsForName:namespace:]",
	    (a = [elem[2] elementsForName: @"bar"
				namespace: @"urn:objfw:test"]) &&
	    [a count] == 1 && [[[a firstObject] XMLString] isEqual:
	    @"<bar xmlns='urn:objfw:test'/>"])

	[pool drain];
}
@end

Modified tests/TestsAppDelegate.m from [28709e403c] to [d504fe249a].

134
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150
151
152
153
154
#ifdef OF_THREADS
	[self threadTests];
#endif
	[self URLTests];
#ifdef OF_THREADS
	[self HTTPRequestTests];
#endif
	[self XMLElementTests];
	[self XMLParserTests];

	[self XMLElementBuilderTests];
#ifdef OF_PLUGINS
	[self pluginTests];
#endif
#ifdef OF_HAVE_PROPERTIES
	[self propertiesTests];
#endif

	if (fails > 0)
		[OFApplication terminateWithStatus: fails];
}
@end







<

>












134
135
136
137
138
139
140

141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifdef OF_THREADS
	[self threadTests];
#endif
	[self URLTests];
#ifdef OF_THREADS
	[self HTTPRequestTests];
#endif

	[self XMLParserTests];
	[self XMLElementTests];
	[self XMLElementBuilderTests];
#ifdef OF_PLUGINS
	[self pluginTests];
#endif
#ifdef OF_HAVE_PROPERTIES
	[self propertiesTests];
#endif

	if (fails > 0)
		[OFApplication terminateWithStatus: fails];
}
@end