-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCFXMLParser.h
307 lines (257 loc) · 14.6 KB
/
CFXMLParser.h
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
/*
* Copyright (c) 2008-2012 Brent Fulgham <bfulgham@gmail.org>. All rights reserved.
*
* This source code is a modified version of the CoreFoundation sources released by Apple Inc. under
* the terms of the APSL version 2.0 (see below).
*
* For information about changes from the original Apple source release can be found by reviewing the
* source control system for the project at https://sourceforge.net/svn/?group_id=246198.
*
* The original license information is as follows:
*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFXMLParser.h
Copyright (c) 1998-2011, Apple Inc. All rights reserved.
*/
/* CFXMLParser is deprecated. Clients should be aware of the fact that CFXMLParser has some serious
deficiencies in terms of both performance and standards compliance and should migrate their XML
parsing to NSXMLParser, NSXMLDocument, or other XML parsing technologies.
*/
#if !defined(__COREFOUNDATION_CFXMLPARSER__)
#define __COREFOUNDATION_CFXMLPARSER__ 1
#include <CoreFoundation/CFBase.h>
#include <CoreFoundation/CFArray.h>
#include <CoreFoundation/CFData.h>
#include <CoreFoundation/CFDictionary.h>
#include <CoreFoundation/CFTree.h>
#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFXMLNode.h>
CF_EXTERN_C_BEGIN
typedef struct __CFXMLParser * CFXMLParserRef;
/* These are the various options you can configure the parser with. These are
chosen such that an option flag of 0 (kCFXMLParserNoOptions) leaves the XML
as "intact" as possible (reports all structures; performs no replacements).
Hence, to make the parser do the most work, returning only the pure element
tree, set the option flag to kCFXMLParserAllOptions.
kCFXMLParserValidateDocument -
validate the document against its grammar from the DTD, reporting any errors.
Currently not supported.
kCFXMLParserSkipMetaData -
silently skip over metadata constructs (the DTD and comments)
kCFXMLParserReplacePhysicalEntities -
replace declared entities like <. Note that other than the 5 predefined
entities (lt, gt, quot, amp, apos), these must be defined in the DTD.
Currently not supported.
kCFXMLParserSkipWhitespace -
skip over all whitespace that does not abut non-whitespace character data.
In other words, given <foo> <bar> blah </bar></foo>, the whitespace between
foo's open tag and bar's open tag would be suppressed, but the whitespace
around blah would be preserved.
kCFXMLParserAddImpliedAttributes -
where the DTD specifies implied attribute-value pairs for a particular element,
add those pairs to any occurances of the element in the element tree.
Currently not supported.
*/
enum {
kCFXMLParserValidateDocument = (1UL << 0),
kCFXMLParserSkipMetaData = (1UL << 1),
kCFXMLParserReplacePhysicalEntities = (1UL << 2),
kCFXMLParserSkipWhitespace = (1UL << 3),
kCFXMLParserResolveExternalEntities = (1UL << 4),
kCFXMLParserAddImpliedAttributes = (1UL << 5),
kCFXMLParserAllOptions = 0x00FFFFFF,
kCFXMLParserNoOptions = 0
};
typedef CFOptionFlags CFXMLParserOptions;
/* This list is expected to grow */
enum {
kCFXMLStatusParseNotBegun = -2,
kCFXMLStatusParseInProgress = -1,
kCFXMLStatusParseSuccessful = 0,
kCFXMLErrorUnexpectedEOF = 1,
kCFXMLErrorUnknownEncoding,
kCFXMLErrorEncodingConversionFailure,
kCFXMLErrorMalformedProcessingInstruction,
kCFXMLErrorMalformedDTD,
kCFXMLErrorMalformedName,
kCFXMLErrorMalformedCDSect,
kCFXMLErrorMalformedCloseTag,
kCFXMLErrorMalformedStartTag,
kCFXMLErrorMalformedDocument,
kCFXMLErrorElementlessDocument,
kCFXMLErrorMalformedComment,
kCFXMLErrorMalformedCharacterReference,
kCFXMLErrorMalformedParsedCharacterData,
kCFXMLErrorNoData
};
typedef CFIndex CFXMLParserStatusCode;
/* These functions are called as a parse progresses.
createXMLStructure -
called as new XML structures are encountered by the parser. May return NULL to indicate
that the given structure should be skipped; if NULL is returned for a given structure,
only minimal parsing is done for that structure (enough to correctly determine its end,
and to extract any data necessary for the remainder of the parse, such as Entity definitions).
createXMLStructure (or indeed, any of the tree-creation callbacks) will not be called for any
children of the skipped structure. The only exception is that the top-most element will always
be reported even if NULL was returned for the document as a whole. NOTE: for performance reasons,
the node passed to createXMLStructure cannot be safely retained by the client; the node as
a whole must be copied (via CFXMLNodeCreateCopy), or its contents must be extracted and copied.
addChild -
called as children are parsed and are ready to be added to the tree. If createXMLStructure
returns NULL for a given structure, that structure is omitted entirely, and addChild will
NOT be called for either a NULL child or parent.
endXMLStructure -
called once a structure (and all its children) are completely parsed. As elements are encountered,
createXMLStructure is called for them first, then addChild to add the new structure to its parent,
then addChild (potentially several times) to add the new structure's children to it, then finally
endXMLStructure to show that the structure has been fully parsed.
createXMLStructure, addChild, and endXMLStructure are all REQUIRED TO BE NON-NULL.
resolveExternalEntity -
called when external entities are referenced (NOT when they are simply defined). If the function
pointer is NULL, the parser uses its internal routines to try and resolve the entity. If the
function pointer is set, and the function returns NULL, a place holder for the external entity
is inserted into the tree. In this manner, the parser's client can prevent any external network
or file accesses.
handleError - called as errors/warnings are encountered in the data stream. At some point, we will
have an enum of the expected errors, some of which will be fatal, others of which will not. If
the function pointer is NULL, the parser will silently attempt to recover. The
handleError function may always return false to force the parser to stop; if handleError returns
true, the parser will attempt to recover (fatal errors will still cause the parse to abort
immediately).
*/
typedef void * (*CFXMLParserCreateXMLStructureCallBack)(CFXMLParserRef parser, CFXMLNodeRef nodeDesc, void *info);
typedef void (*CFXMLParserAddChildCallBack)(CFXMLParserRef parser, void *parent, void *child, void *info);
typedef void (*CFXMLParserEndXMLStructureCallBack)(CFXMLParserRef parser, void *xmlType, void *info);
typedef CFDataRef (*CFXMLParserResolveExternalEntityCallBack)(CFXMLParserRef parser, CFXMLExternalID *extID, void *info);
typedef Boolean (*CFXMLParserHandleErrorCallBack)(CFXMLParserRef parser, CFXMLParserStatusCode error, void *info);
typedef struct {
CFIndex version;
CFXMLParserCreateXMLStructureCallBack createXMLStructure;
CFXMLParserAddChildCallBack addChild;
CFXMLParserEndXMLStructureCallBack endXMLStructure;
CFXMLParserResolveExternalEntityCallBack resolveExternalEntity;
CFXMLParserHandleErrorCallBack handleError;
} CFXMLParserCallBacks;
typedef const void * (*CFXMLParserRetainCallBack)(const void *info);
typedef void (*CFXMLParserReleaseCallBack)(const void *info);
typedef CFStringRef (*CFXMLParserCopyDescriptionCallBack)(const void *info);
typedef struct {
CFIndex version;
void * info;
CFXMLParserRetainCallBack retain;
CFXMLParserReleaseCallBack release;
CFXMLParserCopyDescriptionCallBack copyDescription;
} CFXMLParserContext;
CF_EXPORT
CFTypeID CFXMLParserGetTypeID(void);
/* Creates a parser which will parse the given data with the given options. xmlData may not be NULL.
dataSource should be the URL from which the data came, and may be NULL; it is used to resolve any
relative references found in xmlData. versionOfNodes determines which version CFXMLNodes are produced
by the parser; see CFXMLNode.h for more details. callBacks are the callbacks called by the parser as
the parse progresses; callBacks, callBacks->createXMLStructure, callBacks->addChild, and
callBacks->endXMLStructure must all be non-NULL. context determines what if any info pointer is
passed to the callbacks as the parse progresses; context may be NULL. */
CF_EXPORT
CFXMLParserRef CFXMLParserCreate(CFAllocatorRef allocator, CFDataRef xmlData, CFURLRef dataSource, CFOptionFlags parseOptions, CFIndex versionOfNodes, CFXMLParserCallBacks *callBacks, CFXMLParserContext *context);
/* Arguments as above, except that the data to be parsed is loaded directly
from dataSource. dataSource may not be NULL. */
CF_EXPORT
CFXMLParserRef CFXMLParserCreateWithDataFromURL(CFAllocatorRef allocator, CFURLRef dataSource, CFOptionFlags parseOptions, CFIndex versionOfNodes, CFXMLParserCallBacks *callBacks, CFXMLParserContext *context);
CF_EXPORT
void CFXMLParserGetContext(CFXMLParserRef parser, CFXMLParserContext *context);
CF_EXPORT
void CFXMLParserGetCallBacks(CFXMLParserRef parser, CFXMLParserCallBacks *callBacks);
CF_EXPORT
CFURLRef CFXMLParserGetSourceURL(CFXMLParserRef parser);
/* Returns the character index of the current parse location */
CF_EXPORT
CFIndex CFXMLParserGetLocation(CFXMLParserRef parser);
/* Returns the line number of the current parse location */
CF_EXPORT
CFIndex CFXMLParserGetLineNumber(CFXMLParserRef parser);
/* Returns the top-most object returned by the createXMLStructure callback */
CF_EXPORT
void *CFXMLParserGetDocument(CFXMLParserRef parser);
/* Get the status code or a user-readable description of the last error that occurred in a parse.
If no error has occurred, a null description string is returned. See the enum above for
possible status returns */
CF_EXPORT
CFXMLParserStatusCode CFXMLParserGetStatusCode(CFXMLParserRef parser);
CF_EXPORT
CFStringRef CFXMLParserCopyErrorDescription(CFXMLParserRef parser);
/* Cause any in-progress parse to abort with the given error code and description. errorCode
must be positive, and errorDescription may not be NULL. Cannot be called asynchronously
(i.e. must be called from within a parser callback) */
CF_EXPORT
void CFXMLParserAbort(CFXMLParserRef parser, CFXMLParserStatusCode errorCode, CFStringRef errorDescription);
/* Starts a parse of the data the parser was created with; returns success or failure.
Upon success, use CFXMLParserGetDocument() to get the product of the parse. Upon
failure, use CFXMLParserGetErrorCode() or CFXMLParserCopyErrorDescription() to get
information about the error. It is an error to call CFXMLParserParse() while a
parse is already underway. */
CF_EXPORT
Boolean CFXMLParserParse(CFXMLParserRef parser);
/* These functions provide a higher-level interface. The XML data is parsed to a
special CFTree (an CFXMLTree) with known contexts and callbacks. See CFXMLNode.h
for full details on using an CFXMLTree and the CFXMLNodes contained therein.
*/
/* Parse to an CFXMLTreeRef. parseOptions are as above. versionOfNodes determines
what version CFXMLNodes are used to populate the tree. */
CF_EXPORT
CFXMLTreeRef CFXMLTreeCreateFromData(CFAllocatorRef allocator, CFDataRef xmlData, CFURLRef dataSource, CFOptionFlags parseOptions, CFIndex versionOfNodes);
/* As above, with the additional by-reference pass of a CFDictionaryRef containing
various error information (see below). The caller is responsible for releasing the
returned dictionary. If the error dictionary is not desired, pass NULL. */
CF_EXPORT
CFXMLTreeRef CFXMLTreeCreateFromDataWithError(CFAllocatorRef allocator, CFDataRef xmlData, CFURLRef dataSource, CFOptionFlags parseOptions, CFIndex versionOfNodes, CFDictionaryRef *errorDict);
/* Loads the data to be parsed directly from dataSource. Arguments as above. */
CF_EXPORT
CFXMLTreeRef CFXMLTreeCreateWithDataFromURL(CFAllocatorRef allocator, CFURLRef dataSource, CFOptionFlags parseOptions, CFIndex versionOfNodes);
/* Generate the XMLData (ready to be written to whatever permanent storage is to be
used) from an CFXMLTree. Will NOT regenerate entity references (except those
required for syntactic correctness) if they were replaced at the parse time;
clients that wish this should walk the tree and re-insert any entity references
that should appear in the final output file. */
CF_EXPORT
CFDataRef CFXMLTreeCreateXMLData(CFAllocatorRef allocator, CFXMLTreeRef xmlTree);
/* Escaping and unescaping XML entities in CFStrings. The standard XML entities
are always replaced. */
/* Creates a CFString by replacing entities that appear in the entities dictionary.
Dictionary keys are the entities themselves, and the values should be CFStrings
containing the expansion. Pass NULL for entitiesDictionary to indicate no entities
other than the standard five. */
CF_EXPORT
CFStringRef CFXMLCreateStringByEscapingEntities(CFAllocatorRef allocator, CFStringRef string, CFDictionaryRef entitiesDictionary);
CF_EXPORT
CFStringRef CFXMLCreateStringByUnescapingEntities(CFAllocatorRef allocator, CFStringRef string, CFDictionaryRef entitiesDictionary);
/* CFXMLTreeCreateFromDataWithError error dictionary key constants. */
CF_EXPORT const CFStringRef kCFXMLTreeErrorDescription;
/* value is a CFString containing the readable error string. */
CF_EXPORT const CFStringRef kCFXMLTreeErrorLineNumber;
/* value is a CFNumber containing the line on which the error appears. */
CF_EXPORT const CFStringRef kCFXMLTreeErrorLocation;
/* value is a CFNumber containing the byte location at which the error occurred. */
CF_EXPORT const CFStringRef kCFXMLTreeErrorStatusCode;
/* value is a CFNumber containing the error status code. */
CF_EXTERN_C_END
#endif /* ! __COREFOUNDATION_CFXMLPARSER__ */