xml.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_XML_H__
  20. #define __PJ_XML_H__
  21. /**
  22. * @file xml.h
  23. * @brief PJLIB XML Parser/Helper.
  24. */
  25. #include <pj/types.h>
  26. #include <pj/list.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJ_TINY_XML Mini/Tiny XML Parser/Helper
  30. * @ingroup PJ_FILE_FMT
  31. * @{
  32. */
  33. /** Typedef for XML attribute. */
  34. typedef struct pj_xml_attr pj_xml_attr;
  35. /** Typedef for XML nodes. */
  36. typedef struct pj_xml_node pj_xml_node;
  37. /** This structure declares XML attribute. */
  38. struct pj_xml_attr
  39. {
  40. PJ_DECL_LIST_MEMBER(pj_xml_attr); /**< Standard list elements. */
  41. pj_str_t name; /**< Attribute name. */
  42. pj_str_t value; /**< Attribute value. */
  43. };
  44. /** This structure describes XML node head inside XML node structure.
  45. */
  46. typedef struct pj_xml_node_head
  47. {
  48. PJ_DECL_LIST_MEMBER(pj_xml_node); /**< Standard list elements. */
  49. } pj_xml_node_head;
  50. /** This structure describes XML node. */
  51. struct pj_xml_node
  52. {
  53. PJ_DECL_LIST_MEMBER(pj_xml_node); /**< List @a prev and @a next member */
  54. pj_str_t name; /**< Node name. */
  55. pj_xml_attr attr_head; /**< Attribute list. */
  56. pj_xml_node_head node_head; /**< Node list. */
  57. pj_str_t content; /**< Node content. */
  58. };
  59. /**
  60. * Parse XML message into XML document with a single root node. The parser
  61. * is capable of parsing XML processing instruction construct ("<?") and
  62. * XML comments ("<!--"), however such constructs will be ignored and will not
  63. * be included in the resulted XML node tree.
  64. *
  65. * Note that the XML message input buffer MUST be NULL terminated and have
  66. * length at least len+1 (len MUST NOT include the NULL terminator).
  67. *
  68. * @param pool Pool to allocate memory from.
  69. * @param msg The XML message to parse, MUST be NULL terminated.
  70. * @param len The length of the message, not including NULL terminator.
  71. *
  72. * @return XML root node, or NULL if the XML document can not be parsed.
  73. */
  74. PJ_DECL(pj_xml_node*) pj_xml_parse( pj_pool_t *pool, char *msg, pj_size_t len);
  75. /**
  76. * Print XML into XML message. Note that the function WILL NOT NULL terminate
  77. * the output.
  78. *
  79. * @param node The XML node to print.
  80. * @param buf Buffer to hold the output message.
  81. * @param len The length of the buffer.
  82. * @param prolog If set to nonzero, will print XML prolog ("<?xml..")
  83. *
  84. * @return The size of the printed message, or -1 if there is not
  85. * sufficient space in the buffer to print the message.
  86. */
  87. PJ_DECL(int) pj_xml_print( const pj_xml_node *node, char *buf, pj_size_t len,
  88. pj_bool_t prolog);
  89. /**
  90. * Clone XML node and all subnodes.
  91. *
  92. * @param pool Pool to allocate memory for new nodes.
  93. * @param rhs The node to clone.
  94. *
  95. * @return Cloned XML node, or NULL on fail.
  96. */
  97. PJ_DECL(pj_xml_node*) pj_xml_clone( pj_pool_t *pool, const pj_xml_node *rhs);
  98. /**
  99. * Create an empty node.
  100. *
  101. * @param pool Pool.
  102. * @param name Node name.
  103. *
  104. * @return The new node.
  105. */
  106. PJ_DECL(pj_xml_node*) pj_xml_node_new(pj_pool_t *pool, const pj_str_t *name);
  107. /**
  108. * Create new XML attribute.
  109. *
  110. * @param pool Pool.
  111. * @param name Attribute name.
  112. * @param value Attribute value.
  113. *
  114. * @return The new XML attribute.
  115. */
  116. PJ_DECL(pj_xml_attr*) pj_xml_attr_new(pj_pool_t *pool, const pj_str_t *name,
  117. const pj_str_t *value);
  118. /**
  119. * Add node to another node.
  120. *
  121. * @param parent Parent node.
  122. * @param node Node to be added to parent.
  123. */
  124. PJ_DECL(void) pj_xml_add_node( pj_xml_node *parent, pj_xml_node *node );
  125. /**
  126. * Add attribute to a node.
  127. *
  128. * @param node Node.
  129. * @param attr Attribute to add to node.
  130. */
  131. PJ_DECL(void) pj_xml_add_attr( pj_xml_node *node, pj_xml_attr *attr );
  132. /**
  133. * Find first direct child node with the specified name.
  134. *
  135. * @param parent Parent node.
  136. * @param name Node name to find.
  137. *
  138. * @return XML node found or NULL.
  139. */
  140. PJ_DECL(pj_xml_node*) pj_xml_find_node(const pj_xml_node *parent,
  141. const pj_str_t *name);
  142. /**
  143. * Find next direct child node with the specified name.
  144. *
  145. * @param parent Parent node.
  146. * @param node node->next is the starting point.
  147. * @param name Node name to find.
  148. *
  149. * @return XML node found or NULL.
  150. */
  151. PJ_DECL(pj_xml_node*) pj_xml_find_next_node(const pj_xml_node *parent,
  152. const pj_xml_node *node,
  153. const pj_str_t *name);
  154. /**
  155. * Recursively find the first node with the specified name in the child nodes
  156. * and their children.
  157. *
  158. * @param parent Parent node.
  159. * @param name Node name to find.
  160. *
  161. * @return XML node found or NULL.
  162. */
  163. PJ_DECL(pj_xml_node*) pj_xml_find_node_rec(const pj_xml_node *parent,
  164. const pj_str_t *name);
  165. /**
  166. * Find first attribute within a node with the specified name and optional
  167. * value.
  168. *
  169. * @param node XML Node.
  170. * @param name Attribute name to find.
  171. * @param value Optional value to match.
  172. *
  173. * @return XML attribute found, or NULL.
  174. */
  175. PJ_DECL(pj_xml_attr*) pj_xml_find_attr(const pj_xml_node *node,
  176. const pj_str_t *name,
  177. const pj_str_t *value);
  178. /**
  179. * Find a direct child node with the specified name and match the function.
  180. *
  181. * @param parent Parent node.
  182. * @param name Optional name. If this is NULL, the name will not be
  183. * matched.
  184. * @param data Data to be passed to matching function.
  185. * @param match Optional matching function.
  186. *
  187. * @return The first matched node, or NULL.
  188. */
  189. PJ_DECL(pj_xml_node*) pj_xml_find( const pj_xml_node *parent,
  190. const pj_str_t *name,
  191. const void *data,
  192. pj_bool_t (*match)(const pj_xml_node *,
  193. const void*));
  194. /**
  195. * Recursively find a child node with the specified name and match the
  196. * function.
  197. *
  198. * @param parent Parent node.
  199. * @param name Optional name. If this is NULL, the name will not be
  200. * matched.
  201. * @param data Data to be passed to matching function.
  202. * @param match Optional matching function.
  203. *
  204. * @return The first matched node, or NULL.
  205. */
  206. PJ_DECL(pj_xml_node*) pj_xml_find_rec(const pj_xml_node *parent,
  207. const pj_str_t *name,
  208. const void *data,
  209. pj_bool_t (*match)(const pj_xml_node*,
  210. const void*));
  211. /**
  212. * @}
  213. */
  214. PJ_END_DECL
  215. #endif /* __PJ_XML_H__ */