This repository was archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMPWXmlFastInfosetParser.m
291 lines (246 loc) · 6.8 KB
/
MPWXmlFastInfosetParser.m
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
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
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
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
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
283
284
285
286
287
288
289
290
291
//
// MPWXmlFastInfosetParser.m
// MPWXmlKit
//
// Created by Marcel Weiher on 10/4/07.
// Copyright 2007 Marcel Weiher. All rights reserved.
//
//
// Note to self: this is currently a subclass of MPWXmlParser, but
// that won't work in the long run because of subclassing going on
// in other dimensions, so one (or both) have to be changed to
// composition and/or categories.
//
#import "MPWXmlFastInfosetParser.h"
#import "MPWXmlParserTesting.h"
#import "SaxDocumentHandler.h"
#import "mpwfoundation_imports.h"
@implementation MPWXmlFastInfosetParser
#define SKIP(n) (curpos+=(n))
#define PARSERSELF ((NSXMLParser*)self)
-(void)skip:(int)n
{
SKIP(n);
}
-(int)read2ByteInt
{
int value =(curpos[0]<<8 ) + curpos[1];
SKIP(2);
return value;
}
-(unsigned char)readByte
{
return *curpos++;
}
-(int)scanVersion
{
return fiVersion=[self read2ByteInt];
}
-(int)verifyFastInfosetDocumentAndReturnHeaderLength
{
if ( bytestart[0]==0xe0 && bytestart[1]==0 ) {
return 2;
}
return 0;
}
-(BOOL)verifyFastInfosetDocument
{
return [self verifyFastInfosetDocumentAndReturnHeaderLength]>0;
}
-(BOOL)verifyFastInfosetAndSkip
{
int headerLen=[self verifyFastInfosetDocumentAndReturnHeaderLength];
SKIP(headerLen);
return headerLen > 0;
}
-stringFromBytes:(int)numBytes
{
id result=[[[NSString alloc] initWithBytes:curpos length:numBytes encoding:NSUTF8StringEncoding] autorelease];
SKIP(numBytes);
return result;
}
objectAccessor( NSData, fiData , _setFiData )
-(void)setFiData:(NSData*)xmldata
{
[self _setFiData:xmldata];
bytestart=[xmldata bytes];
byteend=bytestart+[xmldata length];
curpos=bytestart;
}
-(BOOL)hasAddtionalData { return optionalComponentsMask & 0x40; }
-(BOOL)hasInitialVocabulary { return optionalComponentsMask & 0x20; }
-(BOOL)hasNotations { return optionalComponentsMask & 0x10; }
-(BOOL)hasUnparsedEntities { return optionalComponentsMask & 0x08; }
-(BOOL)hasCharacterEncodingScheme { return optionalComponentsMask & 0x04; }
-(BOOL)hasStandalone { return optionalComponentsMask & 0x02; }
-(BOOL)hasVersion { return optionalComponentsMask & 0x01; }
-parseNamespace
{
[NSException raise:@"notsupported" format:@"method %@ not implemented",NSStringFromSelector(_cmd)];
return self;
}
-parsePrefix
{
[NSException raise:@"notsupported" format:@"method %@ not implemented",NSStringFromSelector(_cmd)];
return self;
}
-(int)read32BitInt
{
[NSException raise:@"unimplemented" format:@"read32BitInt not implemented yet"];
return 0;
}
-parseBytesStartingOnSeventhBit:(unsigned char)startByte
{
switch ( startByte & 0x3 ) {
case 0:
case 1:
return [self stringFromBytes:(startByte&0x1)+1];
break;
case 2:
return [self stringFromBytes:[self readByte]+3];
break;
case 3:
return [self stringFromBytes:[self read32BitInt]+265];
break;
}
return nil;
}
-parseEncodedCharacterStringOnFifthBit:(unsigned char)startByte // C.20
{
if ( (startByte & 0xc) == 0 ) {
return [self parseBytesStartingOnSeventhBit:startByte];
} else {
[NSException raise:@"encoding-not-supperted" format:@"unsupported non-UTF-8 encoding"];
return nil;
}
}
-(void)parseCharacterContent:(unsigned char)startByte // starting on the third bit (C.15)
{
BOOL literalCharacter=(startByte & 0x020) == 0;
if ( literalCharacter ) {
// BOOL addToTable=(startByte & 0x010) != 0;
id name=[self parseEncodedCharacterStringOnFifthBit:startByte];
[documentHandler parser:PARSERSELF foundCharacters:name];
} else {
[NSException raise:@"unsupported" format:@"string index not supported"];
}
}
-parseNameStartingOnFirstBit //
{
id name=nil;
unsigned char nameDefByte=[self readByte];
BOOL isLiteral=(nameDefByte & 0x80) == 0;
if ( isLiteral ) {
if ( (nameDefByte & 0x40) == 0) {
int length=(nameDefByte&63)+1;
name=[self stringFromBytes:length];
} else {
[NSException raise:@"lengthtype" format:@"length type not supported"];
}
} else {
[NSException raise:@"lengthtype" format:@"non-literal names not supported"];
}
return name;
}
-(void)parseElementContent
{
do {
unsigned char nextByte=[self readByte];
if ( (nextByte & 0xf0) == 0xf0 ) {
return;
} else if ( (nextByte & 0xc0) == 0x80 ) {
[self parseCharacterContent:nextByte];
} else if ( (nextByte & 0x80) == 0 ) {
[self parseElement:nextByte];
}
} while ( YES );
}
-(void)parseElement:(unsigned char)startByte
{
BOOL hasAttributes = (startByte & 0x40 ) != 0;
if ( !hasAttributes ) {
BOOL isLiteralQualifiedName=(startByte & 0x3c)==0x3c;
if ( isLiteralQualifiedName ) {
#if 1
BOOL hasPrefix = (startByte & 0x2) != 0;
BOOL hasNamespace = (startByte & 0x1) != 0;
id prefix,namespace,name;
if ( hasPrefix ) {
prefix = [self parsePrefix];
}
if ( hasNamespace ) {
namespace = [self parseNamespace];
}
name=[self parseNameStartingOnFirstBit];
[documentHandler parser:PARSERSELF didStartElement:name namespaceURI:nil qualifiedName:nil attributes:@{}];
[self parseElementContent];
[documentHandler parser:PARSERSELF didEndElement:name namespaceURI:nil qualifiedName:nil ];
#endif
}
}
}
-(void)scanDocument
{
unsigned char startByte=[self readByte];
BOOL isElement = (startByte & 0x80) == 0;
[documentHandler parserDidStartDocument:PARSERSELF];
if ( isElement ) {
[self parseElement:startByte];
}
[documentHandler parserDidEndDocument:PARSERSELF];
}
-(BOOL)parse:xmldata
{
int headerskip=0;
[self setFiData:xmldata];
headerskip = [self verifyFastInfosetDocumentAndReturnHeaderLength];
if ( headerskip > 0 ) {
SKIP(headerskip);
fiVersion=[self scanVersion];
optionalComponentsMask=[self readByte];
if ( optionalComponentsMask == 0 ) {
[self scanDocument];
}
} else {
}
return YES;
}
+testSelectors { return [NSArray array]; }
@end
@interface MPWFastInfoSetParserTesting : MPWXmlParserTesting
@end
@implementation MPWFastInfoSetParserTesting
+xmlResourceWithName:(NSString*)name
{
return [self resourceWithName:name type:@"fi"];
}
+parser
{
return [[[MPWXmlFastInfosetParser alloc] init] autorelease];
}
+(void)testVerifyFI
{
MPWXmlFastInfosetParser* parser=[self parser];
[parser setFiData:[self xmlResourceWithName:@"test1"]];
NSAssert( [parser verifyFastInfosetDocument], @"test1.fi should be a fast infoset document");
[parser skip:2];
INTEXPECT( [parser scanVersion], 1 , @"version encoded in document");
INTEXPECT( [parser readByte], 0 , @"optional components");
[parser setFiData:[self resourceWithName:@"test1" type:@"xml"]];
NSAssert( ![parser verifyFastInfosetDocument], @"test1.xml should not be a fast infoset document");
}
+(NSArray*)testSelectors
{
return @[
#if 0
@"testVerifyFI",
@"testBasicSaxParse",
@"testBasicSaxParseWithCharacterContent",
@"testBasicSaxParseWithElementAndCharacterContent",
@"testBasicSaxParseOfEmptyElement",
@"testEmptyElementWithAttribute",
@"testNamespaceParsingMPWXML",
#endif
];
}
@end