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
|
#import "OFXMLParser.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFAutoreleasePool.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "main.h"
static OFString *module = @"OFXMLParser";
static int i = 0;
enum event_type {
TAG_START,
TAG_END,
STRING,
COMMENT
};
static void
callback(enum event_type et, OFString *name, OFString *prefix, OFString *ns,
OFArray *attrs, OFString *string, OFString *comment)
{
OFString *msg;
id *carray;
size_t count;
i++;
msg = [OFString stringWithFormat: @"Parsing part #%d", i];
|
|
|
|
|
>
>
>
>
>
|
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
|
#import "OFXMLParser.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFAutoreleasePool.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "TestsAppDelegate.h"
static OFString *module = @"OFXMLParser";
static int i = 0;
enum event_type {
TAG_START,
TAG_END,
STRING,
COMMENT
};
@implementation TestsAppDelegate (OFXMLParser)
- (void)xmlParserCallbackWithEventType: (enum event_type)et
name: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
string: (OFString*)string
comment: (OFString*)comment
{
OFString *msg;
id *carray;
size_t count;
i++;
msg = [OFString stringWithFormat: @"Parsing part #%d", i];
|
112
113
114
115
116
117
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
break;
default:
TEST(msg, NO)
break;
}
}
@interface ParserDelegate: OFObject
@end
@implementation ParserDelegate
- (void)xmlParser: (OFXMLParser*)parser
didStartTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
{
callback(TAG_START, name, prefix, ns, attrs, nil, nil);
}
- (void)xmlParser: (OFXMLParser*)parser
didEndTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
{
callback(TAG_END, name, prefix, ns, nil, nil, nil);
}
- (void)xmlParser: (OFXMLParser*)parser
foundString: (OFString*)string
{
callback(STRING, nil, nil, nil, nil, string, nil);
}
- (void)xmlParser: (OFXMLParser*)parser
foundComment: (OFString*)comment
{
callback(COMMENT, nil, nil, nil, nil, nil, comment);
}
- (OFString*)xmlParser: (OFXMLParser*)parser
foundUnknownEntityNamed: (OFString*)entity
{
if ([entity isEqual: @"foo"])
return @"foobar";
return nil;
}
@end
void
xmlparser_tests()
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFXMLParser *parser;
const char *str = "bar<foo:bar bar='b&az' qux:qux=\" quux \">\r\n"
"foo<bar<qux >bar <baz name='' test='&foo;'/> quxbar\r\n</qux>"
"</foo:bar><!-- foo bär-baz -->";
size_t j, len;
TEST(@"+[xmlParser]", (parser = [OFXMLParser xmlParser]))
TEST(@"-[setDelegate:]",
[parser setDelegate: [[[ParserDelegate alloc] init] autorelease]])
/* Simulate a stream where we only get chunks */
len = strlen(str);
for (j = 0; j < len; j+= 2) {
if (j + 2 > len)
[parser parseBuffer: str + j
withSize: 1];
else
[parser parseBuffer: str + j
withSize: 2];
}
TEST(@"Checking if everything was parsed", i == 11)
[pool drain];
}
|
<
<
<
<
>
>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
<
|
<
|
<
>
|
117
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
173
174
175
176
177
178
179
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
206
207
208
209
210
211
212
213
214
215
|
break;
default:
TEST(msg, NO)
break;
}
}
- (void)xmlParser: (OFXMLParser*)parser
didStartTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
{
[self xmlParserCallbackWithEventType: TAG_START
name: name
prefix: prefix
namespace: ns
attributes: attrs
string: nil
comment: nil];
}
- (void)xmlParser: (OFXMLParser*)parser
didEndTagWithName: (OFString*)name
prefix: (OFString*)prefix
namespace: (OFString*)ns
{
[self xmlParserCallbackWithEventType: TAG_END
name: name
prefix: prefix
namespace: ns
attributes: nil
string: nil
comment: nil];
}
- (void)xmlParser: (OFXMLParser*)parser
foundString: (OFString*)string
{
[self xmlParserCallbackWithEventType: STRING
name: nil
prefix: nil
namespace: nil
attributes: nil
string: string
comment: nil];
}
- (void)xmlParser: (OFXMLParser*)parser
foundComment: (OFString*)comment
{
[self xmlParserCallbackWithEventType: COMMENT
name: nil
prefix: nil
namespace: nil
attributes: nil
string: nil
comment: comment];
}
- (OFString*)xmlParser: (OFXMLParser*)parser
foundUnknownEntityNamed: (OFString*)entity
{
if ([entity isEqual: @"foo"])
return @"foobar";
return nil;
}
- (void)XMLParserTests
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFXMLParser *parser;
const char *str = "bar<foo:bar bar='b&az' qux:qux=\" quux \">\r\n"
"foo<bar<qux >bar <baz name='' test='&foo;'/> quxbar\r\n</qux>"
"</foo:bar><!-- foo bär-baz -->";
size_t j, len;
TEST(@"+[xmlParser]", (parser = [OFXMLParser xmlParser]))
TEST(@"-[setDelegate:]", [parser setDelegate: self])
/* Simulate a stream where we only get chunks */
len = strlen(str);
for (j = 0; j < len; j+= 2) {
if (j + 2 > len)
[parser parseBuffer: str + j
withSize: 1];
else
[parser parseBuffer: str + j
withSize: 2];
}
TEST(@"Checking if everything was parsed", i == 11)
[pool drain];
}
@end
|