ObjFW  Check-in [6f61176375]

Overview
Comment:Add support for comments to OFXMLParser.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6f61176375718b721b8efe35334e7ac4b3db0e33471ec19ed1d2426496dbfaef
User & Date: js on 2009-08-26 15:00:32
Other Links: manifest | tags
Context
2009-08-26
19:40
Improve documentation. check-in: 146db53e4a user: js tags: trunk
15:00
Add support for comments to OFXMLParser. check-in: 6f61176375 user: js tags: trunk
14:24
Add default implementation for OFXMLParserDelegate. check-in: 280071188d user: js tags: trunk
Changes

Modified src/OFXMLParser.h from [0e9bee0cbd] to [12a6876562].

24
25
26
27
28
29
30


31
32
33
34
35
36
37
	   attributes: (OFArray*)attrs;
-   (void)xmlParser: (OFXMLParser*)parser
  didEndTagWithName: (OFString*)name
	     prefix: (OFString*)prefix
	  namespace: (OFString*)ns;
- (void)xmlParser: (OFXMLParser*)parser
      foundString: (OFString*)string;


-    (OFString*)xmlParser: (OFXMLParser*)parser
  foundUnknownEntityNamed: (OFString*)entity;
@end

@protocol OFXMLUnescapingDelegate
- (OFString*)foundUnknownEntityNamed: (OFString*)entitiy;
@end







>
>







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	   attributes: (OFArray*)attrs;
-   (void)xmlParser: (OFXMLParser*)parser
  didEndTagWithName: (OFString*)name
	     prefix: (OFString*)prefix
	  namespace: (OFString*)ns;
- (void)xmlParser: (OFXMLParser*)parser
      foundString: (OFString*)string;
- (void)xmlParser: (OFXMLParser*)parser
     foundComment: (OFString*)comment;
-    (OFString*)xmlParser: (OFXMLParser*)parser
  foundUnknownEntityNamed: (OFString*)entity;
@end

@protocol OFXMLUnescapingDelegate
- (OFString*)foundUnknownEntityNamed: (OFString*)entitiy;
@end
45
46
47
48
49
50
51
52




53
54
55
56
57
58
59
		OF_XMLPARSER_IN_TAG_NAME,
		OF_XMLPARSER_IN_CLOSE_TAG_NAME,
		OF_XMLPARSER_IN_TAG,
		OF_XMLPARSER_IN_ATTR_NAME,
		OF_XMLPARSER_EXPECT_DELIM,
		OF_XMLPARSER_IN_ATTR_VALUE,
		OF_XMLPARSER_EXPECT_CLOSE,
		OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE




	} state;
	OFString *cache;
	OFString *name;
	OFString *prefix;
	OFString *ns;
	OFArray *attrs;
	OFString *attr_name;







|
>
>
>
>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
		OF_XMLPARSER_IN_TAG_NAME,
		OF_XMLPARSER_IN_CLOSE_TAG_NAME,
		OF_XMLPARSER_IN_TAG,
		OF_XMLPARSER_IN_ATTR_NAME,
		OF_XMLPARSER_EXPECT_DELIM,
		OF_XMLPARSER_IN_ATTR_VALUE,
		OF_XMLPARSER_EXPECT_CLOSE,
		OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE,
		OF_XMLPARSER_IN_COMMENT_1,
		OF_XMLPARSER_IN_COMMENT_2,
		OF_XMLPARSER_IN_COMMENT_3,
		OF_XMLPARSER_IN_COMMENT_4
	} state;
	OFString *cache;
	OFString *name;
	OFString *prefix;
	OFString *ns;
	OFArray *attrs;
	OFString *attr_name;

Modified src/OFXMLParser.m from [dc40641988] to [a3249dff8b].

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



173
174
175
176
177
178
179
						  withLength: len];

				if ([cache length] > 0) {
					OFString *str;

					pool = [[OFAutoreleasePool alloc] init];
					str = transform_string(cache, self);

					[delegate xmlParser: self
						foundString: str];

					[pool release];
				}

				[cache setToCString: ""];

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

		/* Tag was just opened */
		case OF_XMLPARSER_TAG_OPENED:
			if (buf[i] == '/') {
				last = i + 1;
				state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;



			} else {
				state = OF_XMLPARSER_IN_TAG_NAME;
				i--;
			}
			break;

		/* Inside a tag, no name yet */







>


<















>
>
>







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
173
174
175
176
177
178
179
180
181
182
						  withLength: len];

				if ([cache length] > 0) {
					OFString *str;

					pool = [[OFAutoreleasePool alloc] init];
					str = transform_string(cache, self);
					str = [[str copy] autorelease];
					[delegate xmlParser: self
						foundString: str];

					[pool release];
				}

				[cache setToCString: ""];

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

		/* Tag was just opened */
		case OF_XMLPARSER_TAG_OPENED:
			if (buf[i] == '/') {
				last = i + 1;
				state = OF_XMLPARSER_IN_CLOSE_TAG_NAME;
			} else if(buf[i] == '!') {
				last = i + 1;
				state = OF_XMLPARSER_IN_COMMENT_1;
			} else {
				state = OF_XMLPARSER_IN_TAG_NAME;
				i--;
			}
			break;

		/* Inside a tag, no name yet */
