apr_xml.inc 12 KB

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