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