CFXMLParser.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. { CFXMLParser.h
  2. Copyright (c) 1998-2005, Apple, Inc. All rights reserved.
  3. }
  4. { Pascal Translation Updated: Peter N Lewis, <[email protected]>, November 2005 }
  5. {
  6. Modified for use with Free Pascal
  7. Version 210
  8. Please report any bugs to <[email protected]>
  9. }
  10. {$mode macpas}
  11. {$packenum 1}
  12. {$macro on}
  13. {$inline on}
  14. {$calling mwpascal}
  15. unit CFXMLParser;
  16. interface
  17. {$setc UNIVERSAL_INTERFACES_VERSION := $0342}
  18. {$setc GAP_INTERFACES_VERSION := $0210}
  19. {$ifc not defined USE_CFSTR_CONSTANT_MACROS}
  20. {$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
  21. {$endc}
  22. {$ifc defined CPUPOWERPC and defined CPUI386}
  23. {$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
  24. {$endc}
  25. {$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
  26. {$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
  27. {$endc}
  28. {$ifc not defined __ppc__ and defined CPUPOWERPC}
  29. {$setc __ppc__ := 1}
  30. {$elsec}
  31. {$setc __ppc__ := 0}
  32. {$endc}
  33. {$ifc not defined __i386__ and defined CPUI386}
  34. {$setc __i386__ := 1}
  35. {$elsec}
  36. {$setc __i386__ := 0}
  37. {$endc}
  38. {$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
  39. {$error Conflicting definitions for __ppc__ and __i386__}
  40. {$endc}
  41. {$ifc defined __ppc__ and __ppc__}
  42. {$setc TARGET_CPU_PPC := TRUE}
  43. {$setc TARGET_CPU_X86 := FALSE}
  44. {$elifc defined __i386__ and __i386__}
  45. {$setc TARGET_CPU_PPC := FALSE}
  46. {$setc TARGET_CPU_X86 := TRUE}
  47. {$elsec}
  48. {$error Neither __ppc__ nor __i386__ is defined.}
  49. {$endc}
  50. {$setc TARGET_CPU_PPC_64 := FALSE}
  51. {$ifc defined FPC_BIG_ENDIAN}
  52. {$setc TARGET_RT_BIG_ENDIAN := TRUE}
  53. {$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
  54. {$elifc defined FPC_LITTLE_ENDIAN}
  55. {$setc TARGET_RT_BIG_ENDIAN := FALSE}
  56. {$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
  57. {$elsec}
  58. {$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
  59. {$endc}
  60. {$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
  61. {$setc CALL_NOT_IN_CARBON := FALSE}
  62. {$setc OLDROUTINENAMES := FALSE}
  63. {$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
  64. {$setc OPAQUE_UPP_TYPES := TRUE}
  65. {$setc OTCARBONAPPLICATION := TRUE}
  66. {$setc OTKERNEL := FALSE}
  67. {$setc PM_USE_SESSION_APIS := TRUE}
  68. {$setc TARGET_API_MAC_CARBON := TRUE}
  69. {$setc TARGET_API_MAC_OS8 := FALSE}
  70. {$setc TARGET_API_MAC_OSX := TRUE}
  71. {$setc TARGET_CARBON := TRUE}
  72. {$setc TARGET_CPU_68K := FALSE}
  73. {$setc TARGET_CPU_MIPS := FALSE}
  74. {$setc TARGET_CPU_SPARC := FALSE}
  75. {$setc TARGET_OS_MAC := TRUE}
  76. {$setc TARGET_OS_UNIX := FALSE}
  77. {$setc TARGET_OS_WIN32 := FALSE}
  78. {$setc TARGET_RT_MAC_68881 := FALSE}
  79. {$setc TARGET_RT_MAC_CFM := FALSE}
  80. {$setc TARGET_RT_MAC_MACHO := TRUE}
  81. {$setc TYPED_FUNCTION_POINTERS := TRUE}
  82. {$setc TYPE_BOOL := FALSE}
  83. {$setc TYPE_EXTENDED := FALSE}
  84. {$setc TYPE_LONGLONG := TRUE}
  85. uses MacTypes,CFBase,CFArray,CFData,CFDictionary,CFTree,CFURL,CFXMLNode;
  86. {$ALIGN POWER}
  87. type
  88. CFXMLParserRef = ^SInt32; { an opaque 32-bit type }
  89. CFXMLParserRefPtr = ^CFXMLParserRef;
  90. { These are the various options you can configure the parser with. These are
  91. chosen such that an option flag of 0 (kCFXMLParserNoOptions) leaves the XML
  92. as "intact" as possible (reports all structures; performs no replacements).
  93. Hence, to make the parser do the most work, returning only the pure element
  94. tree, set the option flag to kCFXMLParserAllOptions.
  95. kCFXMLParserValidateDocument -
  96. validate the document against its grammar from the DTD, reporting any errors.
  97. Currently not supported.
  98. kCFXMLParserSkipMetaData -
  99. silently skip over metadata constructs (the DTD and comments)
  100. kCFXMLParserReplacePhysicalEntities -
  101. replace declared entities like &lt;. Note that other than the 5 predefined
  102. entities (lt, gt, quot, amp, apos), these must be defined in the DTD.
  103. Currently not supported.
  104. kCFXMLParserSkipWhitespace -
  105. skip over all whitespace that does not abut non-whitespace character data.
  106. In other words, given <foo> <bar> blah </bar></foo>, the whitespace between
  107. foo's open tag and bar's open tag would be suppressed, but the whitespace
  108. around blah would be preserved.
  109. kCFXMLParserAddImpliedAttributes -
  110. where the DTD specifies implied attribute-value pairs for a particular element,
  111. add those pairs to any occurances of the element in the element tree.
  112. Currently not supported.
  113. }
  114. type
  115. CFXMLParserOptions = SInt32;
  116. const
  117. kCFXMLParserValidateDocument = 1 shl 0;
  118. kCFXMLParserSkipMetaData = 1 shl 1;
  119. kCFXMLParserReplacePhysicalEntities = 1 shl 2;
  120. kCFXMLParserSkipWhitespace = 1 shl 3;
  121. kCFXMLParserResolveExternalEntities = 1 shl 4;
  122. kCFXMLParserAddImpliedAttributes = 1 shl 5;
  123. kCFXMLParserAllOptions = $00FFFFFF;
  124. kCFXMLParserNoOptions = 0;
  125. { This list is expected to grow }
  126. type
  127. CFXMLParserStatusCode = SInt32;
  128. const
  129. kCFXMLStatusParseNotBegun = -2;
  130. kCFXMLStatusParseInProgress = -1;
  131. kCFXMLStatusParseSuccessful = 0;
  132. kCFXMLErrorUnexpectedEOF = 1;
  133. kCFXMLErrorUnknownEncoding = 2;
  134. kCFXMLErrorEncodingConversionFailure = 3;
  135. kCFXMLErrorMalformedProcessingInstruction = 4;
  136. kCFXMLErrorMalformedDTD = 5;
  137. kCFXMLErrorMalformedName = 6;
  138. kCFXMLErrorMalformedCDSect = 7;
  139. kCFXMLErrorMalformedCloseTag = 8;
  140. kCFXMLErrorMalformedStartTag = 9;
  141. kCFXMLErrorMalformedDocument = 10;
  142. kCFXMLErrorElementlessDocument = 11;
  143. kCFXMLErrorMalformedComment = 12;
  144. kCFXMLErrorMalformedCharacterReference = 13;
  145. kCFXMLErrorMalformedParsedCharacterData = 14;
  146. kCFXMLErrorNoData = 15;
  147. { These functions are called as a parse progresses.
  148. createXMLStructure -
  149. called as new XML structures are encountered by the parser. May return NULL to indicate
  150. that the given structure should be skipped; if NULL is returned for a given structure,
  151. only minimal parsing is done for that structure (enough to correctly determine its end,
  152. and to extract any data necessary for the remainder of the parse, such as Entity definitions).
  153. createXMLStructure (or indeed, any of the tree-creation callbacks) will not be called for any
  154. children of the skipped structure. The only exception is that the top-most element will always
  155. be reported even if NULL was returned for the document as a whole. NOTE: for performance reasons,
  156. the node passed to createXMLStructure cannot be safely retained by the client; the node as
  157. a whole must be copied (via CFXMLNodeCreateCopy), or its contents must be extracted and copied.
  158. addChild -
  159. called as children are parsed and are ready to be added to the tree. If createXMLStructure
  160. returns NULL for a given structure, that structure is omitted entirely, and addChild will
  161. NOT be called for either a NULL child or parent.
  162. endXMLStructure -
  163. called once a structure (and all its children) are completely parsed. As elements are encountered,
  164. createXMLStructure is called for them first, then addChild to add the new structure to its parent,
  165. then addChild (potentially several times) to add the new structure's children to it, then finally
  166. endXMLStructure to show that the structure has been fully parsed.
  167. createXMLStructure, addChild, and endXMLStructure are all REQUIRED TO BE NON-NULL.
  168. resolveExternalEntity -
  169. called when external entities are referenced (NOT when they are simply defined). If the function
  170. pointer is NULL, the parser uses its internal routines to try and resolve the entity. If the
  171. function pointer is set, and the function returns NULL, a place holder for the external entity
  172. is inserted into the tree. In this manner, the parser's client can prevent any external network
  173. or file accesses.
  174. handleError - called as errors/warnings are encountered in the data stream. At some point, we will
  175. have an enum of the expected errors, some of which will be fatal, others of which will not. If
  176. the function pointer is NULL, the parser will silently attempt to recover. The
  177. handleError function may always return false to force the parser to stop; if handleError returns
  178. true, the parser will attempt to recover (fatal errors will still cause the parse to abort
  179. immediately).
  180. }
  181. type
  182. CFXMLParserCreateXMLStructureCallBack = function( parser: CFXMLParserRef; nodeDesc: CFXMLNodeRef; info: UnivPtr ): UnivPtr;
  183. CFXMLParserAddChildCallBack = procedure( parser: CFXMLParserRef; parent: UnivPtr; child: UnivPtr; info: UnivPtr );
  184. CFXMLParserEndXMLStructureCallBack = procedure( parser: CFXMLParserRef; xmlType: UnivPtr; info: UnivPtr );
  185. CFXMLParserResolveExternalEntityCallBack = function( parser: CFXMLParserRef; var extID: CFXMLExternalID; info: UnivPtr ): CFDataRef;
  186. CFXMLParserHandleErrorCallBack = function( parser: CFXMLParserRef; error: CFXMLParserStatusCode; info: UnivPtr ): Boolean;
  187. CFXMLParserCallBacksPtr = ^CFXMLParserCallBacks;
  188. CFXMLParserCallBacks = record
  189. version: CFIndex;
  190. createXMLStructure: CFXMLParserCreateXMLStructureCallBack;
  191. addChild: CFXMLParserAddChildCallBack;
  192. endXMLStructure: CFXMLParserEndXMLStructureCallBack;
  193. resolveExternalEntity: CFXMLParserResolveExternalEntityCallBack;
  194. handleError: CFXMLParserHandleErrorCallBack;
  195. end;
  196. type
  197. CFXMLParserRetainCallBack = function( info: {const} UnivPtr ): UnivPtr;
  198. CFXMLParserReleaseCallBack = procedure( info: {const} UnivPtr );
  199. CFXMLParserCopyDescriptionCallBack = function( info: {const} UnivPtr ): CFStringRef;
  200. CFXMLParserContextPtr = ^CFXMLParserContext;
  201. CFXMLParserContext = record
  202. version: CFIndex;
  203. info: UnivPtr;
  204. retain: CFXMLParserRetainCallBack;
  205. release: CFXMLParserReleaseCallBack;
  206. copyDescription: CFXMLParserCopyDescriptionCallBack;
  207. end;
  208. function CFXMLParserGetTypeID: CFTypeID; external name '_CFXMLParserGetTypeID';
  209. { Creates a parser which will parse the given data with the given options. xmlData may not be NULL.
  210. dataSource should be the URL from which the data came, and may be NULL; it is used to resolve any
  211. relative references found in xmlData. versionOfNodes determines which version CFXMLNodes are produced
  212. by the parser; see CFXMLNode.h for more details. callBacks are the callbacks called by the parser as
  213. the parse progresses; callBacks, callBacks->createXMLStructure, callBacks->addChild, and
  214. callBacks->endXMLStructure must all be non-NULL. context determines what if any info pointer is
  215. passed to the callbacks as the parse progresses; context may be NULL. }
  216. function CFXMLParserCreate( allocator: CFAllocatorRef; xmlData: CFDataRef; dataSource: CFURLRef; parseOptions: CFOptionFlags; versionOfNodes: CFIndex; var callBacks: CFXMLParserCallBacks; var context: CFXMLParserContext ): CFXMLParserRef; external name '_CFXMLParserCreate';
  217. { Arguments as above, except that the data to be parsed is loaded directly
  218. from dataSource. dataSource may not be NULL. }
  219. function CFXMLParserCreateWithDataFromURL( allocator: CFAllocatorRef; dataSource: CFURLRef; parseOptions: CFOptionFlags; versionOfNodes: CFIndex; var callBacks: CFXMLParserCallBacks; var context: CFXMLParserContext ): CFXMLParserRef; external name '_CFXMLParserCreateWithDataFromURL';
  220. procedure CFXMLParserGetContext( parser: CFXMLParserRef; var context: CFXMLParserContext ); external name '_CFXMLParserGetContext';
  221. procedure CFXMLParserGetCallBacks( parser: CFXMLParserRef; var callBacks: CFXMLParserCallBacks ); external name '_CFXMLParserGetCallBacks';
  222. function CFXMLParserGetSourceURL( parser: CFXMLParserRef ): CFURLRef; external name '_CFXMLParserGetSourceURL';
  223. { Returns the character index of the current parse location }
  224. function CFXMLParserGetLocation( parser: CFXMLParserRef ): CFIndex; external name '_CFXMLParserGetLocation';
  225. { Returns the line number of the current parse location }
  226. function CFXMLParserGetLineNumber( parser: CFXMLParserRef ): CFIndex; external name '_CFXMLParserGetLineNumber';
  227. { Returns the top-most object returned by the createXMLStructure callback }
  228. function CFXMLParserGetDocument( parser: CFXMLParserRef ): UnivPtr; external name '_CFXMLParserGetDocument';
  229. { Get the status code or a user-readable description of the last error that occurred in a parse.
  230. If no error has occurred, a null description string is returned. See the enum above for
  231. possible status returns }
  232. function CFXMLParserGetStatusCode( parser: CFXMLParserRef ): CFXMLParserStatusCode; external name '_CFXMLParserGetStatusCode';
  233. function CFXMLParserCopyErrorDescription( parser: CFXMLParserRef ): CFStringRef; external name '_CFXMLParserCopyErrorDescription';
  234. { Cause any in-progress parse to abort with the given error code and description. errorCode
  235. must be positive, and errorDescription may not be NULL. Cannot be called asynchronously
  236. (i.e. must be called from within a parser callback) }
  237. procedure CFXMLParserAbort( parser: CFXMLParserRef; errorCode: CFXMLParserStatusCode; errorDescription: CFStringRef ); external name '_CFXMLParserAbort';
  238. { Starts a parse of the data the parser was created with; returns success or failure.
  239. Upon success, use CFXMLParserGetDocument() to get the product of the parse. Upon
  240. failure, use CFXMLParserGetErrorCode() or CFXMLParserCopyErrorDescription() to get
  241. information about the error. It is an error to call CFXMLParserParse() while a
  242. parse is already underway. }
  243. function CFXMLParserParse( parser: CFXMLParserRef ): Boolean; external name '_CFXMLParserParse';
  244. { These functions provide a higher-level interface. The XML data is parsed to a
  245. special CFTree (an CFXMLTree) with known contexts and callbacks. See CFXMLNode.h
  246. for full details on using an CFXMLTree and the CFXMLNodes contained therein.
  247. }
  248. { Parse to an CFXMLTreeRef. parseOptions are as above. versionOfNodes determines
  249. what version CFXMLNodes are used to populate the tree. }
  250. function CFXMLTreeCreateFromData( allocator: CFAllocatorRef; xmlData: CFDataRef; dataSource: CFURLRef; parseOptions: CFOptionFlags; versionOfNodes: CFIndex ): CFXMLTreeRef; external name '_CFXMLTreeCreateFromData';
  251. { As above, with the additional by-reference pass of a CFDictionaryRef containing
  252. various error information (see below). The caller is responsible for releasing the
  253. returned dictionary. If the error dictionary is not desired, pass NULL. }
  254. function CFXMLTreeCreateFromDataWithError( allocator: CFAllocatorRef; xmlData: CFDataRef; dataSource: CFURLRef; parseOptions: CFOptionFlags; versionOfNodes: CFIndex; var errorDict: CFDictionaryRef ): CFXMLTreeRef; external name '_CFXMLTreeCreateFromDataWithError';
  255. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  256. { Loads the data to be parsed directly from dataSource. Arguments as above. }
  257. function CFXMLTreeCreateWithDataFromURL( allocator: CFAllocatorRef; dataSource: CFURLRef; parseOptions: CFOptionFlags; versionOfNodes: CFIndex ): CFXMLTreeRef; external name '_CFXMLTreeCreateWithDataFromURL';
  258. { Generate the XMLData (ready to be written to whatever permanent storage is to be
  259. used) from an CFXMLTree. Will NOT regenerate entity references (except those
  260. required for syntactic correctness) if they were replaced at the parse time;
  261. clients that wish this should walk the tree and re-insert any entity references
  262. that should appear in the final output file. }
  263. function CFXMLTreeCreateXMLData( allocator: CFAllocatorRef; xmlTree: CFXMLTreeRef ): CFDataRef; external name '_CFXMLTreeCreateXMLData';
  264. { Escaping and unescaping XML entities in CFStrings. The standard XML entities
  265. are always replaced. }
  266. { Creates a CFString by replacing entities that appear in the entities dictionary.
  267. Dictionary keys are the entities themselves, and the values should be CFStrings
  268. containing the expansion. Pass NULL for entitiesDictionary to indicate no entities
  269. other than the standard five. }
  270. function CFXMLCreateStringByEscapingEntities( allocator: CFAllocatorRef; strng: CFStringRef; entitiesDictionary: CFDictionaryRef ): CFStringRef; external name '_CFXMLCreateStringByEscapingEntities';
  271. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  272. function CFXMLCreateStringByUnescapingEntities( allocator: CFAllocatorRef; strng: CFStringRef; entitiesDictionary: CFDictionaryRef ): CFStringRef; external name '_CFXMLCreateStringByUnescapingEntities';
  273. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  274. { CFXMLTreeCreateFromDataWithError error dictionary key constants. }
  275. var kCFXMLTreeErrorDescription: CFStringRef; external name '_kCFXMLTreeErrorDescription'; (* attribute const *)
  276. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  277. { value is a CFString containing the readable error string. }
  278. var kCFXMLTreeErrorLineNumber: CFStringRef; external name '_kCFXMLTreeErrorLineNumber'; (* attribute const *)
  279. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  280. { value is a CFNumber containing the line on which the error appears. }
  281. var kCFXMLTreeErrorLocation: CFStringRef; external name '_kCFXMLTreeErrorLocation'; (* attribute const *)
  282. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  283. { value is a CFNumber containing the byte location at which the error occurred. }
  284. var kCFXMLTreeErrorStatusCode: CFStringRef; external name '_kCFXMLTreeErrorStatusCode'; (* attribute const *)
  285. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  286. { value is a CFNumber containing the error status code. }
  287. end.