ObjFW  Check-in [2086095795]

Overview
Comment:OFXMLElement improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2086095795e02db2ead51425dbf647e4c40ceea5db7b65abd7ea084b0af28e44
User & Date: js on 2011-02-19 15:06:34
Other Links: manifest | tags
Context
2011-02-19
16:08
Add -[elementsForName:namespace:] to OFXMLElement. check-in: 8732769f4c user: js tags: trunk
15:06
OFXMLElement improvements. check-in: 2086095795 user: js tags: trunk
13:14
of_asprintf: Use format even if object is nil. check-in: 5fdaa5b909 user: js tags: trunk
Changes

Modified src/OFXMLElement.h from [cc4813c83c] to [315d8e460f].

205
206
207
208
209
210
211



212
213
214
215
216
217
218



219
220
221
222
223
224
225
226
227



228
229
230
231
232
233
234
235
236






























237
238
239
240
241
242
243
 * \return A new autoreleased OFString representing the OFXMLElement as an
 * XML string
 */
- (OFString*)stringValue;

/**
 * Adds the specified attribute.



 *
 * \param attr The attribute to add
 */
- (void)addAttribute: (OFXMLAttribute*)attr;

/**
 * Adds the specified attribute with the specified value.



 *
 * \param name The name of the attribute
 * \param value The value of the attribute
 */
- (void)addAttributeWithName: (OFString*)name
		 stringValue: (OFString*)value;

/**
 * Adds the specified attribute with the specified namespace and value.



 *
 * \param name The name of the attribute
 * \param ns The namespace of the attribute
 * \param value The value of the attribute
 */
- (void)addAttributeWithName: (OFString*)name
		   namespace: (OFString*)ns
		 stringValue: (OFString*)value;































/**
 * Sets a prefix for a namespace.
 *
 * \param prefix The prefix for the namespace
 * \param ns The namespace for which the prefix is set
 */
- (void)setPrefix: (OFString*)prefix







>
>
>







>
>
>









>
>
>









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







205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
277
278
279
280
281
282
 * \return A new autoreleased OFString representing the OFXMLElement as an
 * XML string
 */
- (OFString*)stringValue;

/**
 * Adds the specified attribute.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param attr The attribute to add
 */
- (void)addAttribute: (OFXMLAttribute*)attr;

/**
 * Adds the specified attribute with the specified value.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param name The name of the attribute
 * \param value The value of the attribute
 */
- (void)addAttributeWithName: (OFString*)name
		 stringValue: (OFString*)value;

/**
 * Adds the specified attribute with the specified namespace and value.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param name The name of the attribute
 * \param ns The namespace of the attribute
 * \param value The value of the attribute
 */
- (void)addAttributeWithName: (OFString*)name
		   namespace: (OFString*)ns
		 stringValue: (OFString*)value;

/**
 * \param attrname The name of the attribute
 * \return The attribute with the specified name
 */
- (OFXMLAttribute*)attributeForName: (OFString*)attrname;

/**
 * \param attrname The name of the attribute
 * \param attrns The namespace of the attribute
 * \return The attribute with the specified name and namespace
 */
- (OFXMLAttribute*)attributeForName: (OFString*)attrname
			  namespace: (OFString*)attrns;

/**
 * Removes the attribute with the specified name.
 *
 * \param attrname The name of the attribute
 */
- (void)removeAttributeForName: (OFString*)attrname;

/**
 * Removes the attribute with the specified name and namespace.
 *
 * \param attrname The name of the attribute
 * \param attrns The namespace of the attribute
 */
- (void)removeAttributeForName: (OFString*)attrname
		     namespace: (OFString*)attrns;

/**
 * Sets a prefix for a namespace.
 *
 * \param prefix The prefix for the namespace
 * \param ns The namespace for which the prefix is set
 */
- (void)setPrefix: (OFString*)prefix

Modified src/OFXMLElement.m from [ae14e81241] to [4c4b122bc4].

420
421
422
423
424
425
426
427
428
429
430
431
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
	if (name == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if (attributes == nil)
		attributes = [[OFMutableArray alloc] init];

	/* FIXME: Prevent having it twice! */

	[attributes addObject: attr];
}

