ObjFW  Check-in [6f001b8016]

Overview
Comment:New way for handling and storing XML attributes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6f001b801649eb77a8e34783b49d79465029a04741d5b5c930eed62295d44040
User & Date: js on 2009-08-12 16:55:16
Other Links: manifest | tags
Context
2009-08-14
01:01
Treat \n and \r as whitespaces in whitespace removing methods. check-in: a5aed6da30 user: js tags: trunk
2009-08-12
16:55
New way for handling and storing XML attributes. check-in: 6f001b8016 user: js tags: trunk
15:37
Rename +[name] and -[name] to +[className] and -[className]. check-in: ce8d36d5c4 user: js tags: trunk
Changes

Modified src/OFXMLElement.h from [1247541a3e] to [cc70c8e478].

12
13
14
15
16
17
18
19


























































20
21
22
23
24
25
26

27
28
29
30
31
32
33
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








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+






-
+







#import "OFObject.h"
#import "OFString.h"
#import "OFDictionary.h"
#import "OFArray.h"

extern int _OFXMLElement_reference;

/**
 * The OFXMLAttribute represents an attribute of an XML element as an object.
 */
@interface OFXMLAttribute: OFObject
{
	OFString *prefix;
	OFString *name;
	OFString *ns;
	OFString *value;
}

/**
 * \param name The name of the attribute
 * \param prefix The prefix of the attribute
 * \param ns The namespace of the attribute
 * \param value The string value of the attribute
 * \return A new autoreleased OFXMLAttribute with the specified parameters
 */
+ attributeWithName: (OFString*)name
	     prefix: (OFString*)prefix
	  namespace: (OFString*)ns
	stringValue: (OFString*)value;

/**
 * Initializes an already allocated OFXMLAttribute.
 *
 * \param name The name of the attribute
 * \param prefix The prefix of the attribute
 * \param ns The namespace of the attribute
 * \param value The string value of the attribute
 * \return An initialized OFXMLAttribute with the specified parameters
 */
- initWithName: (OFString*)name
	prefix: (OFString*)prefix
     namespace: (OFString*)ns
   stringValue: (OFString*)value;

/**
 * \return The name of the attribute as an autoreleased OFString
 */
- (OFString*)name;

/**
 * \return The prefix of the attribute as an autoreleased OFString
 */
- (OFString*)prefix;

/**
 * \return The namespace of the attribute as an autoreleased OFString
 */
- (OFString*)namespace;

/**
 * \return The string value of the attribute as an autoreleased OFString
 */
- (OFString*)stringValue;
@end

/**
 * The OFXMLElement represents an XML element as an object which can be
 * modified and converted back to XML again.
 */
@interface OFXMLElement: OFObject
{
	OFString *name;
	OFDictionary *attrs;
	OFArray *attrs;
	OFString *stringval;
	OFArray *children;
}

/**
 * \param name The name for the element
 * \return A new autorelease OFXMLElement with the specified element name
65
66
67
68
69
70
71







72
73
74
75
76
77
78
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143







+
+
+
+
+
+
+








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

/**
 * Adds the specified attribute.
 *
 * \param attr The attribute to add
 */
- 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
 */
- addAttributeWithName: (OFString*)name

Modified src/OFXMLElement.m from [f1c60b4b45] to [12bad844a7].

16
17
18
19
20
21
22


























































23
24
25
26
27
28
29
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#include <string.h>

#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"

int _OFXMLElement_reference;

@implementation OFXMLAttribute
+ attributeWithName: (OFString*)name_
	     prefix: (OFString*)prefix_
	  namespace: (OFString*)ns_
	stringValue: (OFString*)value_
{
	return [[[self alloc] initWithName: name_
				    prefix: prefix_
				 namespace: ns_
			       stringValue: value_] autorelease];
}

- initWithName: (OFString*)name_
	prefix: (OFString*)prefix_
     namespace: (OFString*)ns_
   stringValue: (OFString*)value_
{
	self = [super init];

	name = [name_ copy];
	prefix = [prefix_ copy];
	ns = [ns_ copy];
	value = [value_ copy];

	return self;
}

- (void)dealloc
{
	[name release];
	[prefix release];
	[ns release];
	[value release];

	[super dealloc];
}

- (OFString*)name
{
	return [[name copy] autorelease];
}

- (OFString*)prefix
{
	return [[prefix copy] autorelease];
}

- (OFString*)namespace
{
	return [[ns copy] autorelease];
}