431
432
433
434
435
436
437






































438
439
440
441
442
443
444
		case OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE:
			if (buf[i] == '>') {
				last = i + 1;
				state = OF_XMLPARSER_OUTSIDE_TAG;
			} else if (buf[i] != ' ')
				@throw [OFMalformedXMLException
				    newWithClass: isa];






































			break;
		}
	}

	len = size - last;
	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (len > 0 && state != OF_XMLPARSER_IN_TAG)







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







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
		case OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE:
			if (buf[i] == '>') {
				last = i + 1;
				state = OF_XMLPARSER_OUTSIDE_TAG;
			} else if (buf[i] != ' ')
				@throw [OFMalformedXMLException
				    newWithClass: isa];
			break;

		/* Comment */
		case OF_XMLPARSER_IN_COMMENT_1:
		case OF_XMLPARSER_IN_COMMENT_2:
			if (buf[i] != '-')
				@throw [OFMalformedXMLException
				    newWithClass: isa];
			last = i + 1;
			state++;
			break;
		case OF_XMLPARSER_IN_COMMENT_3:
			if (buf[i] == '-')
				state = OF_XMLPARSER_IN_COMMENT_4;
			break;
		case OF_XMLPARSER_IN_COMMENT_4:
			if (buf[i] == '-') {
				OFString *str;

				[cache appendCString: buf + last
					  withLength: i - last];

				pool = [[OFAutoreleasePool alloc] init];
				str = [OFMutableString
				    stringWithCString: [cache cString]
					       length: [cache length] - 1];
				[str removeLeadingAndTrailingWhitespaces];
				[delegate xmlParser: self
				       foundComment: str];
				[pool release];

				[cache setToCString: ""];

				last = i + 1;
				state = OF_XMLPARSER_EXPECT_CLOSE;
			} else
				state = OF_XMLPARSER_IN_COMMENT_3;

			break;
		}
	}

	len = size - last;
	/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
	if (len > 0 && state != OF_XMLPARSER_IN_TAG)
556
557
558
559
560
561
562





563
564
565
566
567
568
569
{
}

- (void)xmlParser: (OFXMLParser*)parser
      foundString: (OFString*)string
{
}






-    (OFString*)xmlParser: (OFXMLParser*)parser
  foundUnknownEntityNamed: (OFString*)entity
{
	return nil;
}
@end







>
>
>
>
>







597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
{
}

- (void)xmlParser: (OFXMLParser*)parser
      foundString: (OFString*)string
{
}

- (void)xmlParser: (OFXMLParser*)parser
     foundComment: (OFString*)comment
{
}

-    (OFString*)xmlParser: (OFXMLParser*)parser
  foundUnknownEntityNamed: (OFString*)entity
{
	return nil;
}
@end

Modified tests/OFXMLParser/OFXMLParser.m from [afd9073f80] to [655d941dba].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "config.h"

#include <stdio.h>
#include <string.h>

#import "OFXMLParser.h"

@interface ParserDelegate: OFObject <OFXMLParserDelegate>
@end

@implementation ParserDelegate
-     (void)xmlParser: (OFXMLParser*)parser
  didStartTagWithName: (OFString*)name
	       prefix: (OFString*)prefix
	    namespace: (OFString*)ns







|







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "config.h"

#include <stdio.h>
#include <string.h>

#import "OFXMLParser.h"

@interface ParserDelegate: OFObject
@end

@implementation ParserDelegate
-     (void)xmlParser: (OFXMLParser*)parser
  didStartTagWithName: (OFString*)name
	       prefix: (OFString*)prefix
	    namespace: (OFString*)ns
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
}

- (void)xmlParser: (OFXMLParser*)parser
      foundString: (OFString*)string
{
	printf("STRING\n\"%s\"\n\n", [string cString]);
}







-    (OFString*)xmlParser: (OFXMLParser*)parser
  foundUnknownEntityNamed: (OFString*)entity
{
	if ([entity isEqual: @"foo"])
		return @"foobar";

	return nil;
}
@end

int
main()
{
	const char *foo = "bar<foo:bar  bar='b&amp;az'  qux:qux=\" quux \">\r\n"
	    "foo&lt;bar<qux  >bar <baz name='' test='&foo;'/>  quxbar\r\n</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 */







>
>
>
>
>
>
















|







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
}

- (void)xmlParser: (OFXMLParser*)parser
      foundString: (OFString*)string
{
	printf("STRING\n\"%s\"\n\n", [string cString]);
}

- (void)xmlParser: (OFXMLParser*)parser
     foundComment: (OFString*)comment
{
	printf("COMMENT\n\"%s\"\n\n", [comment cString]);
}

-    (OFString*)xmlParser: (OFXMLParser*)parser
  foundUnknownEntityNamed: (OFString*)entity
{
	if ([entity isEqual: @"foo"])
		return @"foobar";

	return nil;
}
@end

int
main()
{
	const char *foo = "bar<foo:bar  bar='b&amp;az'  qux:qux=\" quux \">\r\n"
	    "foo&lt;bar<qux  >bar <baz name='' test='&foo;'/>  quxbar\r\n</qux>"
	    "</foo:bar><!-- foo bar-baz -->";
	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 */