This repository has been 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
/
MPWXmlParser.m
312 lines (276 loc) · 10.7 KB
/
MPWXmlParser.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* MPWXmlParser.m Copyright (c) Marcel P. Weiher 1999-2006, All Rights Reserved,
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
Neither the name Marcel Weiher nor the names of contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
, created on Sun 05-Sep-1999 */
#import "MPWXmlParser.h"
#import "MPWMAXParser_private.h"
#import "MPWXmlAttributes.h"
#import "SaxDocumentHandler.h"
#import "mpwfoundation_imports.h"
@implementation MPWSAXParser
-initWithData:aData
{
self=[super initWithData:aData];
[self setDelegate:nil];
return self;
}
-(BOOL)endElement:(const char*)start length:(long)len namespaceLen:(long)namespaceLen
{
id endName=nil;
id prefix=nil;
id fullyQualified=nil;
id namespaceURI=nil;
const char *namespaceLocation=NULL;
const char *namespacePrefix=NULL;
// const char *namespace;
RECORDSCANPOSITION( start, len );
start+=2;
len-=3;
#if 1
id openTag=CURRENTTAG;
const char *b=[openTag bytes];
for (int i=0;i<len;i++) {
if ( start[i]!=b[i]) {
endName=TAGFORCSTRING( start, len); // MPWUniqueStringWithCString( start, len );
break;
}
}
if (!endName) {
endName=openTag;
}
#else
endName=TAGFORCSTRING( start, len); // MPWUniqueStringWithCString( start, len );
#endif
// NSLog(@"end element </%@>, currentTag: %@ tagStackLen: %d",endName,CURRENTTAG,tagStackLen);
if ( shouldProcessNamespaces && (namespaceLen>1)) {
namespaceLocation=start+namespaceLen-2;
fullyQualified=endName;
namespacePrefix=start;
namespaceLen=len-(namespaceLocation-start)-1; // namespaceLocation-namespacePrefix-1;
endName=TAGFORCSTRING( namespaceLocation+1, namespaceLen);
long prefixLen=namespaceLocation-start;
prefix = TAGFORCSTRING( start, prefixLen );
namespaceURI=[namespacePrefixToURIMap objectForKey:prefix];
// NSLog(@"tag: %@ prefix: %@ fullyQualified: %@ uri: %@",endName,prefix,fullyQualified,namespaceURI);
}
// NSLog(@"end tag, tagStackLen: %d",tagStackLen);
if ( CURRENTTAG == endName || [CURRENTTAG isEqual: endName] ) {
POPTAG;
// [documentHandler parser:(NSXMLParser*)self didEndElement:endName namespaceURI:namespaceURI qualifiedName:fullyQualified];
ENDELEMENT( endName,namespaceURI, fullyQualified );
// NSLog(@"end tag end, tagStackLen: %d",tagStackLen);
return YES;
} else {
if ( enforceTagNesting ) {
[self setParserError:[NSError errorWithDomain:@"XML" code:76 userInfo:nil]];
[NSException raise:@"non-matching tags" format:@"non matching tags expecting: '%@' (at %ld) actual end tag: '%@' tag stack:%@",[self currentTag],(long)tagStackLen ,endName,[self _fullTagStackString]];
return NO;
} else {
while ( ![CURRENTTAG isEqual: endName] && tagStackLen>0 ) {
// NSLog(@"stack[%d] non matching end-tags: %@",tagStackLen,CURRENTTAG);
POPTAG;
}
// NSLog(@"will call ENDELEMENT with target: %@ tag: %@ selector: %x %@",documentHandler,endName,ENDELEMENTSELECTOR,NSStringFromSelector(ENDELEMENTSELECTOR) );
ENDELEMENT( endName, namespaceURI, fullyQualified );
// [documentHandler parser:(NSXMLParser*)self didEndElement:endName namespaceURI:namespaceURI qualifiedName:fullyQualified];
// ENDELEMENT( endName,namespaceURI, fullyQualified );
// NSLog(@"did call ENDELEMENT");
// NSLog(@"end tag end, tagStackLen: %d",tagStackLen);
return YES;
}
}
}
-(BOOL)attributeName:(const char*)nameStart length:(long)nameLen value:(const char*)valueStart length:(long)valueLen namespaceLen:(long)namespaceLen valueHasHighBit:(BOOL)highBit
/*"
"*/
{
id name = MAKEDATA( nameStart, nameLen );
id value=nil;
if ( autotranslateUTF8 && highBit) {
#ifdef GS_API_LATEST
value = [[[NSString alloc] initWithBytes:valueStart length:valueLen encoding:NSUTF8StringEncoding] autorelease];
#else
value = [(id)CFStringCreateWithBytes(NULL, (const unsigned char*)valueStart, valueLen, kCFStringEncodingUTF8, NO) autorelease];
#endif
}
if (! value ) {
value = MAKEDATA( valueStart, valueLen );
}
if ( shouldProcessNamespaces && nameLen >= 5 && !strncmp( nameStart, "xmlns", 5 ) ) {
if ( nameLen > 6 ) {
const char *tagStart=nameStart+6;
long tagLen=nameLen-6;
name=TAGFORCSTRING( tagStart, tagLen );
} else {
name=@"";
}
[self handleNameSpaceAttribute:name withValue:value];
} else {
if ( !_attributes ) {
// NSLog(@"getObject: %p %p",attributeCache->getObject,[attributeCache getObjectIMP]);
id att=GETOBJECT( (MPWObjectCache*)attributeCache);
[self _setAttributes:att];
[_attributes removeAllObjects];
}
[_attributes setValue:value forAttribute:name];
}
return YES;
}
-(BOOL)beginElement:(const char*)fullyQualifedPtr length:(long)len nameLen:(long)fullyQualifiedLen namespaceLen:(long)namespaceLen
{
const char *tagStartPtr=fullyQualifedPtr;
long tagLen=fullyQualifiedLen;
id tag=nil;
BOOL isEmpty=NO;
// NSString *namespacePrefixTag=nil;
NSString *namespaceURI=nil;
NSString *fullyQualifiedTag=nil;
NSXMLElementInfo *currentElement = (((NSXMLElementInfo*)_elementStack)+tagStackLen );
RECORDSCANPOSITION( fullyQualifedPtr, len );
if ( currentElement ) {
currentElement->start = fullyQualifedPtr;
currentElement->fullyQualifiedLen = fullyQualifiedLen;
}
// if ( NO && charactersAreSpace && !reportIgnoreableWhitespace) {
// NSLog(@"characters before begin: %.*s were space",nameLen,start);
// [self flushPureSpace];
// }
lastTagWasOpen=YES;
charactersAreSpace=YES;
if ( fullyQualifedPtr[len-2]=='/' ) {
// trailing '/>' means this is an empty element
isEmpty=YES;
if ( fullyQualifedPtr[fullyQualifiedLen-1]=='/' ) {
// if it's '<name/>', we must also adjust the name length
fullyQualifiedLen--;
}
}
//--- remove brackets from name
fullyQualifedPtr++;
fullyQualifiedLen--;
len-=2;
//--- support for partial parsing to a specified depth (for lazy DOM parser...)
currentElement->isIncomplete=NO;
if ( fullyQualifiedLen > 0 ) {
id attrs=_attributes;
// id prefixTag=nil;
//--- handle namespace
fullyQualifiedTag=TAGFORCSTRING( fullyQualifedPtr, fullyQualifiedLen);
if ( namespaceLen > 0) {
namespaceLen-=1;
tagLen-=namespaceLen+2;
tagStartPtr=fullyQualifedPtr+namespaceLen+1;
id namespacePrefixTag=TAGFORCSTRING( fullyQualifedPtr, namespaceLen );
namespaceURI=[namespacePrefixToURIMap objectForKey:namespacePrefixTag];
tag=TAGFORCSTRING( tagStartPtr, tagLen);
} else {
tagStartPtr=fullyQualifedPtr;
tagLen=fullyQualifiedLen;
tag=fullyQualifiedTag;
}
//---- meta tags need special case handling (charset decl.)
if ( tagLen == 4 && !strncasecmp(tagStartPtr, "meta", 4) ) {
[self handleMetaTag];
}
if ( !tag ) {
}
PUSHTAG( tag);
#if 0
NSLog(@"begin element: <%@> stackDepth %d stack: %@",tag,tagStackLen,[self _fullTagStackString]);
#endif
// NSLog(@"beginelement: %@/%x",documentHandler,beginElement);
#if 1
if ( !attrs ) {
static id _emptyDict=nil;
if ( !_emptyDict ) {
_emptyDict=GETOBJECT( (MPWObjectCache*)attributeCache ); // [[NSXMLAttributes alloc] init];
[_emptyDict removeAllObjects];
[_emptyDict retain];
}
attrs=_emptyDict;
}
#endif
BEGINELEMENT( tag, namespaceURI, fullyQualifiedTag ,attrs );
if ([attrs retainCount] <= 2 ) {
[attrs removeAllObjects];
}
// fprintf(stderr,"BEGINELEMENT: self=%p beginElement: %p documentHandler: %p\n",self,beginElement,documentHandler);
if ( _attributes ) {
[self clearAttributes];
}
if ( isEmpty ) {
[tag retain];
POPTAG;
lastTagWasOpen=NO;
ENDELEMENT(tag,namespaceURI,fullyQualifiedTag);
[tag release];
}
} else {
NSLog(@"nameLen <= 0!");
}
// NSLog(@"begin tag (end), tagStackLen: %d",tagStackLen);
return YES;
}
-(BOOL)makeSpace:(const char*)start length:(long)len
/*"
Call-back for spaces. Create text data.
"*/
{
RECORDSCANPOSITION( start, len );
return [self makeText:start length:len firstEntityOffset:-1];
}
-(BOOL)characterDataAllowed:parser
{
return tagStackLen > 0 ? 1 : 0;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {}
-(void)cdata:chars{}
-(void)characters:chars{}
-(void)ignorableWhiteSpace:chars{}
-(void)processingInstructionTarget:piTarget data:piData{}
-(void)parserDidStartDocument:(NSXMLParser*)parser {}
-(void)parserDidEndDocument:(NSXMLParser*)parser {}
-(void)handleNameSpaceAttribute:name withValue:value
{
if ( [documentHandler respondsToSelector:@selector(parser:didStartMappingPrefix:toURI:)] ) {
// NSLog(@"map name: '%@' toURI: '%@'",name,value);
if ( !namespacePrefixToURIMap ) {
namespacePrefixToURIMap=[[NSMutableDictionary alloc] init];
}
[namespacePrefixToURIMap setObject:[value stringValue] forKey:[name stringValue]];
// [super handleNameSpaceAttribute:name withValue:value];
// NSLog(@"prefix2uri map: %@",namespacePrefixToURIMap);
if ( shouldReportNamespacePrefixes && [documentHandler respondsToSelector:@selector(parser:didStartMappingPrefix:toURI:)] ) {
[ documentHandler parser:(NSXMLParser*)self didStartMappingPrefix:name toURI:value];
}
}
}
-(void)setCharacterHandlerWithDocumentHandler:newCharHandler
{
characterHandler=newCharHandler;
}
+testSelectors
{
return [NSArray array];
}
@end