Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * debugXML.c : This is a set of routines used for debugging the tree |
| 3 | * produced by the XML parser. |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 7 | * Daniel Veillard <daniel@veillard.com> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | #ifdef LIBXML_DEBUG_ENABLED |
| 12 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #ifdef HAVE_STDLIB_H |
| 15 | #include <stdlib.h> |
| 16 | #endif |
| 17 | #ifdef HAVE_STRING_H |
| 18 | #include <string.h> |
| 19 | #endif |
| 20 | #include <libxml/xmlmemory.h> |
| 21 | #include <libxml/tree.h> |
| 22 | #include <libxml/parser.h> |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 23 | #include <libxml/parserInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 24 | #include <libxml/valid.h> |
| 25 | #include <libxml/debugXML.h> |
| 26 | #include <libxml/HTMLtree.h> |
| 27 | #include <libxml/HTMLparser.h> |
| 28 | #include <libxml/xmlerror.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 29 | #include <libxml/globals.h> |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 30 | #include <libxml/xpathInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 31 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 32 | /** |
| 33 | * xmlDebugDumpString: |
| 34 | * @output: the FILE * for the output |
| 35 | * @str: the string |
| 36 | * |
| 37 | * Dumps informations about the string, shorten it if necessary |
| 38 | */ |
| 39 | void |
| 40 | xmlDebugDumpString(FILE * output, const xmlChar * str) |
| 41 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 42 | int i; |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 43 | |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 44 | if (output == NULL) |
| 45 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 46 | if (str == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 47 | fprintf(output, "(NULL)"); |
| 48 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 49 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 50 | for (i = 0; i < 40; i++) |
| 51 | if (str[i] == 0) |
| 52 | return; |
| 53 | else if (IS_BLANK(str[i])) |
| 54 | fputc(' ', output); |
| 55 | else if (str[i] >= 0x80) |
| 56 | fprintf(output, "#%X", str[i]); |
| 57 | else |
| 58 | fputc(str[i], output); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 59 | fprintf(output, "..."); |
| 60 | } |
| 61 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 62 | static void |
| 63 | xmlDebugDumpDtdNode(FILE *output, xmlDtdPtr dtd, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 64 | int i; |
| 65 | char shift[100]; |
| 66 | |
| 67 | for (i = 0;((i < depth) && (i < 25));i++) |
| 68 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 69 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 70 | |
| 71 | fprintf(output, shift); |
| 72 | |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 73 | if (dtd == NULL) { |
| 74 | fprintf(output, "DTD node is NULL\n"); |
| 75 | return; |
| 76 | } |
| 77 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 78 | if (dtd->type != XML_DTD_NODE) { |
| 79 | fprintf(output, "PBM: not a DTD\n"); |
| 80 | return; |
| 81 | } |
| 82 | if (dtd->name != NULL) |
| 83 | fprintf(output, "DTD(%s)", dtd->name); |
| 84 | else |
| 85 | fprintf(output, "DTD"); |
| 86 | if (dtd->ExternalID != NULL) |
| 87 | fprintf(output, ", PUBLIC %s", dtd->ExternalID); |
| 88 | if (dtd->SystemID != NULL) |
| 89 | fprintf(output, ", SYSTEM %s", dtd->SystemID); |
| 90 | fprintf(output, "\n"); |
| 91 | /* |
| 92 | * Do a bit of checking |
| 93 | */ |
| 94 | if (dtd->parent == NULL) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 95 | fprintf(output, "PBM: DTD has no parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 96 | if (dtd->doc == NULL) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 97 | fprintf(output, "PBM: DTD has no doc\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 98 | if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc)) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 99 | fprintf(output, "PBM: DTD doc differs from parent's one\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 100 | if (dtd->prev == NULL) { |
| 101 | if ((dtd->parent != NULL) && (dtd->parent->children != (xmlNodePtr)dtd)) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 102 | fprintf(output, "PBM: DTD has no prev and not first of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 103 | } else { |
| 104 | if (dtd->prev->next != (xmlNodePtr) dtd) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 105 | fprintf(output, "PBM: DTD prev->next : back link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 106 | } |
| 107 | if (dtd->next == NULL) { |
| 108 | if ((dtd->parent != NULL) && (dtd->parent->last != (xmlNodePtr) dtd)) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 109 | fprintf(output, "PBM: DTD has no next and not last of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 110 | } else { |
| 111 | if (dtd->next->prev != (xmlNodePtr) dtd) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 112 | fprintf(output, "PBM: DTD next->prev : forward link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 116 | static void |
| 117 | xmlDebugDumpAttrDecl(FILE *output, xmlAttributePtr attr, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 118 | int i; |
| 119 | char shift[100]; |
| 120 | |
| 121 | for (i = 0;((i < depth) && (i < 25));i++) |
| 122 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 123 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 124 | |
| 125 | fprintf(output, shift); |
| 126 | |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 127 | if (attr == NULL) { |
| 128 | fprintf(output, "Attribute declaration is NULL\n"); |
| 129 | return; |
| 130 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 131 | if (attr->type != XML_ATTRIBUTE_DECL) { |
| 132 | fprintf(output, "PBM: not a Attr\n"); |
| 133 | return; |
| 134 | } |
| 135 | if (attr->name != NULL) |
| 136 | fprintf(output, "ATTRDECL(%s)", attr->name); |
| 137 | else |
| 138 | fprintf(output, "PBM ATTRDECL noname!!!"); |
| 139 | if (attr->elem != NULL) |
| 140 | fprintf(output, " for %s", attr->elem); |
| 141 | else |
| 142 | fprintf(output, " PBM noelem!!!"); |
| 143 | switch (attr->atype) { |
| 144 | case XML_ATTRIBUTE_CDATA: |
| 145 | fprintf(output, " CDATA"); |
| 146 | break; |
| 147 | case XML_ATTRIBUTE_ID: |
| 148 | fprintf(output, " ID"); |
| 149 | break; |
| 150 | case XML_ATTRIBUTE_IDREF: |
| 151 | fprintf(output, " IDREF"); |
| 152 | break; |
| 153 | case XML_ATTRIBUTE_IDREFS: |
| 154 | fprintf(output, " IDREFS"); |
| 155 | break; |
| 156 | case XML_ATTRIBUTE_ENTITY: |
| 157 | fprintf(output, " ENTITY"); |
| 158 | break; |
| 159 | case XML_ATTRIBUTE_ENTITIES: |
| 160 | fprintf(output, " ENTITIES"); |
| 161 | break; |
| 162 | case XML_ATTRIBUTE_NMTOKEN: |
| 163 | fprintf(output, " NMTOKEN"); |
| 164 | break; |
| 165 | case XML_ATTRIBUTE_NMTOKENS: |
| 166 | fprintf(output, " NMTOKENS"); |
| 167 | break; |
| 168 | case XML_ATTRIBUTE_ENUMERATION: |
| 169 | fprintf(output, " ENUMERATION"); |
| 170 | break; |
| 171 | case XML_ATTRIBUTE_NOTATION: |
| 172 | fprintf(output, " NOTATION "); |
| 173 | break; |
| 174 | } |
| 175 | if (attr->tree != NULL) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 176 | int indx; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 177 | xmlEnumerationPtr cur = attr->tree; |
| 178 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 179 | for (indx = 0;indx < 5; indx++) { |
| 180 | if (indx != 0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 181 | fprintf(output, "|%s", cur->name); |
| 182 | else |
| 183 | fprintf(output, " (%s", cur->name); |
| 184 | cur = cur->next; |
| 185 | if (cur == NULL) break; |
| 186 | } |
| 187 | if (cur == NULL) |
| 188 | fprintf(output, ")"); |
| 189 | else |
| 190 | fprintf(output, "...)"); |
| 191 | } |
| 192 | switch (attr->def) { |
| 193 | case XML_ATTRIBUTE_NONE: |
| 194 | break; |
| 195 | case XML_ATTRIBUTE_REQUIRED: |
| 196 | fprintf(output, " REQUIRED"); |
| 197 | break; |
| 198 | case XML_ATTRIBUTE_IMPLIED: |
| 199 | fprintf(output, " IMPLIED"); |
| 200 | break; |
| 201 | case XML_ATTRIBUTE_FIXED: |
| 202 | fprintf(output, " FIXED"); |
| 203 | break; |
| 204 | } |
| 205 | if (attr->defaultValue != NULL) { |
| 206 | fprintf(output, "\""); |
| 207 | xmlDebugDumpString(output, attr->defaultValue); |
| 208 | fprintf(output, "\""); |
| 209 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 210 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * Do a bit of checking |
| 214 | */ |
| 215 | if (attr->parent == NULL) |
| 216 | fprintf(output, "PBM: Attr has no parent\n"); |
| 217 | if (attr->doc == NULL) |
| 218 | fprintf(output, "PBM: Attr has no doc\n"); |
| 219 | if ((attr->parent != NULL) && (attr->doc != attr->parent->doc)) |
| 220 | fprintf(output, "PBM: Attr doc differs from parent's one\n"); |
| 221 | if (attr->prev == NULL) { |
| 222 | if ((attr->parent != NULL) && (attr->parent->children != (xmlNodePtr)attr)) |
| 223 | fprintf(output, "PBM: Attr has no prev and not first of list\n"); |
| 224 | } else { |
| 225 | if (attr->prev->next != (xmlNodePtr) attr) |
| 226 | fprintf(output, "PBM: Attr prev->next : back link wrong\n"); |
| 227 | } |
| 228 | if (attr->next == NULL) { |
| 229 | if ((attr->parent != NULL) && (attr->parent->last != (xmlNodePtr) attr)) |
| 230 | fprintf(output, "PBM: Attr has no next and not last of list\n"); |
| 231 | } else { |
| 232 | if (attr->next->prev != (xmlNodePtr) attr) |
| 233 | fprintf(output, "PBM: Attr next->prev : forward link wrong\n"); |
| 234 | } |
| 235 | } |
| 236 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 237 | static void |
| 238 | xmlDebugDumpElemDecl(FILE *output, xmlElementPtr elem, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 239 | int i; |
| 240 | char shift[100]; |
| 241 | |
| 242 | for (i = 0;((i < depth) && (i < 25));i++) |
| 243 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 244 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 245 | |
| 246 | fprintf(output, shift); |
| 247 | |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 248 | if (elem == NULL) { |
| 249 | fprintf(output, "Element declaration is NULL\n"); |
| 250 | return; |
| 251 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 252 | if (elem->type != XML_ELEMENT_DECL) { |
| 253 | fprintf(output, "PBM: not a Elem\n"); |
| 254 | return; |
| 255 | } |
| 256 | if (elem->name != NULL) { |
| 257 | fprintf(output, "ELEMDECL("); |
| 258 | xmlDebugDumpString(output, elem->name); |
| 259 | fprintf(output, ")"); |
| 260 | } else |
| 261 | fprintf(output, "PBM ELEMDECL noname!!!"); |
| 262 | switch (elem->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 263 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 264 | fprintf(output, ", UNDEFINED"); |
| 265 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 266 | case XML_ELEMENT_TYPE_EMPTY: |
| 267 | fprintf(output, ", EMPTY"); |
| 268 | break; |
| 269 | case XML_ELEMENT_TYPE_ANY: |
| 270 | fprintf(output, ", ANY"); |
| 271 | break; |
| 272 | case XML_ELEMENT_TYPE_MIXED: |
| 273 | fprintf(output, ", MIXED "); |
| 274 | break; |
| 275 | case XML_ELEMENT_TYPE_ELEMENT: |
| 276 | fprintf(output, ", MIXED "); |
| 277 | break; |
| 278 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 279 | if ((elem->type != XML_ELEMENT_NODE) && |
| 280 | (elem->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 281 | char buf[5001]; |
| 282 | |
| 283 | buf[0] = 0; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 284 | xmlSnprintfElementContent(buf, 5000, elem->content, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 285 | buf[5000] = 0; |
| 286 | fprintf(output, "%s", buf); |
| 287 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 288 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Do a bit of checking |
| 292 | */ |
| 293 | if (elem->parent == NULL) |
| 294 | fprintf(output, "PBM: Elem has no parent\n"); |
| 295 | if (elem->doc == NULL) |
| 296 | fprintf(output, "PBM: Elem has no doc\n"); |
| 297 | if ((elem->parent != NULL) && (elem->doc != elem->parent->doc)) |
| 298 | fprintf(output, "PBM: Elem doc differs from parent's one\n"); |
| 299 | if (elem->prev == NULL) { |
| 300 | if ((elem->parent != NULL) && (elem->parent->children != (xmlNodePtr)elem)) |
| 301 | fprintf(output, "PBM: Elem has no prev and not first of list\n"); |
| 302 | } else { |
| 303 | if (elem->prev->next != (xmlNodePtr) elem) |
| 304 | fprintf(output, "PBM: Elem prev->next : back link wrong\n"); |
| 305 | } |
| 306 | if (elem->next == NULL) { |
| 307 | if ((elem->parent != NULL) && (elem->parent->last != (xmlNodePtr) elem)) |
| 308 | fprintf(output, "PBM: Elem has no next and not last of list\n"); |
| 309 | } else { |
| 310 | if (elem->next->prev != (xmlNodePtr) elem) |
| 311 | fprintf(output, "PBM: Elem next->prev : forward link wrong\n"); |
| 312 | } |
| 313 | } |
| 314 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 315 | static void |
| 316 | xmlDebugDumpEntityDecl(FILE *output, xmlEntityPtr ent, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 317 | int i; |
| 318 | char shift[100]; |
| 319 | |
| 320 | for (i = 0;((i < depth) && (i < 25));i++) |
| 321 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 322 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 323 | |
| 324 | fprintf(output, shift); |
| 325 | |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 326 | if (ent == NULL) { |
| 327 | fprintf(output, "Entity declaration is NULL\n"); |
| 328 | return; |
| 329 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 330 | if (ent->type != XML_ENTITY_DECL) { |
| 331 | fprintf(output, "PBM: not a Entity decl\n"); |
| 332 | return; |
| 333 | } |
| 334 | if (ent->name != NULL) { |
| 335 | fprintf(output, "ENTITYDECL("); |
| 336 | xmlDebugDumpString(output, ent->name); |
| 337 | fprintf(output, ")"); |
| 338 | } else |
| 339 | fprintf(output, "PBM ENTITYDECL noname!!!"); |
| 340 | switch (ent->etype) { |
| 341 | case XML_INTERNAL_GENERAL_ENTITY: |
| 342 | fprintf(output, ", internal\n"); |
| 343 | break; |
| 344 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 345 | fprintf(output, ", external parsed\n"); |
| 346 | break; |
| 347 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 348 | fprintf(output, ", unparsed\n"); |
| 349 | break; |
| 350 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 351 | fprintf(output, ", parameter\n"); |
| 352 | break; |
| 353 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 354 | fprintf(output, ", external parameter\n"); |
| 355 | break; |
| 356 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 357 | fprintf(output, ", predefined\n"); |
| 358 | break; |
| 359 | } |
| 360 | if (ent->ExternalID) { |
| 361 | fprintf(output, shift); |
| 362 | fprintf(output, " ExternalID=%s\n", ent->ExternalID); |
| 363 | } |
| 364 | if (ent->SystemID) { |
| 365 | fprintf(output, shift); |
| 366 | fprintf(output, " SystemID=%s\n", ent->SystemID); |
| 367 | } |
| 368 | if (ent->URI != NULL) { |
| 369 | fprintf(output, shift); |
| 370 | fprintf(output, " URI=%s\n", ent->URI); |
| 371 | } |
| 372 | if (ent->content) { |
| 373 | fprintf(output, shift); |
| 374 | fprintf(output, " content="); |
| 375 | xmlDebugDumpString(output, ent->content); |
| 376 | fprintf(output, "\n"); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Do a bit of checking |
| 381 | */ |
| 382 | if (ent->parent == NULL) |
| 383 | fprintf(output, "PBM: Ent has no parent\n"); |
| 384 | if (ent->doc == NULL) |
| 385 | fprintf(output, "PBM: Ent has no doc\n"); |
| 386 | if ((ent->parent != NULL) && (ent->doc != ent->parent->doc)) |
| 387 | fprintf(output, "PBM: Ent doc differs from parent's one\n"); |
| 388 | if (ent->prev == NULL) { |
| 389 | if ((ent->parent != NULL) && (ent->parent->children != (xmlNodePtr)ent)) |
| 390 | fprintf(output, "PBM: Ent has no prev and not first of list\n"); |
| 391 | } else { |
| 392 | if (ent->prev->next != (xmlNodePtr) ent) |
| 393 | fprintf(output, "PBM: Ent prev->next : back link wrong\n"); |
| 394 | } |
| 395 | if (ent->next == NULL) { |
| 396 | if ((ent->parent != NULL) && (ent->parent->last != (xmlNodePtr) ent)) |
| 397 | fprintf(output, "PBM: Ent has no next and not last of list\n"); |
| 398 | } else { |
| 399 | if (ent->next->prev != (xmlNodePtr) ent) |
| 400 | fprintf(output, "PBM: Ent next->prev : forward link wrong\n"); |
| 401 | } |
| 402 | } |
| 403 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 404 | static void |
| 405 | xmlDebugDumpNamespace(FILE *output, xmlNsPtr ns, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 406 | int i; |
| 407 | char shift[100]; |
| 408 | |
| 409 | for (i = 0;((i < depth) && (i < 25));i++) |
| 410 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 411 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 412 | |
| 413 | fprintf(output, shift); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 414 | |
| 415 | if (ns == NULL) { |
| 416 | fprintf(output, "namespace node is NULL\n"); |
| 417 | return; |
| 418 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 419 | if (ns->type != XML_NAMESPACE_DECL) { |
| 420 | fprintf(output, "invalid namespace node %d\n", ns->type); |
| 421 | return; |
| 422 | } |
| 423 | if (ns->href == NULL) { |
| 424 | if (ns->prefix != NULL) |
| 425 | fprintf(output, "incomplete namespace %s href=NULL\n", ns->prefix); |
| 426 | else |
| 427 | fprintf(output, "incomplete default namespace href=NULL\n"); |
| 428 | } else { |
| 429 | if (ns->prefix != NULL) |
| 430 | fprintf(output, "namespace %s href=", ns->prefix); |
| 431 | else |
| 432 | fprintf(output, "default namespace href="); |
| 433 | |
| 434 | xmlDebugDumpString(output, ns->href); |
| 435 | fprintf(output, "\n"); |
| 436 | } |
| 437 | } |
| 438 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 439 | static void |
| 440 | xmlDebugDumpNamespaceList(FILE *output, xmlNsPtr ns, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 441 | while (ns != NULL) { |
| 442 | xmlDebugDumpNamespace(output, ns, depth); |
| 443 | ns = ns->next; |
| 444 | } |
| 445 | } |
| 446 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 447 | static void |
| 448 | xmlDebugDumpEntity(FILE *output, xmlEntityPtr ent, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 449 | int i; |
| 450 | char shift[100]; |
| 451 | |
| 452 | for (i = 0;((i < depth) && (i < 25));i++) |
| 453 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 454 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 455 | |
| 456 | fprintf(output, shift); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 457 | |
| 458 | if (ent == NULL) { |
| 459 | fprintf(output, "Entity is NULL\n"); |
| 460 | return; |
| 461 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 462 | switch (ent->etype) { |
| 463 | case XML_INTERNAL_GENERAL_ENTITY: |
| 464 | fprintf(output, "INTERNAL_GENERAL_ENTITY "); |
| 465 | break; |
| 466 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 467 | fprintf(output, "EXTERNAL_GENERAL_PARSED_ENTITY "); |
| 468 | break; |
| 469 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 470 | fprintf(output, "EXTERNAL_GENERAL_UNPARSED_ENTITY "); |
| 471 | break; |
| 472 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 473 | fprintf(output, "INTERNAL_PARAMETER_ENTITY "); |
| 474 | break; |
| 475 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 476 | fprintf(output, "EXTERNAL_PARAMETER_ENTITY "); |
| 477 | break; |
| 478 | default: |
| 479 | fprintf(output, "ENTITY_%d ! ", ent->etype); |
| 480 | } |
| 481 | fprintf(output, "%s\n", ent->name); |
| 482 | if (ent->ExternalID) { |
| 483 | fprintf(output, shift); |
| 484 | fprintf(output, "ExternalID=%s\n", ent->ExternalID); |
| 485 | } |
| 486 | if (ent->SystemID) { |
| 487 | fprintf(output, shift); |
| 488 | fprintf(output, "SystemID=%s\n", ent->SystemID); |
| 489 | } |
| 490 | if (ent->URI) { |
| 491 | fprintf(output, shift); |
| 492 | fprintf(output, "URI=%s\n", ent->URI); |
| 493 | } |
| 494 | if (ent->content) { |
| 495 | fprintf(output, shift); |
| 496 | fprintf(output, "content="); |
| 497 | xmlDebugDumpString(output, ent->content); |
| 498 | fprintf(output, "\n"); |
| 499 | } |
| 500 | } |
| 501 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 502 | /** |
| 503 | * xmlDebugDumpAttr: |
| 504 | * @output: the FILE * for the output |
| 505 | * @attr: the attribute |
| 506 | * @depth: the indentation level. |
| 507 | * |
| 508 | * Dumps debug information for the attribute |
| 509 | */ |
| 510 | void |
| 511 | xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 512 | int i; |
| 513 | char shift[100]; |
| 514 | |
| 515 | for (i = 0;((i < depth) && (i < 25));i++) |
| 516 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 517 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 518 | |
| 519 | fprintf(output, shift); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 520 | |
| 521 | if (attr == NULL) { |
| 522 | fprintf(output, "Attr is NULL"); |
| 523 | return; |
| 524 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 525 | fprintf(output, "ATTRIBUTE "); |
| 526 | xmlDebugDumpString(output, attr->name); |
| 527 | fprintf(output, "\n"); |
| 528 | if (attr->children != NULL) |
| 529 | xmlDebugDumpNodeList(output, attr->children, depth + 1); |
| 530 | |
| 531 | /* |
| 532 | * Do a bit of checking |
| 533 | */ |
| 534 | if (attr->parent == NULL) |
| 535 | fprintf(output, "PBM: Attr has no parent\n"); |
| 536 | if (attr->doc == NULL) |
| 537 | fprintf(output, "PBM: Attr has no doc\n"); |
| 538 | if ((attr->parent != NULL) && (attr->doc != attr->parent->doc)) |
| 539 | fprintf(output, "PBM: Attr doc differs from parent's one\n"); |
| 540 | if (attr->prev == NULL) { |
| 541 | if ((attr->parent != NULL) && (attr->parent->properties != attr)) |
| 542 | fprintf(output, "PBM: Attr has no prev and not first of list\n"); |
| 543 | } else { |
| 544 | if (attr->prev->next != attr) |
| 545 | fprintf(output, "PBM: Attr prev->next : back link wrong\n"); |
| 546 | } |
| 547 | if (attr->next != NULL) { |
| 548 | if (attr->next->prev != attr) |
| 549 | fprintf(output, "PBM: Attr next->prev : forward link wrong\n"); |
| 550 | } |
| 551 | } |
| 552 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 553 | /** |
| 554 | * xmlDebugDumpAttrList: |
| 555 | * @output: the FILE * for the output |
| 556 | * @attr: the attribute list |
| 557 | * @depth: the indentation level. |
| 558 | * |
| 559 | * Dumps debug information for the attribute list |
| 560 | */ |
| 561 | void |
| 562 | xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth) |
| 563 | { |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 564 | if (output == NULL) |
| 565 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 566 | while (attr != NULL) { |
| 567 | xmlDebugDumpAttr(output, attr, depth); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 568 | attr = attr->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 572 | /** |
| 573 | * xmlDebugDumpOneNode: |
| 574 | * @output: the FILE * for the output |
| 575 | * @node: the node |
| 576 | * @depth: the indentation level. |
| 577 | * |
| 578 | * Dumps debug information for the element node, it is not recursive |
| 579 | */ |
| 580 | void |
| 581 | xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth) |
| 582 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 583 | int i; |
| 584 | char shift[100]; |
| 585 | |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 586 | if (output == NULL) |
| 587 | output = stdout; |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 588 | for (i = 0; ((i < depth) && (i < 25)); i++) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 589 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 590 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 591 | |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 592 | if (node == NULL) { |
| 593 | fprintf(output, shift); |
| 594 | fprintf(output, "node is NULL\n"); |
| 595 | return; |
| 596 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 597 | switch (node->type) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 598 | case XML_ELEMENT_NODE: |
| 599 | fprintf(output, shift); |
| 600 | fprintf(output, "ELEMENT "); |
| 601 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 602 | xmlDebugDumpString(output, node->ns->prefix); |
| 603 | fprintf(output, ":"); |
| 604 | } |
| 605 | xmlDebugDumpString(output, node->name); |
| 606 | fprintf(output, "\n"); |
| 607 | break; |
| 608 | case XML_ATTRIBUTE_NODE: |
| 609 | fprintf(output, shift); |
| 610 | fprintf(output, "Error, ATTRIBUTE found here\n"); |
| 611 | break; |
| 612 | case XML_TEXT_NODE: |
| 613 | fprintf(output, shift); |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 614 | if (node->name == (const xmlChar *) xmlStringTextNoenc) |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 615 | fprintf(output, "TEXT no enc\n"); |
| 616 | else |
| 617 | fprintf(output, "TEXT\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 618 | break; |
| 619 | case XML_CDATA_SECTION_NODE: |
| 620 | fprintf(output, shift); |
| 621 | fprintf(output, "CDATA_SECTION\n"); |
| 622 | break; |
| 623 | case XML_ENTITY_REF_NODE: |
| 624 | fprintf(output, shift); |
| 625 | fprintf(output, "ENTITY_REF(%s)\n", node->name); |
| 626 | break; |
| 627 | case XML_ENTITY_NODE: |
| 628 | fprintf(output, shift); |
| 629 | fprintf(output, "ENTITY\n"); |
| 630 | break; |
| 631 | case XML_PI_NODE: |
| 632 | fprintf(output, shift); |
| 633 | fprintf(output, "PI %s\n", node->name); |
| 634 | break; |
| 635 | case XML_COMMENT_NODE: |
| 636 | fprintf(output, shift); |
| 637 | fprintf(output, "COMMENT\n"); |
| 638 | break; |
| 639 | case XML_DOCUMENT_NODE: |
| 640 | case XML_HTML_DOCUMENT_NODE: |
| 641 | fprintf(output, shift); |
| 642 | fprintf(output, "Error, DOCUMENT found here\n"); |
| 643 | break; |
| 644 | case XML_DOCUMENT_TYPE_NODE: |
| 645 | fprintf(output, shift); |
| 646 | fprintf(output, "DOCUMENT_TYPE\n"); |
| 647 | break; |
| 648 | case XML_DOCUMENT_FRAG_NODE: |
| 649 | fprintf(output, shift); |
| 650 | fprintf(output, "DOCUMENT_FRAG\n"); |
| 651 | break; |
| 652 | case XML_NOTATION_NODE: |
| 653 | fprintf(output, shift); |
| 654 | fprintf(output, "NOTATION\n"); |
| 655 | break; |
| 656 | case XML_DTD_NODE: |
| 657 | xmlDebugDumpDtdNode(output, (xmlDtdPtr) node, depth); |
| 658 | return; |
| 659 | case XML_ELEMENT_DECL: |
| 660 | xmlDebugDumpElemDecl(output, (xmlElementPtr) node, depth); |
| 661 | return; |
| 662 | case XML_ATTRIBUTE_DECL: |
| 663 | xmlDebugDumpAttrDecl(output, (xmlAttributePtr) node, depth); |
| 664 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 665 | case XML_ENTITY_DECL: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 666 | xmlDebugDumpEntityDecl(output, (xmlEntityPtr) node, depth); |
| 667 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 668 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 669 | xmlDebugDumpNamespace(output, (xmlNsPtr) node, depth); |
| 670 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 671 | case XML_XINCLUDE_START: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 672 | fprintf(output, shift); |
| 673 | fprintf(output, "INCLUDE START\n"); |
| 674 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 675 | case XML_XINCLUDE_END: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 676 | fprintf(output, shift); |
| 677 | fprintf(output, "INCLUDE END\n"); |
| 678 | return; |
| 679 | default: |
| 680 | fprintf(output, shift); |
| 681 | fprintf(output, "NODE_%d !!!\n", node->type); |
| 682 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 683 | } |
| 684 | if (node->doc == NULL) { |
| 685 | fprintf(output, shift); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 686 | fprintf(output, "doc == NULL !!!\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 687 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 688 | if (node->nsDef != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 689 | xmlDebugDumpNamespaceList(output, node->nsDef, depth + 1); |
| 690 | if (node->properties != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 691 | xmlDebugDumpAttrList(output, node->properties, depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 692 | if (node->type != XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 693 | if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) { |
| 694 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 695 | shift[2 * i + 2] = shift[2 * i + 3] = 0; |
| 696 | fprintf(output, shift); |
| 697 | fprintf(output, "content="); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 698 | xmlDebugDumpString(output, node->content); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 699 | fprintf(output, "\n"); |
| 700 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 701 | } else { |
| 702 | xmlEntityPtr ent; |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 703 | |
| 704 | ent = xmlGetDocEntity(node->doc, node->name); |
| 705 | if (ent != NULL) |
| 706 | xmlDebugDumpEntity(output, ent, depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 707 | } |
| 708 | /* |
| 709 | * Do a bit of checking |
| 710 | */ |
| 711 | if (node->parent == NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 712 | fprintf(output, "PBM: Node has no parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 713 | if (node->doc == NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 714 | fprintf(output, "PBM: Node has no doc\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 715 | if ((node->parent != NULL) && (node->doc != node->parent->doc)) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 716 | fprintf(output, "PBM: Node doc differs from parent's one\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 717 | if (node->prev == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 718 | if ((node->parent != NULL) && (node->parent->children != node)) |
| 719 | fprintf(output, |
| 720 | "PBM: Node has no prev and not first of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 721 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 722 | if (node->prev->next != node) |
| 723 | fprintf(output, "PBM: Node prev->next : back link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 724 | } |
| 725 | if (node->next == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 726 | if ((node->parent != NULL) && (node->parent->last != node)) |
| 727 | fprintf(output, |
| 728 | "PBM: Node has no next and not last of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 729 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 730 | if (node->next->prev != node) |
| 731 | fprintf(output, "PBM: Node next->prev : forward link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 735 | /** |
| 736 | * xmlDebugDumpNode: |
| 737 | * @output: the FILE * for the output |
| 738 | * @node: the node |
| 739 | * @depth: the indentation level. |
| 740 | * |
| 741 | * Dumps debug information for the element node, it is recursive |
| 742 | */ |
| 743 | void |
| 744 | xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth) |
| 745 | { |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 746 | if (output == NULL) |
| 747 | output = stdout; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 748 | if (node == NULL) { |
| 749 | int i; |
| 750 | char shift[100]; |
| 751 | |
| 752 | for (i = 0; ((i < depth) && (i < 25)); i++) |
| 753 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 754 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 755 | |
| 756 | fprintf(output, shift); |
| 757 | fprintf(output, "node is NULL\n"); |
| 758 | return; |
| 759 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 760 | xmlDebugDumpOneNode(output, node, depth); |
| 761 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 762 | xmlDebugDumpNodeList(output, node->children, depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 765 | /** |
| 766 | * xmlDebugDumpNodeList: |
| 767 | * @output: the FILE * for the output |
| 768 | * @node: the node list |
| 769 | * @depth: the indentation level. |
| 770 | * |
| 771 | * Dumps debug information for the list of element node, it is recursive |
| 772 | */ |
| 773 | void |
| 774 | xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth) |
| 775 | { |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 776 | if (output == NULL) |
| 777 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 778 | while (node != NULL) { |
| 779 | xmlDebugDumpNode(output, node, depth); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 780 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 785 | /** |
| 786 | * xmlDebugDumpDocumentHead: |
| 787 | * @output: the FILE * for the output |
| 788 | * @doc: the document |
| 789 | * |
| 790 | * Dumps debug information cncerning the document, not recursive |
| 791 | */ |
| 792 | void |
| 793 | xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc) |
| 794 | { |
| 795 | if (output == NULL) |
| 796 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 797 | if (doc == NULL) { |
| 798 | fprintf(output, "DOCUMENT == NULL !\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 799 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | switch (doc->type) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 803 | case XML_ELEMENT_NODE: |
| 804 | fprintf(output, "Error, ELEMENT found here "); |
| 805 | break; |
| 806 | case XML_ATTRIBUTE_NODE: |
| 807 | fprintf(output, "Error, ATTRIBUTE found here\n"); |
| 808 | break; |
| 809 | case XML_TEXT_NODE: |
| 810 | fprintf(output, "Error, TEXT\n"); |
| 811 | break; |
| 812 | case XML_CDATA_SECTION_NODE: |
| 813 | fprintf(output, "Error, CDATA_SECTION\n"); |
| 814 | break; |
| 815 | case XML_ENTITY_REF_NODE: |
| 816 | fprintf(output, "Error, ENTITY_REF\n"); |
| 817 | break; |
| 818 | case XML_ENTITY_NODE: |
| 819 | fprintf(output, "Error, ENTITY\n"); |
| 820 | break; |
| 821 | case XML_PI_NODE: |
| 822 | fprintf(output, "Error, PI\n"); |
| 823 | break; |
| 824 | case XML_COMMENT_NODE: |
| 825 | fprintf(output, "Error, COMMENT\n"); |
| 826 | break; |
| 827 | case XML_DOCUMENT_NODE: |
| 828 | fprintf(output, "DOCUMENT\n"); |
| 829 | break; |
| 830 | case XML_HTML_DOCUMENT_NODE: |
| 831 | fprintf(output, "HTML DOCUMENT\n"); |
| 832 | break; |
| 833 | case XML_DOCUMENT_TYPE_NODE: |
| 834 | fprintf(output, "Error, DOCUMENT_TYPE\n"); |
| 835 | break; |
| 836 | case XML_DOCUMENT_FRAG_NODE: |
| 837 | fprintf(output, "Error, DOCUMENT_FRAG\n"); |
| 838 | break; |
| 839 | case XML_NOTATION_NODE: |
| 840 | fprintf(output, "Error, NOTATION\n"); |
| 841 | break; |
| 842 | default: |
| 843 | fprintf(output, "NODE_%d\n", doc->type); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 844 | } |
| 845 | if (doc->name != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 846 | fprintf(output, "name="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 847 | xmlDebugDumpString(output, BAD_CAST doc->name); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 848 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 849 | } |
| 850 | if (doc->version != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 851 | fprintf(output, "version="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 852 | xmlDebugDumpString(output, doc->version); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 853 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 854 | } |
| 855 | if (doc->encoding != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 856 | fprintf(output, "encoding="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 857 | xmlDebugDumpString(output, doc->encoding); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 858 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 859 | } |
| 860 | if (doc->URL != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 861 | fprintf(output, "URL="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 862 | xmlDebugDumpString(output, doc->URL); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 863 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 864 | } |
| 865 | if (doc->standalone) |
| 866 | fprintf(output, "standalone=true\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 867 | if (doc->oldNs != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 868 | xmlDebugDumpNamespaceList(output, doc->oldNs, 0); |
| 869 | } |
| 870 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 871 | /** |
| 872 | * xmlDebugDumpDocument: |
| 873 | * @output: the FILE * for the output |
| 874 | * @doc: the document |
| 875 | * |
| 876 | * Dumps debug information for the document, it's recursive |
| 877 | */ |
| 878 | void |
| 879 | xmlDebugDumpDocument(FILE * output, xmlDocPtr doc) |
| 880 | { |
| 881 | if (output == NULL) |
| 882 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 883 | if (doc == NULL) { |
| 884 | fprintf(output, "DOCUMENT == NULL !\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 885 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 886 | } |
| 887 | xmlDebugDumpDocumentHead(output, doc); |
| 888 | if (((doc->type == XML_DOCUMENT_NODE) || |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 889 | (doc->type == XML_HTML_DOCUMENT_NODE)) && (doc->children != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 890 | xmlDebugDumpNodeList(output, doc->children, 1); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 891 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 892 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 893 | /** |
| 894 | * xmlDebugDumpDTD: |
| 895 | * @output: the FILE * for the output |
| 896 | * @dtd: the DTD |
| 897 | * |
| 898 | * Dumps debug information for the DTD |
| 899 | */ |
| 900 | void |
| 901 | xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd) |
| 902 | { |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 903 | if (output == NULL) |
| 904 | output = stdout; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 905 | if (dtd == NULL) { |
| 906 | fprintf(output, "DTD is NULL\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 907 | return; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 908 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 909 | if (dtd->type != XML_DTD_NODE) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 910 | fprintf(output, "PBM: not a DTD\n"); |
| 911 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 912 | } |
| 913 | if (dtd->name != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 914 | fprintf(output, "DTD(%s)", dtd->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 915 | else |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 916 | fprintf(output, "DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | if (dtd->ExternalID != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 918 | fprintf(output, ", PUBLIC %s", dtd->ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 919 | if (dtd->SystemID != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 920 | fprintf(output, ", SYSTEM %s", dtd->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 921 | fprintf(output, "\n"); |
| 922 | /* |
| 923 | * Do a bit of checking |
| 924 | */ |
| 925 | if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc)) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 926 | fprintf(output, "PBM: DTD doc differs from parent's one\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 927 | if (dtd->prev == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 928 | if ((dtd->parent != NULL) |
| 929 | && (dtd->parent->children != (xmlNodePtr) dtd)) |
| 930 | fprintf(output, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 931 | "PBM: DTD has no prev and not first of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 932 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 933 | if (dtd->prev->next != (xmlNodePtr) dtd) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 934 | fprintf(output, "PBM: DTD prev->next : back link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | } |
| 936 | if (dtd->next == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 937 | if ((dtd->parent != NULL) |
| 938 | && (dtd->parent->last != (xmlNodePtr) dtd)) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 939 | fprintf(output, "PBM: DTD has no next and not last of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 940 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 941 | if (dtd->next->prev != (xmlNodePtr) dtd) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 942 | fprintf(output, "PBM: DTD next->prev : forward link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 943 | } |
| 944 | if (dtd->children == NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 945 | fprintf(output, " DTD is empty\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 946 | else |
| 947 | xmlDebugDumpNodeList(output, dtd->children, 1); |
| 948 | } |
| 949 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 950 | static void |
| 951 | xmlDebugDumpEntityCallback(xmlEntityPtr cur, FILE *output) { |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 952 | if (cur == NULL) { |
| 953 | fprintf(output, "Entity is NULL"); |
| 954 | return; |
| 955 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 956 | fprintf(output, "%s : ", cur->name); |
| 957 | switch (cur->etype) { |
| 958 | case XML_INTERNAL_GENERAL_ENTITY: |
| 959 | fprintf(output, "INTERNAL GENERAL, "); |
| 960 | break; |
| 961 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 962 | fprintf(output, "EXTERNAL PARSED, "); |
| 963 | break; |
| 964 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 965 | fprintf(output, "EXTERNAL UNPARSED, "); |
| 966 | break; |
| 967 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 968 | fprintf(output, "INTERNAL PARAMETER, "); |
| 969 | break; |
| 970 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 971 | fprintf(output, "EXTERNAL PARAMETER, "); |
| 972 | break; |
| 973 | default: |
| 974 | fprintf(output, "UNKNOWN TYPE %d", |
| 975 | cur->etype); |
| 976 | } |
| 977 | if (cur->ExternalID != NULL) |
| 978 | fprintf(output, "ID \"%s\"", cur->ExternalID); |
| 979 | if (cur->SystemID != NULL) |
| 980 | fprintf(output, "SYSTEM \"%s\"", cur->SystemID); |
| 981 | if (cur->orig != NULL) |
| 982 | fprintf(output, "\n orig \"%s\"", cur->orig); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 983 | if ((cur->type != XML_ELEMENT_NODE) && |
| 984 | (cur->content != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 985 | fprintf(output, "\n content \"%s\"", cur->content); |
| 986 | fprintf(output, "\n"); |
| 987 | } |
| 988 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 989 | /** |
| 990 | * xmlDebugDumpEntities: |
| 991 | * @output: the FILE * for the output |
| 992 | * @doc: the document |
| 993 | * |
| 994 | * Dumps debug information for all the entities in use by the document |
| 995 | */ |
| 996 | void |
| 997 | xmlDebugDumpEntities(FILE * output, xmlDocPtr doc) |
| 998 | { |
| 999 | if (output == NULL) |
| 1000 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1001 | if (doc == NULL) { |
| 1002 | fprintf(output, "DOCUMENT == NULL !\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1003 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | switch (doc->type) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1007 | case XML_ELEMENT_NODE: |
| 1008 | fprintf(output, "Error, ELEMENT found here "); |
| 1009 | break; |
| 1010 | case XML_ATTRIBUTE_NODE: |
| 1011 | fprintf(output, "Error, ATTRIBUTE found here\n"); |
| 1012 | break; |
| 1013 | case XML_TEXT_NODE: |
| 1014 | fprintf(output, "Error, TEXT\n"); |
| 1015 | break; |
| 1016 | case XML_CDATA_SECTION_NODE: |
| 1017 | fprintf(output, "Error, CDATA_SECTION\n"); |
| 1018 | break; |
| 1019 | case XML_ENTITY_REF_NODE: |
| 1020 | fprintf(output, "Error, ENTITY_REF\n"); |
| 1021 | break; |
| 1022 | case XML_ENTITY_NODE: |
| 1023 | fprintf(output, "Error, ENTITY\n"); |
| 1024 | break; |
| 1025 | case XML_PI_NODE: |
| 1026 | fprintf(output, "Error, PI\n"); |
| 1027 | break; |
| 1028 | case XML_COMMENT_NODE: |
| 1029 | fprintf(output, "Error, COMMENT\n"); |
| 1030 | break; |
| 1031 | case XML_DOCUMENT_NODE: |
| 1032 | fprintf(output, "DOCUMENT\n"); |
| 1033 | break; |
| 1034 | case XML_HTML_DOCUMENT_NODE: |
| 1035 | fprintf(output, "HTML DOCUMENT\n"); |
| 1036 | break; |
| 1037 | case XML_DOCUMENT_TYPE_NODE: |
| 1038 | fprintf(output, "Error, DOCUMENT_TYPE\n"); |
| 1039 | break; |
| 1040 | case XML_DOCUMENT_FRAG_NODE: |
| 1041 | fprintf(output, "Error, DOCUMENT_FRAG\n"); |
| 1042 | break; |
| 1043 | case XML_NOTATION_NODE: |
| 1044 | fprintf(output, "Error, NOTATION\n"); |
| 1045 | break; |
| 1046 | default: |
| 1047 | fprintf(output, "NODE_%d\n", doc->type); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1048 | } |
| 1049 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1050 | xmlEntitiesTablePtr table = (xmlEntitiesTablePtr) |
| 1051 | doc->intSubset->entities; |
| 1052 | |
| 1053 | fprintf(output, "Entities in internal subset\n"); |
| 1054 | xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback, |
| 1055 | output); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1056 | } else |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1057 | fprintf(output, "No entities in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1058 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1059 | xmlEntitiesTablePtr table = (xmlEntitiesTablePtr) |
| 1060 | doc->extSubset->entities; |
| 1061 | |
| 1062 | fprintf(output, "Entities in external subset\n"); |
| 1063 | xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback, |
| 1064 | output); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1065 | } else |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1066 | fprintf(output, "No entities in external subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1069 | /** |
| 1070 | * xmlLsCountNode: |
| 1071 | * @node: the node to count |
| 1072 | * |
| 1073 | * Count the children of @node. |
| 1074 | * |
| 1075 | * Returns the number of children of @node. |
| 1076 | */ |
| 1077 | int |
| 1078 | xmlLsCountNode(xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1079 | int ret = 0; |
| 1080 | xmlNodePtr list = NULL; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1081 | |
| 1082 | if (node == NULL) |
| 1083 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1084 | |
| 1085 | switch (node->type) { |
| 1086 | case XML_ELEMENT_NODE: |
| 1087 | list = node->children; |
| 1088 | break; |
| 1089 | case XML_DOCUMENT_NODE: |
| 1090 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1091 | #ifdef LIBXML_DOCB_ENABLED |
| 1092 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1093 | #endif |
| 1094 | list = ((xmlDocPtr) node)->children; |
| 1095 | break; |
| 1096 | case XML_ATTRIBUTE_NODE: |
| 1097 | list = ((xmlAttrPtr) node)->children; |
| 1098 | break; |
| 1099 | case XML_TEXT_NODE: |
| 1100 | case XML_CDATA_SECTION_NODE: |
| 1101 | case XML_PI_NODE: |
| 1102 | case XML_COMMENT_NODE: |
| 1103 | if (node->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1104 | ret = xmlStrlen(node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1105 | } |
| 1106 | break; |
| 1107 | case XML_ENTITY_REF_NODE: |
| 1108 | case XML_DOCUMENT_TYPE_NODE: |
| 1109 | case XML_ENTITY_NODE: |
| 1110 | case XML_DOCUMENT_FRAG_NODE: |
| 1111 | case XML_NOTATION_NODE: |
| 1112 | case XML_DTD_NODE: |
| 1113 | case XML_ELEMENT_DECL: |
| 1114 | case XML_ATTRIBUTE_DECL: |
| 1115 | case XML_ENTITY_DECL: |
| 1116 | case XML_NAMESPACE_DECL: |
| 1117 | case XML_XINCLUDE_START: |
| 1118 | case XML_XINCLUDE_END: |
| 1119 | ret = 1; |
| 1120 | break; |
| 1121 | } |
| 1122 | for (;list != NULL;ret++) |
| 1123 | list = list->next; |
| 1124 | return(ret); |
| 1125 | } |
| 1126 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1127 | /** |
| 1128 | * xmlLsOneNode: |
| 1129 | * @output: the FILE * for the output |
| 1130 | * @node: the node to dump |
| 1131 | * |
| 1132 | * Dump to @output the type and name of @node. |
| 1133 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1134 | void |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1135 | xmlLsOneNode(FILE *output, xmlNodePtr node) { |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1136 | if (node == NULL) { |
| 1137 | fprintf(output, "NULL\n"); |
| 1138 | return; |
| 1139 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1140 | switch (node->type) { |
| 1141 | case XML_ELEMENT_NODE: |
| 1142 | fprintf(output, "-"); |
| 1143 | break; |
| 1144 | case XML_ATTRIBUTE_NODE: |
| 1145 | fprintf(output, "a"); |
| 1146 | break; |
| 1147 | case XML_TEXT_NODE: |
| 1148 | fprintf(output, "t"); |
| 1149 | break; |
| 1150 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 1151 | fprintf(output, "C"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1152 | break; |
| 1153 | case XML_ENTITY_REF_NODE: |
| 1154 | fprintf(output, "e"); |
| 1155 | break; |
| 1156 | case XML_ENTITY_NODE: |
| 1157 | fprintf(output, "E"); |
| 1158 | break; |
| 1159 | case XML_PI_NODE: |
| 1160 | fprintf(output, "p"); |
| 1161 | break; |
| 1162 | case XML_COMMENT_NODE: |
| 1163 | fprintf(output, "c"); |
| 1164 | break; |
| 1165 | case XML_DOCUMENT_NODE: |
| 1166 | fprintf(output, "d"); |
| 1167 | break; |
| 1168 | case XML_HTML_DOCUMENT_NODE: |
| 1169 | fprintf(output, "h"); |
| 1170 | break; |
| 1171 | case XML_DOCUMENT_TYPE_NODE: |
| 1172 | fprintf(output, "T"); |
| 1173 | break; |
| 1174 | case XML_DOCUMENT_FRAG_NODE: |
| 1175 | fprintf(output, "F"); |
| 1176 | break; |
| 1177 | case XML_NOTATION_NODE: |
| 1178 | fprintf(output, "N"); |
| 1179 | break; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1180 | case XML_NAMESPACE_DECL: |
| 1181 | fprintf(output, "n"); |
| 1182 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1183 | default: |
| 1184 | fprintf(output, "?"); |
| 1185 | } |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1186 | if (node->type != XML_NAMESPACE_DECL) { |
| 1187 | if (node->properties != NULL) |
| 1188 | fprintf(output, "a"); |
| 1189 | else |
| 1190 | fprintf(output, "-"); |
| 1191 | if (node->nsDef != NULL) |
| 1192 | fprintf(output, "n"); |
| 1193 | else |
| 1194 | fprintf(output, "-"); |
| 1195 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1196 | |
| 1197 | fprintf(output, " %8d ", xmlLsCountNode(node)); |
| 1198 | |
| 1199 | switch (node->type) { |
| 1200 | case XML_ELEMENT_NODE: |
| 1201 | if (node->name != NULL) |
| 1202 | fprintf(output, "%s", node->name); |
| 1203 | break; |
| 1204 | case XML_ATTRIBUTE_NODE: |
| 1205 | if (node->name != NULL) |
| 1206 | fprintf(output, "%s", node->name); |
| 1207 | break; |
| 1208 | case XML_TEXT_NODE: |
| 1209 | if (node->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1210 | xmlDebugDumpString(output, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1211 | } |
| 1212 | break; |
| 1213 | case XML_CDATA_SECTION_NODE: |
| 1214 | break; |
| 1215 | case XML_ENTITY_REF_NODE: |
| 1216 | if (node->name != NULL) |
| 1217 | fprintf(output, "%s", node->name); |
| 1218 | break; |
| 1219 | case XML_ENTITY_NODE: |
| 1220 | if (node->name != NULL) |
| 1221 | fprintf(output, "%s", node->name); |
| 1222 | break; |
| 1223 | case XML_PI_NODE: |
| 1224 | if (node->name != NULL) |
| 1225 | fprintf(output, "%s", node->name); |
| 1226 | break; |
| 1227 | case XML_COMMENT_NODE: |
| 1228 | break; |
| 1229 | case XML_DOCUMENT_NODE: |
| 1230 | break; |
| 1231 | case XML_HTML_DOCUMENT_NODE: |
| 1232 | break; |
| 1233 | case XML_DOCUMENT_TYPE_NODE: |
| 1234 | break; |
| 1235 | case XML_DOCUMENT_FRAG_NODE: |
| 1236 | break; |
| 1237 | case XML_NOTATION_NODE: |
| 1238 | break; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1239 | case XML_NAMESPACE_DECL: { |
| 1240 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1241 | |
| 1242 | if (ns->prefix == NULL) |
| 1243 | fprintf(output, "default -> %s", ns->href); |
| 1244 | else |
| 1245 | fprintf(output, "%s -> %s", ns->prefix, ns->href); |
| 1246 | break; |
| 1247 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1248 | default: |
| 1249 | if (node->name != NULL) |
| 1250 | fprintf(output, "%s", node->name); |
| 1251 | } |
| 1252 | fprintf(output, "\n"); |
| 1253 | } |
| 1254 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1255 | /** |
| 1256 | * xmlBoolToText: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1257 | * @boolval: a bool to turn into text |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1258 | * |
| 1259 | * Convenient way to turn bool into text |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1260 | * |
| 1261 | * Returns a pointer to either "True" or "False" |
| 1262 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1263 | const char * |
Daniel Veillard | ebd38c5 | 2001-11-01 08:38:12 +0000 | [diff] [blame] | 1264 | xmlBoolToText(int boolval) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1265 | { |
Daniel Veillard | ebd38c5 | 2001-11-01 08:38:12 +0000 | [diff] [blame] | 1266 | if (boolval) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1267 | return("True"); |
| 1268 | else |
| 1269 | return("False"); |
| 1270 | } |
| 1271 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1272 | /**************************************************************** |
| 1273 | * * |
| 1274 | * The XML shell related functions * |
| 1275 | * * |
| 1276 | ****************************************************************/ |
| 1277 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1278 | |
| 1279 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1280 | /* |
| 1281 | * TODO: Improvement/cleanups for the XML shell |
| 1282 | * - allow to shell out an editor on a subpart |
| 1283 | * - cleanup function registrations (with help) and calling |
| 1284 | * - provide registration routines |
| 1285 | */ |
| 1286 | |
| 1287 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1288 | * xmlShellPrintXPathError: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1289 | * @errorType: valid xpath error id |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1290 | * @arg: the argument that cause xpath to fail |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1291 | * |
| 1292 | * Print the xpath error to libxml default error channel |
| 1293 | */ |
| 1294 | void |
| 1295 | xmlShellPrintXPathError(int errorType, const char *arg) |
| 1296 | { |
| 1297 | const char *default_arg = "Result"; |
| 1298 | |
| 1299 | if (!arg) |
| 1300 | arg = default_arg; |
| 1301 | |
| 1302 | switch (errorType) { |
| 1303 | case XPATH_UNDEFINED: |
| 1304 | xmlGenericError(xmlGenericErrorContext, |
| 1305 | "%s: no such node\n", arg); |
| 1306 | break; |
| 1307 | |
| 1308 | case XPATH_BOOLEAN: |
| 1309 | xmlGenericError(xmlGenericErrorContext, |
| 1310 | "%s is a Boolean\n", arg); |
| 1311 | break; |
| 1312 | case XPATH_NUMBER: |
| 1313 | xmlGenericError(xmlGenericErrorContext, |
| 1314 | "%s is a number\n", arg); |
| 1315 | break; |
| 1316 | case XPATH_STRING: |
| 1317 | xmlGenericError(xmlGenericErrorContext, |
| 1318 | "%s is a string\n", arg); |
| 1319 | break; |
| 1320 | case XPATH_POINT: |
| 1321 | xmlGenericError(xmlGenericErrorContext, |
| 1322 | "%s is a point\n", arg); |
| 1323 | break; |
| 1324 | case XPATH_RANGE: |
| 1325 | xmlGenericError(xmlGenericErrorContext, |
| 1326 | "%s is a range\n", arg); |
| 1327 | break; |
| 1328 | case XPATH_LOCATIONSET: |
| 1329 | xmlGenericError(xmlGenericErrorContext, |
| 1330 | "%s is a range\n", arg); |
| 1331 | break; |
| 1332 | case XPATH_USERS: |
| 1333 | xmlGenericError(xmlGenericErrorContext, |
| 1334 | "%s is user-defined\n", arg); |
| 1335 | break; |
| 1336 | case XPATH_XSLT_TREE: |
| 1337 | xmlGenericError(xmlGenericErrorContext, |
| 1338 | "%s is an XSLT value tree\n", arg); |
| 1339 | break; |
| 1340 | } |
| 1341 | xmlGenericError(xmlGenericErrorContext, |
| 1342 | "Try casting the result string function (xpath builtin)\n", |
| 1343 | arg); |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | /** |
| 1348 | * xmlShellPrintNode: |
| 1349 | * @node : a non-null node to print to stdout |
| 1350 | * |
| 1351 | * Print node to stdout |
| 1352 | */ |
| 1353 | void |
| 1354 | xmlShellPrintNode(xmlNodePtr node) |
| 1355 | { |
| 1356 | if (!node) |
| 1357 | return; |
| 1358 | |
| 1359 | if (node->type == XML_DOCUMENT_NODE) |
| 1360 | xmlDocDump(stdout, (xmlDocPtr) node); |
| 1361 | else if (node->type == XML_ATTRIBUTE_NODE) |
| 1362 | xmlDebugDumpAttrList(stdout, (xmlAttrPtr) node, 0); |
| 1363 | else |
| 1364 | xmlElemDump(stdout, node->doc, node); |
| 1365 | |
| 1366 | fprintf(stdout, "\n"); |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | /** |
| 1371 | * xmlShellPrintXPathResult: |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1372 | * @list: a valid result generated by an xpath evaluation |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1373 | * |
| 1374 | * Prints result to stdout |
| 1375 | */ |
| 1376 | void |
| 1377 | xmlShellPrintXPathResult(xmlXPathObjectPtr list) |
| 1378 | { |
| 1379 | int i = 0; |
| 1380 | |
| 1381 | if (list != NULL) { |
| 1382 | switch (list->type) { |
| 1383 | case XPATH_NODESET:{ |
| 1384 | int indx; |
| 1385 | |
| 1386 | if (list->nodesetval) { |
| 1387 | for (indx = 0; indx < list->nodesetval->nodeNr; |
| 1388 | indx++) { |
| 1389 | if (i > 0) |
| 1390 | fprintf(stderr, " -------\n"); |
| 1391 | xmlShellPrintNode(list->nodesetval-> |
| 1392 | nodeTab[indx]); |
| 1393 | } |
| 1394 | } else { |
| 1395 | xmlGenericError(xmlGenericErrorContext, |
| 1396 | "Empty node set\n"); |
| 1397 | } |
| 1398 | break; |
| 1399 | } |
| 1400 | case XPATH_BOOLEAN: |
| 1401 | xmlGenericError(xmlGenericErrorContext, |
| 1402 | "Is a Boolean:%s\n", |
| 1403 | xmlBoolToText(list->boolval)); |
| 1404 | break; |
| 1405 | case XPATH_NUMBER: |
| 1406 | xmlGenericError(xmlGenericErrorContext, |
| 1407 | "Is a number:%0g\n", list->floatval); |
| 1408 | break; |
| 1409 | case XPATH_STRING: |
| 1410 | xmlGenericError(xmlGenericErrorContext, |
| 1411 | "Is a string:%s\n", list->stringval); |
| 1412 | break; |
| 1413 | |
| 1414 | default: |
| 1415 | xmlShellPrintXPathError(list->type, NULL); |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1421 | * xmlShellList: |
| 1422 | * @ctxt: the shell context |
| 1423 | * @arg: unused |
| 1424 | * @node: a node |
| 1425 | * @node2: unused |
| 1426 | * |
| 1427 | * Implements the XML shell function "ls" |
| 1428 | * Does an Unix like listing of the given node (like a directory) |
| 1429 | * |
| 1430 | * Returns 0 |
| 1431 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1432 | int |
| 1433 | xmlShellList(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1434 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1435 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1436 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1437 | xmlNodePtr cur; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1438 | if (node == NULL) { |
| 1439 | fprintf(stdout, "NULL\n"); |
| 1440 | return (0); |
| 1441 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1442 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1443 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 1444 | cur = ((xmlDocPtr) node)->children; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1445 | } else if (node->type == XML_NAMESPACE_DECL) { |
| 1446 | xmlLsOneNode(stdout, node); |
| 1447 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1448 | } else if (node->children != NULL) { |
| 1449 | cur = node->children; |
| 1450 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1451 | xmlLsOneNode(stdout, node); |
| 1452 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1453 | } |
| 1454 | while (cur != NULL) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1455 | xmlLsOneNode(stdout, cur); |
| 1456 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1457 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1458 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | /** |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1462 | * xmlShellBase: |
| 1463 | * @ctxt: the shell context |
| 1464 | * @arg: unused |
| 1465 | * @node: a node |
| 1466 | * @node2: unused |
| 1467 | * |
| 1468 | * Implements the XML shell function "base" |
| 1469 | * dumps the current XML base of the node |
| 1470 | * |
| 1471 | * Returns 0 |
| 1472 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1473 | int |
| 1474 | xmlShellBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1475 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1476 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1477 | { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1478 | xmlChar *base; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1479 | if (node == NULL) { |
| 1480 | fprintf(stdout, "NULL\n"); |
| 1481 | return (0); |
| 1482 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1483 | |
| 1484 | base = xmlNodeGetBase(node->doc, node); |
| 1485 | |
| 1486 | if (base == NULL) { |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 1487 | fprintf(stdout, " No base found !!!\n"); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1488 | } else { |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 1489 | fprintf(stdout, "%s\n", base); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1490 | xmlFree(base); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1491 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1492 | return (0); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /** |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 1496 | * xmlShellSetBase: |
| 1497 | * @ctxt: the shell context |
| 1498 | * @arg: the new base |
| 1499 | * @node: a node |
| 1500 | * @node2: unused |
| 1501 | * |
| 1502 | * Implements the XML shell function "setbase" |
| 1503 | * change the current XML base of the node |
| 1504 | * |
| 1505 | * Returns 0 |
| 1506 | */ |
| 1507 | static int |
| 1508 | xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1509 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1510 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1511 | { |
| 1512 | xmlNodeSetBase(node, (xmlChar*) arg); |
| 1513 | return (0); |
| 1514 | } |
| 1515 | |
| 1516 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1517 | * xmlShellDir: |
| 1518 | * @ctxt: the shell context |
| 1519 | * @arg: unused |
| 1520 | * @node: a node |
| 1521 | * @node2: unused |
| 1522 | * |
| 1523 | * Implements the XML shell function "dir" |
| 1524 | * dumps informations about the node (namespace, attributes, content). |
| 1525 | * |
| 1526 | * Returns 0 |
| 1527 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1528 | int |
| 1529 | xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1530 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1531 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1532 | { |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1533 | if (node == NULL) { |
| 1534 | fprintf(stdout, "NULL\n"); |
| 1535 | return (0); |
| 1536 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1537 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1538 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1539 | xmlDebugDumpDocumentHead(stdout, (xmlDocPtr) node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1541 | xmlDebugDumpAttr(stdout, (xmlAttrPtr) node, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1542 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1543 | xmlDebugDumpOneNode(stdout, node, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1544 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1545 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | /** |
| 1549 | * xmlShellCat: |
| 1550 | * @ctxt: the shell context |
| 1551 | * @arg: unused |
| 1552 | * @node: a node |
| 1553 | * @node2: unused |
| 1554 | * |
| 1555 | * Implements the XML shell function "cat" |
| 1556 | * dumps the serialization node content (XML or HTML). |
| 1557 | * |
| 1558 | * Returns 0 |
| 1559 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1560 | int |
| 1561 | xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED, |
| 1562 | xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1563 | { |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1564 | if (node == NULL) { |
| 1565 | fprintf(stdout, "NULL\n"); |
| 1566 | return (0); |
| 1567 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1568 | if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) { |
| 1569 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1570 | if (node->type == XML_HTML_DOCUMENT_NODE) |
| 1571 | htmlDocDump(stdout, (htmlDocPtr) node); |
| 1572 | else |
| 1573 | htmlNodeDumpFile(stdout, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1574 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1575 | if (node->type == XML_DOCUMENT_NODE) |
| 1576 | xmlDocDump(stdout, (xmlDocPtr) node); |
| 1577 | else |
| 1578 | xmlElemDump(stdout, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1579 | #endif /* LIBXML_HTML_ENABLED */ |
| 1580 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1581 | if (node->type == XML_DOCUMENT_NODE) |
| 1582 | xmlDocDump(stdout, (xmlDocPtr) node); |
| 1583 | else |
| 1584 | xmlElemDump(stdout, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1585 | } |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 1586 | fprintf(stdout, "\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1587 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /** |
| 1591 | * xmlShellLoad: |
| 1592 | * @ctxt: the shell context |
| 1593 | * @filename: the file name |
| 1594 | * @node: unused |
| 1595 | * @node2: unused |
| 1596 | * |
| 1597 | * Implements the XML shell function "load" |
| 1598 | * loads a new document specified by the filename |
| 1599 | * |
| 1600 | * Returns 0 or -1 if loading failed |
| 1601 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1602 | int |
| 1603 | xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename, |
| 1604 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 1605 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1606 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1607 | xmlDocPtr doc; |
| 1608 | int html = 0; |
| 1609 | |
| 1610 | if (ctxt->doc != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1611 | html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1612 | |
| 1613 | if (html) { |
| 1614 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1615 | doc = htmlParseFile(filename, NULL); |
| 1616 | #else |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 1617 | fprintf(stdout, "HTML support not compiled in\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1618 | doc = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1619 | #endif /* LIBXML_HTML_ENABLED */ |
| 1620 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1621 | doc = xmlParseFile(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1622 | } |
| 1623 | if (doc != NULL) { |
| 1624 | if (ctxt->loaded == 1) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1625 | xmlFreeDoc(ctxt->doc); |
| 1626 | } |
| 1627 | ctxt->loaded = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1628 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1629 | xmlXPathFreeContext(ctxt->pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1630 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1631 | xmlFree(ctxt->filename); |
| 1632 | ctxt->doc = doc; |
| 1633 | ctxt->node = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1634 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1635 | ctxt->pctxt = xmlXPathNewContext(doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1636 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1637 | ctxt->filename = (char *) xmlStrdup((xmlChar *) filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1638 | } else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1639 | return (-1); |
| 1640 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /** |
| 1644 | * xmlShellWrite: |
| 1645 | * @ctxt: the shell context |
| 1646 | * @filename: the file name |
| 1647 | * @node: a node in the tree |
| 1648 | * @node2: unused |
| 1649 | * |
| 1650 | * Implements the XML shell function "write" |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1651 | * Write the current node to the filename, it saves the serialization |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1652 | * of the subtree under the @node specified |
| 1653 | * |
| 1654 | * Returns 0 or -1 in case of error |
| 1655 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1656 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1657 | xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1658 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1659 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1660 | if (node == NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1661 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1662 | if ((filename == NULL) || (filename[0] == 0)) { |
| 1663 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1664 | "Write command requires a filename argument\n"); |
| 1665 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1666 | } |
| 1667 | #ifdef W_OK |
| 1668 | if (access((char *) filename, W_OK)) { |
| 1669 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1670 | "Cannot write to %s\n", filename); |
| 1671 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1672 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1673 | #endif |
| 1674 | switch (node->type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1675 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1676 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 1677 | xmlGenericError(xmlGenericErrorContext, |
| 1678 | "Failed to write to %s\n", filename); |
| 1679 | return (-1); |
| 1680 | } |
| 1681 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1682 | case XML_HTML_DOCUMENT_NODE: |
| 1683 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1684 | if (htmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1685 | xmlGenericError(xmlGenericErrorContext, |
| 1686 | "Failed to write to %s\n", filename); |
| 1687 | return (-1); |
| 1688 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1689 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1690 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 1691 | xmlGenericError(xmlGenericErrorContext, |
| 1692 | "Failed to write to %s\n", filename); |
| 1693 | return (-1); |
| 1694 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1695 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | default:{ |
| 1698 | FILE *f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1699 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1700 | f = fopen((char *) filename, "w"); |
| 1701 | if (f == NULL) { |
| 1702 | xmlGenericError(xmlGenericErrorContext, |
| 1703 | "Failed to write to %s\n", filename); |
| 1704 | return (-1); |
| 1705 | } |
| 1706 | xmlElemDump(f, ctxt->doc, node); |
| 1707 | fclose(f); |
| 1708 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1709 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1710 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | /** |
| 1714 | * xmlShellSave: |
| 1715 | * @ctxt: the shell context |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1716 | * @filename: the file name (optional) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1717 | * @node: unused |
| 1718 | * @node2: unused |
| 1719 | * |
| 1720 | * Implements the XML shell function "save" |
| 1721 | * Write the current document to the filename, or it's original name |
| 1722 | * |
| 1723 | * Returns 0 or -1 in case of error |
| 1724 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1725 | int |
| 1726 | xmlShellSave(xmlShellCtxtPtr ctxt, char *filename, |
| 1727 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 1728 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1729 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1730 | if (ctxt->doc == NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1731 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1732 | if ((filename == NULL) || (filename[0] == 0)) |
| 1733 | filename = ctxt->filename; |
| 1734 | #ifdef W_OK |
| 1735 | if (access((char *) filename, W_OK)) { |
| 1736 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1737 | "Cannot save to %s\n", filename); |
| 1738 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1739 | } |
| 1740 | #endif |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1741 | switch (ctxt->doc->type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1742 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1743 | if (xmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1744 | xmlGenericError(xmlGenericErrorContext, |
| 1745 | "Failed to save to %s\n", filename); |
| 1746 | } |
| 1747 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1748 | case XML_HTML_DOCUMENT_NODE: |
| 1749 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1750 | if (htmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1751 | xmlGenericError(xmlGenericErrorContext, |
| 1752 | "Failed to save to %s\n", filename); |
| 1753 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1754 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1755 | if (xmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1756 | xmlGenericError(xmlGenericErrorContext, |
| 1757 | "Failed to save to %s\n", filename); |
| 1758 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1759 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1760 | break; |
| 1761 | default: |
| 1762 | xmlGenericError(xmlGenericErrorContext, |
| 1763 | "To save to subparts of a document use the 'write' command\n"); |
| 1764 | return (-1); |
| 1765 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1766 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1767 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /** |
| 1771 | * xmlShellValidate: |
| 1772 | * @ctxt: the shell context |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1773 | * @dtd: the DTD URI (optional) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1774 | * @node: unused |
| 1775 | * @node2: unused |
| 1776 | * |
| 1777 | * Implements the XML shell function "validate" |
| 1778 | * Validate the document, if a DTD path is provided, then the validation |
| 1779 | * is done against the given DTD. |
| 1780 | * |
| 1781 | * Returns 0 or -1 in case of error |
| 1782 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1783 | int |
| 1784 | xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd, |
| 1785 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 1786 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1787 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1788 | xmlValidCtxt vctxt; |
| 1789 | int res = -1; |
| 1790 | |
| 1791 | vctxt.userData = stderr; |
| 1792 | vctxt.error = (xmlValidityErrorFunc) fprintf; |
| 1793 | vctxt.warning = (xmlValidityWarningFunc) fprintf; |
| 1794 | |
| 1795 | if ((dtd == NULL) || (dtd[0] == 0)) { |
| 1796 | res = xmlValidateDocument(&vctxt, ctxt->doc); |
| 1797 | } else { |
| 1798 | xmlDtdPtr subset; |
| 1799 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1800 | subset = xmlParseDTD(NULL, (xmlChar *) dtd); |
| 1801 | if (subset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1802 | res = xmlValidateDtd(&vctxt, ctxt->doc, subset); |
| 1803 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1804 | xmlFreeDtd(subset); |
| 1805 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1806 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1807 | return (res); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | /** |
| 1811 | * xmlShellDu: |
| 1812 | * @ctxt: the shell context |
| 1813 | * @arg: unused |
| 1814 | * @tree: a node defining a subtree |
| 1815 | * @node2: unused |
| 1816 | * |
| 1817 | * Implements the XML shell function "du" |
| 1818 | * show the structure of the subtree under node @tree |
| 1819 | * If @tree is null, the command works on the current node. |
| 1820 | * |
| 1821 | * Returns 0 or -1 in case of error |
| 1822 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1823 | int |
| 1824 | xmlShellDu(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1825 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree, |
| 1826 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1827 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1828 | xmlNodePtr node; |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1829 | int indent = 0, i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1830 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1831 | if (tree == NULL) |
| 1832 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1833 | node = tree; |
| 1834 | while (node != NULL) { |
| 1835 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1836 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 1837 | fprintf(stdout, "/\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1838 | } else if (node->type == XML_ELEMENT_NODE) { |
| 1839 | for (i = 0; i < indent; i++) |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 1840 | fprintf(stdout, " "); |
| 1841 | fprintf(stdout, "%s\n", node->name); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1842 | } else { |
| 1843 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1844 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1845 | /* |
| 1846 | * Browse the full subtree, deep first |
| 1847 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1848 | |
| 1849 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1850 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1851 | node = ((xmlDocPtr) node)->children; |
| 1852 | } else if ((node->children != NULL) |
| 1853 | && (node->type != XML_ENTITY_REF_NODE)) { |
| 1854 | /* deep first */ |
| 1855 | node = node->children; |
| 1856 | indent++; |
| 1857 | } else if ((node != tree) && (node->next != NULL)) { |
| 1858 | /* then siblings */ |
| 1859 | node = node->next; |
| 1860 | } else if (node != tree) { |
| 1861 | /* go up to parents->next if needed */ |
| 1862 | while (node != tree) { |
| 1863 | if (node->parent != NULL) { |
| 1864 | node = node->parent; |
| 1865 | indent--; |
| 1866 | } |
| 1867 | if ((node != tree) && (node->next != NULL)) { |
| 1868 | node = node->next; |
| 1869 | break; |
| 1870 | } |
| 1871 | if (node->parent == NULL) { |
| 1872 | node = NULL; |
| 1873 | break; |
| 1874 | } |
| 1875 | if (node == tree) { |
| 1876 | node = NULL; |
| 1877 | break; |
| 1878 | } |
| 1879 | } |
| 1880 | /* exit condition */ |
| 1881 | if (node == tree) |
| 1882 | node = NULL; |
| 1883 | } else |
| 1884 | node = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1885 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1886 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | /** |
| 1890 | * xmlShellPwd: |
| 1891 | * @ctxt: the shell context |
| 1892 | * @buffer: the output buffer |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1893 | * @node: a node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1894 | * @node2: unused |
| 1895 | * |
| 1896 | * Implements the XML shell function "pwd" |
| 1897 | * Show the full path from the root to the node, if needed building |
| 1898 | * thumblers when similar elements exists at a given ancestor level. |
| 1899 | * The output is compatible with XPath commands. |
| 1900 | * |
| 1901 | * Returns 0 or -1 in case of error |
| 1902 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1903 | int |
| 1904 | xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer, |
| 1905 | xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1906 | { |
Daniel Veillard | c6e013a | 2001-11-10 10:08:57 +0000 | [diff] [blame] | 1907 | xmlChar *path; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1908 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1909 | if (node == NULL) |
| 1910 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1911 | |
Daniel Veillard | c6e013a | 2001-11-10 10:08:57 +0000 | [diff] [blame] | 1912 | path = xmlGetNodePath(node); |
| 1913 | if (path == NULL) |
| 1914 | return (-1); |
| 1915 | |
| 1916 | /* |
| 1917 | * This test prevents buffer overflow, because this routine |
| 1918 | * is only called by xmlShell, in which the second argument is |
| 1919 | * 500 chars long. |
| 1920 | * It is a dirty hack before a cleaner solution is found. |
| 1921 | * Documentation should mention that the second argument must |
| 1922 | * be at least 500 chars long, and could be stripped if too long. |
| 1923 | */ |
| 1924 | snprintf(buffer, 499, "%s", path); |
| 1925 | buffer[499] = '0'; |
| 1926 | xmlFree(path); |
| 1927 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1928 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | /** |
| 1932 | * xmlShell |
| 1933 | * @doc: the initial document |
| 1934 | * @filename: the output buffer |
| 1935 | * @input: the line reading function |
| 1936 | * @output: the output FILE* |
| 1937 | * |
| 1938 | * Implements the XML shell |
| 1939 | * This allow to load, validate, view, modify and save a document |
| 1940 | * using a environment similar to a UNIX commandline. |
| 1941 | */ |
| 1942 | void |
| 1943 | xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1944 | FILE * output) |
| 1945 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1946 | char prompt[500] = "/ > "; |
| 1947 | char *cmdline = NULL, *cur; |
| 1948 | int nbargs; |
| 1949 | char command[100]; |
| 1950 | char arg[400]; |
| 1951 | int i; |
| 1952 | xmlShellCtxtPtr ctxt; |
| 1953 | xmlXPathObjectPtr list; |
| 1954 | |
| 1955 | if (doc == NULL) |
| 1956 | return; |
| 1957 | if (filename == NULL) |
| 1958 | return; |
| 1959 | if (input == NULL) |
| 1960 | return; |
| 1961 | if (output == NULL) |
| 1962 | return; |
| 1963 | ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt)); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1964 | if (ctxt == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1965 | return; |
| 1966 | ctxt->loaded = 0; |
| 1967 | ctxt->doc = doc; |
| 1968 | ctxt->input = input; |
| 1969 | ctxt->output = output; |
| 1970 | ctxt->filename = (char *) xmlStrdup((xmlChar *) filename); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1971 | ctxt->node = (xmlNodePtr) ctxt->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1972 | |
| 1973 | #ifdef LIBXML_XPATH_ENABLED |
| 1974 | ctxt->pctxt = xmlXPathNewContext(ctxt->doc); |
| 1975 | if (ctxt->pctxt == NULL) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1976 | xmlFree(ctxt); |
| 1977 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1978 | } |
| 1979 | #endif /* LIBXML_XPATH_ENABLED */ |
| 1980 | while (1) { |
| 1981 | if (ctxt->node == (xmlNodePtr) ctxt->doc) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1982 | sprintf(prompt, "%s > ", "/"); |
| 1983 | else if (ctxt->node->name) |
| 1984 | snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1985 | else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1986 | sprintf(prompt, "? > "); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1987 | prompt[sizeof(prompt) - 1] = 0; |
| 1988 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1989 | /* |
| 1990 | * Get a new command line |
| 1991 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1992 | cmdline = ctxt->input(prompt); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1993 | if (cmdline == NULL) |
| 1994 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1995 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1996 | /* |
| 1997 | * Parse the command itself |
| 1998 | */ |
| 1999 | cur = cmdline; |
| 2000 | nbargs = 0; |
| 2001 | while ((*cur == ' ') || (*cur == '\t')) |
| 2002 | cur++; |
| 2003 | i = 0; |
| 2004 | while ((*cur != ' ') && (*cur != '\t') && |
| 2005 | (*cur != '\n') && (*cur != '\r')) { |
| 2006 | if (*cur == 0) |
| 2007 | break; |
| 2008 | command[i++] = *cur++; |
| 2009 | } |
| 2010 | command[i] = 0; |
| 2011 | if (i == 0) |
| 2012 | continue; |
| 2013 | nbargs++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2014 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2015 | /* |
| 2016 | * Parse the argument |
| 2017 | */ |
| 2018 | while ((*cur == ' ') || (*cur == '\t')) |
| 2019 | cur++; |
| 2020 | i = 0; |
| 2021 | while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) { |
| 2022 | if (*cur == 0) |
| 2023 | break; |
| 2024 | arg[i++] = *cur++; |
| 2025 | } |
| 2026 | arg[i] = 0; |
| 2027 | if (i != 0) |
| 2028 | nbargs++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2029 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2030 | /* |
| 2031 | * start interpreting the command |
| 2032 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2033 | if (!strcmp(command, "exit")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2034 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2035 | if (!strcmp(command, "quit")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2036 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2037 | if (!strcmp(command, "bye")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2038 | break; |
Daniel Veillard | 5004f42 | 2001-11-08 13:53:05 +0000 | [diff] [blame] | 2039 | if (!strcmp(command, "help")) { |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 2040 | fprintf(stdout, "\tbase display XML base of the node\n"); |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 2041 | fprintf(stdout, "\tsetbase URI change the XML base of the node\n"); |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 2042 | fprintf(stdout, "\tbye leave shell\n"); |
| 2043 | fprintf(stdout, "\tcat [node] display node or current node\n"); |
| 2044 | fprintf(stdout, "\tcd [path] change directory to path or to root\n"); |
| 2045 | fprintf(stdout, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n"); |
| 2046 | fprintf(stdout, "\tdu [path] show the structure of the subtree under path or the current node\n"); |
| 2047 | fprintf(stdout, "\texit leave shell\n"); |
| 2048 | fprintf(stdout, "\thelp display this help\n"); |
| 2049 | fprintf(stdout, "\tfree display memory usage\n"); |
| 2050 | fprintf(stdout, "\tload [name] load a new document with name\n"); |
| 2051 | fprintf(stdout, "\tls [path] list contents of path or the current directory\n"); |
Daniel Veillard | 2070c48 | 2002-01-22 22:12:19 +0000 | [diff] [blame] | 2052 | #ifdef LIBXML_XPATH_ENABLED |
| 2053 | fprintf(stdout, "\txpath expr evaluate the XPath expression in that context and print the result\n"); |
| 2054 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 2055 | fprintf(stdout, "\tpwd display current working directory\n"); |
| 2056 | fprintf(stdout, "\tquit leave shell\n"); |
| 2057 | fprintf(stdout, "\tsave [name] save this document to name or the original name\n"); |
| 2058 | fprintf(stdout, "\tvalidate check the document for errors\n"); |
| 2059 | fprintf(stdout, "\twrite [name] write the current node to the filename\n"); |
Daniel Veillard | 5004f42 | 2001-11-08 13:53:05 +0000 | [diff] [blame] | 2060 | } else if (!strcmp(command, "validate")) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2061 | xmlShellValidate(ctxt, arg, NULL, NULL); |
| 2062 | } else if (!strcmp(command, "load")) { |
| 2063 | xmlShellLoad(ctxt, arg, NULL, NULL); |
| 2064 | } else if (!strcmp(command, "save")) { |
| 2065 | xmlShellSave(ctxt, arg, NULL, NULL); |
| 2066 | } else if (!strcmp(command, "write")) { |
| 2067 | xmlShellWrite(ctxt, arg, NULL, NULL); |
| 2068 | } else if (!strcmp(command, "free")) { |
| 2069 | if (arg[0] == 0) { |
| 2070 | xmlMemShow(stdout, 0); |
| 2071 | } else { |
| 2072 | int len = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2073 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2074 | sscanf(arg, "%d", &len); |
| 2075 | xmlMemShow(stdout, len); |
| 2076 | } |
| 2077 | } else if (!strcmp(command, "pwd")) { |
| 2078 | char dir[500]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2079 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2080 | if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL)) |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 2081 | fprintf(stdout, "%s\n", dir); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2082 | } else if (!strcmp(command, "du")) { |
| 2083 | xmlShellDu(ctxt, NULL, ctxt->node, NULL); |
| 2084 | } else if (!strcmp(command, "base")) { |
| 2085 | xmlShellBase(ctxt, NULL, ctxt->node, NULL); |
Daniel Veillard | 2070c48 | 2002-01-22 22:12:19 +0000 | [diff] [blame] | 2086 | #ifdef LIBXML_XPATH_ENABLED |
| 2087 | } else if (!strcmp(command, "xpath")) { |
| 2088 | if (arg[0] == 0) { |
| 2089 | xmlGenericError(xmlGenericErrorContext, |
| 2090 | "xpath: expression required\n"); |
| 2091 | } else { |
| 2092 | ctxt->pctxt->node = ctxt->node; |
| 2093 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2094 | xmlXPathDebugDumpObject(stdout, list, 0); |
| 2095 | xmlXPathFreeObject(list); |
| 2096 | } |
| 2097 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 2098 | } else if (!strcmp(command, "setbase")) { |
| 2099 | xmlShellSetBase(ctxt, arg, ctxt->node, NULL); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2100 | } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) { |
| 2101 | int dir = (!strcmp(command, "dir")); |
| 2102 | |
| 2103 | if (arg[0] == 0) { |
| 2104 | if (dir) |
| 2105 | xmlShellDir(ctxt, NULL, ctxt->node, NULL); |
| 2106 | else |
| 2107 | xmlShellList(ctxt, NULL, ctxt->node, NULL); |
| 2108 | } else { |
| 2109 | ctxt->pctxt->node = ctxt->node; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 2110 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2111 | ctxt->pctxt->node = ctxt->node; |
| 2112 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2113 | #else |
| 2114 | list = NULL; |
| 2115 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2116 | if (list != NULL) { |
| 2117 | switch (list->type) { |
| 2118 | case XPATH_UNDEFINED: |
| 2119 | xmlGenericError(xmlGenericErrorContext, |
| 2120 | "%s: no such node\n", arg); |
| 2121 | break; |
| 2122 | case XPATH_NODESET:{ |
| 2123 | int indx; |
| 2124 | |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2125 | if (list->nodesetval == NULL) |
| 2126 | break; |
| 2127 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2128 | for (indx = 0; |
| 2129 | indx < list->nodesetval->nodeNr; |
| 2130 | indx++) { |
| 2131 | if (dir) |
| 2132 | xmlShellDir(ctxt, NULL, |
| 2133 | list->nodesetval-> |
| 2134 | nodeTab[indx], NULL); |
| 2135 | else |
| 2136 | xmlShellList(ctxt, NULL, |
| 2137 | list->nodesetval-> |
| 2138 | nodeTab[indx], NULL); |
| 2139 | } |
| 2140 | break; |
| 2141 | } |
| 2142 | case XPATH_BOOLEAN: |
| 2143 | xmlGenericError(xmlGenericErrorContext, |
| 2144 | "%s is a Boolean\n", arg); |
| 2145 | break; |
| 2146 | case XPATH_NUMBER: |
| 2147 | xmlGenericError(xmlGenericErrorContext, |
| 2148 | "%s is a number\n", arg); |
| 2149 | break; |
| 2150 | case XPATH_STRING: |
| 2151 | xmlGenericError(xmlGenericErrorContext, |
| 2152 | "%s is a string\n", arg); |
| 2153 | break; |
| 2154 | case XPATH_POINT: |
| 2155 | xmlGenericError(xmlGenericErrorContext, |
| 2156 | "%s is a point\n", arg); |
| 2157 | break; |
| 2158 | case XPATH_RANGE: |
| 2159 | xmlGenericError(xmlGenericErrorContext, |
| 2160 | "%s is a range\n", arg); |
| 2161 | break; |
| 2162 | case XPATH_LOCATIONSET: |
| 2163 | xmlGenericError(xmlGenericErrorContext, |
| 2164 | "%s is a range\n", arg); |
| 2165 | break; |
| 2166 | case XPATH_USERS: |
| 2167 | xmlGenericError(xmlGenericErrorContext, |
| 2168 | "%s is user-defined\n", arg); |
| 2169 | break; |
| 2170 | case XPATH_XSLT_TREE: |
| 2171 | xmlGenericError(xmlGenericErrorContext, |
| 2172 | "%s is an XSLT value tree\n", |
| 2173 | arg); |
| 2174 | break; |
| 2175 | } |
| 2176 | #ifdef LIBXML_XPATH_ENABLED |
| 2177 | xmlXPathFreeObject(list); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 2178 | #endif |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2179 | } else { |
| 2180 | xmlGenericError(xmlGenericErrorContext, |
| 2181 | "%s: no such node\n", arg); |
| 2182 | } |
| 2183 | ctxt->pctxt->node = NULL; |
| 2184 | } |
| 2185 | } else if (!strcmp(command, "cd")) { |
| 2186 | if (arg[0] == 0) { |
| 2187 | ctxt->node = (xmlNodePtr) ctxt->doc; |
| 2188 | } else { |
| 2189 | #ifdef LIBXML_XPATH_ENABLED |
| 2190 | ctxt->pctxt->node = ctxt->node; |
| 2191 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2192 | #else |
| 2193 | list = NULL; |
| 2194 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2195 | if (list != NULL) { |
| 2196 | switch (list->type) { |
| 2197 | case XPATH_UNDEFINED: |
| 2198 | xmlGenericError(xmlGenericErrorContext, |
| 2199 | "%s: no such node\n", arg); |
| 2200 | break; |
| 2201 | case XPATH_NODESET: |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2202 | if (list->nodesetval != NULL) { |
| 2203 | if (list->nodesetval->nodeNr == 1) { |
| 2204 | ctxt->node = list->nodesetval->nodeTab[0]; |
| 2205 | } else |
| 2206 | xmlGenericError(xmlGenericErrorContext, |
| 2207 | "%s is a %d Node Set\n", |
| 2208 | arg, |
| 2209 | list->nodesetval->nodeNr); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2210 | } else |
| 2211 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2212 | "%s is an empty Node Set\n", |
| 2213 | arg); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | case XPATH_BOOLEAN: |
| 2216 | xmlGenericError(xmlGenericErrorContext, |
| 2217 | "%s is a Boolean\n", arg); |
| 2218 | break; |
| 2219 | case XPATH_NUMBER: |
| 2220 | xmlGenericError(xmlGenericErrorContext, |
| 2221 | "%s is a number\n", arg); |
| 2222 | break; |
| 2223 | case XPATH_STRING: |
| 2224 | xmlGenericError(xmlGenericErrorContext, |
| 2225 | "%s is a string\n", arg); |
| 2226 | break; |
| 2227 | case XPATH_POINT: |
| 2228 | xmlGenericError(xmlGenericErrorContext, |
| 2229 | "%s is a point\n", arg); |
| 2230 | break; |
| 2231 | case XPATH_RANGE: |
| 2232 | xmlGenericError(xmlGenericErrorContext, |
| 2233 | "%s is a range\n", arg); |
| 2234 | break; |
| 2235 | case XPATH_LOCATIONSET: |
| 2236 | xmlGenericError(xmlGenericErrorContext, |
| 2237 | "%s is a range\n", arg); |
| 2238 | break; |
| 2239 | case XPATH_USERS: |
| 2240 | xmlGenericError(xmlGenericErrorContext, |
| 2241 | "%s is user-defined\n", arg); |
| 2242 | break; |
| 2243 | case XPATH_XSLT_TREE: |
| 2244 | xmlGenericError(xmlGenericErrorContext, |
| 2245 | "%s is an XSLT value tree\n", |
| 2246 | arg); |
| 2247 | break; |
| 2248 | } |
| 2249 | #ifdef LIBXML_XPATH_ENABLED |
| 2250 | xmlXPathFreeObject(list); |
| 2251 | #endif |
| 2252 | } else { |
| 2253 | xmlGenericError(xmlGenericErrorContext, |
| 2254 | "%s: no such node\n", arg); |
| 2255 | } |
| 2256 | ctxt->pctxt->node = NULL; |
| 2257 | } |
| 2258 | } else if (!strcmp(command, "cat")) { |
| 2259 | if (arg[0] == 0) { |
| 2260 | xmlShellCat(ctxt, NULL, ctxt->node, NULL); |
| 2261 | } else { |
| 2262 | ctxt->pctxt->node = ctxt->node; |
| 2263 | #ifdef LIBXML_XPATH_ENABLED |
| 2264 | ctxt->pctxt->node = ctxt->node; |
| 2265 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2266 | #else |
| 2267 | list = NULL; |
| 2268 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2269 | if (list != NULL) { |
| 2270 | switch (list->type) { |
| 2271 | case XPATH_UNDEFINED: |
| 2272 | xmlGenericError(xmlGenericErrorContext, |
| 2273 | "%s: no such node\n", arg); |
| 2274 | break; |
| 2275 | case XPATH_NODESET:{ |
| 2276 | int indx; |
| 2277 | |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2278 | if (list->nodesetval == NULL) |
| 2279 | break; |
| 2280 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2281 | for (indx = 0; |
| 2282 | indx < list->nodesetval->nodeNr; |
| 2283 | indx++) { |
| 2284 | if (i > 0) |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 2285 | fprintf(stdout, " -------\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2286 | xmlShellCat(ctxt, NULL, |
| 2287 | list->nodesetval-> |
| 2288 | nodeTab[indx], NULL); |
| 2289 | } |
| 2290 | break; |
| 2291 | } |
| 2292 | case XPATH_BOOLEAN: |
| 2293 | xmlGenericError(xmlGenericErrorContext, |
| 2294 | "%s is a Boolean\n", arg); |
| 2295 | break; |
| 2296 | case XPATH_NUMBER: |
| 2297 | xmlGenericError(xmlGenericErrorContext, |
| 2298 | "%s is a number\n", arg); |
| 2299 | break; |
| 2300 | case XPATH_STRING: |
| 2301 | xmlGenericError(xmlGenericErrorContext, |
| 2302 | "%s is a string\n", arg); |
| 2303 | break; |
| 2304 | case XPATH_POINT: |
| 2305 | xmlGenericError(xmlGenericErrorContext, |
| 2306 | "%s is a point\n", arg); |
| 2307 | break; |
| 2308 | case XPATH_RANGE: |
| 2309 | xmlGenericError(xmlGenericErrorContext, |
| 2310 | "%s is a range\n", arg); |
| 2311 | break; |
| 2312 | case XPATH_LOCATIONSET: |
| 2313 | xmlGenericError(xmlGenericErrorContext, |
| 2314 | "%s is a range\n", arg); |
| 2315 | break; |
| 2316 | case XPATH_USERS: |
| 2317 | xmlGenericError(xmlGenericErrorContext, |
| 2318 | "%s is user-defined\n", arg); |
| 2319 | break; |
| 2320 | case XPATH_XSLT_TREE: |
| 2321 | xmlGenericError(xmlGenericErrorContext, |
| 2322 | "%s is an XSLT value tree\n", |
| 2323 | arg); |
| 2324 | break; |
| 2325 | } |
| 2326 | #ifdef LIBXML_XPATH_ENABLED |
| 2327 | xmlXPathFreeObject(list); |
| 2328 | #endif |
| 2329 | } else { |
| 2330 | xmlGenericError(xmlGenericErrorContext, |
| 2331 | "%s: no such node\n", arg); |
| 2332 | } |
| 2333 | ctxt->pctxt->node = NULL; |
| 2334 | } |
| 2335 | } else { |
| 2336 | xmlGenericError(xmlGenericErrorContext, |
| 2337 | "Unknown command %s\n", command); |
| 2338 | } |
| 2339 | free(cmdline); /* not xmlFree here ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2340 | } |
| 2341 | #ifdef LIBXML_XPATH_ENABLED |
| 2342 | xmlXPathFreeContext(ctxt->pctxt); |
| 2343 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2344 | if (ctxt->loaded) { |
| 2345 | xmlFreeDoc(ctxt->doc); |
| 2346 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 2347 | if (ctxt->filename != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2348 | xmlFree(ctxt->filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2349 | xmlFree(ctxt); |
| 2350 | if (cmdline != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2351 | free(cmdline); /* not xmlFree here ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | #endif /* LIBXML_DEBUG_ENABLED */ |