apr_xml.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. { Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. }
  16. {
  17. * @file apr_xml.h
  18. * @brief APR-UTIL XML Library
  19. }
  20. {
  21. * @defgroup APR_Util_XML XML
  22. * @ingroup APR_Util
  23. }
  24. {#include "apr_pools.h"
  25. #include "apr_tables.h"
  26. #include "apr_file_io.h"
  27. #include "apu.h"}
  28. {
  29. * @package Apache XML library
  30. }
  31. { -------------------------------------------------------------------- }
  32. { ### these will need to move at some point to a more logical spot }
  33. { @see apr_text }
  34. type
  35. Papr_text = ^apr_text;
  36. { Structure to keep a linked list of pieces of text }
  37. apr_text = record
  38. { The current piece of text }
  39. text: PChar;
  40. { a pointer to the next piece of text }
  41. next: Papr_text;
  42. end;
  43. { @see apr_text_header }
  44. Papr_text_header = ^apr_text_header;
  45. { A list of pieces of text }
  46. apr_text_header = record
  47. { The first piece of text in the list }
  48. first: Papr_text;
  49. { The last piece of text in the list }
  50. last: Papr_text;
  51. end;
  52. {
  53. * Append a piece of text to the end of a list
  54. * @param p The pool to allocate out of
  55. * @param hdr The text header to append to
  56. * @param text The new text to append
  57. }
  58. procedure apr_text_append(p: Papr_pool_t; hdr: Papr_text_header;
  59. const text: PChar);
  60. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  61. external LibAPRUtil name LibNamePrefix + 'apr_text_append' + LibSuff12;
  62. { --------------------------------------------------------------------
  63. **
  64. ** XML PARSING
  65. }
  66. {
  67. ** Qualified namespace values
  68. **
  69. ** APR_XML_NS_DAV_ID
  70. ** We always insert the "DAV:" namespace URI at the head of the
  71. ** namespace array. This means that it will always be at ID==0,
  72. ** making it much easier to test for.
  73. **
  74. ** APR_XML_NS_NONE
  75. ** This special ID is used for two situations:
  76. **
  77. ** 1) The namespace prefix begins with "xml" (and we do not know
  78. ** what it means). Namespace prefixes with "xml" (any case) as
  79. ** their first three characters are reserved by the XML Namespaces
  80. ** specification for future use. mod_dav will pass these through
  81. ** unchanged. When this identifier is used, the prefix is LEFT in
  82. ** the element/attribute name. Downstream processing should not
  83. ** prepend another prefix.
  84. **
  85. ** 2) The element/attribute does not have a namespace.
  86. **
  87. ** a) No prefix was used, and a default namespace has not been
  88. ** defined.
  89. ** b) No prefix was used, and the default namespace was specified
  90. ** to mean "no namespace". This is done with a namespace
  91. ** declaration of: xmlns=""
  92. ** (this declaration is typically used to override a previous
  93. ** specification for the default namespace)
  94. **
  95. ** In these cases, we need to record that the elem/attr has no
  96. ** namespace so that we will not attempt to prepend a prefix.
  97. ** All namespaces that are used will have a prefix assigned to
  98. ** them -- mod_dav will never set or use the default namespace
  99. ** when generating XML. This means that "no prefix" will always
  100. ** mean "no namespace".
  101. **
  102. ** In both cases, the XML generation will avoid prepending a prefix.
  103. ** For the first case, this means the original prefix/name will be
  104. ** inserted into the output stream. For the latter case, it means
  105. ** the name will have no prefix, and since we never define a default
  106. ** namespace, this means it will have no namespace.
  107. **
  108. ** Note: currently, mod_dav understands the "xmlns" prefix and the
  109. ** "xml:lang" attribute. These are handled specially (they aren't
  110. ** left within the XML tree), so the APR_XML_NS_NONE value won't ever
  111. ** really apply to these values.
  112. }
  113. const
  114. APR_XML_NS_DAV_ID = 0; {< namespace ID for "DAV:" }
  115. APR_XML_NS_NONE = -10; {< no namespace for this elem/attr }
  116. APR_XML_NS_ERROR_BASE = -100; {< used only during processing }
  117. { Is this namespace an error? }
  118. // APR_XML_NS_IS_ERROR(e) ((e) <= APR_XML_NS_ERROR_BASE)
  119. type
  120. { @see apr_xml_attr }
  121. Papr_xml_attr = ^apr_xml_attr;
  122. { @see apr_xml_elem }
  123. Papr_xml_elem = ^apr_xml_elem;
  124. { @see apr_xml_doc }
  125. Papr_xml_doc = ^apr_xml_doc;
  126. PPapr_xml_doc = ^Papr_xml_doc;
  127. { apr_xml_attr: holds a parsed XML attribute }
  128. apr_xml_attr = record
  129. { attribute name }
  130. name: PChar;
  131. { index into namespace array }
  132. ns: Integer;
  133. { attribute value }
  134. value: PChar;
  135. { next attribute }
  136. next: Papr_xml_attr;
  137. end;
  138. { apr_xml_elem: holds a parsed XML element }
  139. apr_xml_elem = record
  140. { element name }
  141. name: PChar;
  142. { index into namespace array }
  143. ns: Integer;
  144. { xml:lang for attrs/contents }
  145. lang: PChar;
  146. { cdata right after start tag }
  147. first_cdata: apr_text_header;
  148. { cdata after MY end tag }
  149. following_cdata: apr_text_header;
  150. { parent element }
  151. parent: Papr_xml_elem;
  152. { next (sibling) element }
  153. next: Papr_xml_elem;
  154. { first child element }
  155. first_child: Papr_xml_elem;
  156. { first attribute }
  157. attr: Papr_xml_attr;
  158. { used only during parsing }
  159. { last child element }
  160. last_child: Papr_xml_elem;
  161. { namespaces scoped by this elem }
  162. ns_scope: Papr_xml_ns_scope;
  163. { used by modules during request processing }
  164. { Place for modules to store private data }
  165. priv: Pointer;
  166. end;
  167. { Is this XML element empty? }
  168. //#define APR_XML_ELEM_IS_EMPTY(e) ((e)->first_child == NULL && \
  169. // (e)->first_cdata.first == NULL)
  170. { apr_xml_doc: holds a parsed XML document }
  171. apr_xml_doc = record
  172. { root element }
  173. root: Papr_xml_elem;
  174. { array of namespaces used }
  175. namespaces: Papr_array_header_t;
  176. end;
  177. { Opaque XML parser structure }
  178. apr_xml_parser = record end;
  179. Papr_xml_parser = ^apr_xml_parser;
  180. PPapr_xml_parser = ^Papr_xml_parser;
  181. {
  182. * Create an XML parser
  183. * @param pool The pool for allocating the parser and the parse results.
  184. * @return The new parser.
  185. }
  186. function apr_xml_parser_create(pool: Papr_pool_t): Papr_xml_parser;
  187. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  188. external LibAPRUtil name LibNamePrefix + 'apr_xml_parser_create' + LibSuff4;
  189. {
  190. * Parse a File, producing a xml_doc
  191. * @param p The pool for allocating the parse results.
  192. * @param parser A pointer to *parser (needed so calling function can get
  193. * errors), will be set to NULL on successfull completion.
  194. * @param ppdoc A pointer to *apr_xml_doc (which has the parsed results in it)
  195. * @param xmlfd A file to read from.
  196. * @param buffer_length Buffer length which would be suitable
  197. * @return Any errors found during parsing.
  198. }
  199. function apr_xml_parse_file(p: Papr_pool_t;
  200. parser: PPapr_xml_parser; ppdoc: PPapr_xml_doc;
  201. xmlfd: Papr_file_t; buffer_length: apr_size_t): apr_status_t;
  202. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  203. external LibAPRUtil name LibNamePrefix + 'apr_xml_parse_file' + LibSuff20;
  204. {
  205. * Feed input into the parser
  206. * @param parser The XML parser for parsing this data.
  207. * @param data The data to parse.
  208. * @param len The length of the data.
  209. * @return Any errors found during parsing.
  210. * @remark Use apr_xml_parser_geterror() to get more error information.
  211. }
  212. function apr_xml_parser_feed(parser: Papr_xml_parser;
  213. const data: PChar; len: apr_size_t): apr_status_t;
  214. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  215. external LibAPRUtil name LibNamePrefix + 'apr_xml_parser_feed' + LibSuff12;
  216. {
  217. * Terminate the parsing and return the result
  218. * @param parser The XML parser for parsing this data.
  219. * @param pdoc The resulting parse information. May be NULL to simply
  220. * terminate the parsing without fetching the info.
  221. * @return Any errors found during the final stage of parsing.
  222. * @remark Use apr_xml_parser_geterror() to get more error information.
  223. }
  224. function apr_xml_parser_done(parser: Papr_xml_parser;
  225. pdoc: PPapr_xml_doc): apr_status_t;
  226. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  227. external LibAPRUtil name LibNamePrefix + 'apr_xml_parser_done' + LibSuff8;
  228. {
  229. * Fetch additional error information from the parser.
  230. * @param parser The XML parser to query for errors.
  231. * @param errbuf A buffer for storing error text.
  232. * @param errbufsize The length of the error text buffer.
  233. * @return The error buffer
  234. }
  235. function apr_xml_parser_geterror(parser: Papr_xml_parser;
  236. errbuf: PChar; errbufsize: apr_size_t): PChar;
  237. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  238. external LibAPRUtil name LibNamePrefix + 'apr_xml_parser_geterror' + LibSuff12;
  239. {
  240. * Converts an XML element tree to flat text
  241. * @param p The pool to allocate out of
  242. * @param elem The XML element to convert
  243. * @param style How to covert the XML. One of:
  244. * <PRE>
  245. * APR_XML_X2T_FULL start tag, contents, end tag
  246. * APR_XML_X2T_INNER contents only
  247. * APR_XML_X2T_LANG_INNER xml:lang + inner contents
  248. * APR_XML_X2T_FULL_NS_LANG FULL + ns defns + xml:lang
  249. * </PRE>
  250. * @param namespaces The namespace of the current XML element
  251. * @param ns_map Namespace mapping
  252. * @param pbuf Buffer to put the converted text into
  253. * @param psize Size of the converted text
  254. }
  255. procedure apr_xml_to_text(p: Papr_pool_t; const elem: Papr_xml_elem;
  256. style: Integer; namespaces: Papr_array_header_t;
  257. ns_map: PInteger; const pbuf: PPChar; psize: Papr_size_t);
  258. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  259. external LibAPRUtil name LibNamePrefix + 'apr_xml_to_text' + LibSuff28;
  260. { style argument values: }
  261. const
  262. APR_XML_X2T_FULL = 0; {< start tag, contents, end tag }
  263. APR_XML_X2T_INNER = 1; {< contents only }
  264. APR_XML_X2T_LANG_INNER = 2; {< xml:lang + inner contents }
  265. APR_XML_X2T_FULL_NS_LANG = 3; {< FULL + ns defns + xml:lang }
  266. {
  267. * empty XML element
  268. * @param p The pool to allocate out of
  269. * @param elem The XML element to empty
  270. * @return the string that was stored in the XML element
  271. }
  272. function apr_xml_empty_elem(p: Papr_pool_t; const elem: Papr_xml_elem): PChar;
  273. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  274. external LibAPRUtil name LibNamePrefix + 'apr_xml_empty_elem' + LibSuff8;
  275. {
  276. * quote an XML string
  277. * Replace '<', '>', and '&' with '&lt;', '&gt;', and '&amp;'.
  278. * @param p The pool to allocate out of
  279. * @param s The string to quote
  280. * @param quotes If quotes is true, then replace '"' with '&quot;'.
  281. * @return The quoted string
  282. * @note If the string does not contain special characters, it is not
  283. * duplicated into the pool and the original string is returned.
  284. }
  285. function apr_xml_quote_string(p: Papr_pool_t; const s: PChar;
  286. quotes: Integer): PChar;
  287. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  288. external LibAPRUtil name LibNamePrefix + 'apr_xml_quote_string' + LibSuff12;
  289. {
  290. * Quote an XML element
  291. * @param p The pool to allocate out of
  292. * @param elem The element to quote
  293. }
  294. procedure apr_xml_quote_elem(p: Papr_pool_t; elem: Papr_xml_elem);
  295. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  296. external LibAPRUtil name LibNamePrefix + 'apr_xml_quote_elem' + LibSuff8;
  297. { manage an array of unique URIs: apr_xml_insert_uri() and APR_XML_URI_ITEM() }
  298. {
  299. * return the URI's (existing) index, or insert it and return a new index
  300. * @param uri_array array to insert into
  301. * @param uri The uri to insert
  302. * @return int The uri's index
  303. }
  304. function apr_xml_insert_uri(uri_array: Papr_array_header_t;
  305. const uri: PChar): Integer;
  306. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  307. external LibAPRUtil name LibNamePrefix + 'apr_xml_insert_uri' + LibSuff8;
  308. { Get the URI item for this XML element }
  309. //#define APR_XML_GET_URI_ITEM(ary, i) (((const char * const *)(ary)->elts)[i])