-
Notifications
You must be signed in to change notification settings - Fork 149
/
AQChunkedXMLParser.m
159 lines (134 loc) · 4.73 KB
/
AQChunkedXMLParser.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
/*
* AQChunkedXMLData.m
* AQToolkit
*
* Created by Jim Dovey on 21/02/2009.
*
* Copyright (c) 2009, Jim Dovey
* 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 of this project's author nor the names of its
* 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
* HOLDER 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.
*
*/
// access to _NSXMLParserInfo class and to member variables doesn't work with 64-bit ObjC ABI
#ifndef __LP64__
#import "AQChunkedXMLParser.h"
#import <libxml/parser.h> // add /usr/include/libxml2 to header search paths
// this is what the NSXMLParser really has inside it
/*
@interface NSXMLParser : NSObject
{
void * _parser;
id _delegate;
_NSXMLParserInfo * _reserved1;
NSData * _reserved2;
NSMutableArray * _reserved3;
}
*/
// from this object we only use the first two instance variables
@interface _NSXMLParserInfo : NSObject
{
@public
xmlSAXHandlerPtr saxHandler;
xmlParserCtxtPtr parserContext;
unsigned int parserFlags;
NSError * error;
NSMutableArray * namespaces;
BOOL delegateAborted;
}
@end
@interface NSXMLParser ()
- (void) _setParserError: (int) xmlErr;
@end
@interface NSXMLParser (AQChunkedParserAccessorMethods)
- (_NSXMLParserInfo *) aq__parserInfo;
- (AQChunkedXMLData *) aq__data;
@end
@implementation NSXMLParser (AQChunkedParserAccessorMethods)
- (_NSXMLParserInfo *) aq__parserInfo
{
return ( (_NSXMLParserInfo *)_reserved1 );
}
- (AQChunkedXMLData *) aq__data
{
return ( (AQChunkedXMLData *)_reserved2 );
}
@end
#pragma mark -
@implementation AQChunkedXMLParser
- (id) initWithContentsOfURL: (NSURL *) url
{
if ( [url isFileURL] )
{
AQChunkedXMLData * data = [AQChunkedXMLData dataWithContentsOfFile: [url path]];
return ( [self initWithData: data] );
}
else if ( [[url scheme] isEqualToString: @"ftp"] )
{
CFReadStreamRef stream = CFReadStreamCreateWithFTPURL( kCFAllocatorDefault, (CFURLRef)url );
AQChunkedXMLData * data = [AQChunkedXMLData dataFromStream: NSMakeCollectable(stream)];
CFRelease( stream );
return ( [self initWithData: data] );
}
NSLog( @"AQChunkedXMLParser initialized with %@ URL, not using chunked data", [url scheme] );
return ( [super initWithContentsOfURL: url] );
}
- (BOOL) parse
{
// fallback to standard if we don't have a chunked data object
if ( [[self aq__data] isKindOfClass: [AQChunkedXMLData class]] == NO )
return ( [super parse] );
// a cast to ensure the pointer indirection works without the compiler bitching
_NSXMLParserInfo * info = [self aq__parserInfo];
AQChunkedXMLData * data = [self aq__data];
xmlSAXHandlerPtr saxHandler = NULL;
if ( [self delegate] != nil )
saxHandler = info->saxHandler;
info->parserContext = xmlCreatePushParserCtxt( saxHandler, self, [data bytes],
[data length], NULL );
int options = [self shouldResolveExternalEntities] ? XML_PARSE_RECOVER|XML_PARSE_NOENT|XML_PARSE_DTDLOAD : XML_PARSE_RECOVER|XML_PARSE_DTDATTR;
if ( saxHandler == NULL )
options |= XML_PARSE_NOERROR|XML_PARSE_NOWARNING;
xmlCtxtUseOptions( info->parserContext, options );
// now the changes: we loop through the chunks of data, parsing each chunk in turn
if ( [data getNextChunk] == NO )
return ( YES );
while ( [data length] != 0 )
{
int err = xmlParseChunk( info->parserContext, [data bytes], [data length],
[data getNextChunk] ? 0 : 1 );
if ( err == -1 )
{
[self _setParserError: err];
return ( NO );
}
}
return ( YES );
}
@end
#endif // __LP64__