The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1 | <?xml version="1.0" encoding="iso-8859-1"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 3 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 4 | <html> |
| 5 | <head> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 6 | <!-- |
| 7 | __ __ _ |
| 8 | ___\ \/ /_ __ __ _| |_ |
| 9 | / _ \\ /| '_ \ / _` | __| |
| 10 | | __// \| |_) | (_| | |_ |
| 11 | \___/_/\_\ .__/ \__,_|\__| |
| 12 | |_| XML parser |
| 13 | |
| 14 | Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net> |
| 15 | Copyright (c) 2000-2004 Fred L. Drake, Jr. <fdrake@users.sourceforge.net> |
| 16 | Copyright (c) 2002-2012 Karl Waclawek <karl@waclawek.net> |
| 17 | Copyright (c) 2017-2021 Sebastian Pipping <sebastian@pipping.org> |
| 18 | Copyright (c) 2017 Jakub Wilk <jwilk@jwilk.net> |
| 19 | Copyright (c) 2021 Tomas Korbar <tkorbar@redhat.com> |
| 20 | Licensed under the MIT license: |
| 21 | |
| 22 | Permission is hereby granted, free of charge, to any person obtaining |
| 23 | a copy of this software and associated documentation files (the |
| 24 | "Software"), to deal in the Software without restriction, including |
| 25 | without limitation the rights to use, copy, modify, merge, publish, |
| 26 | distribute, sublicense, and/or sell copies of the Software, and to permit |
| 27 | persons to whom the Software is furnished to do so, subject to the |
| 28 | following conditions: |
| 29 | |
| 30 | The above copyright notice and this permission notice shall be included |
| 31 | in all copies or substantial portions of the Software. |
| 32 | |
| 33 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 34 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 35 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 36 | NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 37 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 38 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 39 | USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 40 | --> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 41 | <title>Expat XML Parser</title> |
| 42 | <meta name="author" content="Clark Cooper, coopercc@netheaven.com" /> |
| 43 | <meta http-equiv="Content-Style-Type" content="text/css" /> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 44 | <link href="ok.min.css" rel="stylesheet" type="text/css" /> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 45 | <link href="style.css" rel="stylesheet" type="text/css" /> |
| 46 | </head> |
| 47 | <body> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 48 | <div> |
| 49 | <h1> |
| 50 | The Expat XML Parser |
| 51 | <small>Release 2.4.1</small> |
| 52 | </h1> |
| 53 | </div> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 54 | <div class="content"> |
| 55 | |
| 56 | <p>Expat is a library, written in C, for parsing XML documents. It's |
| 57 | the underlying XML parser for the open source Mozilla project, Perl's |
| 58 | <code>XML::Parser</code>, Python's <code>xml.parsers.expat</code>, and |
| 59 | other open-source XML parsers.</p> |
| 60 | |
| 61 | <p>This library is the creation of James Clark, who's also given us |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 62 | groff (an nroff look-alike), Jade (an implementation of ISO's DSSSL |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 63 | stylesheet language for SGML), XP (a Java XML parser package), XT (a |
| 64 | Java XSL engine). James was also the technical lead on the XML |
| 65 | Working Group at W3C that produced the XML specification.</p> |
| 66 | |
| 67 | <p>This is free software, licensed under the <a |
| 68 | href="../COPYING">MIT/X Consortium license</a>. You may download it |
| 69 | from <a href="http://www.libexpat.org/">the Expat home page</a>. |
| 70 | </p> |
| 71 | |
| 72 | <p>The bulk of this document was originally commissioned as an article |
| 73 | by <a href="http://www.xml.com/">XML.com</a>. They graciously allowed |
| 74 | Clark Cooper to retain copyright and to distribute it with Expat. |
| 75 | This version has been substantially extended to include documentation |
| 76 | on features which have been added since the original article was |
| 77 | published, and additional information on using the original |
| 78 | interface.</p> |
| 79 | |
| 80 | <hr /> |
| 81 | <h2>Table of Contents</h2> |
| 82 | <ul> |
| 83 | <li><a href="#overview">Overview</a></li> |
| 84 | <li><a href="#building">Building and Installing</a></li> |
| 85 | <li><a href="#using">Using Expat</a></li> |
| 86 | <li><a href="#reference">Reference</a> |
| 87 | <ul> |
| 88 | <li><a href="#creation">Parser Creation Functions</a> |
| 89 | <ul> |
| 90 | <li><a href="#XML_ParserCreate">XML_ParserCreate</a></li> |
| 91 | <li><a href="#XML_ParserCreateNS">XML_ParserCreateNS</a></li> |
| 92 | <li><a href="#XML_ParserCreate_MM">XML_ParserCreate_MM</a></li> |
| 93 | <li><a href="#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></li> |
| 94 | <li><a href="#XML_ParserFree">XML_ParserFree</a></li> |
| 95 | <li><a href="#XML_ParserReset">XML_ParserReset</a></li> |
| 96 | </ul> |
| 97 | </li> |
| 98 | <li><a href="#parsing">Parsing Functions</a> |
| 99 | <ul> |
| 100 | <li><a href="#XML_Parse">XML_Parse</a></li> |
| 101 | <li><a href="#XML_ParseBuffer">XML_ParseBuffer</a></li> |
| 102 | <li><a href="#XML_GetBuffer">XML_GetBuffer</a></li> |
| 103 | <li><a href="#XML_StopParser">XML_StopParser</a></li> |
| 104 | <li><a href="#XML_ResumeParser">XML_ResumeParser</a></li> |
| 105 | <li><a href="#XML_GetParsingStatus">XML_GetParsingStatus</a></li> |
| 106 | </ul> |
| 107 | </li> |
| 108 | <li><a href="#setting">Handler Setting Functions</a> |
| 109 | <ul> |
| 110 | <li><a href="#XML_SetStartElementHandler">XML_SetStartElementHandler</a></li> |
| 111 | <li><a href="#XML_SetEndElementHandler">XML_SetEndElementHandler</a></li> |
| 112 | <li><a href="#XML_SetElementHandler">XML_SetElementHandler</a></li> |
| 113 | <li><a href="#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</a></li> |
| 114 | <li><a href="#XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</a></li> |
| 115 | <li><a href="#XML_SetCommentHandler">XML_SetCommentHandler</a></li> |
| 116 | <li><a href="#XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</a></li> |
| 117 | <li><a href="#XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</a></li> |
| 118 | <li><a href="#XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</a></li> |
| 119 | <li><a href="#XML_SetDefaultHandler">XML_SetDefaultHandler</a></li> |
| 120 | <li><a href="#XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</a></li> |
| 121 | <li><a href="#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></li> |
| 122 | <li><a href="#XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg</a></li> |
| 123 | <li><a href="#XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler</a></li> |
| 124 | <li><a href="#XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</a></li> |
| 125 | <li><a href="#XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</a></li> |
| 126 | <li><a href="#XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</a></li> |
| 127 | <li><a href="#XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</a></li> |
| 128 | <li><a href="#XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</a></li> |
| 129 | <li><a href="#XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</a></li> |
| 130 | <li><a href="#XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</a></li> |
| 131 | <li><a href="#XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</a></li> |
| 132 | <li><a href="#XML_SetElementDeclHandler">XML_SetElementDeclHandler</a></li> |
| 133 | <li><a href="#XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</a></li> |
| 134 | <li><a href="#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</a></li> |
| 135 | <li><a href="#XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</a></li> |
| 136 | <li><a href="#XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</a></li> |
| 137 | <li><a href="#XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</a></li> |
| 138 | </ul> |
| 139 | </li> |
| 140 | <li><a href="#position">Parse Position and Error Reporting Functions</a> |
| 141 | <ul> |
| 142 | <li><a href="#XML_GetErrorCode">XML_GetErrorCode</a></li> |
| 143 | <li><a href="#XML_ErrorString">XML_ErrorString</a></li> |
| 144 | <li><a href="#XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</a></li> |
| 145 | <li><a href="#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</a></li> |
| 146 | <li><a href="#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</a></li> |
| 147 | <li><a href="#XML_GetCurrentByteCount">XML_GetCurrentByteCount</a></li> |
| 148 | <li><a href="#XML_GetInputContext">XML_GetInputContext</a></li> |
| 149 | </ul> |
| 150 | </li> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 151 | <li> |
| 152 | <a href="#billion-laughs">Billion Laughs Attack Protection</a> |
| 153 | <ul> |
| 154 | <li><a href="#XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</a></li> |
| 155 | <li><a href="#XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</a></li> |
| 156 | </ul> |
| 157 | </li> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 158 | <li><a href="#miscellaneous">Miscellaneous Functions</a> |
| 159 | <ul> |
| 160 | <li><a href="#XML_SetUserData">XML_SetUserData</a></li> |
| 161 | <li><a href="#XML_GetUserData">XML_GetUserData</a></li> |
| 162 | <li><a href="#XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</a></li> |
| 163 | <li><a href="#XML_SetBase">XML_SetBase</a></li> |
| 164 | <li><a href="#XML_GetBase">XML_GetBase</a></li> |
| 165 | <li><a href="#XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</a></li> |
| 166 | <li><a href="#XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</a></li> |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 167 | <li><a href="#XML_GetAttributeInfo">XML_GetAttributeInfo</a></li> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 168 | <li><a href="#XML_SetEncoding">XML_SetEncoding</a></li> |
| 169 | <li><a href="#XML_SetParamEntityParsing">XML_SetParamEntityParsing</a></li> |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 170 | <li><a href="#XML_SetHashSalt">XML_SetHashSalt</a></li> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 171 | <li><a href="#XML_UseForeignDTD">XML_UseForeignDTD</a></li> |
| 172 | <li><a href="#XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</a></li> |
| 173 | <li><a href="#XML_DefaultCurrent">XML_DefaultCurrent</a></li> |
| 174 | <li><a href="#XML_ExpatVersion">XML_ExpatVersion</a></li> |
| 175 | <li><a href="#XML_ExpatVersionInfo">XML_ExpatVersionInfo</a></li> |
| 176 | <li><a href="#XML_GetFeatureList">XML_GetFeatureList</a></li> |
| 177 | <li><a href="#XML_FreeContentModel">XML_FreeContentModel</a></li> |
| 178 | <li><a href="#XML_MemMalloc">XML_MemMalloc</a></li> |
| 179 | <li><a href="#XML_MemRealloc">XML_MemRealloc</a></li> |
| 180 | <li><a href="#XML_MemFree">XML_MemFree</a></li> |
| 181 | </ul> |
| 182 | </li> |
| 183 | </ul> |
| 184 | </li> |
| 185 | </ul> |
| 186 | |
| 187 | <hr /> |
| 188 | <h2><a name="overview">Overview</a></h2> |
| 189 | |
| 190 | <p>Expat is a stream-oriented parser. You register callback (or |
| 191 | handler) functions with the parser and then start feeding it the |
| 192 | document. As the parser recognizes parts of the document, it will |
| 193 | call the appropriate handler for that part (if you've registered one.) |
| 194 | The document is fed to the parser in pieces, so you can start parsing |
| 195 | before you have all the document. This also allows you to parse really |
| 196 | huge documents that won't fit into memory.</p> |
| 197 | |
| 198 | <p>Expat can be intimidating due to the many kinds of handlers and |
| 199 | options you can set. But you only need to learn four functions in |
| 200 | order to do 90% of what you'll want to do with it:</p> |
| 201 | |
| 202 | <dl> |
| 203 | |
| 204 | <dt><code><a href= "#XML_ParserCreate" |
| 205 | >XML_ParserCreate</a></code></dt> |
| 206 | <dd>Create a new parser object.</dd> |
| 207 | |
| 208 | <dt><code><a href= "#XML_SetElementHandler" |
| 209 | >XML_SetElementHandler</a></code></dt> |
| 210 | <dd>Set handlers for start and end tags.</dd> |
| 211 | |
| 212 | <dt><code><a href= "#XML_SetCharacterDataHandler" |
| 213 | >XML_SetCharacterDataHandler</a></code></dt> |
| 214 | <dd>Set handler for text.</dd> |
| 215 | |
| 216 | <dt><code><a href= "#XML_Parse" |
| 217 | >XML_Parse</a></code></dt> |
| 218 | <dd>Pass a buffer full of document to the parser</dd> |
| 219 | </dl> |
| 220 | |
| 221 | <p>These functions and others are described in the <a |
| 222 | href="#reference">reference</a> part of this document. The reference |
| 223 | section also describes in detail the parameters passed to the |
| 224 | different types of handlers.</p> |
| 225 | |
| 226 | <p>Let's look at a very simple example program that only uses 3 of the |
| 227 | above functions (it doesn't need to set a character handler.) The |
| 228 | program <a href="../examples/outline.c">outline.c</a> prints an |
| 229 | element outline, indenting child elements to distinguish them from the |
| 230 | parent element that contains them. The start handler does all the |
| 231 | work. It prints two indenting spaces for every level of ancestor |
| 232 | elements, then it prints the element and attribute |
| 233 | information. Finally it increments the global <code>Depth</code> |
| 234 | variable.</p> |
| 235 | |
| 236 | <pre class="eg"> |
| 237 | int Depth; |
| 238 | |
| 239 | void XMLCALL |
| 240 | start(void *data, const char *el, const char **attr) { |
| 241 | int i; |
| 242 | |
| 243 | for (i = 0; i < Depth; i++) |
| 244 | printf(" "); |
| 245 | |
| 246 | printf("%s", el); |
| 247 | |
| 248 | for (i = 0; attr[i]; i += 2) { |
| 249 | printf(" %s='%s'", attr[i], attr[i + 1]); |
| 250 | } |
| 251 | |
| 252 | printf("\n"); |
| 253 | Depth++; |
| 254 | } /* End of start handler */ |
| 255 | </pre> |
| 256 | |
| 257 | <p>The end tag simply does the bookkeeping work of decrementing |
| 258 | <code>Depth</code>.</p> |
| 259 | <pre class="eg"> |
| 260 | void XMLCALL |
| 261 | end(void *data, const char *el) { |
| 262 | Depth--; |
| 263 | } /* End of end handler */ |
| 264 | </pre> |
| 265 | |
| 266 | <p>Note the <code>XMLCALL</code> annotation used for the callbacks. |
| 267 | This is used to ensure that the Expat and the callbacks are using the |
| 268 | same calling convention in case the compiler options used for Expat |
| 269 | itself and the client code are different. Expat tries not to care |
| 270 | what the default calling convention is, though it may require that it |
| 271 | be compiled with a default convention of "cdecl" on some platforms. |
| 272 | For code which uses Expat, however, the calling convention is |
| 273 | specified by the <code>XMLCALL</code> annotation on most platforms; |
| 274 | callbacks should be defined using this annotation.</p> |
| 275 | |
| 276 | <p>The <code>XMLCALL</code> annotation was added in Expat 1.95.7, but |
| 277 | existing working Expat applications don't need to add it (since they |
| 278 | are already using the "cdecl" calling convention, or they wouldn't be |
| 279 | working). The annotation is only needed if the default calling |
| 280 | convention may be something other than "cdecl". To use the annotation |
| 281 | safely with older versions of Expat, you can conditionally define it |
| 282 | <em>after</em> including Expat's header file:</p> |
| 283 | |
| 284 | <pre class="eg"> |
| 285 | #include <expat.h> |
| 286 | |
| 287 | #ifndef XMLCALL |
| 288 | #if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) && !defined(__CYGWIN__) |
| 289 | #define XMLCALL __cdecl |
| 290 | #elif defined(__GNUC__) |
| 291 | #define XMLCALL __attribute__((cdecl)) |
| 292 | #else |
| 293 | #define XMLCALL |
| 294 | #endif |
| 295 | #endif |
| 296 | </pre> |
| 297 | |
| 298 | <p>After creating the parser, the main program just has the job of |
| 299 | shoveling the document to the parser so that it can do its work.</p> |
| 300 | |
| 301 | <hr /> |
| 302 | <h2><a name="building">Building and Installing Expat</a></h2> |
| 303 | |
| 304 | <p>The Expat distribution comes as a compressed (with GNU gzip) tar |
| 305 | file. You may download the latest version from <a href= |
| 306 | "http://sourceforge.net/projects/expat/" >Source Forge</a>. After |
| 307 | unpacking this, cd into the directory. Then follow either the Win32 |
| 308 | directions or Unix directions below.</p> |
| 309 | |
| 310 | <h3>Building under Win32</h3> |
| 311 | |
| 312 | <p>If you're using the GNU compiler under cygwin, follow the Unix |
| 313 | directions in the next section. Otherwise if you have Microsoft's |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 314 | Developer Studio installed, |
| 315 | you can use CMake to generate a <code>.sln</code> file, e.g. |
| 316 | <code> |
| 317 | cmake -G"Visual Studio 15 2017" -DCMAKE_BUILD_TYPE=RelWithDebInfo . |
| 318 | </code>, and build Expat using <code>msbuild /m expat.sln</code> after.</p> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 319 | |
| 320 | <p>Alternatively, you may download the Win32 binary package that |
| 321 | contains the "expat.h" include file and a pre-built DLL.</p> |
| 322 | |
| 323 | <h3>Building under Unix (or GNU)</h3> |
| 324 | |
| 325 | <p>First you'll need to run the configure shell script in order to |
| 326 | configure the Makefiles and headers for your system.</p> |
| 327 | |
| 328 | <p>If you're happy with all the defaults that configure picks for you, |
| 329 | and you have permission on your system to install into /usr/local, you |
| 330 | can install Expat with this sequence of commands:</p> |
| 331 | |
| 332 | <pre class="eg"> |
| 333 | ./configure |
| 334 | make |
| 335 | make install |
| 336 | </pre> |
| 337 | |
| 338 | <p>There are some options that you can provide to this script, but the |
| 339 | only one we'll mention here is the <code>--prefix</code> option. You |
| 340 | can find out all the options available by running configure with just |
| 341 | the <code>--help</code> option.</p> |
| 342 | |
| 343 | <p>By default, the configure script sets things up so that the library |
| 344 | gets installed in <code>/usr/local/lib</code> and the associated |
| 345 | header file in <code>/usr/local/include</code>. But if you were to |
| 346 | give the option, <code>--prefix=/home/me/mystuff</code>, then the |
| 347 | library and header would get installed in |
| 348 | <code>/home/me/mystuff/lib</code> and |
| 349 | <code>/home/me/mystuff/include</code> respectively.</p> |
| 350 | |
| 351 | <h3>Configuring Expat Using the Pre-Processor</h3> |
| 352 | |
| 353 | <p>Expat's feature set can be configured using a small number of |
| 354 | pre-processor definitions. The definition of this symbols does not |
| 355 | affect the set of entry points for Expat, only the behavior of the API |
| 356 | and the definition of character types in the case of |
| 357 | <code>XML_UNICODE_WCHAR_T</code>. The symbols are:</p> |
| 358 | |
| 359 | <dl class="cpp-symbols"> |
| 360 | <dt>XML_DTD</dt> |
| 361 | <dd>Include support for using and reporting DTD-based content. If |
| 362 | this is defined, default attribute values from an external DTD subset |
| 363 | are reported and attribute value normalization occurs based on the |
| 364 | type of attributes defined in the external subset. Without |
| 365 | this, Expat has a smaller memory footprint and can be faster, but will |
| 366 | not load external entities or process conditional sections. This does |
| 367 | not affect the set of functions available in the API.</dd> |
| 368 | |
| 369 | <dt>XML_NS</dt> |
| 370 | <dd>When defined, support for the <cite><a href= |
| 371 | "http://www.w3.org/TR/REC-xml-names/" >Namespaces in XML</a></cite> |
| 372 | specification is included.</dd> |
| 373 | |
| 374 | <dt>XML_UNICODE</dt> |
| 375 | <dd>When defined, character data reported to the application is |
| 376 | encoded in UTF-16 using wide characters of the type |
| 377 | <code>XML_Char</code>. This is implied if |
| 378 | <code>XML_UNICODE_WCHAR_T</code> is defined.</dd> |
| 379 | |
| 380 | <dt>XML_UNICODE_WCHAR_T</dt> |
| 381 | <dd>If defined, causes the <code>XML_Char</code> character type to be |
| 382 | defined using the <code>wchar_t</code> type; otherwise, <code>unsigned |
| 383 | short</code> is used. Defining this implies |
| 384 | <code>XML_UNICODE</code>.</dd> |
| 385 | |
| 386 | <dt>XML_LARGE_SIZE</dt> |
| 387 | <dd>If defined, causes the <code>XML_Size</code> and <code>XML_Index</code> |
| 388 | integer types to be at least 64 bits in size. This is intended to support |
| 389 | processing of very large input streams, where the return values of |
| 390 | <code><a href="#XML_GetCurrentByteIndex" >XML_GetCurrentByteIndex</a></code>, |
| 391 | <code><a href="#XML_GetCurrentLineNumber" >XML_GetCurrentLineNumber</a></code> and |
| 392 | <code><a href="#XML_GetCurrentColumnNumber" >XML_GetCurrentColumnNumber</a></code> |
| 393 | could overflow. It may not be supported by all compilers, and is turned |
| 394 | off by default.</dd> |
| 395 | |
| 396 | <dt>XML_CONTEXT_BYTES</dt> |
| 397 | <dd>The number of input bytes of markup context which the parser will |
| 398 | ensure are available for reporting via <code><a href= |
| 399 | "#XML_GetInputContext" >XML_GetInputContext</a></code>. This is |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 400 | normally set to 1024, and must be set to a positive integer. If this |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 401 | is not defined, the input context will not be available and <code><a |
| 402 | href= "#XML_GetInputContext" >XML_GetInputContext</a></code> will |
| 403 | always report NULL. Without this, Expat has a smaller memory |
| 404 | footprint and can be faster.</dd> |
| 405 | |
| 406 | <dt>XML_STATIC</dt> |
| 407 | <dd>On Windows, this should be set if Expat is going to be linked |
| 408 | statically with the code that calls it; this is required to get all |
| 409 | the right MSVC magic annotations correct. This is ignored on other |
| 410 | platforms.</dd> |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 411 | |
| 412 | <dt>XML_ATTR_INFO</dt> |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 413 | <dd>If defined, makes the additional function <code><a href= |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 414 | "#XML_GetAttributeInfo" >XML_GetAttributeInfo</a></code> available |
| 415 | for reporting attribute byte offsets.</dd> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 416 | </dl> |
| 417 | |
| 418 | <hr /> |
| 419 | <h2><a name="using">Using Expat</a></h2> |
| 420 | |
| 421 | <h3>Compiling and Linking Against Expat</h3> |
| 422 | |
| 423 | <p>Unless you installed Expat in a location not expected by your |
| 424 | compiler and linker, all you have to do to use Expat in your programs |
| 425 | is to include the Expat header (<code>#include <expat.h></code>) |
| 426 | in your files that make calls to it and to tell the linker that it |
| 427 | needs to link against the Expat library. On Unix systems, this would |
| 428 | usually be done with the <code>-lexpat</code> argument. Otherwise, |
| 429 | you'll need to tell the compiler where to look for the Expat header |
| 430 | and the linker where to find the Expat library. You may also need to |
Elliott Hughes | d07d5a7 | 2009-09-25 16:04:37 -0700 | [diff] [blame] | 431 | take steps to tell the operating system where to find this library at |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 432 | run time.</p> |
| 433 | |
| 434 | <p>On a Unix-based system, here's what a Makefile might look like when |
| 435 | Expat is installed in a standard location:</p> |
| 436 | |
| 437 | <pre class="eg"> |
| 438 | CC=cc |
| 439 | LDFLAGS= |
| 440 | LIBS= -lexpat |
| 441 | xmlapp: xmlapp.o |
| 442 | $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS) |
| 443 | </pre> |
| 444 | |
| 445 | <p>If you installed Expat in, say, <code>/home/me/mystuff</code>, then |
| 446 | the Makefile would look like this:</p> |
| 447 | |
| 448 | <pre class="eg"> |
| 449 | CC=cc |
| 450 | CFLAGS= -I/home/me/mystuff/include |
| 451 | LDFLAGS= |
| 452 | LIBS= -L/home/me/mystuff/lib -lexpat |
| 453 | xmlapp: xmlapp.o |
| 454 | $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS) |
| 455 | </pre> |
| 456 | |
| 457 | <p>You'd also have to set the environment variable |
| 458 | <code>LD_LIBRARY_PATH</code> to <code>/home/me/mystuff/lib</code> (or |
| 459 | to <code>${LD_LIBRARY_PATH}:/home/me/mystuff/lib</code> if |
| 460 | LD_LIBRARY_PATH already has some directories in it) in order to run |
| 461 | your application.</p> |
| 462 | |
| 463 | <h3>Expat Basics</h3> |
| 464 | |
| 465 | <p>As we saw in the example in the overview, the first step in parsing |
| 466 | an XML document with Expat is to create a parser object. There are <a |
| 467 | href="#creation">three functions</a> in the Expat API for creating a |
| 468 | parser object. However, only two of these (<code><a href= |
| 469 | "#XML_ParserCreate" >XML_ParserCreate</a></code> and <code><a href= |
| 470 | "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>) can be used for |
| 471 | constructing a parser for a top-level document. The object returned |
| 472 | by these functions is an opaque pointer (i.e. "expat.h" declares it as |
| 473 | void *) to data with further internal structure. In order to free the |
| 474 | memory associated with this object you must call <code><a href= |
| 475 | "#XML_ParserFree" >XML_ParserFree</a></code>. Note that if you have |
| 476 | provided any <a href="#userdata">user data</a> that gets stored in the |
| 477 | parser, then your application is responsible for freeing it prior to |
| 478 | calling <code>XML_ParserFree</code>.</p> |
| 479 | |
| 480 | <p>The objects returned by the parser creation functions are good for |
| 481 | parsing only one XML document or external parsed entity. If your |
| 482 | application needs to parse many XML documents, then it needs to create |
| 483 | a parser object for each one. The best way to deal with this is to |
| 484 | create a higher level object that contains all the default |
| 485 | initialization you want for your parser objects.</p> |
| 486 | |
| 487 | <p>Walking through a document hierarchy with a stream oriented parser |
| 488 | will require a good stack mechanism in order to keep track of current |
| 489 | context. For instance, to answer the simple question, "What element |
| 490 | does this text belong to?" requires a stack, since the parser may have |
| 491 | descended into other elements that are children of the current one and |
| 492 | has encountered this text on the way out.</p> |
| 493 | |
| 494 | <p>The things you're likely to want to keep on a stack are the |
| 495 | currently opened element and it's attributes. You push this |
| 496 | information onto the stack in the start handler and you pop it off in |
| 497 | the end handler.</p> |
| 498 | |
| 499 | <p>For some tasks, it is sufficient to just keep information on what |
| 500 | the depth of the stack is (or would be if you had one.) The outline |
| 501 | program shown above presents one example. Another such task would be |
| 502 | skipping over a complete element. When you see the start tag for the |
| 503 | element you want to skip, you set a skip flag and record the depth at |
| 504 | which the element started. When the end tag handler encounters the |
| 505 | same depth, the skipped element has ended and the flag may be |
| 506 | cleared. If you follow the convention that the root element starts at |
| 507 | 1, then you can use the same variable for skip flag and skip |
| 508 | depth.</p> |
| 509 | |
| 510 | <pre class="eg"> |
| 511 | void |
| 512 | init_info(Parseinfo *info) { |
| 513 | info->skip = 0; |
| 514 | info->depth = 1; |
| 515 | /* Other initializations here */ |
| 516 | } /* End of init_info */ |
| 517 | |
| 518 | void XMLCALL |
| 519 | rawstart(void *data, const char *el, const char **attr) { |
| 520 | Parseinfo *inf = (Parseinfo *) data; |
| 521 | |
| 522 | if (! inf->skip) { |
| 523 | if (should_skip(inf, el, attr)) { |
| 524 | inf->skip = inf->depth; |
| 525 | } |
| 526 | else |
| 527 | start(inf, el, attr); /* This does rest of start handling */ |
| 528 | } |
| 529 | |
| 530 | inf->depth++; |
| 531 | } /* End of rawstart */ |
| 532 | |
| 533 | void XMLCALL |
| 534 | rawend(void *data, const char *el) { |
| 535 | Parseinfo *inf = (Parseinfo *) data; |
| 536 | |
| 537 | inf->depth--; |
| 538 | |
| 539 | if (! inf->skip) |
| 540 | end(inf, el); /* This does rest of end handling */ |
| 541 | |
| 542 | if (inf->skip == inf->depth) |
| 543 | inf->skip = 0; |
| 544 | } /* End rawend */ |
| 545 | </pre> |
| 546 | |
| 547 | <p>Notice in the above example the difference in how depth is |
| 548 | manipulated in the start and end handlers. The end tag handler should |
| 549 | be the mirror image of the start tag handler. This is necessary to |
| 550 | properly model containment. Since, in the start tag handler, we |
| 551 | incremented depth <em>after</em> the main body of start tag code, then |
| 552 | in the end handler, we need to manipulate it <em>before</em> the main |
| 553 | body. If we'd decided to increment it first thing in the start |
| 554 | handler, then we'd have had to decrement it last thing in the end |
| 555 | handler.</p> |
| 556 | |
| 557 | <h3 id="userdata">Communicating between handlers</h3> |
| 558 | |
| 559 | <p>In order to be able to pass information between different handlers |
| 560 | without using globals, you'll need to define a data structure to hold |
| 561 | the shared variables. You can then tell Expat (with the <code><a href= |
| 562 | "#XML_SetUserData" >XML_SetUserData</a></code> function) to pass a |
| 563 | pointer to this structure to the handlers. This is the first |
| 564 | argument received by most handlers. In the <a href="#reference" |
| 565 | >reference section</a>, an argument to a callback function is named |
| 566 | <code>userData</code> and have type <code>void *</code> if the user |
| 567 | data is passed; it will have the type <code>XML_Parser</code> if the |
| 568 | parser itself is passed. When the parser is passed, the user data may |
| 569 | be retrieved using <code><a href="#XML_GetUserData" |
| 570 | >XML_GetUserData</a></code>.</p> |
| 571 | |
| 572 | <p>One common case where multiple calls to a single handler may need |
| 573 | to communicate using an application data structure is the case when |
| 574 | content passed to the character data handler (set by <code><a href= |
| 575 | "#XML_SetCharacterDataHandler" |
| 576 | >XML_SetCharacterDataHandler</a></code>) needs to be accumulated. A |
| 577 | common first-time mistake with any of the event-oriented interfaces to |
| 578 | an XML parser is to expect all the text contained in an element to be |
| 579 | reported by a single call to the character data handler. Expat, like |
| 580 | many other XML parsers, reports such data as a sequence of calls; |
| 581 | there's no way to know when the end of the sequence is reached until a |
| 582 | different callback is made. A buffer referenced by the user data |
| 583 | structure proves both an effective and convenient place to accumulate |
| 584 | character data.</p> |
| 585 | |
| 586 | <!-- XXX example needed here --> |
| 587 | |
| 588 | |
| 589 | <h3>XML Version</h3> |
| 590 | |
| 591 | <p>Expat is an XML 1.0 parser, and as such never complains based on |
| 592 | the value of the <code>version</code> pseudo-attribute in the XML |
| 593 | declaration, if present.</p> |
| 594 | |
| 595 | <p>If an application needs to check the version number (to support |
| 596 | alternate processing), it should use the <code><a href= |
| 597 | "#XML_SetXmlDeclHandler" >XML_SetXmlDeclHandler</a></code> function to |
| 598 | set a handler that uses the information in the XML declaration to |
| 599 | determine what to do. This example shows how to check that only a |
| 600 | version number of <code>"1.0"</code> is accepted:</p> |
| 601 | |
| 602 | <pre class="eg"> |
| 603 | static int wrong_version; |
| 604 | static XML_Parser parser; |
| 605 | |
| 606 | static void XMLCALL |
| 607 | xmldecl_handler(void *userData, |
| 608 | const XML_Char *version, |
| 609 | const XML_Char *encoding, |
| 610 | int standalone) |
| 611 | { |
| 612 | static const XML_Char Version_1_0[] = {'1', '.', '0', 0}; |
| 613 | |
| 614 | int i; |
| 615 | |
| 616 | for (i = 0; i < (sizeof(Version_1_0) / sizeof(Version_1_0[0])); ++i) { |
| 617 | if (version[i] != Version_1_0[i]) { |
| 618 | wrong_version = 1; |
| 619 | /* also clear all other handlers: */ |
| 620 | XML_SetCharacterDataHandler(parser, NULL); |
| 621 | ... |
| 622 | return; |
| 623 | } |
| 624 | } |
| 625 | ... |
| 626 | } |
| 627 | </pre> |
| 628 | |
| 629 | <h3>Namespace Processing</h3> |
| 630 | |
| 631 | <p>When the parser is created using the <code><a href= |
| 632 | "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>, function, Expat |
| 633 | performs namespace processing. Under namespace processing, Expat |
| 634 | consumes <code>xmlns</code> and <code>xmlns:...</code> attributes, |
| 635 | which declare namespaces for the scope of the element in which they |
| 636 | occur. This means that your start handler will not see these |
| 637 | attributes. Your application can still be informed of these |
| 638 | declarations by setting namespace declaration handlers with <a href= |
| 639 | "#XML_SetNamespaceDeclHandler" |
| 640 | ><code>XML_SetNamespaceDeclHandler</code></a>.</p> |
| 641 | |
| 642 | <p>Element type and attribute names that belong to a given namespace |
| 643 | are passed to the appropriate handler in expanded form. By default |
| 644 | this expanded form is a concatenation of the namespace URI, the |
| 645 | separator character (which is the 2nd argument to <code><a href= |
| 646 | "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>), and the local |
| 647 | name (i.e. the part after the colon). Names with undeclared prefixes |
| 648 | are not well-formed when namespace processing is enabled, and will |
| 649 | trigger an error. Unprefixed attribute names are never expanded, |
| 650 | and unprefixed element names are only expanded when they are in the |
| 651 | scope of a default namespace.</p> |
| 652 | |
| 653 | <p>However if <code><a href= "#XML_SetReturnNSTriplet" |
| 654 | >XML_SetReturnNSTriplet</a></code> has been called with a non-zero |
| 655 | <code>do_nst</code> parameter, then the expanded form for names with |
| 656 | an explicit prefix is a concatenation of: URI, separator, local name, |
| 657 | separator, prefix.</p> |
| 658 | |
| 659 | <p>You can set handlers for the start of a namespace declaration and |
| 660 | for the end of a scope of a declaration with the <code><a href= |
| 661 | "#XML_SetNamespaceDeclHandler" >XML_SetNamespaceDeclHandler</a></code> |
| 662 | function. The StartNamespaceDeclHandler is called prior to the start |
| 663 | tag handler and the EndNamespaceDeclHandler is called after the |
| 664 | corresponding end tag that ends the namespace's scope. The namespace |
| 665 | start handler gets passed the prefix and URI for the namespace. For a |
| 666 | default namespace declaration (xmlns='...'), the prefix will be null. |
| 667 | The URI will be null for the case where the default namespace is being |
| 668 | unset. The namespace end handler just gets the prefix for the closing |
| 669 | scope.</p> |
| 670 | |
| 671 | <p>These handlers are called for each declaration. So if, for |
| 672 | instance, a start tag had three namespace declarations, then the |
| 673 | StartNamespaceDeclHandler would be called three times before the start |
| 674 | tag handler is called, once for each declaration.</p> |
| 675 | |
| 676 | <h3>Character Encodings</h3> |
| 677 | |
| 678 | <p>While XML is based on Unicode, and every XML processor is required |
| 679 | to recognized UTF-8 and UTF-16 (1 and 2 byte encodings of Unicode), |
| 680 | other encodings may be declared in XML documents or entities. For the |
| 681 | main document, an XML declaration may contain an encoding |
| 682 | declaration:</p> |
| 683 | <pre> |
| 684 | <?xml version="1.0" encoding="ISO-8859-2"?> |
| 685 | </pre> |
| 686 | |
| 687 | <p>External parsed entities may begin with a text declaration, which |
| 688 | looks like an XML declaration with just an encoding declaration:</p> |
| 689 | <pre> |
| 690 | <?xml encoding="Big5"?> |
| 691 | </pre> |
| 692 | |
| 693 | <p>With Expat, you may also specify an encoding at the time of |
| 694 | creating a parser. This is useful when the encoding information may |
| 695 | come from a source outside the document itself (like a higher level |
| 696 | protocol.)</p> |
| 697 | |
| 698 | <p><a name="builtin_encodings"></a>There are four built-in encodings |
| 699 | in Expat:</p> |
| 700 | <ul> |
| 701 | <li>UTF-8</li> |
| 702 | <li>UTF-16</li> |
| 703 | <li>ISO-8859-1</li> |
| 704 | <li>US-ASCII</li> |
| 705 | </ul> |
| 706 | |
| 707 | <p>Anything else discovered in an encoding declaration or in the |
| 708 | protocol encoding specified in the parser constructor, triggers a call |
| 709 | to the <code>UnknownEncodingHandler</code>. This handler gets passed |
| 710 | the encoding name and a pointer to an <code>XML_Encoding</code> data |
| 711 | structure. Your handler must fill in this structure and return |
| 712 | <code>XML_STATUS_OK</code> if it knows how to deal with the |
| 713 | encoding. Otherwise the handler should return |
| 714 | <code>XML_STATUS_ERROR</code>. The handler also gets passed a pointer |
| 715 | to an optional application data structure that you may indicate when |
| 716 | you set the handler.</p> |
| 717 | |
| 718 | <p>Expat places restrictions on character encodings that it can |
| 719 | support by filling in the <code>XML_Encoding</code> structure. |
| 720 | include file:</p> |
| 721 | <ol> |
| 722 | <li>Every ASCII character that can appear in a well-formed XML document |
| 723 | must be represented by a single byte, and that byte must correspond to |
| 724 | it's ASCII encoding (except for the characters $@\^'{}~)</li> |
| 725 | <li>Characters must be encoded in 4 bytes or less.</li> |
| 726 | <li>All characters encoded must have Unicode scalar values less than or |
| 727 | equal to 65535 (0xFFFF)<em>This does not apply to the built-in support |
| 728 | for UTF-16 and UTF-8</em></li> |
| 729 | <li>No character may be encoded by more that one distinct sequence of |
| 730 | bytes</li> |
| 731 | </ol> |
| 732 | |
| 733 | <p><code>XML_Encoding</code> contains an array of integers that |
| 734 | correspond to the 1st byte of an encoding sequence. If the value in |
| 735 | the array for a byte is zero or positive, then the byte is a single |
| 736 | byte encoding that encodes the Unicode scalar value contained in the |
| 737 | array. A -1 in this array indicates a malformed byte. If the value is |
| 738 | -2, -3, or -4, then the byte is the beginning of a 2, 3, or 4 byte |
| 739 | sequence respectively. Multi-byte sequences are sent to the convert |
| 740 | function pointed at in the <code>XML_Encoding</code> structure. This |
| 741 | function should return the Unicode scalar value for the sequence or -1 |
| 742 | if the sequence is malformed.</p> |
| 743 | |
| 744 | <p>One pitfall that novice Expat users are likely to fall into is that |
| 745 | although Expat may accept input in various encodings, the strings that |
| 746 | it passes to the handlers are always encoded in UTF-8 or UTF-16 |
| 747 | (depending on how Expat was compiled). Your application is responsible |
| 748 | for any translation of these strings into other encodings.</p> |
| 749 | |
| 750 | <h3>Handling External Entity References</h3> |
| 751 | |
| 752 | <p>Expat does not read or parse external entities directly. Note that |
| 753 | any external DTD is a special case of an external entity. If you've |
| 754 | set no <code>ExternalEntityRefHandler</code>, then external entity |
| 755 | references are silently ignored. Otherwise, it calls your handler with |
| 756 | the information needed to read and parse the external entity.</p> |
| 757 | |
| 758 | <p>Your handler isn't actually responsible for parsing the entity, but |
| 759 | it is responsible for creating a subsidiary parser with <code><a href= |
| 760 | "#XML_ExternalEntityParserCreate" |
| 761 | >XML_ExternalEntityParserCreate</a></code> that will do the job. This |
| 762 | returns an instance of <code>XML_Parser</code> that has handlers and |
| 763 | other data structures initialized from the parent parser. You may then |
| 764 | use <code><a href= "#XML_Parse" >XML_Parse</a></code> or <code><a |
| 765 | href= "#XML_ParseBuffer">XML_ParseBuffer</a></code> calls against this |
| 766 | parser. Since external entities my refer to other external entities, |
| 767 | your handler should be prepared to be called recursively.</p> |
| 768 | |
| 769 | <h3>Parsing DTDs</h3> |
| 770 | |
| 771 | <p>In order to parse parameter entities, before starting the parse, |
| 772 | you must call <code><a href= "#XML_SetParamEntityParsing" |
| 773 | >XML_SetParamEntityParsing</a></code> with one of the following |
| 774 | arguments:</p> |
| 775 | <dl> |
| 776 | <dt><code>XML_PARAM_ENTITY_PARSING_NEVER</code></dt> |
| 777 | <dd>Don't parse parameter entities or the external subset</dd> |
| 778 | <dt><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code></dt> |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 779 | <dd>Parse parameter entities and the external subset unless |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 780 | <code>standalone</code> was set to "yes" in the XML declaration.</dd> |
| 781 | <dt><code>XML_PARAM_ENTITY_PARSING_ALWAYS</code></dt> |
| 782 | <dd>Always parse parameter entities and the external subset</dd> |
| 783 | </dl> |
| 784 | |
| 785 | <p>In order to read an external DTD, you also have to set an external |
| 786 | entity reference handler as described above.</p> |
| 787 | |
| 788 | <h3 id="stop-resume">Temporarily Stopping Parsing</h3> |
| 789 | |
| 790 | <p>Expat 1.95.8 introduces a new feature: its now possible to stop |
| 791 | parsing temporarily from within a handler function, even if more data |
| 792 | has already been passed into the parser. Applications for this |
| 793 | include</p> |
| 794 | |
| 795 | <ul> |
| 796 | <li>Supporting the <a href= "http://www.w3.org/TR/xinclude/" |
| 797 | >XInclude</a> specification.</li> |
| 798 | |
| 799 | <li>Delaying further processing until additional information is |
| 800 | available from some other source.</li> |
| 801 | |
| 802 | <li>Adjusting processor load as task priorities shift within an |
| 803 | application.</li> |
| 804 | |
| 805 | <li>Stopping parsing completely (simply free or reset the parser |
| 806 | instead of resuming in the outer parsing loop). This can be useful |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 807 | if an application-domain error is found in the XML being parsed or if |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 808 | the result of the parse is determined not to be useful after |
| 809 | all.</li> |
| 810 | </ul> |
| 811 | |
| 812 | <p>To take advantage of this feature, the main parsing loop of an |
| 813 | application needs to support this specifically. It cannot be |
| 814 | supported with a parsing loop compatible with Expat 1.95.7 or |
| 815 | earlier (though existing loops will continue to work without |
| 816 | supporting the stop/resume feature).</p> |
| 817 | |
| 818 | <p>An application that uses this feature for a single parser will have |
| 819 | the rough structure (in pseudo-code):</p> |
| 820 | |
| 821 | <pre class="pseudocode"> |
| 822 | fd = open_input() |
| 823 | p = create_parser() |
| 824 | |
| 825 | if parse_xml(p, fd) { |
| 826 | /* suspended */ |
| 827 | |
| 828 | int suspended = 1; |
| 829 | |
| 830 | while (suspended) { |
| 831 | do_something_else() |
| 832 | if ready_to_resume() { |
| 833 | suspended = continue_parsing(p, fd); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | </pre> |
| 838 | |
| 839 | <p>An application that may resume any of several parsers based on |
| 840 | input (either from the XML being parsed or some other source) will |
| 841 | certainly have more interesting control structures.</p> |
| 842 | |
| 843 | <p>This C function could be used for the <code>parse_xml</code> |
| 844 | function mentioned in the pseudo-code above:</p> |
| 845 | |
| 846 | <pre class="eg"> |
| 847 | #define BUFF_SIZE 10240 |
| 848 | |
| 849 | /* Parse a document from the open file descriptor 'fd' until the parse |
| 850 | is complete (the document has been completely parsed, or there's |
| 851 | been an error), or the parse is stopped. Return non-zero when |
| 852 | the parse is merely suspended. |
| 853 | */ |
| 854 | int |
| 855 | parse_xml(XML_Parser p, int fd) |
| 856 | { |
| 857 | for (;;) { |
| 858 | int last_chunk; |
| 859 | int bytes_read; |
| 860 | enum XML_Status status; |
| 861 | |
| 862 | void *buff = XML_GetBuffer(p, BUFF_SIZE); |
| 863 | if (buff == NULL) { |
| 864 | /* handle error... */ |
| 865 | return 0; |
| 866 | } |
| 867 | bytes_read = read(fd, buff, BUFF_SIZE); |
| 868 | if (bytes_read < 0) { |
| 869 | /* handle error... */ |
| 870 | return 0; |
| 871 | } |
| 872 | status = XML_ParseBuffer(p, bytes_read, bytes_read == 0); |
| 873 | switch (status) { |
| 874 | case XML_STATUS_ERROR: |
| 875 | /* handle error... */ |
| 876 | return 0; |
| 877 | case XML_STATUS_SUSPENDED: |
| 878 | return 1; |
| 879 | } |
| 880 | if (bytes_read == 0) |
| 881 | return 0; |
| 882 | } |
| 883 | } |
| 884 | </pre> |
| 885 | |
| 886 | <p>The corresponding <code>continue_parsing</code> function is |
| 887 | somewhat simpler, since it only need deal with the return code from |
| 888 | <code><a href= "#XML_ResumeParser">XML_ResumeParser</a></code>; it can |
| 889 | delegate the input handling to the <code>parse_xml</code> |
| 890 | function:</p> |
| 891 | |
| 892 | <pre class="eg"> |
| 893 | /* Continue parsing a document which had been suspended. The 'p' and |
| 894 | 'fd' arguments are the same as passed to parse_xml(). Return |
| 895 | non-zero when the parse is suspended. |
| 896 | */ |
| 897 | int |
| 898 | continue_parsing(XML_Parser p, int fd) |
| 899 | { |
| 900 | enum XML_Status status = XML_ResumeParser(p); |
| 901 | switch (status) { |
| 902 | case XML_STATUS_ERROR: |
| 903 | /* handle error... */ |
| 904 | return 0; |
| 905 | case XML_ERROR_NOT_SUSPENDED: |
| 906 | /* handle error... */ |
| 907 | return 0;. |
| 908 | case XML_STATUS_SUSPENDED: |
| 909 | return 1; |
| 910 | } |
| 911 | return parse_xml(p, fd); |
| 912 | } |
| 913 | </pre> |
| 914 | |
| 915 | <p>Now that we've seen what a mess the top-level parsing loop can |
| 916 | become, what have we gained? Very simply, we can now use the <code><a |
| 917 | href= "#XML_StopParser" >XML_StopParser</a></code> function to stop |
| 918 | parsing, without having to go to great lengths to avoid additional |
| 919 | processing that we're expecting to ignore. As a bonus, we get to stop |
| 920 | parsing <em>temporarily</em>, and come back to it when we're |
| 921 | ready.</p> |
| 922 | |
| 923 | <p>To stop parsing from a handler function, use the <code><a href= |
| 924 | "#XML_StopParser" >XML_StopParser</a></code> function. This function |
| 925 | takes two arguments; the parser being stopped and a flag indicating |
| 926 | whether the parse can be resumed in the future.</p> |
| 927 | |
| 928 | <!-- XXX really need more here --> |
| 929 | |
| 930 | |
| 931 | <hr /> |
| 932 | <!-- ================================================================ --> |
| 933 | |
| 934 | <h2><a name="reference">Expat Reference</a></h2> |
| 935 | |
| 936 | <h3><a name="creation">Parser Creation</a></h3> |
| 937 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 938 | <h4 id="XML_ParserCreate">XML_ParserCreate</h4> |
| 939 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 940 | XML_Parser XMLCALL |
| 941 | XML_ParserCreate(const XML_Char *encoding); |
| 942 | </pre> |
| 943 | <div class="fcndef"> |
| 944 | Construct a new parser. If encoding is non-null, it specifies a |
| 945 | character encoding to use for the document. This overrides the document |
| 946 | encoding declaration. There are four built-in encodings: |
| 947 | <ul> |
| 948 | <li>US-ASCII</li> |
| 949 | <li>UTF-8</li> |
| 950 | <li>UTF-16</li> |
| 951 | <li>ISO-8859-1</li> |
| 952 | </ul> |
| 953 | Any other value will invoke a call to the UnknownEncodingHandler. |
| 954 | </div> |
| 955 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 956 | <h4 id="XML_ParserCreateNS">XML_ParserCreateNS</h4> |
| 957 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 958 | XML_Parser XMLCALL |
| 959 | XML_ParserCreateNS(const XML_Char *encoding, |
| 960 | XML_Char sep); |
| 961 | </pre> |
| 962 | <div class="fcndef"> |
| 963 | Constructs a new parser that has namespace processing in effect. Namespace |
| 964 | expanded element names and attribute names are returned as a concatenation |
| 965 | of the namespace URI, <em>sep</em>, and the local part of the name. This |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 966 | means that you should pick a character for <em>sep</em> that can't be part |
| 967 | of an URI. Since Expat does not check namespace URIs for conformance, the |
| 968 | only safe choice for a namespace separator is a character that is illegal |
| 969 | in XML. For instance, <code>'\xFF'</code> is not legal in UTF-8, and |
| 970 | <code>'\xFFFF'</code> is not legal in UTF-16. There is a special case when |
| 971 | <em>sep</em> is the null character <code>'\0'</code>: the namespace URI and |
| 972 | the local part will be concatenated without any separator - this is intended |
| 973 | to support RDF processors. It is a programming error to use the null separator |
| 974 | with <a href= "#XML_SetReturnNSTriplet">namespace triplets</a>.</div> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 975 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 976 | <h4 id="XML_ParserCreate_MM">XML_ParserCreate_MM</h4> |
| 977 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 978 | XML_Parser XMLCALL |
| 979 | XML_ParserCreate_MM(const XML_Char *encoding, |
| 980 | const XML_Memory_Handling_Suite *ms, |
| 981 | const XML_Char *sep); |
| 982 | </pre> |
| 983 | <pre class="signature"> |
| 984 | typedef struct { |
| 985 | void *(XMLCALL *malloc_fcn)(size_t size); |
| 986 | void *(XMLCALL *realloc_fcn)(void *ptr, size_t size); |
| 987 | void (XMLCALL *free_fcn)(void *ptr); |
| 988 | } XML_Memory_Handling_Suite; |
| 989 | </pre> |
| 990 | <div class="fcndef"> |
| 991 | <p>Construct a new parser using the suite of memory handling functions |
| 992 | specified in <code>ms</code>. If <code>ms</code> is NULL, then use the |
| 993 | standard set of memory management functions. If <code>sep</code> is |
| 994 | non NULL, then namespace processing is enabled in the created parser |
| 995 | and the character pointed at by sep is used as the separator between |
| 996 | the namespace URI and the local part of the name.</p> |
| 997 | </div> |
| 998 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 999 | <h4 id="XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</h4> |
| 1000 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1001 | XML_Parser XMLCALL |
| 1002 | XML_ExternalEntityParserCreate(XML_Parser p, |
| 1003 | const XML_Char *context, |
| 1004 | const XML_Char *encoding); |
| 1005 | </pre> |
| 1006 | <div class="fcndef"> |
| 1007 | Construct a new <code>XML_Parser</code> object for parsing an external |
| 1008 | general entity. Context is the context argument passed in a call to a |
| 1009 | ExternalEntityRefHandler. Other state information such as handlers, |
| 1010 | user data, namespace processing is inherited from the parser passed as |
| 1011 | the 1st argument. So you shouldn't need to call any of the behavior |
| 1012 | changing functions on this parser (unless you want it to act |
| 1013 | differently than the parent parser). |
| 1014 | </div> |
| 1015 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1016 | <h4 id="XML_ParserFree">XML_ParserFree</h4> |
| 1017 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1018 | void XMLCALL |
| 1019 | XML_ParserFree(XML_Parser p); |
| 1020 | </pre> |
| 1021 | <div class="fcndef"> |
| 1022 | Free memory used by the parser. Your application is responsible for |
| 1023 | freeing any memory associated with <a href="#userdata">user data</a>. |
| 1024 | </div> |
| 1025 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1026 | <h4 id="XML_ParserReset">XML_ParserReset</h4> |
| 1027 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1028 | XML_Bool XMLCALL |
| 1029 | XML_ParserReset(XML_Parser p, |
| 1030 | const XML_Char *encoding); |
| 1031 | </pre> |
| 1032 | <div class="fcndef"> |
| 1033 | Clean up the memory structures maintained by the parser so that it may |
| 1034 | be used again. After this has been called, <code>parser</code> is |
| 1035 | ready to start parsing a new document. All handlers are cleared from |
| 1036 | the parser, except for the unknownEncodingHandler. The parser's external |
| 1037 | state is re-initialized except for the values of ns and ns_triplets. |
| 1038 | This function may not be used on a parser created using <code><a href= |
| 1039 | "#XML_ExternalEntityParserCreate" >XML_ExternalEntityParserCreate</a |
| 1040 | ></code>; it will return <code>XML_FALSE</code> in that case. Returns |
| 1041 | <code>XML_TRUE</code> on success. Your application is responsible for |
| 1042 | dealing with any memory associated with <a href="#userdata">user data</a>. |
| 1043 | </div> |
| 1044 | |
| 1045 | <h3><a name="parsing">Parsing</a></h3> |
| 1046 | |
| 1047 | <p>To state the obvious: the three parsing functions <code><a href= |
| 1048 | "#XML_Parse" >XML_Parse</a></code>, <code><a href= "#XML_ParseBuffer"> |
| 1049 | XML_ParseBuffer</a></code> and <code><a href= "#XML_GetBuffer"> |
| 1050 | XML_GetBuffer</a></code> must not be called from within a handler |
| 1051 | unless they operate on a separate parser instance, that is, one that |
| 1052 | did not call the handler. For example, it is OK to call the parsing |
| 1053 | functions from within an <code>XML_ExternalEntityRefHandler</code>, |
| 1054 | if they apply to the parser created by |
| 1055 | <code><a href= "#XML_ExternalEntityParserCreate" |
| 1056 | >XML_ExternalEntityParserCreate</a></code>.</p> |
| 1057 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1058 | <p>Note: The <code>len</code> argument passed to these functions |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1059 | should be considerably less than the maximum value for an integer, |
| 1060 | as it could create an integer overflow situation if the added |
| 1061 | lengths of a buffer and the unprocessed portion of the previous buffer |
| 1062 | exceed the maximum integer value. Input data at the end of a buffer |
| 1063 | will remain unprocessed if it is part of an XML token for which the |
| 1064 | end is not part of that buffer.</p> |
| 1065 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1066 | <h4 id="XML_Parse">XML_Parse</h4> |
| 1067 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1068 | enum XML_Status XMLCALL |
| 1069 | XML_Parse(XML_Parser p, |
| 1070 | const char *s, |
| 1071 | int len, |
| 1072 | int isFinal); |
| 1073 | </pre> |
| 1074 | <pre class="signature"> |
| 1075 | enum XML_Status { |
| 1076 | XML_STATUS_ERROR = 0, |
| 1077 | XML_STATUS_OK = 1 |
| 1078 | }; |
| 1079 | </pre> |
| 1080 | <div class="fcndef"> |
| 1081 | Parse some more of the document. The string <code>s</code> is a buffer |
| 1082 | containing part (or perhaps all) of the document. The number of bytes of s |
| 1083 | that are part of the document is indicated by <code>len</code>. This means |
| 1084 | that <code>s</code> doesn't have to be null terminated. It also means that |
| 1085 | if <code>len</code> is larger than the number of bytes in the block of |
| 1086 | memory that <code>s</code> points at, then a memory fault is likely. The |
| 1087 | <code>isFinal</code> parameter informs the parser that this is the last |
| 1088 | piece of the document. Frequently, the last piece is empty (i.e. |
| 1089 | <code>len</code> is zero.) |
| 1090 | If a parse error occurred, it returns <code>XML_STATUS_ERROR</code>. |
| 1091 | Otherwise it returns <code>XML_STATUS_OK</code> value. |
| 1092 | </div> |
| 1093 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1094 | <h4 id="XML_ParseBuffer">XML_ParseBuffer</h4> |
| 1095 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1096 | enum XML_Status XMLCALL |
| 1097 | XML_ParseBuffer(XML_Parser p, |
| 1098 | int len, |
| 1099 | int isFinal); |
| 1100 | </pre> |
| 1101 | <div class="fcndef"> |
| 1102 | This is just like <code><a href= "#XML_Parse" >XML_Parse</a></code>, |
| 1103 | except in this case Expat provides the buffer. By obtaining the |
| 1104 | buffer from Expat with the <code><a href= "#XML_GetBuffer" |
| 1105 | >XML_GetBuffer</a></code> function, the application can avoid double |
| 1106 | copying of the input. |
| 1107 | </div> |
| 1108 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1109 | <h4 id="XML_GetBuffer">XML_GetBuffer</h4> |
| 1110 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1111 | void * XMLCALL |
| 1112 | XML_GetBuffer(XML_Parser p, |
| 1113 | int len); |
| 1114 | </pre> |
| 1115 | <div class="fcndef"> |
| 1116 | Obtain a buffer of size <code>len</code> to read a piece of the document |
| 1117 | into. A NULL value is returned if Expat can't allocate enough memory for |
| 1118 | this buffer. This has to be called prior to every call to |
| 1119 | <code><a href= "#XML_ParseBuffer" >XML_ParseBuffer</a></code>. A |
| 1120 | typical use would look like this: |
| 1121 | |
| 1122 | <pre class="eg"> |
| 1123 | for (;;) { |
| 1124 | int bytes_read; |
| 1125 | void *buff = XML_GetBuffer(p, BUFF_SIZE); |
| 1126 | if (buff == NULL) { |
| 1127 | /* handle error */ |
| 1128 | } |
| 1129 | |
| 1130 | bytes_read = read(docfd, buff, BUFF_SIZE); |
| 1131 | if (bytes_read < 0) { |
| 1132 | /* handle error */ |
| 1133 | } |
| 1134 | |
| 1135 | if (! XML_ParseBuffer(p, bytes_read, bytes_read == 0)) { |
| 1136 | /* handle parse error */ |
| 1137 | } |
| 1138 | |
| 1139 | if (bytes_read == 0) |
| 1140 | break; |
| 1141 | } |
| 1142 | </pre> |
| 1143 | </div> |
| 1144 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1145 | <h4 id="XML_StopParser">XML_StopParser</h4> |
| 1146 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1147 | enum XML_Status XMLCALL |
| 1148 | XML_StopParser(XML_Parser p, |
| 1149 | XML_Bool resumable); |
| 1150 | </pre> |
| 1151 | <div class="fcndef"> |
| 1152 | |
| 1153 | <p>Stops parsing, causing <code><a href= "#XML_Parse" |
| 1154 | >XML_Parse</a></code> or <code><a href= "#XML_ParseBuffer" |
| 1155 | >XML_ParseBuffer</a></code> to return. Must be called from within a |
| 1156 | call-back handler, except when aborting (when <code>resumable</code> |
| 1157 | is <code>XML_FALSE</code>) an already suspended parser. Some |
| 1158 | call-backs may still follow because they would otherwise get |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1159 | lost, including</p> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1160 | <ul> |
| 1161 | <li> the end element handler for empty elements when stopped in the |
| 1162 | start element handler,</li> |
Elliott Hughes | d07d5a7 | 2009-09-25 16:04:37 -0700 | [diff] [blame] | 1163 | <li> the end namespace declaration handler when stopped in the end |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1164 | element handler,</li> |
Elliott Hughes | d07d5a7 | 2009-09-25 16:04:37 -0700 | [diff] [blame] | 1165 | <li> the character data handler when stopped in the character data handler |
| 1166 | while making multiple call-backs on a contiguous chunk of characters,</li> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1167 | </ul> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1168 | <p>and possibly others.</p> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1169 | |
| 1170 | <p>This can be called from most handlers, including DTD related |
| 1171 | call-backs, except when parsing an external parameter entity and |
| 1172 | <code>resumable</code> is <code>XML_TRUE</code>. Returns |
| 1173 | <code>XML_STATUS_OK</code> when successful, |
| 1174 | <code>XML_STATUS_ERROR</code> otherwise. The possible error codes |
| 1175 | are:</p> |
| 1176 | <dl> |
| 1177 | <dt><code>XML_ERROR_SUSPENDED</code></dt> |
| 1178 | <dd>when suspending an already suspended parser.</dd> |
| 1179 | <dt><code>XML_ERROR_FINISHED</code></dt> |
| 1180 | <dd>when the parser has already finished.</dd> |
| 1181 | <dt><code>XML_ERROR_SUSPEND_PE</code></dt> |
| 1182 | <dd>when suspending while parsing an external PE.</dd> |
| 1183 | </dl> |
| 1184 | |
| 1185 | <p>Since the stop/resume feature requires application support in the |
| 1186 | outer parsing loop, it is an error to call this function for a parser |
| 1187 | not being handled appropriately; see <a href= "#stop-resume" |
| 1188 | >Temporarily Stopping Parsing</a> for more information.</p> |
| 1189 | |
| 1190 | <p>When <code>resumable</code> is <code>XML_TRUE</code> then parsing |
| 1191 | is <em>suspended</em>, that is, <code><a href= "#XML_Parse" |
| 1192 | >XML_Parse</a></code> and <code><a href= "#XML_ParseBuffer" |
| 1193 | >XML_ParseBuffer</a></code> return <code>XML_STATUS_SUSPENDED</code>. |
| 1194 | Otherwise, parsing is <em>aborted</em>, that is, <code><a href= |
| 1195 | "#XML_Parse" >XML_Parse</a></code> and <code><a href= |
| 1196 | "#XML_ParseBuffer" >XML_ParseBuffer</a></code> return |
| 1197 | <code>XML_STATUS_ERROR</code> with error code |
| 1198 | <code>XML_ERROR_ABORTED</code>.</p> |
| 1199 | |
| 1200 | <p><strong>Note:</strong> |
| 1201 | This will be applied to the current parser instance only, that is, if |
| 1202 | there is a parent parser then it will continue parsing when the |
| 1203 | external entity reference handler returns. It is up to the |
| 1204 | implementation of that handler to call <code><a href= |
| 1205 | "#XML_StopParser" >XML_StopParser</a></code> on the parent parser |
| 1206 | (recursively), if one wants to stop parsing altogether.</p> |
| 1207 | |
| 1208 | <p>When suspended, parsing can be resumed by calling <code><a href= |
| 1209 | "#XML_ResumeParser" >XML_ResumeParser</a></code>.</p> |
| 1210 | |
| 1211 | <p>New in Expat 1.95.8.</p> |
| 1212 | </div> |
| 1213 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1214 | <h4 id="XML_ResumeParser">XML_ResumeParser</h4> |
| 1215 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1216 | enum XML_Status XMLCALL |
| 1217 | XML_ResumeParser(XML_Parser p); |
| 1218 | </pre> |
| 1219 | <div class="fcndef"> |
| 1220 | <p>Resumes parsing after it has been suspended with <code><a href= |
| 1221 | "#XML_StopParser" >XML_StopParser</a></code>. Must not be called from |
| 1222 | within a handler call-back. Returns same status codes as <code><a |
| 1223 | href= "#XML_Parse">XML_Parse</a></code> or <code><a href= |
| 1224 | "#XML_ParseBuffer" >XML_ParseBuffer</a></code>. An additional error |
| 1225 | code, <code>XML_ERROR_NOT_SUSPENDED</code>, will be returned if the |
| 1226 | parser was not currently suspended.</p> |
| 1227 | |
| 1228 | <p><strong>Note:</strong> |
| 1229 | This must be called on the most deeply nested child parser instance |
| 1230 | first, and on its parent parser only after the child parser has |
| 1231 | finished, to be applied recursively until the document entity's parser |
| 1232 | is restarted. That is, the parent parser will not resume by itself |
| 1233 | and it is up to the application to call <code><a href= |
| 1234 | "#XML_ResumeParser" >XML_ResumeParser</a></code> on it at the |
| 1235 | appropriate moment.</p> |
| 1236 | |
| 1237 | <p>New in Expat 1.95.8.</p> |
| 1238 | </div> |
| 1239 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1240 | <h4 id="XML_GetParsingStatus">XML_GetParsingStatus</h4> |
| 1241 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1242 | void XMLCALL |
| 1243 | XML_GetParsingStatus(XML_Parser p, |
| 1244 | XML_ParsingStatus *status); |
| 1245 | </pre> |
| 1246 | <pre class="signature"> |
| 1247 | enum XML_Parsing { |
| 1248 | XML_INITIALIZED, |
| 1249 | XML_PARSING, |
| 1250 | XML_FINISHED, |
| 1251 | XML_SUSPENDED |
| 1252 | }; |
| 1253 | |
| 1254 | typedef struct { |
| 1255 | enum XML_Parsing parsing; |
| 1256 | XML_Bool finalBuffer; |
| 1257 | } XML_ParsingStatus; |
| 1258 | </pre> |
| 1259 | <div class="fcndef"> |
| 1260 | <p>Returns status of parser with respect to being initialized, |
| 1261 | parsing, finished, or suspended, and whether the final buffer is being |
| 1262 | processed. The <code>status</code> parameter <em>must not</em> be |
| 1263 | NULL.</p> |
| 1264 | |
| 1265 | <p>New in Expat 1.95.8.</p> |
| 1266 | </div> |
| 1267 | |
| 1268 | |
| 1269 | <h3><a name="setting">Handler Setting</a></h3> |
| 1270 | |
| 1271 | <p>Although handlers are typically set prior to parsing and left alone, an |
| 1272 | application may choose to set or change the handler for a parsing event |
| 1273 | while the parse is in progress. For instance, your application may choose |
| 1274 | to ignore all text not descended from a <code>para</code> element. One |
| 1275 | way it could do this is to set the character handler when a para start tag |
| 1276 | is seen, and unset it for the corresponding end tag.</p> |
| 1277 | |
| 1278 | <p>A handler may be <em>unset</em> by providing a NULL pointer to the |
| 1279 | appropriate handler setter. None of the handler setting functions have |
| 1280 | a return value.</p> |
| 1281 | |
| 1282 | <p>Your handlers will be receiving strings in arrays of type |
| 1283 | <code>XML_Char</code>. This type is conditionally defined in expat.h as |
| 1284 | either <code>char</code>, <code>wchar_t</code> or <code>unsigned short</code>. |
| 1285 | The former implies UTF-8 encoding, the latter two imply UTF-16 encoding. |
| 1286 | Note that you'll receive them in this form independent of the original |
| 1287 | encoding of the document.</p> |
| 1288 | |
| 1289 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1290 | <h4 id="XML_SetStartElementHandler">XML_SetStartElementHandler</h4> |
| 1291 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1292 | void XMLCALL |
| 1293 | XML_SetStartElementHandler(XML_Parser p, |
| 1294 | XML_StartElementHandler start); |
| 1295 | </pre> |
| 1296 | <pre class="signature"> |
| 1297 | typedef void |
| 1298 | (XMLCALL *XML_StartElementHandler)(void *userData, |
| 1299 | const XML_Char *name, |
| 1300 | const XML_Char **atts); |
| 1301 | </pre> |
| 1302 | <p>Set handler for start (and empty) tags. Attributes are passed to the start |
| 1303 | handler as a pointer to a vector of char pointers. Each attribute seen in |
| 1304 | a start (or empty) tag occupies 2 consecutive places in this vector: the |
| 1305 | attribute name followed by the attribute value. These pairs are terminated |
| 1306 | by a null pointer.</p> |
| 1307 | <p>Note that an empty tag generates a call to both start and end handlers |
| 1308 | (in that order).</p> |
| 1309 | </div> |
| 1310 | |
| 1311 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1312 | <h4 id="XML_SetEndElementHandler">XML_SetEndElementHandler</h4> |
| 1313 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1314 | void XMLCALL |
| 1315 | XML_SetEndElementHandler(XML_Parser p, |
| 1316 | XML_EndElementHandler); |
| 1317 | </pre> |
| 1318 | <pre class="signature"> |
| 1319 | typedef void |
| 1320 | (XMLCALL *XML_EndElementHandler)(void *userData, |
| 1321 | const XML_Char *name); |
| 1322 | </pre> |
| 1323 | <p>Set handler for end (and empty) tags. As noted above, an empty tag |
| 1324 | generates a call to both start and end handlers.</p> |
| 1325 | </div> |
| 1326 | |
| 1327 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1328 | <h4 id="XML_SetElementHandler">XML_SetElementHandler</h4> |
| 1329 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1330 | void XMLCALL |
| 1331 | XML_SetElementHandler(XML_Parser p, |
| 1332 | XML_StartElementHandler start, |
| 1333 | XML_EndElementHandler end); |
| 1334 | </pre> |
| 1335 | <p>Set handlers for start and end tags with one call.</p> |
| 1336 | </div> |
| 1337 | |
| 1338 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1339 | <h4 id="XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</h4> |
| 1340 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1341 | void XMLCALL |
| 1342 | XML_SetCharacterDataHandler(XML_Parser p, |
| 1343 | XML_CharacterDataHandler charhndl) |
| 1344 | </pre> |
| 1345 | <pre class="signature"> |
| 1346 | typedef void |
| 1347 | (XMLCALL *XML_CharacterDataHandler)(void *userData, |
| 1348 | const XML_Char *s, |
| 1349 | int len); |
| 1350 | </pre> |
| 1351 | <p>Set a text handler. The string your handler receives |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 1352 | is <em>NOT null-terminated</em>. You have to use the length argument |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1353 | to deal with the end of the string. A single block of contiguous text |
| 1354 | free of markup may still result in a sequence of calls to this handler. |
| 1355 | In other words, if you're searching for a pattern in the text, it may |
Elliott Hughes | d07d5a7 | 2009-09-25 16:04:37 -0700 | [diff] [blame] | 1356 | be split across calls to this handler. Note: Setting this handler to NULL |
| 1357 | may <em>NOT immediately</em> terminate call-backs if the parser is currently |
| 1358 | processing such a single block of contiguous markup-free text, as the parser |
| 1359 | will continue calling back until the end of the block is reached.</p> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1360 | </div> |
| 1361 | |
| 1362 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1363 | <h4 id="XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</h4> |
| 1364 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1365 | void XMLCALL |
| 1366 | XML_SetProcessingInstructionHandler(XML_Parser p, |
| 1367 | XML_ProcessingInstructionHandler proc) |
| 1368 | </pre> |
| 1369 | <pre class="signature"> |
| 1370 | typedef void |
| 1371 | (XMLCALL *XML_ProcessingInstructionHandler)(void *userData, |
| 1372 | const XML_Char *target, |
| 1373 | const XML_Char *data); |
| 1374 | |
| 1375 | </pre> |
| 1376 | <p>Set a handler for processing instructions. The target is the first word |
| 1377 | in the processing instruction. The data is the rest of the characters in |
| 1378 | it after skipping all whitespace after the initial word.</p> |
| 1379 | </div> |
| 1380 | |
| 1381 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1382 | <h4 id="XML_SetCommentHandler">XML_SetCommentHandler</h4> |
| 1383 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1384 | void XMLCALL |
| 1385 | XML_SetCommentHandler(XML_Parser p, |
| 1386 | XML_CommentHandler cmnt) |
| 1387 | </pre> |
| 1388 | <pre class="signature"> |
| 1389 | typedef void |
| 1390 | (XMLCALL *XML_CommentHandler)(void *userData, |
| 1391 | const XML_Char *data); |
| 1392 | </pre> |
| 1393 | <p>Set a handler for comments. The data is all text inside the comment |
| 1394 | delimiters.</p> |
| 1395 | </div> |
| 1396 | |
| 1397 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1398 | <h4 id="XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</h4> |
| 1399 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1400 | void XMLCALL |
| 1401 | XML_SetStartCdataSectionHandler(XML_Parser p, |
| 1402 | XML_StartCdataSectionHandler start); |
| 1403 | </pre> |
| 1404 | <pre class="signature"> |
| 1405 | typedef void |
| 1406 | (XMLCALL *XML_StartCdataSectionHandler)(void *userData); |
| 1407 | </pre> |
| 1408 | <p>Set a handler that gets called at the beginning of a CDATA section.</p> |
| 1409 | </div> |
| 1410 | |
| 1411 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1412 | <h4 id="XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</h4> |
| 1413 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1414 | void XMLCALL |
| 1415 | XML_SetEndCdataSectionHandler(XML_Parser p, |
| 1416 | XML_EndCdataSectionHandler end); |
| 1417 | </pre> |
| 1418 | <pre class="signature"> |
| 1419 | typedef void |
| 1420 | (XMLCALL *XML_EndCdataSectionHandler)(void *userData); |
| 1421 | </pre> |
| 1422 | <p>Set a handler that gets called at the end of a CDATA section.</p> |
| 1423 | </div> |
| 1424 | |
| 1425 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1426 | <h4 id="XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</h4> |
| 1427 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1428 | void XMLCALL |
| 1429 | XML_SetCdataSectionHandler(XML_Parser p, |
| 1430 | XML_StartCdataSectionHandler start, |
| 1431 | XML_EndCdataSectionHandler end) |
| 1432 | </pre> |
| 1433 | <p>Sets both CDATA section handlers with one call.</p> |
| 1434 | </div> |
| 1435 | |
| 1436 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1437 | <h4 id="XML_SetDefaultHandler">XML_SetDefaultHandler</h4> |
| 1438 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1439 | void XMLCALL |
| 1440 | XML_SetDefaultHandler(XML_Parser p, |
| 1441 | XML_DefaultHandler hndl) |
| 1442 | </pre> |
| 1443 | <pre class="signature"> |
| 1444 | typedef void |
| 1445 | (XMLCALL *XML_DefaultHandler)(void *userData, |
| 1446 | const XML_Char *s, |
| 1447 | int len); |
| 1448 | </pre> |
| 1449 | |
| 1450 | <p>Sets a handler for any characters in the document which wouldn't |
| 1451 | otherwise be handled. This includes both data for which no handlers |
| 1452 | can be set (like some kinds of DTD declarations) and data which could |
| 1453 | be reported but which currently has no handler set. The characters |
| 1454 | are passed exactly as they were present in the XML document except |
| 1455 | that they will be encoded in UTF-8 or UTF-16. Line boundaries are not |
| 1456 | normalized. Note that a byte order mark character is not passed to the |
| 1457 | default handler. There are no guarantees about how characters are |
| 1458 | divided between calls to the default handler: for example, a comment |
| 1459 | might be split between multiple calls. Setting the handler with |
| 1460 | this call has the side effect of turning off expansion of references |
| 1461 | to internally defined general entities. Instead these references are |
| 1462 | passed to the default handler.</p> |
| 1463 | |
| 1464 | <p>See also <code><a |
| 1465 | href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>.</p> |
| 1466 | </div> |
| 1467 | |
| 1468 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1469 | <h4 id="XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</h4> |
| 1470 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1471 | void XMLCALL |
| 1472 | XML_SetDefaultHandlerExpand(XML_Parser p, |
| 1473 | XML_DefaultHandler hndl) |
| 1474 | </pre> |
| 1475 | <pre class="signature"> |
| 1476 | typedef void |
| 1477 | (XMLCALL *XML_DefaultHandler)(void *userData, |
| 1478 | const XML_Char *s, |
| 1479 | int len); |
| 1480 | </pre> |
| 1481 | <p>This sets a default handler, but doesn't inhibit the expansion of |
| 1482 | internal entity references. The entity reference will not be passed |
| 1483 | to the default handler.</p> |
| 1484 | |
| 1485 | <p>See also <code><a |
| 1486 | href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>.</p> |
| 1487 | </div> |
| 1488 | |
| 1489 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1490 | <h4 id="XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</h4> |
| 1491 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1492 | void XMLCALL |
| 1493 | XML_SetExternalEntityRefHandler(XML_Parser p, |
| 1494 | XML_ExternalEntityRefHandler hndl) |
| 1495 | </pre> |
| 1496 | <pre class="signature"> |
| 1497 | typedef int |
| 1498 | (XMLCALL *XML_ExternalEntityRefHandler)(XML_Parser p, |
| 1499 | const XML_Char *context, |
| 1500 | const XML_Char *base, |
| 1501 | const XML_Char *systemId, |
| 1502 | const XML_Char *publicId); |
| 1503 | </pre> |
| 1504 | <p>Set an external entity reference handler. This handler is also |
| 1505 | called for processing an external DTD subset if parameter entity parsing |
| 1506 | is in effect. (See <a href="#XML_SetParamEntityParsing"> |
| 1507 | <code>XML_SetParamEntityParsing</code></a>.)</p> |
| 1508 | |
| 1509 | <p>The <code>context</code> parameter specifies the parsing context in |
| 1510 | the format expected by the <code>context</code> argument to <code><a |
| 1511 | href="#XML_ExternalEntityParserCreate" |
| 1512 | >XML_ExternalEntityParserCreate</a></code>. <code>code</code> is |
| 1513 | valid only until the handler returns, so if the referenced entity is |
| 1514 | to be parsed later, it must be copied. <code>context</code> is NULL |
| 1515 | only when the entity is a parameter entity, which is how one can |
| 1516 | differentiate between general and parameter entities.</p> |
| 1517 | |
| 1518 | <p>The <code>base</code> parameter is the base to use for relative |
| 1519 | system identifiers. It is set by <code><a |
| 1520 | href="#XML_SetBase">XML_SetBase</a></code> and may be NULL. The |
| 1521 | <code>publicId</code> parameter is the public id given in the entity |
| 1522 | declaration and may be NULL. <code>systemId</code> is the system |
| 1523 | identifier specified in the entity declaration and is never NULL.</p> |
| 1524 | |
| 1525 | <p>There are a couple of ways in which this handler differs from |
| 1526 | others. First, this handler returns a status indicator (an |
| 1527 | integer). <code>XML_STATUS_OK</code> should be returned for successful |
| 1528 | handling of the external entity reference. Returning |
| 1529 | <code>XML_STATUS_ERROR</code> indicates failure, and causes the |
| 1530 | calling parser to return an |
| 1531 | <code>XML_ERROR_EXTERNAL_ENTITY_HANDLING</code> error.</p> |
| 1532 | |
| 1533 | <p>Second, instead of having the user data as its first argument, it |
| 1534 | receives the parser that encountered the entity reference. This, along |
| 1535 | with the context parameter, may be used as arguments to a call to |
| 1536 | <code><a href= "#XML_ExternalEntityParserCreate" |
| 1537 | >XML_ExternalEntityParserCreate</a></code>. Using the returned |
| 1538 | parser, the body of the external entity can be recursively parsed.</p> |
| 1539 | |
| 1540 | <p>Since this handler may be called recursively, it should not be saving |
| 1541 | information into global or static variables.</p> |
| 1542 | </div> |
| 1543 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1544 | <h4 id="XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg</h4> |
| 1545 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1546 | void XMLCALL |
| 1547 | XML_SetExternalEntityRefHandlerArg(XML_Parser p, |
| 1548 | void *arg) |
| 1549 | </pre> |
| 1550 | <div class="fcndef"> |
| 1551 | <p>Set the argument passed to the ExternalEntityRefHandler. If |
| 1552 | <code>arg</code> is not NULL, it is the new value passed to the |
| 1553 | handler set using <code><a href="#XML_SetExternalEntityRefHandler" |
| 1554 | >XML_SetExternalEntityRefHandler</a></code>; if <code>arg</code> is |
| 1555 | NULL, the argument passed to the handler function will be the parser |
| 1556 | object itself.</p> |
| 1557 | |
| 1558 | <p><strong>Note:</strong> |
| 1559 | The type of <code>arg</code> and the type of the first argument to the |
| 1560 | ExternalEntityRefHandler do not match. This function takes a |
| 1561 | <code>void *</code> to be passed to the handler, while the handler |
| 1562 | accepts an <code>XML_Parser</code>. This is a historical accident, |
| 1563 | but will not be corrected before Expat 2.0 (at the earliest) to avoid |
| 1564 | causing compiler warnings for code that's known to work with this |
| 1565 | API. It is the responsibility of the application code to know the |
| 1566 | actual type of the argument passed to the handler and to manage it |
| 1567 | properly.</p> |
| 1568 | </div> |
| 1569 | |
| 1570 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1571 | <h4 id="XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler</h4> |
| 1572 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1573 | void XMLCALL |
| 1574 | XML_SetSkippedEntityHandler(XML_Parser p, |
| 1575 | XML_SkippedEntityHandler handler) |
| 1576 | </pre> |
| 1577 | <pre class="signature"> |
| 1578 | typedef void |
| 1579 | (XMLCALL *XML_SkippedEntityHandler)(void *userData, |
| 1580 | const XML_Char *entityName, |
| 1581 | int is_parameter_entity); |
| 1582 | </pre> |
| 1583 | <p>Set a skipped entity handler. This is called in two situations:</p> |
| 1584 | <ol> |
| 1585 | <li>An entity reference is encountered for which no declaration |
| 1586 | has been read <em>and</em> this is not an error.</li> |
| 1587 | <li>An internal entity reference is read, but not expanded, because |
| 1588 | <a href="#XML_SetDefaultHandler"><code>XML_SetDefaultHandler</code></a> |
| 1589 | has been called.</li> |
| 1590 | </ol> |
| 1591 | <p>The <code>is_parameter_entity</code> argument will be non-zero for |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1592 | a parameter entity and zero for a general entity.</p> <p>Note: Skipped |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1593 | parameter entities in declarations and skipped general entities in |
| 1594 | attribute values cannot be reported, because the event would be out of |
| 1595 | sync with the reporting of the declarations or attribute values</p> |
| 1596 | </div> |
| 1597 | |
| 1598 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1599 | <h4 id="XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</h4> |
| 1600 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1601 | void XMLCALL |
| 1602 | XML_SetUnknownEncodingHandler(XML_Parser p, |
| 1603 | XML_UnknownEncodingHandler enchandler, |
| 1604 | void *encodingHandlerData) |
| 1605 | </pre> |
| 1606 | <pre class="signature"> |
| 1607 | typedef int |
| 1608 | (XMLCALL *XML_UnknownEncodingHandler)(void *encodingHandlerData, |
| 1609 | const XML_Char *name, |
| 1610 | XML_Encoding *info); |
| 1611 | |
| 1612 | typedef struct { |
| 1613 | int map[256]; |
| 1614 | void *data; |
| 1615 | int (XMLCALL *convert)(void *data, const char *s); |
| 1616 | void (XMLCALL *release)(void *data); |
| 1617 | } XML_Encoding; |
| 1618 | </pre> |
| 1619 | <p>Set a handler to deal with encodings other than the <a |
| 1620 | href="#builtin_encodings">built in set</a>. This should be done before |
| 1621 | <code><a href= "#XML_Parse" >XML_Parse</a></code> or <code><a href= |
| 1622 | "#XML_ParseBuffer" >XML_ParseBuffer</a></code> have been called on the |
| 1623 | given parser.</p> <p>If the handler knows how to deal with an encoding |
| 1624 | with the given name, it should fill in the <code>info</code> data |
| 1625 | structure and return <code>XML_STATUS_OK</code>. Otherwise it |
| 1626 | should return <code>XML_STATUS_ERROR</code>. The handler will be called |
| 1627 | at most once per parsed (external) entity. The optional application |
| 1628 | data pointer <code>encodingHandlerData</code> will be passed back to |
| 1629 | the handler.</p> |
| 1630 | |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 1631 | <p>The map array contains information for every possible leading |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1632 | byte in a byte sequence. If the corresponding value is >= 0, then it's |
| 1633 | a single byte sequence and the byte encodes that Unicode value. If the |
| 1634 | value is -1, then that byte is invalid as the initial byte in a sequence. |
| 1635 | If the value is -n, where n is an integer > 1, then n is the number of |
| 1636 | bytes in the sequence and the actual conversion is accomplished by a |
| 1637 | call to the function pointed at by convert. This function may return -1 |
| 1638 | if the sequence itself is invalid. The convert pointer may be null if |
| 1639 | there are only single byte codes. The data parameter passed to the convert |
| 1640 | function is the data pointer from <code>XML_Encoding</code>. The |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 1641 | string s is <em>NOT</em> null-terminated and points at the sequence of |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1642 | bytes to be converted.</p> |
| 1643 | |
| 1644 | <p>The function pointed at by <code>release</code> is called by the |
| 1645 | parser when it is finished with the encoding. It may be NULL.</p> |
| 1646 | </div> |
| 1647 | |
| 1648 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1649 | <h4 id="XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</h4> |
| 1650 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1651 | void XMLCALL |
| 1652 | XML_SetStartNamespaceDeclHandler(XML_Parser p, |
| 1653 | XML_StartNamespaceDeclHandler start); |
| 1654 | </pre> |
| 1655 | <pre class="signature"> |
| 1656 | typedef void |
| 1657 | (XMLCALL *XML_StartNamespaceDeclHandler)(void *userData, |
| 1658 | const XML_Char *prefix, |
| 1659 | const XML_Char *uri); |
| 1660 | </pre> |
| 1661 | <p>Set a handler to be called when a namespace is declared. Namespace |
| 1662 | declarations occur inside start tags. But the namespace declaration start |
| 1663 | handler is called before the start tag handler for each namespace declared |
| 1664 | in that start tag.</p> |
| 1665 | </div> |
| 1666 | |
| 1667 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1668 | <h4 id="XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</h4> |
| 1669 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1670 | void XMLCALL |
| 1671 | XML_SetEndNamespaceDeclHandler(XML_Parser p, |
| 1672 | XML_EndNamespaceDeclHandler end); |
| 1673 | </pre> |
| 1674 | <pre class="signature"> |
| 1675 | typedef void |
| 1676 | (XMLCALL *XML_EndNamespaceDeclHandler)(void *userData, |
| 1677 | const XML_Char *prefix); |
| 1678 | </pre> |
| 1679 | <p>Set a handler to be called when leaving the scope of a namespace |
| 1680 | declaration. This will be called, for each namespace declaration, |
| 1681 | after the handler for the end tag of the element in which the |
| 1682 | namespace was declared.</p> |
| 1683 | </div> |
| 1684 | |
| 1685 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1686 | <h4 id="XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</h4> |
| 1687 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1688 | void XMLCALL |
| 1689 | XML_SetNamespaceDeclHandler(XML_Parser p, |
| 1690 | XML_StartNamespaceDeclHandler start, |
| 1691 | XML_EndNamespaceDeclHandler end) |
| 1692 | </pre> |
| 1693 | <p>Sets both namespace declaration handlers with a single call.</p> |
| 1694 | </div> |
| 1695 | |
| 1696 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1697 | <h4 id="XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</h4> |
| 1698 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1699 | void XMLCALL |
| 1700 | XML_SetXmlDeclHandler(XML_Parser p, |
| 1701 | XML_XmlDeclHandler xmldecl); |
| 1702 | </pre> |
| 1703 | <pre class="signature"> |
| 1704 | typedef void |
| 1705 | (XMLCALL *XML_XmlDeclHandler)(void *userData, |
| 1706 | const XML_Char *version, |
| 1707 | const XML_Char *encoding, |
| 1708 | int standalone); |
| 1709 | </pre> |
| 1710 | <p>Sets a handler that is called for XML declarations and also for |
| 1711 | text declarations discovered in external entities. The way to |
| 1712 | distinguish is that the <code>version</code> parameter will be NULL |
| 1713 | for text declarations. The <code>encoding</code> parameter may be NULL |
| 1714 | for an XML declaration. The <code>standalone</code> argument will |
| 1715 | contain -1, 0, or 1 indicating respectively that there was no |
| 1716 | standalone parameter in the declaration, that it was given as no, or |
| 1717 | that it was given as yes.</p> |
| 1718 | </div> |
| 1719 | |
| 1720 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1721 | <h4 id="XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</h4> |
| 1722 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1723 | void XMLCALL |
| 1724 | XML_SetStartDoctypeDeclHandler(XML_Parser p, |
| 1725 | XML_StartDoctypeDeclHandler start); |
| 1726 | </pre> |
| 1727 | <pre class="signature"> |
| 1728 | typedef void |
| 1729 | (XMLCALL *XML_StartDoctypeDeclHandler)(void *userData, |
| 1730 | const XML_Char *doctypeName, |
| 1731 | const XML_Char *sysid, |
| 1732 | const XML_Char *pubid, |
| 1733 | int has_internal_subset); |
| 1734 | </pre> |
| 1735 | <p>Set a handler that is called at the start of a DOCTYPE declaration, |
| 1736 | before any external or internal subset is parsed. Both <code>sysid</code> |
| 1737 | and <code>pubid</code> may be NULL. The <code>has_internal_subset</code> |
| 1738 | will be non-zero if the DOCTYPE declaration has an internal subset.</p> |
| 1739 | </div> |
| 1740 | |
| 1741 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1742 | <h4 id="XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</h4> |
| 1743 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1744 | void XMLCALL |
| 1745 | XML_SetEndDoctypeDeclHandler(XML_Parser p, |
| 1746 | XML_EndDoctypeDeclHandler end); |
| 1747 | </pre> |
| 1748 | <pre class="signature"> |
| 1749 | typedef void |
| 1750 | (XMLCALL *XML_EndDoctypeDeclHandler)(void *userData); |
| 1751 | </pre> |
| 1752 | <p>Set a handler that is called at the end of a DOCTYPE declaration, |
| 1753 | after parsing any external subset.</p> |
| 1754 | </div> |
| 1755 | |
| 1756 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1757 | <h4 id="XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</h4> |
| 1758 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1759 | void XMLCALL |
| 1760 | XML_SetDoctypeDeclHandler(XML_Parser p, |
| 1761 | XML_StartDoctypeDeclHandler start, |
| 1762 | XML_EndDoctypeDeclHandler end); |
| 1763 | </pre> |
| 1764 | <p>Set both doctype handlers with one call.</p> |
| 1765 | </div> |
| 1766 | |
| 1767 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1768 | <h4 id="XML_SetElementDeclHandler">XML_SetElementDeclHandler</h4> |
| 1769 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1770 | void XMLCALL |
| 1771 | XML_SetElementDeclHandler(XML_Parser p, |
| 1772 | XML_ElementDeclHandler eldecl); |
| 1773 | </pre> |
| 1774 | <pre class="signature"> |
| 1775 | typedef void |
| 1776 | (XMLCALL *XML_ElementDeclHandler)(void *userData, |
| 1777 | const XML_Char *name, |
| 1778 | XML_Content *model); |
| 1779 | </pre> |
| 1780 | <pre class="signature"> |
| 1781 | enum XML_Content_Type { |
| 1782 | XML_CTYPE_EMPTY = 1, |
| 1783 | XML_CTYPE_ANY, |
| 1784 | XML_CTYPE_MIXED, |
| 1785 | XML_CTYPE_NAME, |
| 1786 | XML_CTYPE_CHOICE, |
| 1787 | XML_CTYPE_SEQ |
| 1788 | }; |
| 1789 | |
| 1790 | enum XML_Content_Quant { |
| 1791 | XML_CQUANT_NONE, |
| 1792 | XML_CQUANT_OPT, |
| 1793 | XML_CQUANT_REP, |
| 1794 | XML_CQUANT_PLUS |
| 1795 | }; |
| 1796 | |
| 1797 | typedef struct XML_cp XML_Content; |
| 1798 | |
| 1799 | struct XML_cp { |
| 1800 | enum XML_Content_Type type; |
| 1801 | enum XML_Content_Quant quant; |
| 1802 | const XML_Char * name; |
| 1803 | unsigned int numchildren; |
| 1804 | XML_Content * children; |
| 1805 | }; |
| 1806 | </pre> |
| 1807 | <p>Sets a handler for element declarations in a DTD. The handler gets |
| 1808 | called with the name of the element in the declaration and a pointer |
| 1809 | to a structure that contains the element model. It is the |
| 1810 | application's responsibility to free this data structure using |
| 1811 | <code><a href="#XML_FreeContentModel" |
| 1812 | >XML_FreeContentModel</a></code>.</p> |
| 1813 | |
| 1814 | <p>The <code>model</code> argument is the root of a tree of |
| 1815 | <code>XML_Content</code> nodes. If <code>type</code> equals |
| 1816 | <code>XML_CTYPE_EMPTY</code> or <code>XML_CTYPE_ANY</code>, then |
| 1817 | <code>quant</code> will be <code>XML_CQUANT_NONE</code>, and the other |
| 1818 | fields will be zero or NULL. If <code>type</code> is |
| 1819 | <code>XML_CTYPE_MIXED</code>, then <code>quant</code> will be |
| 1820 | <code>XML_CQUANT_NONE</code> or <code>XML_CQUANT_REP</code> and |
| 1821 | <code>numchildren</code> will contain the number of elements that are |
| 1822 | allowed to be mixed in and <code>children</code> points to an array of |
| 1823 | <code>XML_Content</code> structures that will all have type |
| 1824 | XML_CTYPE_NAME with no quantification. Only the root node can be type |
| 1825 | <code>XML_CTYPE_EMPTY</code>, <code>XML_CTYPE_ANY</code>, or |
| 1826 | <code>XML_CTYPE_MIXED</code>.</p> |
| 1827 | |
| 1828 | <p>For type <code>XML_CTYPE_NAME</code>, the <code>name</code> field |
| 1829 | points to the name and the <code>numchildren</code> and |
| 1830 | <code>children</code> fields will be zero and NULL. The |
| 1831 | <code>quant</code> field will indicate any quantifiers placed on the |
| 1832 | name.</p> |
| 1833 | |
| 1834 | <p>Types <code>XML_CTYPE_CHOICE</code> and <code>XML_CTYPE_SEQ</code> |
| 1835 | indicate a choice or sequence respectively. The |
| 1836 | <code>numchildren</code> field indicates how many nodes in the choice |
| 1837 | or sequence and <code>children</code> points to the nodes.</p> |
| 1838 | </div> |
| 1839 | |
| 1840 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1841 | <h4 id="XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</h4> |
| 1842 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1843 | void XMLCALL |
| 1844 | XML_SetAttlistDeclHandler(XML_Parser p, |
| 1845 | XML_AttlistDeclHandler attdecl); |
| 1846 | </pre> |
| 1847 | <pre class="signature"> |
| 1848 | typedef void |
| 1849 | (XMLCALL *XML_AttlistDeclHandler)(void *userData, |
| 1850 | const XML_Char *elname, |
| 1851 | const XML_Char *attname, |
| 1852 | const XML_Char *att_type, |
| 1853 | const XML_Char *dflt, |
| 1854 | int isrequired); |
| 1855 | </pre> |
| 1856 | <p>Set a handler for attlist declarations in the DTD. This handler is |
| 1857 | called for <em>each</em> attribute. So a single attlist declaration |
| 1858 | with multiple attributes declared will generate multiple calls to this |
| 1859 | handler. The <code>elname</code> parameter returns the name of the |
| 1860 | element for which the attribute is being declared. The attribute name |
| 1861 | is in the <code>attname</code> parameter. The attribute type is in the |
| 1862 | <code>att_type</code> parameter. It is the string representing the |
| 1863 | type in the declaration with whitespace removed.</p> |
| 1864 | |
| 1865 | <p>The <code>dflt</code> parameter holds the default value. It will be |
| 1866 | NULL in the case of "#IMPLIED" or "#REQUIRED" attributes. You can |
| 1867 | distinguish these two cases by checking the <code>isrequired</code> |
| 1868 | parameter, which will be true in the case of "#REQUIRED" attributes. |
| 1869 | Attributes which are "#FIXED" will have also have a true |
| 1870 | <code>isrequired</code>, but they will have the non-NULL fixed value |
| 1871 | in the <code>dflt</code> parameter.</p> |
| 1872 | </div> |
| 1873 | |
| 1874 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1875 | <h4 id="XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</h4> |
| 1876 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1877 | void XMLCALL |
| 1878 | XML_SetEntityDeclHandler(XML_Parser p, |
| 1879 | XML_EntityDeclHandler handler); |
| 1880 | </pre> |
| 1881 | <pre class="signature"> |
| 1882 | typedef void |
| 1883 | (XMLCALL *XML_EntityDeclHandler)(void *userData, |
| 1884 | const XML_Char *entityName, |
| 1885 | int is_parameter_entity, |
| 1886 | const XML_Char *value, |
| 1887 | int value_length, |
| 1888 | const XML_Char *base, |
| 1889 | const XML_Char *systemId, |
| 1890 | const XML_Char *publicId, |
| 1891 | const XML_Char *notationName); |
| 1892 | </pre> |
| 1893 | <p>Sets a handler that will be called for all entity declarations. |
| 1894 | The <code>is_parameter_entity</code> argument will be non-zero in the |
| 1895 | case of parameter entities and zero otherwise.</p> |
| 1896 | |
| 1897 | <p>For internal entities (<code><!ENTITY foo "bar"></code>), |
| 1898 | <code>value</code> will be non-NULL and <code>systemId</code>, |
| 1899 | <code>publicId</code>, and <code>notationName</code> will all be NULL. |
| 1900 | The value string is <em>not</em> NULL terminated; the length is |
| 1901 | provided in the <code>value_length</code> parameter. Do not use |
| 1902 | <code>value_length</code> to test for internal entities, since it is |
| 1903 | legal to have zero-length values. Instead check for whether or not |
| 1904 | <code>value</code> is NULL.</p> <p>The <code>notationName</code> |
| 1905 | argument will have a non-NULL value only for unparsed entity |
| 1906 | declarations.</p> |
| 1907 | </div> |
| 1908 | |
| 1909 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1910 | <h4 id="XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</h4> |
| 1911 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1912 | void XMLCALL |
| 1913 | XML_SetUnparsedEntityDeclHandler(XML_Parser p, |
| 1914 | XML_UnparsedEntityDeclHandler h) |
| 1915 | </pre> |
| 1916 | <pre class="signature"> |
| 1917 | typedef void |
| 1918 | (XMLCALL *XML_UnparsedEntityDeclHandler)(void *userData, |
| 1919 | const XML_Char *entityName, |
| 1920 | const XML_Char *base, |
| 1921 | const XML_Char *systemId, |
| 1922 | const XML_Char *publicId, |
| 1923 | const XML_Char *notationName); |
| 1924 | </pre> |
| 1925 | <p>Set a handler that receives declarations of unparsed entities. These |
| 1926 | are entity declarations that have a notation (NDATA) field:</p> |
| 1927 | |
| 1928 | <div id="eg"><pre> |
| 1929 | <!ENTITY logo SYSTEM "images/logo.gif" NDATA gif> |
| 1930 | </pre></div> |
| 1931 | <p>This handler is obsolete and is provided for backwards |
| 1932 | compatibility. Use instead <a href= "#XML_SetEntityDeclHandler" |
| 1933 | >XML_SetEntityDeclHandler</a>.</p> |
| 1934 | </div> |
| 1935 | |
| 1936 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1937 | <h4 id="XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</h4> |
| 1938 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1939 | void XMLCALL |
| 1940 | XML_SetNotationDeclHandler(XML_Parser p, |
| 1941 | XML_NotationDeclHandler h) |
| 1942 | </pre> |
| 1943 | <pre class="signature"> |
| 1944 | typedef void |
| 1945 | (XMLCALL *XML_NotationDeclHandler)(void *userData, |
| 1946 | const XML_Char *notationName, |
| 1947 | const XML_Char *base, |
| 1948 | const XML_Char *systemId, |
| 1949 | const XML_Char *publicId); |
| 1950 | </pre> |
| 1951 | <p>Set a handler that receives notation declarations.</p> |
| 1952 | </div> |
| 1953 | |
| 1954 | <div class="handler"> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1955 | <h4 id="XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</h4> |
| 1956 | <pre class="setter"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1957 | void XMLCALL |
| 1958 | XML_SetNotStandaloneHandler(XML_Parser p, |
| 1959 | XML_NotStandaloneHandler h) |
| 1960 | </pre> |
| 1961 | <pre class="signature"> |
| 1962 | typedef int |
| 1963 | (XMLCALL *XML_NotStandaloneHandler)(void *userData); |
| 1964 | </pre> |
| 1965 | <p>Set a handler that is called if the document is not "standalone". |
| 1966 | This happens when there is an external subset or a reference to a |
| 1967 | parameter entity, but does not have standalone set to "yes" in an XML |
| 1968 | declaration. If this handler returns <code>XML_STATUS_ERROR</code>, |
| 1969 | then the parser will throw an <code>XML_ERROR_NOT_STANDALONE</code> |
| 1970 | error.</p> |
| 1971 | </div> |
| 1972 | |
| 1973 | <h3><a name="position">Parse position and error reporting functions</a></h3> |
| 1974 | |
| 1975 | <p>These are the functions you'll want to call when the parse |
| 1976 | functions return <code>XML_STATUS_ERROR</code> (a parse error has |
| 1977 | occurred), although the position reporting functions are useful outside |
| 1978 | of errors. The position reported is the byte position (in the original |
| 1979 | document or entity encoding) of the first of the sequence of |
| 1980 | characters that generated the current event (or the error that caused |
| 1981 | the parse functions to return <code>XML_STATUS_ERROR</code>.) The |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 1982 | exceptions are callbacks triggered by declarations in the document |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1983 | prologue, in which case they exact position reported is somewhere in the |
| 1984 | relevant markup, but not necessarily as meaningful as for other |
| 1985 | events.</p> |
| 1986 | |
| 1987 | <p>The position reporting functions are accurate only outside of the |
| 1988 | DTD. In other words, they usually return bogus information when |
| 1989 | called from within a DTD declaration handler.</p> |
| 1990 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 1991 | <h4 id="XML_GetErrorCode">XML_GetErrorCode</h4> |
| 1992 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 1993 | enum XML_Error XMLCALL |
| 1994 | XML_GetErrorCode(XML_Parser p); |
| 1995 | </pre> |
| 1996 | <div class="fcndef"> |
| 1997 | Return what type of error has occurred. |
| 1998 | </div> |
| 1999 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2000 | <h4 id="XML_ErrorString">XML_ErrorString</h4> |
| 2001 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2002 | const XML_LChar * XMLCALL |
| 2003 | XML_ErrorString(enum XML_Error code); |
| 2004 | </pre> |
| 2005 | <div class="fcndef"> |
| 2006 | Return a string describing the error corresponding to code. |
| 2007 | The code should be one of the enums that can be returned from |
| 2008 | <code><a href= "#XML_GetErrorCode" >XML_GetErrorCode</a></code>. |
| 2009 | </div> |
| 2010 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2011 | <h4 id="XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</h4> |
| 2012 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2013 | XML_Index XMLCALL |
| 2014 | XML_GetCurrentByteIndex(XML_Parser p); |
| 2015 | </pre> |
| 2016 | <div class="fcndef"> |
| 2017 | Return the byte offset of the position. This always corresponds to |
| 2018 | the values returned by <code><a href= "#XML_GetCurrentLineNumber" |
| 2019 | >XML_GetCurrentLineNumber</a></code> and <code><a href= |
| 2020 | "#XML_GetCurrentColumnNumber" >XML_GetCurrentColumnNumber</a></code>. |
| 2021 | </div> |
| 2022 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2023 | <h4 id="XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</h4> |
| 2024 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2025 | XML_Size XMLCALL |
| 2026 | XML_GetCurrentLineNumber(XML_Parser p); |
| 2027 | </pre> |
| 2028 | <div class="fcndef"> |
| 2029 | Return the line number of the position. The first line is reported as |
| 2030 | <code>1</code>. |
| 2031 | </div> |
| 2032 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2033 | <h4 id="XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</h4> |
| 2034 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2035 | XML_Size XMLCALL |
| 2036 | XML_GetCurrentColumnNumber(XML_Parser p); |
| 2037 | </pre> |
| 2038 | <div class="fcndef"> |
| 2039 | Return the offset, from the beginning of the current line, of |
| 2040 | the position. |
| 2041 | </div> |
| 2042 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2043 | <h4 id="XML_GetCurrentByteCount">XML_GetCurrentByteCount</h4> |
| 2044 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2045 | int XMLCALL |
| 2046 | XML_GetCurrentByteCount(XML_Parser p); |
| 2047 | </pre> |
| 2048 | <div class="fcndef"> |
| 2049 | Return the number of bytes in the current event. Returns |
| 2050 | <code>0</code> if the event is inside a reference to an internal |
| 2051 | entity and for the end-tag event for empty element tags (the later can |
| 2052 | be used to distinguish empty-element tags from empty elements using |
| 2053 | separate start and end tags). |
| 2054 | </div> |
| 2055 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2056 | <h4 id="XML_GetInputContext">XML_GetInputContext</h4> |
| 2057 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2058 | const char * XMLCALL |
| 2059 | XML_GetInputContext(XML_Parser p, |
| 2060 | int *offset, |
| 2061 | int *size); |
| 2062 | </pre> |
| 2063 | <div class="fcndef"> |
| 2064 | |
| 2065 | <p>Returns the parser's input buffer, sets the integer pointed at by |
| 2066 | <code>offset</code> to the offset within this buffer of the current |
| 2067 | parse position, and set the integer pointed at by <code>size</code> to |
| 2068 | the size of the returned buffer.</p> |
| 2069 | |
| 2070 | <p>This should only be called from within a handler during an active |
| 2071 | parse and the returned buffer should only be referred to from within |
| 2072 | the handler that made the call. This input buffer contains the |
| 2073 | untranslated bytes of the input.</p> |
| 2074 | |
| 2075 | <p>Only a limited amount of context is kept, so if the event |
| 2076 | triggering a call spans over a very large amount of input, the actual |
| 2077 | parse position may be before the beginning of the buffer.</p> |
| 2078 | |
| 2079 | <p>If <code>XML_CONTEXT_BYTES</code> is not defined, this will always |
| 2080 | return NULL.</p> |
| 2081 | </div> |
| 2082 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2083 | <h3><a name="billion-laughs">Billion Laughs Attack Protection</a></h3> |
| 2084 | |
| 2085 | <p>The functions in this section configure the built-in |
| 2086 | protection against various forms of |
| 2087 | <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a>.</p> |
| 2088 | |
| 2089 | <h4 id="XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</h4> |
| 2090 | <pre class="fcndec"> |
| 2091 | /* Added in Expat 2.4.0. */ |
| 2092 | XML_Bool XMLCALL |
| 2093 | XML_SetBillionLaughsAttackProtectionMaximumAmplification(XML_Parser p, |
| 2094 | float maximumAmplificationFactor); |
| 2095 | </pre> |
| 2096 | <div class="fcndef"> |
| 2097 | <p> |
| 2098 | Sets the maximum tolerated amplification factor |
| 2099 | for protection against |
| 2100 | <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a> |
| 2101 | (default: <code>100.0</code>) |
| 2102 | of parser <code>p</code> to <code>maximumAmplificationFactor</code>, and |
| 2103 | returns <code>XML_TRUE</code> upon success and <code>XML_TRUE</code> upon error. |
| 2104 | </p> |
| 2105 | |
| 2106 | The amplification factor is calculated as .. |
| 2107 | <pre> |
| 2108 | amplification := (direct + indirect) / direct |
| 2109 | </pre> |
| 2110 | .. while parsing, whereas |
| 2111 | <code>direct</code> is the number of bytes read from the primary document in parsing and |
| 2112 | <code>indirect</code> is the number of bytes added by expanding entities and reading of external DTD files, combined. |
| 2113 | |
| 2114 | <p>For a call to <code>XML_SetBillionLaughsAttackProtectionMaximumAmplification</code> to succeed:</p> |
| 2115 | <ul> |
| 2116 | <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers) and</li> |
| 2117 | <li><code>maximumAmplificationFactor</code> must be non-<code>NaN</code> and greater than or equal to <code>1.0</code>.</li> |
| 2118 | </ul> |
| 2119 | |
| 2120 | <p> |
| 2121 | <strong>Note:</strong> |
| 2122 | If you ever need to increase this value for non-attack payload, |
| 2123 | please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>. |
| 2124 | </p> |
| 2125 | |
| 2126 | <p> |
| 2127 | <strong>Note:</strong> |
| 2128 | Peak amplifications |
| 2129 | of factor 15,000 for the entire payload and |
| 2130 | of factor 30,000 in the middle of parsing |
| 2131 | have been observed with small benign files in practice. |
| 2132 | |
| 2133 | So if you do reduce the maximum allowed amplification, |
| 2134 | please make sure that the activation threshold is still big enough |
| 2135 | to not end up with undesired false positives (i.e. benign files being rejected). |
| 2136 | </p> |
| 2137 | </div> |
| 2138 | |
| 2139 | <h4 id="XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</h4> |
| 2140 | <pre class="fcndec"> |
| 2141 | /* Added in Expat 2.4.0. */ |
| 2142 | XML_Bool XMLCALL |
| 2143 | XML_SetBillionLaughsAttackProtectionActivationThreshold(XML_Parser p, |
| 2144 | unsigned long long activationThresholdBytes); |
| 2145 | </pre> |
| 2146 | <div class="fcndef"> |
| 2147 | <p> |
| 2148 | Sets number of output bytes (including amplification from entity expansion and reading DTD files) |
| 2149 | needed to activate protection against |
| 2150 | <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a> |
| 2151 | (default: <code>8 MiB</code>) |
| 2152 | of parser <code>p</code> to <code>activationThresholdBytes</code>, and |
| 2153 | returns <code>XML_TRUE</code> upon success and <code>XML_TRUE</code> upon error. |
| 2154 | </p> |
| 2155 | |
| 2156 | <p>For a call to <code>XML_SetBillionLaughsAttackProtectionActivationThreshold</code> to succeed:</p> |
| 2157 | <ul> |
| 2158 | <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers).</li> |
| 2159 | </ul> |
| 2160 | |
| 2161 | <p> |
| 2162 | <strong>Note:</strong> |
| 2163 | If you ever need to increase this value for non-attack payload, |
| 2164 | please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>. |
| 2165 | </p> |
| 2166 | |
| 2167 | <p> |
| 2168 | <strong>Note:</strong> |
| 2169 | Activation thresholds below 4 MiB are known to break support for |
| 2170 | <a href="https://en.wikipedia.org/wiki/Darwin_Information_Typing_Architecture">DITA</a> 1.3 payload |
| 2171 | and are hence not recommended. |
| 2172 | </p> |
| 2173 | </div> |
| 2174 | |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2175 | <h3><a name="miscellaneous">Miscellaneous functions</a></h3> |
| 2176 | |
| 2177 | <p>The functions in this section either obtain state information from |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 2178 | the parser or can be used to dynamically set parser options.</p> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2179 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2180 | <h4 id="XML_SetUserData">XML_SetUserData</h4> |
| 2181 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2182 | void XMLCALL |
| 2183 | XML_SetUserData(XML_Parser p, |
| 2184 | void *userData); |
| 2185 | </pre> |
| 2186 | <div class="fcndef"> |
| 2187 | This sets the user data pointer that gets passed to handlers. It |
| 2188 | overwrites any previous value for this pointer. Note that the |
| 2189 | application is responsible for freeing the memory associated with |
| 2190 | <code>userData</code> when it is finished with the parser. So if you |
| 2191 | call this when there's already a pointer there, and you haven't freed |
| 2192 | the memory associated with it, then you've probably just leaked |
| 2193 | memory. |
| 2194 | </div> |
| 2195 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2196 | <h4 id="XML_GetUserData">XML_GetUserData</h4> |
| 2197 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2198 | void * XMLCALL |
| 2199 | XML_GetUserData(XML_Parser p); |
| 2200 | </pre> |
| 2201 | <div class="fcndef"> |
| 2202 | This returns the user data pointer that gets passed to handlers. |
| 2203 | It is actually implemented as a macro. |
| 2204 | </div> |
| 2205 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2206 | <h4 id="XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</h4> |
| 2207 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2208 | void XMLCALL |
| 2209 | XML_UseParserAsHandlerArg(XML_Parser p); |
| 2210 | </pre> |
| 2211 | <div class="fcndef"> |
| 2212 | After this is called, handlers receive the parser in their |
| 2213 | <code>userData</code> arguments. The user data can still be obtained |
| 2214 | using the <code><a href= "#XML_GetUserData" |
| 2215 | >XML_GetUserData</a></code> function. |
| 2216 | </div> |
| 2217 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2218 | <h4 id="XML_SetBase">XML_SetBase</h4> |
| 2219 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2220 | enum XML_Status XMLCALL |
| 2221 | XML_SetBase(XML_Parser p, |
| 2222 | const XML_Char *base); |
| 2223 | </pre> |
| 2224 | <div class="fcndef"> |
| 2225 | Set the base to be used for resolving relative URIs in system |
| 2226 | identifiers. The return value is <code>XML_STATUS_ERROR</code> if |
| 2227 | there's no memory to store base, otherwise it's |
| 2228 | <code>XML_STATUS_OK</code>. |
| 2229 | </div> |
| 2230 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2231 | <h4 id="XML_GetBase">XML_GetBase</h4> |
| 2232 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2233 | const XML_Char * XMLCALL |
| 2234 | XML_GetBase(XML_Parser p); |
| 2235 | </pre> |
| 2236 | <div class="fcndef"> |
| 2237 | Return the base for resolving relative URIs. |
| 2238 | </div> |
| 2239 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2240 | <h4 id="XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</h4> |
| 2241 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2242 | int XMLCALL |
| 2243 | XML_GetSpecifiedAttributeCount(XML_Parser p); |
| 2244 | </pre> |
| 2245 | <div class="fcndef"> |
| 2246 | When attributes are reported to the start handler in the atts vector, |
| 2247 | attributes that were explicitly set in the element occur before any |
| 2248 | attributes that receive their value from default information in an |
| 2249 | ATTLIST declaration. This function returns the number of attributes |
| 2250 | that were explicitly set times two, thus giving the offset in the |
| 2251 | <code>atts</code> array passed to the start tag handler of the first |
| 2252 | attribute set due to defaults. It supplies information for the last |
| 2253 | call to a start handler. If called inside a start handler, then that |
| 2254 | means the current call. |
| 2255 | </div> |
| 2256 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2257 | <h4 id="XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</h4> |
| 2258 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2259 | int XMLCALL |
| 2260 | XML_GetIdAttributeIndex(XML_Parser p); |
| 2261 | </pre> |
| 2262 | <div class="fcndef"> |
| 2263 | Returns the index of the ID attribute passed in the atts array in the |
| 2264 | last call to <code><a href= "#XML_StartElementHandler" |
| 2265 | >XML_StartElementHandler</a></code>, or -1 if there is no ID |
| 2266 | attribute. If called inside a start handler, then that means the |
| 2267 | current call. |
| 2268 | </div> |
| 2269 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2270 | <h4 id="XML_GetAttributeInfo">XML_GetAttributeInfo</h4> |
| 2271 | <pre class="fcndec"> |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 2272 | const XML_AttrInfo * XMLCALL |
| 2273 | XML_GetAttributeInfo(XML_Parser parser); |
| 2274 | </pre> |
| 2275 | <pre class="signature"> |
| 2276 | typedef struct { |
| 2277 | XML_Index nameStart; /* Offset to beginning of the attribute name. */ |
| 2278 | XML_Index nameEnd; /* Offset after the attribute name's last byte. */ |
| 2279 | XML_Index valueStart; /* Offset to beginning of the attribute value. */ |
| 2280 | XML_Index valueEnd; /* Offset after the attribute value's last byte. */ |
| 2281 | } XML_AttrInfo; |
| 2282 | </pre> |
| 2283 | <div class="fcndef"> |
| 2284 | Returns an array of <code>XML_AttrInfo</code> structures for the |
| 2285 | attribute/value pairs passed in the last call to the |
| 2286 | <code>XML_StartElementHandler</code> that were specified |
| 2287 | in the start-tag rather than defaulted. Each attribute/value pair counts |
| 2288 | as 1; thus the number of entries in the array is |
| 2289 | <code>XML_GetSpecifiedAttributeCount(parser) / 2</code>. |
| 2290 | </div> |
| 2291 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2292 | <h4 id="XML_SetEncoding">XML_SetEncoding</h4> |
| 2293 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2294 | enum XML_Status XMLCALL |
| 2295 | XML_SetEncoding(XML_Parser p, |
| 2296 | const XML_Char *encoding); |
| 2297 | </pre> |
| 2298 | <div class="fcndef"> |
| 2299 | Set the encoding to be used by the parser. It is equivalent to |
| 2300 | passing a non-null encoding argument to the parser creation functions. |
| 2301 | It must not be called after <code><a href= "#XML_Parse" |
| 2302 | >XML_Parse</a></code> or <code><a href= "#XML_ParseBuffer" |
| 2303 | >XML_ParseBuffer</a></code> have been called on the given parser. |
| 2304 | Returns <code>XML_STATUS_OK</code> on success or |
| 2305 | <code>XML_STATUS_ERROR</code> on error. |
| 2306 | </div> |
| 2307 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2308 | <h4 id="XML_SetParamEntityParsing">XML_SetParamEntityParsing</h4> |
| 2309 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2310 | int XMLCALL |
| 2311 | XML_SetParamEntityParsing(XML_Parser p, |
| 2312 | enum XML_ParamEntityParsing code); |
| 2313 | </pre> |
| 2314 | <div class="fcndef"> |
| 2315 | This enables parsing of parameter entities, including the external |
| 2316 | parameter entity that is the external DTD subset, according to |
| 2317 | <code>code</code>. |
| 2318 | The choices for <code>code</code> are: |
| 2319 | <ul> |
| 2320 | <li><code>XML_PARAM_ENTITY_PARSING_NEVER</code></li> |
| 2321 | <li><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code></li> |
| 2322 | <li><code>XML_PARAM_ENTITY_PARSING_ALWAYS</code></li> |
| 2323 | </ul> |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 2324 | <b>Note:</b> If <code>XML_SetParamEntityParsing</code> is called after |
| 2325 | <code>XML_Parse</code> or <code>XML_ParseBuffer</code>, then it has |
| 2326 | no effect and will always return 0. |
| 2327 | </div> |
| 2328 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2329 | <h4 id="XML_SetHashSalt">XML_SetHashSalt</h4> |
| 2330 | <pre class="fcndec"> |
Elliott Hughes | 35e432d | 2012-09-09 14:23:38 -0700 | [diff] [blame] | 2331 | int XMLCALL |
| 2332 | XML_SetHashSalt(XML_Parser p, |
| 2333 | unsigned long hash_salt); |
| 2334 | </pre> |
| 2335 | <div class="fcndef"> |
| 2336 | Sets the hash salt to use for internal hash calculations. |
| 2337 | Helps in preventing DoS attacks based on predicting hash |
| 2338 | function behavior. In order to have an effect this must be called |
| 2339 | before parsing has started. Returns 1 if successful, 0 when called |
| 2340 | after <code>XML_Parse</code> or <code>XML_ParseBuffer</code>. |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2341 | <p><b>Note:</b> This call is optional, as the parser will auto-generate |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 2342 | a new random salt value if no value has been set at the start of parsing.</p> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2343 | <p><b>Note:</b> One should not call <code>XML_SetHashSalt</code> with a |
Paul Duffin | cee2349 | 2016-05-04 10:42:31 +0100 | [diff] [blame] | 2344 | hash salt value of 0, as this value is used as sentinel value to indicate |
| 2345 | that <code>XML_SetHashSalt</code> has <b>not</b> been called. Consequently |
| 2346 | such a call will have no effect, even if it returns 1.</p> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2347 | </div> |
| 2348 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2349 | <h4 id="XML_UseForeignDTD">XML_UseForeignDTD</h4> |
| 2350 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2351 | enum XML_Error XMLCALL |
| 2352 | XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD); |
| 2353 | </pre> |
| 2354 | <div class="fcndef"> |
| 2355 | <p>This function allows an application to provide an external subset |
| 2356 | for the document type declaration for documents which do not specify |
| 2357 | an external subset of their own. For documents which specify an |
| 2358 | external subset in their DOCTYPE declaration, the application-provided |
| 2359 | subset will be ignored. If the document does not contain a DOCTYPE |
| 2360 | declaration at all and <code>useDTD</code> is true, the |
| 2361 | application-provided subset will be parsed, but the |
| 2362 | <code>startDoctypeDeclHandler</code> and |
| 2363 | <code>endDoctypeDeclHandler</code> functions, if set, will not be |
| 2364 | called. The setting of parameter entity parsing, controlled using |
| 2365 | <code><a href= "#XML_SetParamEntityParsing" |
| 2366 | >XML_SetParamEntityParsing</a></code>, will be honored.</p> |
| 2367 | |
| 2368 | <p>The application-provided external subset is read by calling the |
| 2369 | external entity reference handler set via <code><a href= |
| 2370 | "#XML_SetExternalEntityRefHandler" |
| 2371 | >XML_SetExternalEntityRefHandler</a></code> with both |
| 2372 | <code>publicId</code> and <code>systemId</code> set to NULL.</p> |
| 2373 | |
| 2374 | <p>If this function is called after parsing has begun, it returns |
| 2375 | <code>XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING</code> and ignores |
| 2376 | <code>useDTD</code>. If called when Expat has been compiled without |
| 2377 | DTD support, it returns |
| 2378 | <code>XML_ERROR_FEATURE_REQUIRES_XML_DTD</code>. Otherwise, it |
| 2379 | returns <code>XML_ERROR_NONE</code>.</p> |
| 2380 | |
| 2381 | <p><b>Note:</b> For the purpose of checking WFC: Entity Declared, passing |
| 2382 | <code>useDTD == XML_TRUE</code> will make the parser behave as if |
| 2383 | the document had a DTD with an external subset. This holds true even if |
| 2384 | the external entity reference handler returns without action.</p> |
| 2385 | </div> |
| 2386 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2387 | <h4 id="XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</h4> |
| 2388 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2389 | void XMLCALL |
| 2390 | XML_SetReturnNSTriplet(XML_Parser parser, |
| 2391 | int do_nst); |
| 2392 | </pre> |
| 2393 | <div class="fcndef"> |
| 2394 | <p> |
| 2395 | This function only has an effect when using a parser created with |
| 2396 | <code><a href= "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>, |
| 2397 | i.e. when namespace processing is in effect. The <code>do_nst</code> |
| 2398 | sets whether or not prefixes are returned with names qualified with a |
| 2399 | namespace prefix. If this function is called with <code>do_nst</code> |
| 2400 | non-zero, then afterwards namespace qualified names (that is qualified |
| 2401 | with a prefix as opposed to belonging to a default namespace) are |
| 2402 | returned as a triplet with the three parts separated by the namespace |
| 2403 | separator specified when the parser was created. The order of |
| 2404 | returned parts is URI, local name, and prefix.</p> <p>If |
| 2405 | <code>do_nst</code> is zero, then namespaces are reported in the |
| 2406 | default manner, URI then local_name separated by the namespace |
| 2407 | separator.</p> |
| 2408 | </div> |
| 2409 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2410 | <h4 id="XML_DefaultCurrent">XML_DefaultCurrent</h4> |
| 2411 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2412 | void XMLCALL |
| 2413 | XML_DefaultCurrent(XML_Parser parser); |
| 2414 | </pre> |
| 2415 | <div class="fcndef"> |
| 2416 | This can be called within a handler for a start element, end element, |
| 2417 | processing instruction or character data. It causes the corresponding |
| 2418 | markup to be passed to the default handler set by <code><a |
| 2419 | href="#XML_SetDefaultHandler" >XML_SetDefaultHandler</a></code> or |
| 2420 | <code><a href="#XML_SetDefaultHandlerExpand" |
| 2421 | >XML_SetDefaultHandlerExpand</a></code>. It does nothing if there is |
| 2422 | not a default handler. |
| 2423 | </div> |
| 2424 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2425 | <h4 id="XML_ExpatVersion">XML_ExpatVersion</h4> |
| 2426 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2427 | XML_LChar * XMLCALL |
| 2428 | XML_ExpatVersion(); |
| 2429 | </pre> |
| 2430 | <div class="fcndef"> |
| 2431 | Return the library version as a string (e.g. <code>"expat_1.95.1"</code>). |
| 2432 | </div> |
| 2433 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2434 | <h4 id="XML_ExpatVersionInfo">XML_ExpatVersionInfo</h4> |
| 2435 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2436 | struct XML_Expat_Version XMLCALL |
| 2437 | XML_ExpatVersionInfo(); |
| 2438 | </pre> |
| 2439 | <pre class="signature"> |
| 2440 | typedef struct { |
| 2441 | int major; |
| 2442 | int minor; |
| 2443 | int micro; |
| 2444 | } XML_Expat_Version; |
| 2445 | </pre> |
| 2446 | <div class="fcndef"> |
| 2447 | Return the library version information as a structure. |
| 2448 | Some macros are also defined that support compile-time tests of the |
| 2449 | library version: |
| 2450 | <ul> |
| 2451 | <li><code>XML_MAJOR_VERSION</code></li> |
| 2452 | <li><code>XML_MINOR_VERSION</code></li> |
| 2453 | <li><code>XML_MICRO_VERSION</code></li> |
| 2454 | </ul> |
| 2455 | Testing these constants is currently the best way to determine if |
| 2456 | particular parts of the Expat API are available. |
| 2457 | </div> |
| 2458 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2459 | <h4 id="XML_GetFeatureList">XML_GetFeatureList</h4> |
| 2460 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2461 | const XML_Feature * XMLCALL |
| 2462 | XML_GetFeatureList(); |
| 2463 | </pre> |
| 2464 | <pre class="signature"> |
| 2465 | enum XML_FeatureEnum { |
| 2466 | XML_FEATURE_END = 0, |
| 2467 | XML_FEATURE_UNICODE, |
| 2468 | XML_FEATURE_UNICODE_WCHAR_T, |
| 2469 | XML_FEATURE_DTD, |
| 2470 | XML_FEATURE_CONTEXT_BYTES, |
| 2471 | XML_FEATURE_MIN_SIZE, |
| 2472 | XML_FEATURE_SIZEOF_XML_CHAR, |
Elliott Hughes | d07d5a7 | 2009-09-25 16:04:37 -0700 | [diff] [blame] | 2473 | XML_FEATURE_SIZEOF_XML_LCHAR, |
| 2474 | XML_FEATURE_NS, |
| 2475 | XML_FEATURE_LARGE_SIZE |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2476 | }; |
| 2477 | |
| 2478 | typedef struct { |
| 2479 | enum XML_FeatureEnum feature; |
| 2480 | XML_LChar *name; |
| 2481 | long int value; |
| 2482 | } XML_Feature; |
| 2483 | </pre> |
| 2484 | <div class="fcndef"> |
| 2485 | <p>Returns a list of "feature" records, providing details on how |
| 2486 | Expat was configured at compile time. Most applications should not |
| 2487 | need to worry about this, but this information is otherwise not |
| 2488 | available from Expat. This function allows code that does need to |
| 2489 | check these features to do so at runtime.</p> |
| 2490 | |
| 2491 | <p>The return value is an array of <code>XML_Feature</code>, |
| 2492 | terminated by a record with a <code>feature</code> of |
| 2493 | <code>XML_FEATURE_END</code> and <code>name</code> of NULL, |
| 2494 | identifying the feature-test macros Expat was compiled with. Since an |
| 2495 | application that requires this kind of information needs to determine |
| 2496 | the type of character the <code>name</code> points to, records for the |
| 2497 | <code>XML_FEATURE_SIZEOF_XML_CHAR</code> and |
| 2498 | <code>XML_FEATURE_SIZEOF_XML_LCHAR</code> will be located at the |
| 2499 | beginning of the list, followed by <code>XML_FEATURE_UNICODE</code> |
| 2500 | and <code>XML_FEATURE_UNICODE_WCHAR_T</code>, if they are present at |
| 2501 | all.</p> |
| 2502 | |
| 2503 | <p>Some features have an associated value. If there isn't an |
| 2504 | associated value, the <code>value</code> field is set to 0. At this |
| 2505 | time, the following features have been defined to have values:</p> |
| 2506 | |
| 2507 | <dl> |
| 2508 | <dt><code>XML_FEATURE_SIZEOF_XML_CHAR</code></dt> |
| 2509 | <dd>The number of bytes occupied by one <code>XML_Char</code> |
| 2510 | character.</dd> |
| 2511 | <dt><code>XML_FEATURE_SIZEOF_XML_LCHAR</code></dt> |
| 2512 | <dd>The number of bytes occupied by one <code>XML_LChar</code> |
| 2513 | character.</dd> |
| 2514 | <dt><code>XML_FEATURE_CONTEXT_BYTES</code></dt> |
| 2515 | <dd>The maximum number of characters of context which can be |
| 2516 | reported by <code><a href= "#XML_GetInputContext" |
| 2517 | >XML_GetInputContext</a></code>.</dd> |
| 2518 | </dl> |
| 2519 | </div> |
| 2520 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2521 | <h4 id="XML_FreeContentModel">XML_FreeContentModel</h4> |
| 2522 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2523 | void XMLCALL |
| 2524 | XML_FreeContentModel(XML_Parser parser, XML_Content *model); |
| 2525 | </pre> |
| 2526 | <div class="fcndef"> |
| 2527 | Function to deallocate the <code>model</code> argument passed to the |
| 2528 | <code>XML_ElementDeclHandler</code> callback set using <code><a |
| 2529 | href="#XML_SetElementDeclHandler" >XML_ElementDeclHandler</a></code>. |
| 2530 | This function should not be used for any other purpose. |
| 2531 | </div> |
| 2532 | |
| 2533 | <p>The following functions allow external code to share the memory |
| 2534 | allocator an <code>XML_Parser</code> has been configured to use. This |
| 2535 | is especially useful for third-party libraries that interact with a |
| 2536 | parser object created by application code, or heavily layered |
| 2537 | applications. This can be essential when using dynamically loaded |
| 2538 | libraries which use different C standard libraries (this can happen on |
| 2539 | Windows, at least).</p> |
| 2540 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2541 | <h4 id="XML_MemMalloc">XML_MemMalloc</h4> |
| 2542 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2543 | void * XMLCALL |
| 2544 | XML_MemMalloc(XML_Parser parser, size_t size); |
| 2545 | </pre> |
| 2546 | <div class="fcndef"> |
| 2547 | Allocate <code>size</code> bytes of memory using the allocator the |
| 2548 | <code>parser</code> object has been configured to use. Returns a |
| 2549 | pointer to the memory or NULL on failure. Memory allocated in this |
| 2550 | way must be freed using <code><a href="#XML_MemFree" |
| 2551 | >XML_MemFree</a></code>. |
| 2552 | </div> |
| 2553 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2554 | <h4 id="XML_MemRealloc">XML_MemRealloc</h4> |
| 2555 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2556 | void * XMLCALL |
| 2557 | XML_MemRealloc(XML_Parser parser, void *ptr, size_t size); |
| 2558 | </pre> |
| 2559 | <div class="fcndef"> |
| 2560 | Allocate <code>size</code> bytes of memory using the allocator the |
| 2561 | <code>parser</code> object has been configured to use. |
| 2562 | <code>ptr</code> must point to a block of memory allocated by <code><a |
| 2563 | href="#XML_MemMalloc" >XML_MemMalloc</a></code> or |
| 2564 | <code>XML_MemRealloc</code>, or be NULL. This function tries to |
| 2565 | expand the block pointed to by <code>ptr</code> if possible. Returns |
| 2566 | a pointer to the memory or NULL on failure. On success, the original |
| 2567 | block has either been expanded or freed. On failure, the original |
| 2568 | block has not been freed; the caller is responsible for freeing the |
| 2569 | original block. Memory allocated in this way must be freed using |
| 2570 | <code><a href="#XML_MemFree" |
| 2571 | >XML_MemFree</a></code>. |
| 2572 | </div> |
| 2573 | |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2574 | <h4 id="XML_MemFree">XML_MemFree</h4> |
| 2575 | <pre class="fcndec"> |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2576 | void XMLCALL |
| 2577 | XML_MemFree(XML_Parser parser, void *ptr); |
| 2578 | </pre> |
| 2579 | <div class="fcndef"> |
| 2580 | Free a block of memory pointed to by <code>ptr</code>. The block must |
| 2581 | have been allocated by <code><a href="#XML_MemMalloc" |
| 2582 | >XML_MemMalloc</a></code> or <code>XML_MemRealloc</code>, or be NULL. |
| 2583 | </div> |
| 2584 | |
| 2585 | <hr /> |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 2586 | |
| 2587 | <div class="footer"> |
| 2588 | Found a bug in the documentation? |
| 2589 | <a href="https://github.com/libexpat/libexpat/issues">Please file a bug report.</a> |
| 2590 | </div> |
| 2591 | |
The Android Open Source Project | b80e287 | 2009-03-03 19:29:30 -0800 | [diff] [blame] | 2592 | </div> |
| 2593 | </body> |
| 2594 | </html> |