- (OFString*)stringValue
{
	return [[value copy] autorelease];
}
@end

@implementation OFXMLElement
+ elementWithName: (OFString*)name_
{
	return [[[self alloc] initWithName: name_] autorelease];
}

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
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
118
119
120
121
122
123
124

125
126
127
128
129
130
131
132
133
134
135
136
137


138
139
140



141


142




143
144
145








146
147
148
149
150
151
152
153
154









155
156
157
158
159
160
161
162
163
164

165

166
167
168
169
170
171
172







-
+
+











-
-
+
+

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

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

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

-
+
-







	return self;
}

- (OFString*)string
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	char *str_c;
	size_t len, i, j;
	size_t len, i, j, attrs_count;
	OFXMLAttribute **attrs_data;
	OFString *ret, *tmp;

	len = [name length] + 4;
	str_c = [self allocMemoryWithSize: len];

	/* Start of tag */
	*str_c = '<';
	memcpy(str_c + 1, [name cString], [name length]);
	i = [name length] + 1;

	/* Attributes */
	if (attrs != nil) {
		OFIterator *iter = [attrs iterator];
	attrs_data = [attrs data];
	attrs_count = [attrs count];

		for (;;) {
			of_iterator_pair_t pair;

	for (j = 0; j < attrs_count; j++) {
			pair = [iter nextKeyObjectPair];

		/* FIXME: Add namespace support */
			if (pair.key == nil || pair.object == nil)
				break;

			tmp = [pair.object stringByXMLEscaping];
		OFString *attr_name = [attrs_data[j] name];
		tmp = [[attrs_data[j] stringValue] stringByXMLEscaping];

			len += [pair.key length] + [tmp length] + 4;
			@try {
				str_c = [self resizeMemory: str_c
						    toSize: len];
			} @catch (OFException *e) {
				[self freeMemory: str_c];
				@throw e;
			}
		len += [attr_name length] + [tmp length] + 4;
		@try {
			str_c = [self resizeMemory: str_c
					    toSize: len];
		} @catch (OFException *e) {
			[self freeMemory: str_c];
			@throw e;
		}

			str_c[i++] = ' ';
			memcpy(str_c + i, [pair.key cString],
			    [pair.key length]);
			i += [pair.key length];
			str_c[i++] = '=';
			str_c[i++] = '\'';
			memcpy(str_c + i, [tmp cString], [tmp length]);
			i += [tmp length];
			str_c[i++] = '\'';
		str_c[i++] = ' ';
		memcpy(str_c + i, [attr_name cString],
				[attr_name length]);
		i += [attr_name length];
		str_c[i++] = '=';
		str_c[i++] = '\'';
		memcpy(str_c + i, [tmp cString], [tmp length]);
		i += [tmp length];
		str_c[i++] = '\'';

			[pool releaseObjects];
		[pool releaseObjects];
		}
	}

	/* Childen */
	if (stringval != nil || children != nil) {
		if (stringval != nil)
			tmp = [stringval stringByXMLEscaping];
		else if (children != nil) {
161
162
163
164
165
166
167
168

169
170
171
172

173

174
175















176
177
178



179
180
181
182
183
184
185
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







-
+
-


-
+

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



+
+
+







	} @finally {
		[self freeMemory: str_c];
	}

	return ret;
}

- addAttributeWithName: (OFString*)name_
- addAttribute: (OFXMLAttribute*)attr
	   stringValue: (OFString*)value_
{
	if (attrs == nil)
		attrs = [[OFMutableDictionary alloc] init];
		attrs = [[OFMutableArray alloc] init];

	/* FIXME: Prevent having it twice! */
	[attrs setObject: value_
		  forKey: name_];

	[attrs addObject: attr];

	return self;
}

- addAttributeWithName: (OFString*)name_
	   stringValue: (OFString*)value
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	[self addAttribute: [OFXMLAttribute attributeWithName: name_
						       prefix: nil
						    namespace: nil
						  stringValue: value]];
	[pool release];

	return self;
}

/* TODO: Replace attribute */
/* TODO: Remove attribute */