- (void)addAttributeWithName: (OFString*)name_
		 stringValue: (OFString*)value
{
	OFAutoreleasePool *pool;

	if (name == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	pool = [[OFAutoreleasePool alloc] init];
	[self addAttribute: [OFXMLAttribute attributeWithName: name_
						    namespace: nil
						  stringValue: value]];
	[pool release];
}

- (void)addAttributeWithName: (OFString*)name_
		   namespace: (OFString*)ns_
		 stringValue: (OFString*)value
{
	OFAutoreleasePool *pool;

	if (name == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	pool = [[OFAutoreleasePool alloc] init];
	[self addAttribute: [OFXMLAttribute attributeWithName: name_
						    namespace: ns_
						  stringValue: value]];
	[pool release];
}






/* TODO: Replace attribute */





/* TODO: Remove attribute */
















































- (void)setPrefix: (OFString*)prefix
     forNamespace: (OFString*)ns_
{
	if (name == nil || prefix == nil || [prefix isEqual: @""])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];







|
|
|





<
<
<
<
<
<
<
|
|
|
<



















>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







420
421
422
423
424
425
426
427
428
429
430
431
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
	if (name == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if (attributes == nil)
		attributes = [[OFMutableArray alloc] init];

	if ([self attributeForName: attr->name
			 namespace: attr->ns] == nil)
		[attributes addObject: attr];
}

- (void)addAttributeWithName: (OFString*)name_
		 stringValue: (OFString*)value
{







	[self addAttributeWithName: name_
			 namespace: nil
		       stringValue: value];

}

- (void)addAttributeWithName: (OFString*)name_
		   namespace: (OFString*)ns_
		 stringValue: (OFString*)value
{
	OFAutoreleasePool *pool;

	if (name == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	pool = [[OFAutoreleasePool alloc] init];
	[self addAttribute: [OFXMLAttribute attributeWithName: name_
						    namespace: ns_
						  stringValue: value]];
	[pool release];
}

- (OFXMLAttribute*)attributeForName: (OFString*)attrname
{
	return [self attributeForName: attrname
			    namespace: nil];
}

- (OFXMLAttribute*)attributeForName: (OFString*)attrname
			  namespace: (OFString*)attrns
{
	OFXMLAttribute **attrs_c = [attributes cArray];
	size_t i, attrs_count = [attributes count];

	if (attrns != nil) {
		for (i = 0; i < attrs_count; i++)
			if ([attrs_c[i]->ns isEqual: attrns] &&
			    [attrs_c[i]->name isEqual: attrname])
				return attrs_c[i];
	} else {
		for (i = 0; i < attrs_count; i++)
			if (attrs_c[i]->ns == nil &&
			    [attrs_c[i]->name isEqual: attrname])
				return attrs_c[i];
	}

	return nil;
}

- (void)removeAttributeForName: (OFString*)attrname
{
	[self removeAttributeForName: attrname
			   namespace: nil];
}

- (void)removeAttributeForName: (OFString*)attrname
		     namespace: (OFString*)attrns
{
	OFXMLAttribute **attrs_c = [attributes cArray];
	size_t i, attrs_count = [attributes count];

	if (attrns != nil) {
		for (i = 0; i < attrs_count; i++) {
			if ([attrs_c[i]->ns isEqual: attrns] &&
			    [attrs_c[i]->name isEqual: attrname]) {
				[attributes removeObjectAtIndex: i];

				return;
			}
		}
	} else {
		for (i = 0; i < attrs_count; i++) {
			if (attrs_c[i]->ns == nil &&
			    [attrs_c[i]->name isEqual: attrname]) {
				[attributes removeObjectAtIndex: i];

				return;
			}
		}
	}
}

- (void)setPrefix: (OFString*)prefix
     forNamespace: (OFString*)ns_
{
	if (name == nil || prefix == nil || [prefix isEqual: @""])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

Modified tests/OFXMLElementTests.m from [af4b374070] to [dbaee50446].

90
91
92
93
94
95
96



97
98
99








100
101
102
103
104
105
106
107
108
109
110
111
	    R([elem[1] setPrefix: @"objfw-test"
		    forNamespace: @"urn:objfw:test"]))

	TEST(@"-[addAttributeWithName:namespace:stringValue:]",
	    R([elem[1] addAttributeWithName: @"foo"
				  namespace: @"urn:objfw:test"
				stringValue: @"bar"]) &&



	    [[elem[1] stringValue] isEqual:
	    @"<foo foo='b&amp;ar' objfw-test:foo='bar'>b&amp;ar</foo>"])









	TEST(@"-[addChild:]",
	    R([elem[0] addChild: [OFXMLElement elementWithName: @"bar"]]) &&
	    [[elem[0] stringValue] isEqual:
	    @"<foo foo='b&amp;ar'><bar/></foo>"] &&
	    R([elem[2] addChild: [OFXMLElement elementWithName: @"bar"
		      namespace: @"urn:objfw:test"]]) &&
	    [[elem[2] stringValue] isEqual:
	    @"<objfw-test:foo test='test'><objfw-test:bar/></objfw-test:foo>"])

	[pool drain];
}
@end







>
>
>



>
>
>
>
>
>
>
>












90
91
92
93
94
95
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
122
	    R([elem[1] setPrefix: @"objfw-test"
		    forNamespace: @"urn:objfw:test"]))

	TEST(@"-[addAttributeWithName:namespace:stringValue:]",
	    R([elem[1] addAttributeWithName: @"foo"
				  namespace: @"urn:objfw:test"
				stringValue: @"bar"]) &&
	    R([elem[1] addAttributeWithName: @"foo"
				  namespace: @"urn:objfw:test"
				stringValue: @"ignored"]) &&
	    [[elem[1] stringValue] isEqual:
	    @"<foo foo='b&amp;ar' objfw-test:foo='bar'>b&amp;ar</foo>"])

	TEST(@"-[removeAttributeForName:namespace:]",
	    R([elem[1] removeAttributeForName: @"foo"]) &&
	    [[elem[1] stringValue] isEqual:
	    @"<foo objfw-test:foo='bar'>b&amp;ar</foo>"] &&
	    R([elem[1] removeAttributeForName: @"foo"
				    namespace: @"urn:objfw:test"]) &&
	    [[elem[1] stringValue] isEqual: @"<foo>b&amp;ar</foo>"])

	TEST(@"-[addChild:]",
	    R([elem[0] addChild: [OFXMLElement elementWithName: @"bar"]]) &&
	    [[elem[0] stringValue] isEqual:
	    @"<foo foo='b&amp;ar'><bar/></foo>"] &&
	    R([elem[2] addChild: [OFXMLElement elementWithName: @"bar"
		      namespace: @"urn:objfw:test"]]) &&
	    [[elem[2] stringValue] isEqual:
	    @"<objfw-test:foo test='test'><objfw-test:bar/></objfw-test:foo>"])

	[pool drain];
}
@end