- addChild: (OFXMLElement*)child
{
	if (stringval != nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

Modified src/OFXMLParser.h from [badfb4ea20] to [4198601055].

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













-










-
+







/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFString.h"
#import "OFDictionary.h"

extern int _OFXMLParser_reference;

@class OFXMLParser;

@protocol OFXMLParserDelegate
-     (BOOL)xmlParser: (OFXMLParser*)parser
  didStartTagWithName: (OFString*)name
	       prefix: (OFString*)prefix
	    namespace: (OFString*)ns
	   attributes: (OFDictionary*)attrs;
	   attributes: (OFArray*)attrs;
-   (BOOL)xmlParser: (OFXMLParser*)parser
  didEndTagWithName: (OFString*)name
	     prefix: (OFString*)prefix
	  namespace: (OFString*)ns;
- (BOOL)xmlParser: (OFXMLParser*)parser
      foundString: (OFString*)string;
-    (OFString*)xmlParser: (OFXMLParser*)parser
52
53
54
55
56
57
58
59

60

61
62
63
64
65
66
67
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67







-
+

+







		OF_XMLPARSER_EXPECT_CLOSE,
		OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE
	} state;
	OFString *cache;
	OFString *name;
	OFString *prefix;
	OFString *ns;
	OFDictionary *attrs;
	OFArray *attrs;
	OFString *attr_name;
	OFString *attr_prefix;
	char delim;
	OFArray *previous;
}

+ xmlParser;
- (id)delegate;
- setDelegate: (OFObject <OFXMLParserDelegate>*)delegate;

Modified src/OFXMLParser.m from [c9bc88e2c8] to [eb5533890d].

95
96
97
98
99
100
101

102
103
104
105
106
107
108
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109







+








	[cache release];
	[name release];
	[prefix release];
	[ns release];
	[attrs release];
	[attr_name release];
	[attr_prefix release];
	[previous release];

	[super dealloc];
}

- (id)delegate
{
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

197
198




199
200
201
202
203
204
205
181
182
183
184
185
186
187



188
189
190
191



192
193

194
195
196
197
198
199
200
201
202
203
204







-
-
-




-
-
-
+

-
+
+
+
+







					[cache appendCString: buf + last
						  withLength: len];
				cache_c = [cache cString];
				cache_len = [cache length];

				if ((tmp = memchr(cache_c, ':',
				    cache_len)) != NULL) {
					prefix = [[OFString alloc]
					    initWithCString: cache_c
						     length: tmp - cache_c];
					name = [[OFString alloc]
					    initWithCString: tmp + 1
						     length: cache_len - (tmp -
							     cache_c) - 1];
				} else {
					prefix = nil;
					name = [[OFString alloc]
					prefix = [[OFString alloc]
					    initWithCString: cache_c
						     length: cache_len];
						     length: tmp - cache_c];
				} else {
					name = [cache copy];
					prefix = nil;
				}

				if (buf[i] == '>' || buf[i] == '/') {
					pool = [[OFAutoreleasePool alloc] init];

					[delegate xmlParser: self
					didStartTagWithName: name
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
243
244
245
246
247
248
249



250
251
252
253



254
255

256
257
258
259
260
261
262
263
264
265
266







-
-
-




-
-
-
+

-
+
+
+
+







					[cache appendCString: buf + last
						  withLength: len];
				cache_c = [cache cString];
				cache_len = [cache length];

				if ((tmp = memchr(cache_c, ':',
				    cache_len)) != NULL) {
					prefix = [[OFString alloc]
					    initWithCString: cache_c
						     length: tmp - cache_c];
					name = [[OFString alloc]
					    initWithCString: tmp + 1
						     length: cache_len - (tmp -
							     cache_c) - 1];
				} else {
					prefix = nil;
					name = [[OFString alloc]
					prefix = [[OFString alloc]
					    initWithCString: cache_c
						     length: cache_len];
						     length: tmp - cache_c];
				} else {
					name = [cache copy];
					prefix = nil;
				}

				if (![[previous lastObject] isEqual: cache])
					@throw [OFMalformedXMLException
					    newWithClass: isa];
				[previous removeNObjects: 1];

333
334
335
336
337
338
339



340
341
342
343
344













345




346
347
348
349
350
351
352
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

358
359
360
361
362
363
364
365
366
367
368







+
+
+





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







				i--;
			}
			break;

		/* Looking for attribute name */
		case OF_XMLPARSER_IN_ATTR_NAME:
			if (buf[i] == '=') {
				const char *cache_c, *tmp;
				size_t cache_len;

				len = i - last;
				if (len > 0)
					[cache appendCString: buf + last
						  withLength: len];

				cache_c = [cache cString];
				cache_len = [cache length];

				if ((tmp = memchr(cache_c, ':',
				    cache_len)) != NULL ) {
					attr_name = [[OFString alloc]
					    initWithCString: tmp + 1
						     length: cache_len - (tmp -
							     cache_c) - 1];
					attr_prefix = [[OFString alloc]
					    initWithCString: cache_c
						     length: tmp - cache_c];
				} else {
				attr_name = [cache copy];
					attr_name = [cache copy];
					attr_prefix = nil;
				}

				[cache setToCString: ""];

				last = i + 1;
				state = OF_XMLPARSER_EXPECT_DELIM;
			}
			break;

368
369
370
371
372
373
374
375

376
377
378
379
380
381
382





383
384
385
386



387
388
389
390
391
392
393
384
385
386
387
388
389
390

391

392
393
394
395


396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414







-
+
-




-
-
+
+
+
+
+




+
+
+








				len = i - last;
				if (len > 0)
					[cache appendCString: buf + last
						  withLength: len];

				if (attrs == nil)
					attrs =
					attrs = [[OFMutableArray alloc] init];
					    [[OFMutableDictionary alloc] init];

				pool = [[OFAutoreleasePool alloc] init];
				attr_val = [cache
				    stringByXMLUnescapingWithHandler: self];
				[attrs setObject: attr_val
					  forKey: attr_name];
				[attrs addObject: [OFXMLAttribute
				    attributeWithName: attr_name
					       prefix: attr_prefix
					    namespace: nil
					  stringValue: attr_val]];
				[pool release];

				[cache setToCString: ""];
				[attr_name release];
				[attr_prefix release];
				attr_name = nil;
				attr_prefix = nil;

				last = i + 1;
				state = OF_XMLPARSER_IN_TAG;
			}
			break;

		/* Expecting closing '>' */

Modified tests/OFXMLParser/OFXMLParser.m from [82a296dd2e] to [7476e2d4e0].

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







-
+

+
+
+



-
-
+
+

-
+
-
-
-
+
+
+
+

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







@end

@implementation ParserDelegate
-     (BOOL)xmlParser: (OFXMLParser*)parser
  didStartTagWithName: (OFString*)name
	       prefix: (OFString*)prefix
	    namespace: (OFString*)ns
	   attributes: (OFDictionary*)attrs
	   attributes: (OFArray*)attrs
{
	OFXMLAttribute **attrs_data;
	size_t i, attrs_count;

	printf("START\nname=\"%s\"\nprefix=\"%s\"\nns=\"%s\"\n",
	    [name cString], [prefix cString], [ns cString]);

	if (attrs) {
		OFIterator *iter = [attrs iterator];
	attrs_data = [attrs data];
	attrs_count = [attrs count];

		for (;;) {
	for (i = 0; i < attrs_count; i++) {
			of_iterator_pair_t pair;

			pair = [iter nextKeyObjectPair];
		OFString *attr_name = [attrs_data[i] name];
		OFString *attr_prefix = [attrs_data[i] prefix];
		OFString *attr_ns = [attrs_data[i] namespace];
		OFString *attr_value = [attrs_data[i] stringValue];

		printf("ATTR:\n      name=\"%s\"\n", [attr_name cString]);
			if (pair.key == nil || pair.object == nil)
		if (attr_prefix != nil)
				break;

			printf("ATTR: \"%s\"=\"%s\"\n",
			    [pair.key cString], [pair.object cString]);
			printf("      prefix=\"%s\"\n", [attr_prefix cString]);
		if (attr_ns != nil)
			printf("      ns=\"%s\"\n", [attr_ns cString]);
		printf("      value=\"%s\"\n", [attr_value cString]);
		}
	}

	puts("");

	return YES;
}

78
79
80
81
82
83
84
85
86



87
88
89
90
91
92
93
82
83
84
85
86
87
88


89
90
91
92
93
94
95
96
97
98







-
-
+
+
+







	return nil;
}
@end

int
main()
{
	const char *foo = "bar<foo:bar  bar='b&amp;az'  qux=\"quux\">foo&lt;bar"
	    "<qux  >bar<baz name='' test='&foo;'/>quxbar</qux></foo:bar>";
	const char *foo = "bar<foo:bar  bar='b&amp;az'  qux:qux=\"quux\">"
	    "foo&lt;bar<qux  >bar<baz name='' test='&foo;'/>quxbar</qux>"
	    "</foo:bar>";
	size_t len = strlen(foo);
	size_t i;
	OFXMLParser *parser = [OFXMLParser xmlParser];

	[parser setDelegate: [[ParserDelegate alloc] init]];

	/* Simulate a stream where we only get chunks */