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 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 10 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 11 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | #ifdef LIBXML_DEBUG_ENABLED |
| 13 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | #ifdef HAVE_STDLIB_H |
| 16 | #include <stdlib.h> |
| 17 | #endif |
| 18 | #ifdef HAVE_STRING_H |
| 19 | #include <string.h> |
| 20 | #endif |
| 21 | #include <libxml/xmlmemory.h> |
| 22 | #include <libxml/tree.h> |
| 23 | #include <libxml/parser.h> |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 24 | #include <libxml/parserInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 25 | #include <libxml/valid.h> |
| 26 | #include <libxml/debugXML.h> |
| 27 | #include <libxml/HTMLtree.h> |
| 28 | #include <libxml/HTMLparser.h> |
| 29 | #include <libxml/xmlerror.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 30 | #include <libxml/globals.h> |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 31 | #include <libxml/xpathInternals.h> |
Igor Zlatkovic | c06bb62 | 2003-04-27 15:59:00 +0000 | [diff] [blame] | 32 | #include <libxml/uri.h> |
Daniel Veillard | 522bc60 | 2004-02-21 11:53:09 +0000 | [diff] [blame] | 33 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 34 | #include <libxml/relaxng.h> |
| 35 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 36 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 37 | typedef struct _xmlDebugCtxt xmlDebugCtxt; |
| 38 | typedef xmlDebugCtxt *xmlDebugCtxtPtr; |
| 39 | struct _xmlDebugCtxt { |
| 40 | FILE *output; /* the output file */ |
| 41 | char shift[101]; /* used for indenting */ |
| 42 | int depth; /* current depth */ |
| 43 | xmlDocPtr doc; /* current document */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 44 | xmlNodePtr node; /* current node */ |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 45 | int check; /* do just checkings */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 46 | int errors; /* number of errors found */ |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node); |
| 50 | |
| 51 | static void |
| 52 | xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | ctxt->depth = 0; |
| 57 | ctxt->check = 0; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 58 | ctxt->errors = 0; |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 59 | ctxt->output = stdout; |
| 60 | for (i = 0; i < 100; i++) |
| 61 | ctxt->shift[i] = ' '; |
| 62 | ctxt->shift[100] = 0; |
| 63 | } |
| 64 | |
| 65 | static void |
William M. Brack | 9638d4c | 2004-10-15 18:25:33 +0000 | [diff] [blame] | 66 | xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED) |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 67 | { |
William M. Brack | 9638d4c | 2004-10-15 18:25:33 +0000 | [diff] [blame] | 68 | /* remove the ATTRIBUTE_UNUSED when this is added */ |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 72 | * xmlNsCheckScope: |
| 73 | * @node: the node |
| 74 | * @ns: the namespace node |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 75 | * |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 76 | * Check that a given namespace is in scope on a node. |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 77 | * |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 78 | * Returns 1 if in scope, -1 in case of argument error, |
| 79 | * -2 if the namespace is not in scope, and -3 if not on |
| 80 | * an ancestor node. |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 81 | */ |
| 82 | static int |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 83 | xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns) |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 84 | { |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 85 | xmlNsPtr cur; |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 86 | |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 87 | if ((node == NULL) || (ns == NULL)) |
| 88 | return(-1); |
| 89 | |
| 90 | if ((node->type != XML_ELEMENT_NODE) && |
| 91 | (node->type != XML_ATTRIBUTE_NODE) && |
| 92 | (node->type != XML_DOCUMENT_NODE) && |
| 93 | (node->type != XML_TEXT_NODE) && |
| 94 | (node->type != XML_HTML_DOCUMENT_NODE) && |
| 95 | (node->type != XML_XINCLUDE_START)) |
| 96 | return(-2); |
| 97 | |
| 98 | while ((node != NULL) && |
| 99 | ((node->type == XML_ELEMENT_NODE) || |
| 100 | (node->type == XML_ATTRIBUTE_NODE) || |
| 101 | (node->type == XML_TEXT_NODE) || |
| 102 | (node->type == XML_XINCLUDE_START))) { |
| 103 | if ((node->type == XML_ELEMENT_NODE) || |
| 104 | (node->type == XML_XINCLUDE_START)) { |
| 105 | cur = node->nsDef; |
| 106 | while (cur != NULL) { |
| 107 | if (cur == ns) |
| 108 | return(1); |
| 109 | if (xmlStrEqual(cur->prefix, ns->prefix)) |
| 110 | return(-2); |
| 111 | cur = cur->next; |
| 112 | } |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 113 | } |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 114 | node = node->parent; |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 115 | } |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 116 | /* the xml namespace may be declared on the document node */ |
| 117 | if ((node != NULL) && |
| 118 | ((node->type == XML_DOCUMENT_NODE) || |
| 119 | (node->type == XML_HTML_DOCUMENT_NODE))) { |
| 120 | xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs; |
| 121 | if (oldNs == ns) |
| 122 | return(1); |
| 123 | } |
| 124 | return(-3); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 125 | } |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 126 | |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 127 | static void |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 128 | xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt) |
| 129 | { |
| 130 | if (ctxt->check) |
| 131 | return; |
| 132 | if ((ctxt->output != NULL) && (ctxt->depth > 0)) { |
| 133 | if (ctxt->depth < 50) |
| 134 | fprintf(ctxt->output, &ctxt->shift[100 - 2 * ctxt->depth]); |
| 135 | else |
| 136 | fprintf(ctxt->output, ctxt->shift); |
| 137 | } |
| 138 | } |
| 139 | |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 140 | /** |
| 141 | * xmlDebugErr: |
| 142 | * @ctxt: a debug context |
| 143 | * @error: the error code |
| 144 | * |
| 145 | * Handle a debug error. |
| 146 | */ |
| 147 | static void |
| 148 | xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg) |
| 149 | { |
| 150 | ctxt->errors++; |
| 151 | __xmlRaiseError(NULL, NULL, NULL, |
| 152 | NULL, ctxt->node, XML_FROM_CHECK, |
| 153 | error, XML_ERR_ERROR, NULL, 0, |
| 154 | NULL, NULL, NULL, 0, 0, |
| 155 | msg); |
| 156 | } |
| 157 | static void |
| 158 | xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra) |
| 159 | { |
| 160 | ctxt->errors++; |
| 161 | __xmlRaiseError(NULL, NULL, NULL, |
| 162 | NULL, ctxt->node, XML_FROM_CHECK, |
| 163 | error, XML_ERR_ERROR, NULL, 0, |
| 164 | NULL, NULL, NULL, 0, 0, |
| 165 | msg, extra); |
| 166 | } |
| 167 | static void |
Daniel Veillard | c609578 | 2004-10-15 14:50:10 +0000 | [diff] [blame] | 168 | xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra) |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 169 | { |
| 170 | ctxt->errors++; |
| 171 | __xmlRaiseError(NULL, NULL, NULL, |
| 172 | NULL, ctxt->node, XML_FROM_CHECK, |
| 173 | error, XML_ERR_ERROR, NULL, 0, |
| 174 | NULL, NULL, NULL, 0, 0, |
| 175 | msg, extra); |
| 176 | } |
| 177 | |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 178 | /** |
| 179 | * xmlCtxtNsCheckScope: |
| 180 | * @ctxt: the debugging context |
| 181 | * @node: the node |
| 182 | * @ns: the namespace node |
| 183 | * |
| 184 | * Report if a given namespace is is not in scope. |
| 185 | */ |
| 186 | static void |
| 187 | xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns) |
| 188 | { |
| 189 | int ret; |
| 190 | |
| 191 | ret = xmlNsCheckScope(node, ns); |
| 192 | if (ret == -2) { |
| 193 | if (ns->prefix == NULL) |
| 194 | xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE, |
| 195 | "Reference to default namespace not in scope\n"); |
| 196 | else |
| 197 | xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE, |
| 198 | "Reference to namespace '%s' not in scope\n", |
| 199 | (char *) ns->prefix); |
| 200 | } |
| 201 | if (ret == -3) { |
| 202 | if (ns->prefix == NULL) |
| 203 | xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR, |
| 204 | "Reference to default namespace not on ancestor\n"); |
| 205 | else |
| 206 | xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR, |
| 207 | "Reference to namespace '%s' not on ancestor\n", |
| 208 | (char *) ns->prefix); |
| 209 | } |
| 210 | } |
| 211 | |
Daniel Veillard | c609578 | 2004-10-15 14:50:10 +0000 | [diff] [blame] | 212 | /** |
| 213 | * xmlCtxtCheckString: |
| 214 | * @ctxt: the debug context |
| 215 | * @str: the string |
| 216 | * |
| 217 | * Do debugging on the string, currently it just checks the UTF-8 content |
| 218 | */ |
| 219 | static void |
| 220 | xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str) |
| 221 | { |
| 222 | if (str == NULL) return; |
| 223 | if (ctxt->check) { |
| 224 | if (!xmlCheckUTF8(str)) { |
| 225 | xmlDebugErr3(ctxt, XML_CHECK_NOT_DTD, |
| 226 | "String is not UTF-8 %s", (const char *) str); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 231 | static void |
| 232 | xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) { |
| 233 | if (node->parent == NULL) |
| 234 | xmlDebugErr(ctxt, XML_CHECK_NO_PARENT, |
| 235 | "Node has no parent\n"); |
| 236 | if (node->doc == NULL) |
| 237 | xmlDebugErr(ctxt, XML_CHECK_NO_DOC, |
| 238 | "Node has no doc\n"); |
| 239 | if ((node->parent != NULL) && (node->doc != node->parent->doc) && |
| 240 | (!xmlStrEqual(node->name, BAD_CAST "pseudoroot"))) |
| 241 | xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC, |
| 242 | "Node doc differs from parent's one\n"); |
| 243 | if (node->prev == NULL) { |
| 244 | if (node->type == XML_ATTRIBUTE_NODE) { |
| 245 | if ((node->parent != NULL) && |
| 246 | (node != (xmlNodePtr) node->parent->properties)) |
| 247 | xmlDebugErr(ctxt, XML_CHECK_NO_PREV, |
| 248 | "Attr has no prev and not first of attr list\n"); |
| 249 | |
| 250 | } else if ((node->parent != NULL) && (node->parent->children != node)) |
| 251 | xmlDebugErr(ctxt, XML_CHECK_NO_PREV, |
| 252 | "Node has no prev and not first of parent list\n"); |
| 253 | } else { |
| 254 | if (node->prev->next != node) |
| 255 | xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV, |
| 256 | "Node prev->next : back link wrong\n"); |
| 257 | } |
| 258 | if (node->next == NULL) { |
| 259 | if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) && |
| 260 | (node->parent->last != node)) |
| 261 | xmlDebugErr(ctxt, XML_CHECK_NO_NEXT, |
| 262 | "Node has no next and not last of parent list\n"); |
| 263 | } else { |
| 264 | if (node->next->prev != node) |
| 265 | xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT, |
| 266 | "Node next->prev : forward link wrong\n"); |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 267 | if (node->next->parent != node->parent) |
| 268 | xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT, |
| 269 | "Node next->prev : forward link wrong\n"); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 270 | } |
Daniel Veillard | 0d24b11 | 2004-10-11 12:28:34 +0000 | [diff] [blame] | 271 | if (node->type == XML_ELEMENT_NODE) { |
| 272 | xmlNsPtr ns; |
| 273 | |
| 274 | ns = node->nsDef; |
| 275 | while (ns != NULL) { |
| 276 | xmlCtxtNsCheckScope(ctxt, node, ns); |
| 277 | ns = ns->next; |
| 278 | } |
| 279 | if (node->ns != NULL) |
| 280 | xmlCtxtNsCheckScope(ctxt, node, node->ns); |
| 281 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
| 282 | if (node->ns != NULL) |
| 283 | xmlCtxtNsCheckScope(ctxt, node, node->ns); |
| 284 | } |
| 285 | |
Daniel Veillard | c609578 | 2004-10-15 14:50:10 +0000 | [diff] [blame] | 286 | if ((node->type != XML_ELEMENT_NODE) && |
William M. Brack | 9638d4c | 2004-10-15 18:25:33 +0000 | [diff] [blame] | 287 | (node->type != XML_ATTRIBUTE_NODE) && |
| 288 | (node->type != XML_ATTRIBUTE_DECL) && |
| 289 | (node->type != XML_DTD_NODE) && |
| 290 | (node->type != XML_HTML_DOCUMENT_NODE) && |
| 291 | (node->type != XML_DOCUMENT_NODE)) { |
Daniel Veillard | c609578 | 2004-10-15 14:50:10 +0000 | [diff] [blame] | 292 | if (node->content != NULL) |
William M. Brack | 9638d4c | 2004-10-15 18:25:33 +0000 | [diff] [blame] | 293 | xmlCtxtCheckString(ctxt, (const xmlChar *) node->content); |
Daniel Veillard | c609578 | 2004-10-15 14:50:10 +0000 | [diff] [blame] | 294 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 297 | static void |
| 298 | xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str) |
| 299 | { |
| 300 | int i; |
| 301 | |
Daniel Veillard | c609578 | 2004-10-15 14:50:10 +0000 | [diff] [blame] | 302 | if (ctxt->check) { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 303 | return; |
Daniel Veillard | c609578 | 2004-10-15 14:50:10 +0000 | [diff] [blame] | 304 | } |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 305 | /* TODO: check UTF8 content of the string */ |
| 306 | if (str == NULL) { |
| 307 | fprintf(ctxt->output, "(NULL)"); |
| 308 | return; |
| 309 | } |
| 310 | for (i = 0; i < 40; i++) |
| 311 | if (str[i] == 0) |
| 312 | return; |
| 313 | else if (IS_BLANK_CH(str[i])) |
| 314 | fputc(' ', ctxt->output); |
| 315 | else if (str[i] >= 0x80) |
| 316 | fprintf(ctxt->output, "#%X", str[i]); |
| 317 | else |
| 318 | fputc(str[i], ctxt->output); |
| 319 | fprintf(ctxt->output, "..."); |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd) |
| 324 | { |
| 325 | xmlCtxtDumpSpaces(ctxt); |
| 326 | |
| 327 | if (dtd == NULL) { |
| 328 | if (!ctxt->check) |
| 329 | fprintf(ctxt->output, "DTD node is NULL\n"); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (dtd->type != XML_DTD_NODE) { |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 334 | xmlDebugErr(ctxt, XML_CHECK_NOT_DTD, |
| 335 | "Node is not a DTD"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | if (!ctxt->check) { |
| 339 | if (dtd->name != NULL) |
| 340 | fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name); |
| 341 | else |
| 342 | fprintf(ctxt->output, "DTD"); |
| 343 | if (dtd->ExternalID != NULL) |
| 344 | fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID); |
| 345 | if (dtd->SystemID != NULL) |
| 346 | fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID); |
| 347 | fprintf(ctxt->output, "\n"); |
| 348 | } |
| 349 | /* |
| 350 | * Do a bit of checking |
| 351 | */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 352 | xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static void |
| 356 | xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr) |
| 357 | { |
| 358 | xmlCtxtDumpSpaces(ctxt); |
| 359 | |
| 360 | if (attr == NULL) { |
| 361 | if (!ctxt->check) |
| 362 | fprintf(ctxt->output, "Attribute declaration is NULL\n"); |
| 363 | return; |
| 364 | } |
| 365 | if (attr->type != XML_ATTRIBUTE_DECL) { |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 366 | xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL, |
| 367 | "Node is not an attribute declaration"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | if (attr->name != NULL) { |
| 371 | if (!ctxt->check) |
| 372 | fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name); |
| 373 | } else |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 374 | xmlDebugErr(ctxt, XML_CHECK_NO_NAME, |
| 375 | "Node attribute declaration has no name"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 376 | if (attr->elem != NULL) { |
| 377 | if (!ctxt->check) |
| 378 | fprintf(ctxt->output, " for %s", (char *) attr->elem); |
| 379 | } else |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 380 | xmlDebugErr(ctxt, XML_CHECK_NO_ELEM, |
| 381 | "Node attribute declaration has no element name"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 382 | if (!ctxt->check) { |
| 383 | switch (attr->atype) { |
| 384 | case XML_ATTRIBUTE_CDATA: |
| 385 | fprintf(ctxt->output, " CDATA"); |
| 386 | break; |
| 387 | case XML_ATTRIBUTE_ID: |
| 388 | fprintf(ctxt->output, " ID"); |
| 389 | break; |
| 390 | case XML_ATTRIBUTE_IDREF: |
| 391 | fprintf(ctxt->output, " IDREF"); |
| 392 | break; |
| 393 | case XML_ATTRIBUTE_IDREFS: |
| 394 | fprintf(ctxt->output, " IDREFS"); |
| 395 | break; |
| 396 | case XML_ATTRIBUTE_ENTITY: |
| 397 | fprintf(ctxt->output, " ENTITY"); |
| 398 | break; |
| 399 | case XML_ATTRIBUTE_ENTITIES: |
| 400 | fprintf(ctxt->output, " ENTITIES"); |
| 401 | break; |
| 402 | case XML_ATTRIBUTE_NMTOKEN: |
| 403 | fprintf(ctxt->output, " NMTOKEN"); |
| 404 | break; |
| 405 | case XML_ATTRIBUTE_NMTOKENS: |
| 406 | fprintf(ctxt->output, " NMTOKENS"); |
| 407 | break; |
| 408 | case XML_ATTRIBUTE_ENUMERATION: |
| 409 | fprintf(ctxt->output, " ENUMERATION"); |
| 410 | break; |
| 411 | case XML_ATTRIBUTE_NOTATION: |
| 412 | fprintf(ctxt->output, " NOTATION "); |
| 413 | break; |
| 414 | } |
| 415 | if (attr->tree != NULL) { |
| 416 | int indx; |
| 417 | xmlEnumerationPtr cur = attr->tree; |
| 418 | |
| 419 | for (indx = 0; indx < 5; indx++) { |
| 420 | if (indx != 0) |
| 421 | fprintf(ctxt->output, "|%s", (char *) cur->name); |
| 422 | else |
| 423 | fprintf(ctxt->output, " (%s", (char *) cur->name); |
| 424 | cur = cur->next; |
| 425 | if (cur == NULL) |
| 426 | break; |
| 427 | } |
| 428 | if (cur == NULL) |
| 429 | fprintf(ctxt->output, ")"); |
| 430 | else |
| 431 | fprintf(ctxt->output, "...)"); |
| 432 | } |
| 433 | switch (attr->def) { |
| 434 | case XML_ATTRIBUTE_NONE: |
| 435 | break; |
| 436 | case XML_ATTRIBUTE_REQUIRED: |
| 437 | fprintf(ctxt->output, " REQUIRED"); |
| 438 | break; |
| 439 | case XML_ATTRIBUTE_IMPLIED: |
| 440 | fprintf(ctxt->output, " IMPLIED"); |
| 441 | break; |
| 442 | case XML_ATTRIBUTE_FIXED: |
| 443 | fprintf(ctxt->output, " FIXED"); |
| 444 | break; |
| 445 | } |
| 446 | if (attr->defaultValue != NULL) { |
| 447 | fprintf(ctxt->output, "\""); |
| 448 | xmlCtxtDumpString(ctxt, attr->defaultValue); |
| 449 | fprintf(ctxt->output, "\""); |
| 450 | } |
| 451 | fprintf(ctxt->output, "\n"); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Do a bit of checking |
| 456 | */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 457 | xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static void |
| 461 | xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem) |
| 462 | { |
| 463 | xmlCtxtDumpSpaces(ctxt); |
| 464 | |
| 465 | if (elem == NULL) { |
| 466 | if (!ctxt->check) |
| 467 | fprintf(ctxt->output, "Element declaration is NULL\n"); |
| 468 | return; |
| 469 | } |
| 470 | if (elem->type != XML_ELEMENT_DECL) { |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 471 | xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL, |
| 472 | "Node is not an element declaration"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | if (elem->name != NULL) { |
| 476 | if (!ctxt->check) { |
| 477 | fprintf(ctxt->output, "ELEMDECL("); |
| 478 | xmlCtxtDumpString(ctxt, elem->name); |
| 479 | fprintf(ctxt->output, ")"); |
| 480 | } |
| 481 | } else |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 482 | xmlDebugErr(ctxt, XML_CHECK_NO_NAME, |
| 483 | "Element declaration has no name"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 484 | if (!ctxt->check) { |
| 485 | switch (elem->etype) { |
| 486 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 487 | fprintf(ctxt->output, ", UNDEFINED"); |
| 488 | break; |
| 489 | case XML_ELEMENT_TYPE_EMPTY: |
| 490 | fprintf(ctxt->output, ", EMPTY"); |
| 491 | break; |
| 492 | case XML_ELEMENT_TYPE_ANY: |
| 493 | fprintf(ctxt->output, ", ANY"); |
| 494 | break; |
| 495 | case XML_ELEMENT_TYPE_MIXED: |
| 496 | fprintf(ctxt->output, ", MIXED "); |
| 497 | break; |
| 498 | case XML_ELEMENT_TYPE_ELEMENT: |
| 499 | fprintf(ctxt->output, ", MIXED "); |
| 500 | break; |
| 501 | } |
| 502 | if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) { |
| 503 | char buf[5001]; |
| 504 | |
| 505 | buf[0] = 0; |
| 506 | xmlSnprintfElementContent(buf, 5000, elem->content, 1); |
| 507 | buf[5000] = 0; |
| 508 | fprintf(ctxt->output, "%s", buf); |
| 509 | } |
| 510 | fprintf(ctxt->output, "\n"); |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Do a bit of checking |
| 515 | */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 516 | xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static void |
| 520 | xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent) |
| 521 | { |
| 522 | xmlCtxtDumpSpaces(ctxt); |
| 523 | |
| 524 | if (ent == NULL) { |
| 525 | if (!ctxt->check) |
| 526 | fprintf(ctxt->output, "Entity declaration is NULL\n"); |
| 527 | return; |
| 528 | } |
| 529 | if (ent->type != XML_ENTITY_DECL) { |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 530 | xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL, |
| 531 | "Node is not an entity declaration"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | if (ent->name != NULL) { |
| 535 | if (!ctxt->check) { |
| 536 | fprintf(ctxt->output, "ENTITYDECL("); |
| 537 | xmlCtxtDumpString(ctxt, ent->name); |
| 538 | fprintf(ctxt->output, ")"); |
| 539 | } |
| 540 | } else |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 541 | xmlDebugErr(ctxt, XML_CHECK_NO_NAME, |
| 542 | "Entity declaration has no name"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 543 | if (!ctxt->check) { |
| 544 | switch (ent->etype) { |
| 545 | case XML_INTERNAL_GENERAL_ENTITY: |
| 546 | fprintf(ctxt->output, ", internal\n"); |
| 547 | break; |
| 548 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 549 | fprintf(ctxt->output, ", external parsed\n"); |
| 550 | break; |
| 551 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 552 | fprintf(ctxt->output, ", unparsed\n"); |
| 553 | break; |
| 554 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 555 | fprintf(ctxt->output, ", parameter\n"); |
| 556 | break; |
| 557 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 558 | fprintf(ctxt->output, ", external parameter\n"); |
| 559 | break; |
| 560 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 561 | fprintf(ctxt->output, ", predefined\n"); |
| 562 | break; |
| 563 | } |
| 564 | if (ent->ExternalID) { |
| 565 | xmlCtxtDumpSpaces(ctxt); |
| 566 | fprintf(ctxt->output, " ExternalID=%s\n", |
| 567 | (char *) ent->ExternalID); |
| 568 | } |
| 569 | if (ent->SystemID) { |
| 570 | xmlCtxtDumpSpaces(ctxt); |
| 571 | fprintf(ctxt->output, " SystemID=%s\n", |
| 572 | (char *) ent->SystemID); |
| 573 | } |
| 574 | if (ent->URI != NULL) { |
| 575 | xmlCtxtDumpSpaces(ctxt); |
| 576 | fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI); |
| 577 | } |
| 578 | if (ent->content) { |
| 579 | xmlCtxtDumpSpaces(ctxt); |
| 580 | fprintf(ctxt->output, " content="); |
| 581 | xmlCtxtDumpString(ctxt, ent->content); |
| 582 | fprintf(ctxt->output, "\n"); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Do a bit of checking |
| 588 | */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 589 | xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static void |
| 593 | xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns) |
| 594 | { |
| 595 | xmlCtxtDumpSpaces(ctxt); |
| 596 | |
| 597 | if (ns == NULL) { |
| 598 | if (!ctxt->check) |
| 599 | fprintf(ctxt->output, "namespace node is NULL\n"); |
| 600 | return; |
| 601 | } |
| 602 | if (ns->type != XML_NAMESPACE_DECL) { |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 603 | xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL, |
| 604 | "Node is not a namespace declaration"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | if (ns->href == NULL) { |
| 608 | if (ns->prefix != NULL) |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 609 | xmlDebugErr3(ctxt, XML_CHECK_NO_HREF, |
| 610 | "Incomplete namespace %s href=NULL\n", |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 611 | (char *) ns->prefix); |
| 612 | else |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 613 | xmlDebugErr(ctxt, XML_CHECK_NO_HREF, |
| 614 | "Incomplete default namespace href=NULL\n"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 615 | } else { |
| 616 | if (!ctxt->check) { |
| 617 | if (ns->prefix != NULL) |
| 618 | fprintf(ctxt->output, "namespace %s href=", |
| 619 | (char *) ns->prefix); |
| 620 | else |
| 621 | fprintf(ctxt->output, "default namespace href="); |
| 622 | |
| 623 | xmlCtxtDumpString(ctxt, ns->href); |
| 624 | fprintf(ctxt->output, "\n"); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static void |
| 630 | xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns) |
| 631 | { |
| 632 | while (ns != NULL) { |
| 633 | xmlCtxtDumpNamespace(ctxt, ns); |
| 634 | ns = ns->next; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static void |
| 639 | xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent) |
| 640 | { |
| 641 | xmlCtxtDumpSpaces(ctxt); |
| 642 | |
| 643 | if (ent == NULL) { |
| 644 | if (!ctxt->check) |
| 645 | fprintf(ctxt->output, "Entity is NULL\n"); |
| 646 | return; |
| 647 | } |
| 648 | if (!ctxt->check) { |
| 649 | switch (ent->etype) { |
| 650 | case XML_INTERNAL_GENERAL_ENTITY: |
| 651 | fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY "); |
| 652 | break; |
| 653 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 654 | fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY "); |
| 655 | break; |
| 656 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 657 | fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY "); |
| 658 | break; |
| 659 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 660 | fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY "); |
| 661 | break; |
| 662 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 663 | fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY "); |
| 664 | break; |
| 665 | default: |
| 666 | fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype); |
| 667 | } |
| 668 | fprintf(ctxt->output, "%s\n", ent->name); |
| 669 | if (ent->ExternalID) { |
| 670 | xmlCtxtDumpSpaces(ctxt); |
| 671 | fprintf(ctxt->output, "ExternalID=%s\n", |
| 672 | (char *) ent->ExternalID); |
| 673 | } |
| 674 | if (ent->SystemID) { |
| 675 | xmlCtxtDumpSpaces(ctxt); |
| 676 | fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID); |
| 677 | } |
| 678 | if (ent->URI) { |
| 679 | xmlCtxtDumpSpaces(ctxt); |
| 680 | fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI); |
| 681 | } |
| 682 | if (ent->content) { |
| 683 | xmlCtxtDumpSpaces(ctxt); |
| 684 | fprintf(ctxt->output, "content="); |
| 685 | xmlCtxtDumpString(ctxt, ent->content); |
| 686 | fprintf(ctxt->output, "\n"); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * xmlCtxtDumpAttr: |
| 693 | * @output: the FILE * for the output |
| 694 | * @attr: the attribute |
| 695 | * @depth: the indentation level. |
| 696 | * |
| 697 | * Dumps debug information for the attribute |
| 698 | */ |
| 699 | static void |
| 700 | xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr) |
| 701 | { |
| 702 | xmlCtxtDumpSpaces(ctxt); |
| 703 | |
| 704 | if (attr == NULL) { |
| 705 | if (!ctxt->check) |
| 706 | fprintf(ctxt->output, "Attr is NULL"); |
| 707 | return; |
| 708 | } |
| 709 | if (!ctxt->check) { |
| 710 | fprintf(ctxt->output, "ATTRIBUTE "); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 711 | xmlCtxtDumpString(ctxt, attr->name); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 712 | fprintf(ctxt->output, "\n"); |
| 713 | if (attr->children != NULL) { |
| 714 | ctxt->depth++; |
| 715 | xmlCtxtDumpNodeList(ctxt, attr->children); |
| 716 | ctxt->depth--; |
| 717 | } |
| 718 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 719 | if (attr->name == NULL) |
| 720 | xmlDebugErr(ctxt, XML_CHECK_NO_NAME, |
| 721 | "Attribute has no name"); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 722 | |
| 723 | /* |
| 724 | * Do a bit of checking |
| 725 | */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 726 | xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /** |
| 730 | * xmlCtxtDumpAttrList: |
| 731 | * @output: the FILE * for the output |
| 732 | * @attr: the attribute list |
| 733 | * @depth: the indentation level. |
| 734 | * |
| 735 | * Dumps debug information for the attribute list |
| 736 | */ |
| 737 | static void |
| 738 | xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr) |
| 739 | { |
| 740 | while (attr != NULL) { |
| 741 | xmlCtxtDumpAttr(ctxt, attr); |
| 742 | attr = attr->next; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * xmlCtxtDumpOneNode: |
| 748 | * @output: the FILE * for the output |
| 749 | * @node: the node |
| 750 | * @depth: the indentation level. |
| 751 | * |
| 752 | * Dumps debug information for the element node, it is not recursive |
| 753 | */ |
| 754 | static void |
| 755 | xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node) |
| 756 | { |
| 757 | if (node == NULL) { |
| 758 | if (!ctxt->check) { |
| 759 | xmlCtxtDumpSpaces(ctxt); |
| 760 | fprintf(ctxt->output, "node is NULL\n"); |
| 761 | } |
| 762 | return; |
| 763 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 764 | ctxt->node = node; |
| 765 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 766 | switch (node->type) { |
| 767 | case XML_ELEMENT_NODE: |
| 768 | if (!ctxt->check) { |
| 769 | xmlCtxtDumpSpaces(ctxt); |
| 770 | fprintf(ctxt->output, "ELEMENT "); |
| 771 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 772 | xmlCtxtDumpString(ctxt, node->ns->prefix); |
| 773 | fprintf(ctxt->output, ":"); |
| 774 | } |
| 775 | xmlCtxtDumpString(ctxt, node->name); |
| 776 | fprintf(ctxt->output, "\n"); |
| 777 | } |
| 778 | break; |
| 779 | case XML_ATTRIBUTE_NODE: |
| 780 | if (!ctxt->check) |
| 781 | xmlCtxtDumpSpaces(ctxt); |
| 782 | fprintf(ctxt->output, "Error, ATTRIBUTE found here\n"); |
| 783 | break; |
| 784 | case XML_TEXT_NODE: |
| 785 | if (!ctxt->check) { |
| 786 | xmlCtxtDumpSpaces(ctxt); |
| 787 | if (node->name == (const xmlChar *) xmlStringTextNoenc) |
| 788 | fprintf(ctxt->output, "TEXT no enc\n"); |
| 789 | else |
| 790 | fprintf(ctxt->output, "TEXT\n"); |
| 791 | } |
| 792 | break; |
| 793 | case XML_CDATA_SECTION_NODE: |
| 794 | if (!ctxt->check) { |
| 795 | xmlCtxtDumpSpaces(ctxt); |
| 796 | fprintf(ctxt->output, "CDATA_SECTION\n"); |
| 797 | } |
| 798 | break; |
| 799 | case XML_ENTITY_REF_NODE: |
| 800 | if (!ctxt->check) { |
| 801 | xmlCtxtDumpSpaces(ctxt); |
| 802 | fprintf(ctxt->output, "ENTITY_REF(%s)\n", |
| 803 | (char *) node->name); |
| 804 | } |
| 805 | break; |
| 806 | case XML_ENTITY_NODE: |
| 807 | if (!ctxt->check) { |
| 808 | xmlCtxtDumpSpaces(ctxt); |
| 809 | fprintf(ctxt->output, "ENTITY\n"); |
| 810 | } |
| 811 | break; |
| 812 | case XML_PI_NODE: |
| 813 | if (!ctxt->check) { |
| 814 | xmlCtxtDumpSpaces(ctxt); |
| 815 | fprintf(ctxt->output, "PI %s\n", (char *) node->name); |
| 816 | } |
| 817 | break; |
| 818 | case XML_COMMENT_NODE: |
| 819 | if (!ctxt->check) { |
| 820 | xmlCtxtDumpSpaces(ctxt); |
| 821 | fprintf(ctxt->output, "COMMENT\n"); |
| 822 | } |
| 823 | break; |
| 824 | case XML_DOCUMENT_NODE: |
| 825 | case XML_HTML_DOCUMENT_NODE: |
| 826 | if (!ctxt->check) { |
| 827 | xmlCtxtDumpSpaces(ctxt); |
| 828 | } |
| 829 | fprintf(ctxt->output, "PBM: DOCUMENT found here\n"); |
| 830 | break; |
| 831 | case XML_DOCUMENT_TYPE_NODE: |
| 832 | if (!ctxt->check) { |
| 833 | xmlCtxtDumpSpaces(ctxt); |
| 834 | fprintf(ctxt->output, "DOCUMENT_TYPE\n"); |
| 835 | } |
| 836 | break; |
| 837 | case XML_DOCUMENT_FRAG_NODE: |
| 838 | if (!ctxt->check) { |
| 839 | xmlCtxtDumpSpaces(ctxt); |
| 840 | fprintf(ctxt->output, "DOCUMENT_FRAG\n"); |
| 841 | } |
| 842 | break; |
| 843 | case XML_NOTATION_NODE: |
| 844 | if (!ctxt->check) { |
| 845 | xmlCtxtDumpSpaces(ctxt); |
| 846 | fprintf(ctxt->output, "NOTATION\n"); |
| 847 | } |
| 848 | break; |
| 849 | case XML_DTD_NODE: |
| 850 | xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node); |
| 851 | return; |
| 852 | case XML_ELEMENT_DECL: |
| 853 | xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node); |
| 854 | return; |
| 855 | case XML_ATTRIBUTE_DECL: |
| 856 | xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node); |
| 857 | return; |
| 858 | case XML_ENTITY_DECL: |
| 859 | xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node); |
| 860 | return; |
| 861 | case XML_NAMESPACE_DECL: |
| 862 | xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node); |
| 863 | return; |
| 864 | case XML_XINCLUDE_START: |
| 865 | if (!ctxt->check) { |
| 866 | xmlCtxtDumpSpaces(ctxt); |
| 867 | fprintf(ctxt->output, "INCLUDE START\n"); |
| 868 | } |
| 869 | return; |
| 870 | case XML_XINCLUDE_END: |
| 871 | if (!ctxt->check) { |
| 872 | xmlCtxtDumpSpaces(ctxt); |
| 873 | fprintf(ctxt->output, "INCLUDE END\n"); |
| 874 | } |
| 875 | return; |
| 876 | default: |
| 877 | if (!ctxt->check) |
| 878 | xmlCtxtDumpSpaces(ctxt); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 879 | xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE, |
| 880 | "Unknown node type %d\n", node->type); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | if (node->doc == NULL) { |
| 884 | if (!ctxt->check) { |
| 885 | xmlCtxtDumpSpaces(ctxt); |
| 886 | } |
| 887 | fprintf(ctxt->output, "PBM: doc == NULL !!!\n"); |
| 888 | } |
| 889 | ctxt->depth++; |
| 890 | if (node->nsDef != NULL) |
| 891 | xmlCtxtDumpNamespaceList(ctxt, node->nsDef); |
| 892 | if (node->properties != NULL) |
| 893 | xmlCtxtDumpAttrList(ctxt, node->properties); |
| 894 | if (node->type != XML_ENTITY_REF_NODE) { |
| 895 | if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) { |
| 896 | if (!ctxt->check) { |
| 897 | xmlCtxtDumpSpaces(ctxt); |
| 898 | fprintf(ctxt->output, "content="); |
| 899 | xmlCtxtDumpString(ctxt, node->content); |
| 900 | fprintf(ctxt->output, "\n"); |
| 901 | } |
| 902 | } |
| 903 | } else { |
| 904 | xmlEntityPtr ent; |
| 905 | |
| 906 | ent = xmlGetDocEntity(node->doc, node->name); |
| 907 | if (ent != NULL) |
| 908 | xmlCtxtDumpEntity(ctxt, ent); |
| 909 | } |
| 910 | ctxt->depth--; |
| 911 | |
| 912 | /* |
| 913 | * Do a bit of checking |
| 914 | */ |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 915 | xmlCtxtGenericNodeCheck(ctxt, node); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /** |
| 919 | * xmlCtxtDumpNode: |
| 920 | * @output: the FILE * for the output |
| 921 | * @node: the node |
| 922 | * @depth: the indentation level. |
| 923 | * |
| 924 | * Dumps debug information for the element node, it is recursive |
| 925 | */ |
| 926 | static void |
| 927 | xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node) |
| 928 | { |
| 929 | if (node == NULL) { |
| 930 | if (!ctxt->check) { |
| 931 | xmlCtxtDumpSpaces(ctxt); |
| 932 | fprintf(ctxt->output, "node is NULL\n"); |
| 933 | } |
| 934 | return; |
| 935 | } |
| 936 | xmlCtxtDumpOneNode(ctxt, node); |
| 937 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
| 938 | ctxt->depth++; |
| 939 | xmlCtxtDumpNodeList(ctxt, node->children); |
| 940 | ctxt->depth--; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * xmlCtxtDumpNodeList: |
| 946 | * @output: the FILE * for the output |
| 947 | * @node: the node list |
| 948 | * @depth: the indentation level. |
| 949 | * |
| 950 | * Dumps debug information for the list of element node, it is recursive |
| 951 | */ |
| 952 | static void |
| 953 | xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node) |
| 954 | { |
| 955 | while (node != NULL) { |
| 956 | xmlCtxtDumpNode(ctxt, node); |
| 957 | node = node->next; |
| 958 | } |
| 959 | } |
| 960 | |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 961 | static void |
| 962 | xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc) |
| 963 | { |
| 964 | if (doc == NULL) { |
| 965 | if (!ctxt->check) |
| 966 | fprintf(ctxt->output, "DOCUMENT == NULL !\n"); |
| 967 | return; |
| 968 | } |
| 969 | ctxt->node = (xmlNodePtr) doc; |
| 970 | |
| 971 | switch (doc->type) { |
| 972 | case XML_ELEMENT_NODE: |
| 973 | xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT, |
| 974 | "Misplaced ELEMENT node\n"); |
| 975 | break; |
| 976 | case XML_ATTRIBUTE_NODE: |
| 977 | xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE, |
| 978 | "Misplaced ATTRIBUTE node\n"); |
| 979 | break; |
| 980 | case XML_TEXT_NODE: |
| 981 | xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT, |
| 982 | "Misplaced TEXT node\n"); |
| 983 | break; |
| 984 | case XML_CDATA_SECTION_NODE: |
| 985 | xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA, |
| 986 | "Misplaced CDATA node\n"); |
| 987 | break; |
| 988 | case XML_ENTITY_REF_NODE: |
| 989 | xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF, |
| 990 | "Misplaced ENTITYREF node\n"); |
| 991 | break; |
| 992 | case XML_ENTITY_NODE: |
| 993 | xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY, |
| 994 | "Misplaced ENTITY node\n"); |
| 995 | break; |
| 996 | case XML_PI_NODE: |
| 997 | xmlDebugErr(ctxt, XML_CHECK_FOUND_PI, |
| 998 | "Misplaced PI node\n"); |
| 999 | break; |
| 1000 | case XML_COMMENT_NODE: |
| 1001 | xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT, |
| 1002 | "Misplaced COMMENT node\n"); |
| 1003 | break; |
| 1004 | case XML_DOCUMENT_NODE: |
| 1005 | if (!ctxt->check) |
| 1006 | fprintf(ctxt->output, "DOCUMENT\n"); |
| 1007 | break; |
| 1008 | case XML_HTML_DOCUMENT_NODE: |
| 1009 | if (!ctxt->check) |
| 1010 | fprintf(ctxt->output, "HTML DOCUMENT\n"); |
| 1011 | break; |
| 1012 | case XML_DOCUMENT_TYPE_NODE: |
| 1013 | xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE, |
| 1014 | "Misplaced DOCTYPE node\n"); |
| 1015 | break; |
| 1016 | case XML_DOCUMENT_FRAG_NODE: |
| 1017 | xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT, |
| 1018 | "Misplaced FRAGMENT node\n"); |
| 1019 | break; |
| 1020 | case XML_NOTATION_NODE: |
| 1021 | xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION, |
| 1022 | "Misplaced NOTATION node\n"); |
| 1023 | break; |
| 1024 | default: |
| 1025 | xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE, |
| 1026 | "Unknown node type %d\n", doc->type); |
| 1027 | } |
| 1028 | } |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1029 | |
| 1030 | /** |
| 1031 | * xmlCtxtDumpDocumentHead: |
| 1032 | * @output: the FILE * for the output |
| 1033 | * @doc: the document |
| 1034 | * |
| 1035 | * Dumps debug information cncerning the document, not recursive |
| 1036 | */ |
| 1037 | static void |
| 1038 | xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc) |
| 1039 | { |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1040 | xmlCtxtDumpDocHead(ctxt, doc); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1041 | if (!ctxt->check) { |
| 1042 | if (doc->name != NULL) { |
| 1043 | fprintf(ctxt->output, "name="); |
| 1044 | xmlCtxtDumpString(ctxt, BAD_CAST doc->name); |
| 1045 | fprintf(ctxt->output, "\n"); |
| 1046 | } |
| 1047 | if (doc->version != NULL) { |
| 1048 | fprintf(ctxt->output, "version="); |
| 1049 | xmlCtxtDumpString(ctxt, doc->version); |
| 1050 | fprintf(ctxt->output, "\n"); |
| 1051 | } |
| 1052 | if (doc->encoding != NULL) { |
| 1053 | fprintf(ctxt->output, "encoding="); |
| 1054 | xmlCtxtDumpString(ctxt, doc->encoding); |
| 1055 | fprintf(ctxt->output, "\n"); |
| 1056 | } |
| 1057 | if (doc->URL != NULL) { |
| 1058 | fprintf(ctxt->output, "URL="); |
| 1059 | xmlCtxtDumpString(ctxt, doc->URL); |
| 1060 | fprintf(ctxt->output, "\n"); |
| 1061 | } |
| 1062 | if (doc->standalone) |
| 1063 | fprintf(ctxt->output, "standalone=true\n"); |
| 1064 | } |
| 1065 | if (doc->oldNs != NULL) |
| 1066 | xmlCtxtDumpNamespaceList(ctxt, doc->oldNs); |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * xmlCtxtDumpDocument: |
| 1071 | * @output: the FILE * for the output |
| 1072 | * @doc: the document |
| 1073 | * |
| 1074 | * Dumps debug information for the document, it's recursive |
| 1075 | */ |
| 1076 | static void |
| 1077 | xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc) |
| 1078 | { |
| 1079 | if (doc == NULL) { |
| 1080 | if (!ctxt->check) |
| 1081 | fprintf(ctxt->output, "DOCUMENT == NULL !\n"); |
| 1082 | return; |
| 1083 | } |
| 1084 | xmlCtxtDumpDocumentHead(ctxt, doc); |
| 1085 | if (((doc->type == XML_DOCUMENT_NODE) || |
| 1086 | (doc->type == XML_HTML_DOCUMENT_NODE)) |
| 1087 | && (doc->children != NULL)) { |
| 1088 | ctxt->depth++; |
| 1089 | xmlCtxtDumpNodeList(ctxt, doc->children); |
| 1090 | ctxt->depth--; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | static void |
| 1095 | xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt) |
| 1096 | { |
| 1097 | if (cur == NULL) { |
| 1098 | if (!ctxt->check) |
| 1099 | fprintf(ctxt->output, "Entity is NULL"); |
| 1100 | return; |
| 1101 | } |
| 1102 | if (!ctxt->check) { |
| 1103 | fprintf(ctxt->output, "%s : ", (char *) cur->name); |
| 1104 | switch (cur->etype) { |
| 1105 | case XML_INTERNAL_GENERAL_ENTITY: |
| 1106 | fprintf(ctxt->output, "INTERNAL GENERAL, "); |
| 1107 | break; |
| 1108 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 1109 | fprintf(ctxt->output, "EXTERNAL PARSED, "); |
| 1110 | break; |
| 1111 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 1112 | fprintf(ctxt->output, "EXTERNAL UNPARSED, "); |
| 1113 | break; |
| 1114 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 1115 | fprintf(ctxt->output, "INTERNAL PARAMETER, "); |
| 1116 | break; |
| 1117 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 1118 | fprintf(ctxt->output, "EXTERNAL PARAMETER, "); |
| 1119 | break; |
| 1120 | default: |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1121 | xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE, |
| 1122 | "Unknown entity type %d\n", cur->etype); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1123 | } |
| 1124 | if (cur->ExternalID != NULL) |
| 1125 | fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID); |
| 1126 | if (cur->SystemID != NULL) |
| 1127 | fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID); |
| 1128 | if (cur->orig != NULL) |
| 1129 | fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig); |
| 1130 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) |
| 1131 | fprintf(ctxt->output, "\n content \"%s\"", |
| 1132 | (char *) cur->content); |
| 1133 | fprintf(ctxt->output, "\n"); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * xmlCtxtDumpEntities: |
| 1139 | * @output: the FILE * for the output |
| 1140 | * @doc: the document |
| 1141 | * |
| 1142 | * Dumps debug information for all the entities in use by the document |
| 1143 | */ |
| 1144 | static void |
| 1145 | xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc) |
| 1146 | { |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1147 | xmlCtxtDumpDocHead(ctxt, doc); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1148 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 1149 | xmlEntitiesTablePtr table = (xmlEntitiesTablePtr) |
| 1150 | doc->intSubset->entities; |
| 1151 | |
| 1152 | if (!ctxt->check) |
| 1153 | fprintf(ctxt->output, "Entities in internal subset\n"); |
| 1154 | xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback, |
| 1155 | ctxt); |
| 1156 | } else |
| 1157 | fprintf(ctxt->output, "No entities in internal subset\n"); |
| 1158 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 1159 | xmlEntitiesTablePtr table = (xmlEntitiesTablePtr) |
| 1160 | doc->extSubset->entities; |
| 1161 | |
| 1162 | if (!ctxt->check) |
| 1163 | fprintf(ctxt->output, "Entities in external subset\n"); |
| 1164 | xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback, |
| 1165 | ctxt); |
| 1166 | } else if (!ctxt->check) |
| 1167 | fprintf(ctxt->output, "No entities in external subset\n"); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * xmlCtxtDumpDTD: |
| 1172 | * @output: the FILE * for the output |
| 1173 | * @dtd: the DTD |
| 1174 | * |
| 1175 | * Dumps debug information for the DTD |
| 1176 | */ |
| 1177 | static void |
| 1178 | xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd) |
| 1179 | { |
| 1180 | if (dtd == NULL) { |
| 1181 | if (!ctxt->check) |
| 1182 | fprintf(ctxt->output, "DTD is NULL\n"); |
| 1183 | return; |
| 1184 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1185 | xmlCtxtDumpDtdNode(ctxt, dtd); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1186 | if (dtd->children == NULL) |
| 1187 | fprintf(ctxt->output, " DTD is empty\n"); |
| 1188 | else { |
| 1189 | ctxt->depth++; |
| 1190 | xmlCtxtDumpNodeList(ctxt, dtd->children); |
| 1191 | ctxt->depth--; |
| 1192 | } |
| 1193 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1194 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1195 | /************************************************************************ |
| 1196 | * * |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1197 | * Public entry points for dump * |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1198 | * * |
| 1199 | ************************************************************************/ |
| 1200 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1201 | /** |
| 1202 | * xmlDebugDumpString: |
| 1203 | * @output: the FILE * for the output |
| 1204 | * @str: the string |
| 1205 | * |
| 1206 | * Dumps informations about the string, shorten it if necessary |
| 1207 | */ |
| 1208 | void |
| 1209 | xmlDebugDumpString(FILE * output, const xmlChar * str) |
| 1210 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1211 | int i; |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1212 | |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 1213 | if (output == NULL) |
| 1214 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1215 | if (str == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1216 | fprintf(output, "(NULL)"); |
| 1217 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1218 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1219 | for (i = 0; i < 40; i++) |
| 1220 | if (str[i] == 0) |
| 1221 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1222 | else if (IS_BLANK_CH(str[i])) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1223 | fputc(' ', output); |
| 1224 | else if (str[i] >= 0x80) |
| 1225 | fprintf(output, "#%X", str[i]); |
| 1226 | else |
| 1227 | fputc(str[i], output); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1228 | fprintf(output, "..."); |
| 1229 | } |
| 1230 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1231 | /** |
| 1232 | * xmlDebugDumpAttr: |
| 1233 | * @output: the FILE * for the output |
| 1234 | * @attr: the attribute |
| 1235 | * @depth: the indentation level. |
| 1236 | * |
| 1237 | * Dumps debug information for the attribute |
| 1238 | */ |
| 1239 | void |
| 1240 | xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1241 | xmlDebugCtxt ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1242 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1243 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1244 | ctxt.output = output; |
| 1245 | ctxt.depth = depth; |
| 1246 | xmlCtxtDumpAttr(&ctxt, attr); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1247 | xmlCtxtDumpCleanCtxt(&ctxt); |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1249 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1250 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1251 | /** |
| 1252 | * xmlDebugDumpEntities: |
| 1253 | * @output: the FILE * for the output |
| 1254 | * @doc: the document |
| 1255 | * |
| 1256 | * Dumps debug information for all the entities in use by the document |
| 1257 | */ |
| 1258 | void |
| 1259 | xmlDebugDumpEntities(FILE * output, xmlDocPtr doc) |
| 1260 | { |
| 1261 | xmlDebugCtxt ctxt; |
| 1262 | |
| 1263 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1264 | ctxt.output = output; |
| 1265 | xmlCtxtDumpEntities(&ctxt, doc); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1266 | xmlCtxtDumpCleanCtxt(&ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1269 | /** |
| 1270 | * xmlDebugDumpAttrList: |
| 1271 | * @output: the FILE * for the output |
| 1272 | * @attr: the attribute list |
| 1273 | * @depth: the indentation level. |
| 1274 | * |
| 1275 | * Dumps debug information for the attribute list |
| 1276 | */ |
| 1277 | void |
| 1278 | xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth) |
| 1279 | { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1280 | xmlDebugCtxt ctxt; |
| 1281 | |
| 1282 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1283 | ctxt.output = output; |
| 1284 | ctxt.depth = depth; |
| 1285 | xmlCtxtDumpAttrList(&ctxt, attr); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1286 | xmlCtxtDumpCleanCtxt(&ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1289 | /** |
| 1290 | * xmlDebugDumpOneNode: |
| 1291 | * @output: the FILE * for the output |
| 1292 | * @node: the node |
| 1293 | * @depth: the indentation level. |
| 1294 | * |
| 1295 | * Dumps debug information for the element node, it is not recursive |
| 1296 | */ |
| 1297 | void |
| 1298 | xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth) |
| 1299 | { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1300 | xmlDebugCtxt ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1301 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1302 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1303 | ctxt.output = output; |
| 1304 | ctxt.depth = depth; |
| 1305 | xmlCtxtDumpOneNode(&ctxt, node); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1306 | xmlCtxtDumpCleanCtxt(&ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1309 | /** |
| 1310 | * xmlDebugDumpNode: |
| 1311 | * @output: the FILE * for the output |
| 1312 | * @node: the node |
| 1313 | * @depth: the indentation level. |
| 1314 | * |
| 1315 | * Dumps debug information for the element node, it is recursive |
| 1316 | */ |
| 1317 | void |
| 1318 | xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth) |
| 1319 | { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1320 | xmlDebugCtxt ctxt; |
| 1321 | |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 1322 | if (output == NULL) |
| 1323 | output = stdout; |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1324 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1325 | ctxt.output = output; |
| 1326 | ctxt.depth = depth; |
| 1327 | xmlCtxtDumpNode(&ctxt, node); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1328 | xmlCtxtDumpCleanCtxt(&ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1331 | /** |
| 1332 | * xmlDebugDumpNodeList: |
| 1333 | * @output: the FILE * for the output |
| 1334 | * @node: the node list |
| 1335 | * @depth: the indentation level. |
| 1336 | * |
| 1337 | * Dumps debug information for the list of element node, it is recursive |
| 1338 | */ |
| 1339 | void |
| 1340 | xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth) |
| 1341 | { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1342 | xmlDebugCtxt ctxt; |
| 1343 | |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 1344 | if (output == NULL) |
| 1345 | output = stdout; |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1346 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1347 | ctxt.output = output; |
| 1348 | ctxt.depth = depth; |
| 1349 | xmlCtxtDumpNodeList(&ctxt, node); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1350 | xmlCtxtDumpCleanCtxt(&ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1353 | /** |
| 1354 | * xmlDebugDumpDocumentHead: |
| 1355 | * @output: the FILE * for the output |
| 1356 | * @doc: the document |
| 1357 | * |
| 1358 | * Dumps debug information cncerning the document, not recursive |
| 1359 | */ |
| 1360 | void |
| 1361 | xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc) |
| 1362 | { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1363 | xmlDebugCtxt ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1364 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1365 | if (output == NULL) |
| 1366 | output = stdout; |
| 1367 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1368 | ctxt.output = output; |
| 1369 | xmlCtxtDumpDocumentHead(&ctxt, doc); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1370 | xmlCtxtDumpCleanCtxt(&ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1373 | /** |
| 1374 | * xmlDebugDumpDocument: |
| 1375 | * @output: the FILE * for the output |
| 1376 | * @doc: the document |
| 1377 | * |
| 1378 | * Dumps debug information for the document, it's recursive |
| 1379 | */ |
| 1380 | void |
| 1381 | xmlDebugDumpDocument(FILE * output, xmlDocPtr doc) |
| 1382 | { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1383 | xmlDebugCtxt ctxt; |
| 1384 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1385 | if (output == NULL) |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1386 | output = stdout; |
| 1387 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1388 | ctxt.output = output; |
| 1389 | xmlCtxtDumpDocument(&ctxt, doc); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1390 | xmlCtxtDumpCleanCtxt(&ctxt); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1391 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1392 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1393 | /** |
| 1394 | * xmlDebugDumpDTD: |
| 1395 | * @output: the FILE * for the output |
| 1396 | * @dtd: the DTD |
| 1397 | * |
| 1398 | * Dumps debug information for the DTD |
| 1399 | */ |
| 1400 | void |
| 1401 | xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd) |
| 1402 | { |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1403 | xmlDebugCtxt ctxt; |
| 1404 | |
Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 1405 | if (output == NULL) |
| 1406 | output = stdout; |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1407 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1408 | ctxt.output = output; |
| 1409 | xmlCtxtDumpDTD(&ctxt, dtd); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1410 | xmlCtxtDumpCleanCtxt(&ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1413 | /************************************************************************ |
| 1414 | * * |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1415 | * Public entry points for checkings * |
| 1416 | * * |
| 1417 | ************************************************************************/ |
| 1418 | |
| 1419 | /** |
| 1420 | * xmlDebugCheckDocument: |
| 1421 | * @output: the FILE * for the output |
| 1422 | * @doc: the document |
| 1423 | * |
| 1424 | * Check the document for potential content problems, and output |
| 1425 | * the errors to @output |
| 1426 | * |
| 1427 | * Returns the number of errors found |
| 1428 | */ |
| 1429 | int |
| 1430 | xmlDebugCheckDocument(FILE * output, xmlDocPtr doc) |
| 1431 | { |
| 1432 | xmlDebugCtxt ctxt; |
| 1433 | |
| 1434 | if (output == NULL) |
| 1435 | output = stdout; |
| 1436 | xmlCtxtDumpInitCtxt(&ctxt); |
| 1437 | ctxt.output = output; |
| 1438 | ctxt.check = 1; |
| 1439 | xmlCtxtDumpDocument(&ctxt, doc); |
Daniel Veillard | 7682114 | 2004-10-09 20:39:04 +0000 | [diff] [blame] | 1440 | xmlCtxtDumpCleanCtxt(&ctxt); |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 1441 | return(ctxt.errors); |
| 1442 | } |
| 1443 | |
| 1444 | /************************************************************************ |
| 1445 | * * |
Daniel Veillard | 22cdb84 | 2004-10-04 14:09:17 +0000 | [diff] [blame] | 1446 | * Helpers for Shell * |
| 1447 | * * |
| 1448 | ************************************************************************/ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1449 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1450 | /** |
| 1451 | * xmlLsCountNode: |
| 1452 | * @node: the node to count |
| 1453 | * |
| 1454 | * Count the children of @node. |
| 1455 | * |
| 1456 | * Returns the number of children of @node. |
| 1457 | */ |
| 1458 | int |
| 1459 | xmlLsCountNode(xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1460 | int ret = 0; |
| 1461 | xmlNodePtr list = NULL; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (node == NULL) |
| 1464 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1465 | |
| 1466 | switch (node->type) { |
| 1467 | case XML_ELEMENT_NODE: |
| 1468 | list = node->children; |
| 1469 | break; |
| 1470 | case XML_DOCUMENT_NODE: |
| 1471 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1472 | #ifdef LIBXML_DOCB_ENABLED |
| 1473 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1474 | #endif |
| 1475 | list = ((xmlDocPtr) node)->children; |
| 1476 | break; |
| 1477 | case XML_ATTRIBUTE_NODE: |
| 1478 | list = ((xmlAttrPtr) node)->children; |
| 1479 | break; |
| 1480 | case XML_TEXT_NODE: |
| 1481 | case XML_CDATA_SECTION_NODE: |
| 1482 | case XML_PI_NODE: |
| 1483 | case XML_COMMENT_NODE: |
| 1484 | if (node->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1485 | ret = xmlStrlen(node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1486 | } |
| 1487 | break; |
| 1488 | case XML_ENTITY_REF_NODE: |
| 1489 | case XML_DOCUMENT_TYPE_NODE: |
| 1490 | case XML_ENTITY_NODE: |
| 1491 | case XML_DOCUMENT_FRAG_NODE: |
| 1492 | case XML_NOTATION_NODE: |
| 1493 | case XML_DTD_NODE: |
| 1494 | case XML_ELEMENT_DECL: |
| 1495 | case XML_ATTRIBUTE_DECL: |
| 1496 | case XML_ENTITY_DECL: |
| 1497 | case XML_NAMESPACE_DECL: |
| 1498 | case XML_XINCLUDE_START: |
| 1499 | case XML_XINCLUDE_END: |
| 1500 | ret = 1; |
| 1501 | break; |
| 1502 | } |
| 1503 | for (;list != NULL;ret++) |
| 1504 | list = list->next; |
| 1505 | return(ret); |
| 1506 | } |
| 1507 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1508 | /** |
| 1509 | * xmlLsOneNode: |
| 1510 | * @output: the FILE * for the output |
| 1511 | * @node: the node to dump |
| 1512 | * |
| 1513 | * Dump to @output the type and name of @node. |
| 1514 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1515 | void |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1516 | xmlLsOneNode(FILE *output, xmlNodePtr node) { |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1517 | if (node == NULL) { |
| 1518 | fprintf(output, "NULL\n"); |
| 1519 | return; |
| 1520 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1521 | switch (node->type) { |
| 1522 | case XML_ELEMENT_NODE: |
| 1523 | fprintf(output, "-"); |
| 1524 | break; |
| 1525 | case XML_ATTRIBUTE_NODE: |
| 1526 | fprintf(output, "a"); |
| 1527 | break; |
| 1528 | case XML_TEXT_NODE: |
| 1529 | fprintf(output, "t"); |
| 1530 | break; |
| 1531 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 1532 | fprintf(output, "C"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1533 | break; |
| 1534 | case XML_ENTITY_REF_NODE: |
| 1535 | fprintf(output, "e"); |
| 1536 | break; |
| 1537 | case XML_ENTITY_NODE: |
| 1538 | fprintf(output, "E"); |
| 1539 | break; |
| 1540 | case XML_PI_NODE: |
| 1541 | fprintf(output, "p"); |
| 1542 | break; |
| 1543 | case XML_COMMENT_NODE: |
| 1544 | fprintf(output, "c"); |
| 1545 | break; |
| 1546 | case XML_DOCUMENT_NODE: |
| 1547 | fprintf(output, "d"); |
| 1548 | break; |
| 1549 | case XML_HTML_DOCUMENT_NODE: |
| 1550 | fprintf(output, "h"); |
| 1551 | break; |
| 1552 | case XML_DOCUMENT_TYPE_NODE: |
| 1553 | fprintf(output, "T"); |
| 1554 | break; |
| 1555 | case XML_DOCUMENT_FRAG_NODE: |
| 1556 | fprintf(output, "F"); |
| 1557 | break; |
| 1558 | case XML_NOTATION_NODE: |
| 1559 | fprintf(output, "N"); |
| 1560 | break; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1561 | case XML_NAMESPACE_DECL: |
| 1562 | fprintf(output, "n"); |
| 1563 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1564 | default: |
| 1565 | fprintf(output, "?"); |
| 1566 | } |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1567 | if (node->type != XML_NAMESPACE_DECL) { |
| 1568 | if (node->properties != NULL) |
| 1569 | fprintf(output, "a"); |
| 1570 | else |
| 1571 | fprintf(output, "-"); |
| 1572 | if (node->nsDef != NULL) |
| 1573 | fprintf(output, "n"); |
| 1574 | else |
| 1575 | fprintf(output, "-"); |
| 1576 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1577 | |
| 1578 | fprintf(output, " %8d ", xmlLsCountNode(node)); |
| 1579 | |
| 1580 | switch (node->type) { |
| 1581 | case XML_ELEMENT_NODE: |
| 1582 | if (node->name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1583 | fprintf(output, "%s", (const char *) node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | case XML_ATTRIBUTE_NODE: |
| 1586 | if (node->name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1587 | fprintf(output, "%s", (const char *) node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1588 | break; |
| 1589 | case XML_TEXT_NODE: |
| 1590 | if (node->content != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1591 | xmlDebugDumpString(output, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1592 | } |
| 1593 | break; |
| 1594 | case XML_CDATA_SECTION_NODE: |
| 1595 | break; |
| 1596 | case XML_ENTITY_REF_NODE: |
| 1597 | if (node->name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1598 | fprintf(output, "%s", (const char *) node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1599 | break; |
| 1600 | case XML_ENTITY_NODE: |
| 1601 | if (node->name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1602 | fprintf(output, "%s", (const char *) node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1603 | break; |
| 1604 | case XML_PI_NODE: |
| 1605 | if (node->name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1606 | fprintf(output, "%s", (const char *) node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1607 | break; |
| 1608 | case XML_COMMENT_NODE: |
| 1609 | break; |
| 1610 | case XML_DOCUMENT_NODE: |
| 1611 | break; |
| 1612 | case XML_HTML_DOCUMENT_NODE: |
| 1613 | break; |
| 1614 | case XML_DOCUMENT_TYPE_NODE: |
| 1615 | break; |
| 1616 | case XML_DOCUMENT_FRAG_NODE: |
| 1617 | break; |
| 1618 | case XML_NOTATION_NODE: |
| 1619 | break; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1620 | case XML_NAMESPACE_DECL: { |
| 1621 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1622 | |
| 1623 | if (ns->prefix == NULL) |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 1624 | fprintf(output, "default -> %s", (char *)ns->href); |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1625 | else |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 1626 | fprintf(output, "%s -> %s", (char *)ns->prefix, |
| 1627 | (char *)ns->href); |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1630 | default: |
| 1631 | if (node->name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1632 | fprintf(output, "%s", (const char *) node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1633 | } |
| 1634 | fprintf(output, "\n"); |
| 1635 | } |
| 1636 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1637 | /** |
| 1638 | * xmlBoolToText: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1639 | * @boolval: a bool to turn into text |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1640 | * |
| 1641 | * Convenient way to turn bool into text |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1642 | * |
| 1643 | * Returns a pointer to either "True" or "False" |
| 1644 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1645 | const char * |
Daniel Veillard | ebd38c5 | 2001-11-01 08:38:12 +0000 | [diff] [blame] | 1646 | xmlBoolToText(int boolval) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1647 | { |
Daniel Veillard | ebd38c5 | 2001-11-01 08:38:12 +0000 | [diff] [blame] | 1648 | if (boolval) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1649 | return("True"); |
| 1650 | else |
| 1651 | return("False"); |
| 1652 | } |
| 1653 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1654 | /**************************************************************** |
| 1655 | * * |
| 1656 | * The XML shell related functions * |
| 1657 | * * |
| 1658 | ****************************************************************/ |
| 1659 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1660 | |
| 1661 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1662 | /* |
| 1663 | * TODO: Improvement/cleanups for the XML shell |
| 1664 | * - allow to shell out an editor on a subpart |
| 1665 | * - cleanup function registrations (with help) and calling |
| 1666 | * - provide registration routines |
| 1667 | */ |
| 1668 | |
| 1669 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1670 | * xmlShellPrintXPathError: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1671 | * @errorType: valid xpath error id |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1672 | * @arg: the argument that cause xpath to fail |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1673 | * |
| 1674 | * Print the xpath error to libxml default error channel |
| 1675 | */ |
| 1676 | void |
| 1677 | xmlShellPrintXPathError(int errorType, const char *arg) |
| 1678 | { |
| 1679 | const char *default_arg = "Result"; |
| 1680 | |
| 1681 | if (!arg) |
| 1682 | arg = default_arg; |
| 1683 | |
| 1684 | switch (errorType) { |
| 1685 | case XPATH_UNDEFINED: |
| 1686 | xmlGenericError(xmlGenericErrorContext, |
| 1687 | "%s: no such node\n", arg); |
| 1688 | break; |
| 1689 | |
| 1690 | case XPATH_BOOLEAN: |
| 1691 | xmlGenericError(xmlGenericErrorContext, |
| 1692 | "%s is a Boolean\n", arg); |
| 1693 | break; |
| 1694 | case XPATH_NUMBER: |
| 1695 | xmlGenericError(xmlGenericErrorContext, |
| 1696 | "%s is a number\n", arg); |
| 1697 | break; |
| 1698 | case XPATH_STRING: |
| 1699 | xmlGenericError(xmlGenericErrorContext, |
| 1700 | "%s is a string\n", arg); |
| 1701 | break; |
| 1702 | case XPATH_POINT: |
| 1703 | xmlGenericError(xmlGenericErrorContext, |
| 1704 | "%s is a point\n", arg); |
| 1705 | break; |
| 1706 | case XPATH_RANGE: |
| 1707 | xmlGenericError(xmlGenericErrorContext, |
| 1708 | "%s is a range\n", arg); |
| 1709 | break; |
| 1710 | case XPATH_LOCATIONSET: |
| 1711 | xmlGenericError(xmlGenericErrorContext, |
| 1712 | "%s is a range\n", arg); |
| 1713 | break; |
| 1714 | case XPATH_USERS: |
| 1715 | xmlGenericError(xmlGenericErrorContext, |
| 1716 | "%s is user-defined\n", arg); |
| 1717 | break; |
| 1718 | case XPATH_XSLT_TREE: |
| 1719 | xmlGenericError(xmlGenericErrorContext, |
| 1720 | "%s is an XSLT value tree\n", arg); |
| 1721 | break; |
| 1722 | } |
| 1723 | xmlGenericError(xmlGenericErrorContext, |
| 1724 | "Try casting the result string function (xpath builtin)\n", |
| 1725 | arg); |
| 1726 | } |
| 1727 | |
| 1728 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1729 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1730 | /** |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1731 | * xmlShellPrintNodeCtxt: |
| 1732 | * @ctxt : a non-null shell context |
| 1733 | * @node : a non-null node to print to the output FILE |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1734 | * |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1735 | * Print node to the output FILE |
| 1736 | */ |
| 1737 | static void |
| 1738 | xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node) |
| 1739 | { |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 1740 | FILE *fp; |
| 1741 | |
| 1742 | if (!node) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1743 | return; |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 1744 | if (ctxt == NULL) |
| 1745 | fp = stdout; |
| 1746 | else |
| 1747 | fp = ctxt->output; |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1748 | |
| 1749 | if (node->type == XML_DOCUMENT_NODE) |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 1750 | xmlDocDump(fp, (xmlDocPtr) node); |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1751 | else if (node->type == XML_ATTRIBUTE_NODE) |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 1752 | xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0); |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1753 | else |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 1754 | xmlElemDump(fp, node->doc, node); |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1755 | |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 1756 | fprintf(fp, "\n"); |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * xmlShellPrintNode: |
| 1761 | * @node : a non-null node to print to the output FILE |
| 1762 | * |
| 1763 | * Print node to the output FILE |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1764 | */ |
| 1765 | void |
| 1766 | xmlShellPrintNode(xmlNodePtr node) |
| 1767 | { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1768 | xmlShellPrintNodeCtxt(NULL, node); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1769 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1770 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1771 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1772 | /** |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1773 | * xmlShellPrintXPathResultCtxt: |
| 1774 | * @ctxt: a valid shell context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1775 | * @list: a valid result generated by an xpath evaluation |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1776 | * |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1777 | * Prints result to the output FILE |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1778 | */ |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1779 | static void |
| 1780 | xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1781 | { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1782 | if (!ctxt) |
| 1783 | return; |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1784 | |
| 1785 | if (list != NULL) { |
| 1786 | switch (list->type) { |
| 1787 | case XPATH_NODESET:{ |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1788 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1789 | int indx; |
| 1790 | |
| 1791 | if (list->nodesetval) { |
| 1792 | for (indx = 0; indx < list->nodesetval->nodeNr; |
| 1793 | indx++) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1794 | xmlShellPrintNodeCtxt(ctxt, |
| 1795 | list->nodesetval->nodeTab[indx]); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1796 | } |
| 1797 | } else { |
| 1798 | xmlGenericError(xmlGenericErrorContext, |
| 1799 | "Empty node set\n"); |
| 1800 | } |
| 1801 | break; |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1802 | #else |
| 1803 | xmlGenericError(xmlGenericErrorContext, |
| 1804 | "Node set\n"); |
| 1805 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1806 | } |
| 1807 | case XPATH_BOOLEAN: |
| 1808 | xmlGenericError(xmlGenericErrorContext, |
| 1809 | "Is a Boolean:%s\n", |
| 1810 | xmlBoolToText(list->boolval)); |
| 1811 | break; |
| 1812 | case XPATH_NUMBER: |
| 1813 | xmlGenericError(xmlGenericErrorContext, |
| 1814 | "Is a number:%0g\n", list->floatval); |
| 1815 | break; |
| 1816 | case XPATH_STRING: |
| 1817 | xmlGenericError(xmlGenericErrorContext, |
| 1818 | "Is a string:%s\n", list->stringval); |
| 1819 | break; |
| 1820 | |
| 1821 | default: |
| 1822 | xmlShellPrintXPathError(list->type, NULL); |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /** |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1828 | * xmlShellPrintXPathResult: |
| 1829 | * @list: a valid result generated by an xpath evaluation |
| 1830 | * |
| 1831 | * Prints result to the output FILE |
| 1832 | */ |
| 1833 | void |
| 1834 | xmlShellPrintXPathResult(xmlXPathObjectPtr list) |
| 1835 | { |
| 1836 | xmlShellPrintXPathResultCtxt(NULL, list); |
| 1837 | } |
| 1838 | |
| 1839 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1840 | * xmlShellList: |
| 1841 | * @ctxt: the shell context |
| 1842 | * @arg: unused |
| 1843 | * @node: a node |
| 1844 | * @node2: unused |
| 1845 | * |
| 1846 | * Implements the XML shell function "ls" |
| 1847 | * Does an Unix like listing of the given node (like a directory) |
| 1848 | * |
| 1849 | * Returns 0 |
| 1850 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1851 | int |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1852 | xmlShellList(xmlShellCtxtPtr ctxt, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1853 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1854 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1855 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1856 | xmlNodePtr cur; |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1857 | if (!ctxt) |
| 1858 | return (0); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1859 | if (node == NULL) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1860 | fprintf(ctxt->output, "NULL\n"); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1861 | return (0); |
| 1862 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1863 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1864 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 1865 | cur = ((xmlDocPtr) node)->children; |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1866 | } else if (node->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1867 | xmlLsOneNode(ctxt->output, node); |
Daniel Veillard | e6a5519 | 2002-01-14 17:11:53 +0000 | [diff] [blame] | 1868 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1869 | } else if (node->children != NULL) { |
| 1870 | cur = node->children; |
| 1871 | } else { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1872 | xmlLsOneNode(ctxt->output, node); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1873 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1874 | } |
| 1875 | while (cur != NULL) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1876 | xmlLsOneNode(ctxt->output, cur); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1877 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1878 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1879 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | /** |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1883 | * xmlShellBase: |
| 1884 | * @ctxt: the shell context |
| 1885 | * @arg: unused |
| 1886 | * @node: a node |
| 1887 | * @node2: unused |
| 1888 | * |
| 1889 | * Implements the XML shell function "base" |
| 1890 | * dumps the current XML base of the node |
| 1891 | * |
| 1892 | * Returns 0 |
| 1893 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1894 | int |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1895 | xmlShellBase(xmlShellCtxtPtr ctxt, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1896 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1897 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1898 | { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1899 | xmlChar *base; |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1900 | if (!ctxt) |
| 1901 | return 0; |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1902 | if (node == NULL) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1903 | fprintf(ctxt->output, "NULL\n"); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 1904 | return (0); |
| 1905 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1906 | |
| 1907 | base = xmlNodeGetBase(node->doc, node); |
| 1908 | |
| 1909 | if (base == NULL) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1910 | fprintf(ctxt->output, " No base found !!!\n"); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1911 | } else { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 1912 | fprintf(ctxt->output, "%s\n", base); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1913 | xmlFree(base); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1914 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1915 | return (0); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Daniel Veillard | b34321c | 2004-03-04 17:09:47 +0000 | [diff] [blame] | 1918 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1919 | /** |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 1920 | * xmlShellSetBase: |
| 1921 | * @ctxt: the shell context |
| 1922 | * @arg: the new base |
| 1923 | * @node: a node |
| 1924 | * @node2: unused |
| 1925 | * |
| 1926 | * Implements the XML shell function "setbase" |
| 1927 | * change the current XML base of the node |
| 1928 | * |
| 1929 | * Returns 0 |
| 1930 | */ |
| 1931 | static int |
| 1932 | xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1933 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1934 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1935 | { |
| 1936 | xmlNodeSetBase(node, (xmlChar*) arg); |
| 1937 | return (0); |
| 1938 | } |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 1939 | #endif |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 1940 | |
Daniel Veillard | bbaa997 | 2004-06-16 14:08:33 +0000 | [diff] [blame] | 1941 | #ifdef LIBXML_XPATH_ENABLED |
| 1942 | /** |
| 1943 | * xmlShellRegisterNamespace: |
| 1944 | * @ctxt: the shell context |
| 1945 | * @arg: a string in prefix=nsuri format |
| 1946 | * @node: unused |
| 1947 | * @node2: unused |
| 1948 | * |
| 1949 | * Implements the XML shell function "setns" |
| 1950 | * register/unregister a prefix=namespace pair |
| 1951 | * on the XPath context |
| 1952 | * |
| 1953 | * Returns 0 on success and a negative value otherwise. |
| 1954 | */ |
| 1955 | static int |
| 1956 | xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg, |
| 1957 | xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1958 | { |
| 1959 | xmlChar* nsListDup; |
| 1960 | xmlChar* prefix; |
| 1961 | xmlChar* href; |
| 1962 | xmlChar* next; |
| 1963 | |
| 1964 | nsListDup = xmlStrdup((xmlChar *) arg); |
| 1965 | next = nsListDup; |
| 1966 | while(next != NULL) { |
| 1967 | /* skip spaces */ |
| 1968 | /*while((*next) == ' ') next++;*/ |
| 1969 | if((*next) == '\0') break; |
| 1970 | |
| 1971 | /* find prefix */ |
| 1972 | prefix = next; |
| 1973 | next = (xmlChar*)xmlStrchr(next, '='); |
| 1974 | if(next == NULL) { |
| 1975 | fprintf(ctxt->output, "setns: prefix=[nsuri] required\n"); |
| 1976 | xmlFree(nsListDup); |
| 1977 | return(-1); |
| 1978 | } |
| 1979 | *(next++) = '\0'; |
| 1980 | |
| 1981 | /* find href */ |
| 1982 | href = next; |
| 1983 | next = (xmlChar*)xmlStrchr(next, ' '); |
| 1984 | if(next != NULL) { |
| 1985 | *(next++) = '\0'; |
| 1986 | } |
| 1987 | |
| 1988 | /* do register namespace */ |
| 1989 | if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) { |
| 1990 | fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href); |
| 1991 | xmlFree(nsListDup); |
| 1992 | return(-1); |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | xmlFree(nsListDup); |
| 1997 | return(0); |
| 1998 | } |
| 1999 | #endif |
| 2000 | |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 2001 | /** |
Daniel Veillard | 1e20822 | 2002-10-22 14:25:25 +0000 | [diff] [blame] | 2002 | * xmlShellGrep: |
| 2003 | * @ctxt: the shell context |
| 2004 | * @arg: the string or regular expression to find |
| 2005 | * @node: a node |
| 2006 | * @node2: unused |
| 2007 | * |
| 2008 | * Implements the XML shell function "grep" |
| 2009 | * dumps informations about the node (namespace, attributes, content). |
| 2010 | * |
| 2011 | * Returns 0 |
| 2012 | */ |
Daniel Veillard | e645e8c | 2002-10-22 17:35:37 +0000 | [diff] [blame] | 2013 | static int |
Daniel Veillard | 1e20822 | 2002-10-22 14:25:25 +0000 | [diff] [blame] | 2014 | xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 2015 | char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2016 | { |
| 2017 | if (!ctxt) |
| 2018 | return (0); |
| 2019 | if (node == NULL) |
| 2020 | return (0); |
| 2021 | if (arg == NULL) |
| 2022 | return (0); |
| 2023 | #ifdef LIBXML_REGEXP_ENABLED |
| 2024 | if ((xmlStrchr((xmlChar *) arg, '?')) || |
| 2025 | (xmlStrchr((xmlChar *) arg, '*')) || |
| 2026 | (xmlStrchr((xmlChar *) arg, '.')) || |
| 2027 | (xmlStrchr((xmlChar *) arg, '['))) { |
| 2028 | } |
| 2029 | #endif |
| 2030 | while (node != NULL) { |
| 2031 | if (node->type == XML_COMMENT_NODE) { |
Daniel Veillard | e645e8c | 2002-10-22 17:35:37 +0000 | [diff] [blame] | 2032 | if (xmlStrstr(node->content, (xmlChar *) arg)) { |
Daniel Veillard | 1e20822 | 2002-10-22 14:25:25 +0000 | [diff] [blame] | 2033 | |
| 2034 | fprintf(ctxt->output, "%s : ", xmlGetNodePath(node)); |
| 2035 | xmlShellList(ctxt, NULL, node, NULL); |
| 2036 | } |
| 2037 | } else if (node->type == XML_TEXT_NODE) { |
Daniel Veillard | e645e8c | 2002-10-22 17:35:37 +0000 | [diff] [blame] | 2038 | if (xmlStrstr(node->content, (xmlChar *) arg)) { |
Daniel Veillard | 1e20822 | 2002-10-22 14:25:25 +0000 | [diff] [blame] | 2039 | |
| 2040 | fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent)); |
Daniel Veillard | e645e8c | 2002-10-22 17:35:37 +0000 | [diff] [blame] | 2041 | xmlShellList(ctxt, NULL, node->parent, NULL); |
Daniel Veillard | 1e20822 | 2002-10-22 14:25:25 +0000 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * Browse the full subtree, deep first |
| 2047 | */ |
| 2048 | |
| 2049 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2050 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 2051 | node = ((xmlDocPtr) node)->children; |
| 2052 | } else if ((node->children != NULL) |
| 2053 | && (node->type != XML_ENTITY_REF_NODE)) { |
| 2054 | /* deep first */ |
| 2055 | node = node->children; |
| 2056 | } else if (node->next != NULL) { |
| 2057 | /* then siblings */ |
| 2058 | node = node->next; |
| 2059 | } else { |
| 2060 | /* go up to parents->next if needed */ |
| 2061 | while (node != NULL) { |
| 2062 | if (node->parent != NULL) { |
| 2063 | node = node->parent; |
| 2064 | } |
| 2065 | if (node->next != NULL) { |
| 2066 | node = node->next; |
| 2067 | break; |
| 2068 | } |
| 2069 | if (node->parent == NULL) { |
| 2070 | node = NULL; |
| 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | return (0); |
| 2077 | } |
| 2078 | |
| 2079 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2080 | * xmlShellDir: |
| 2081 | * @ctxt: the shell context |
| 2082 | * @arg: unused |
| 2083 | * @node: a node |
| 2084 | * @node2: unused |
| 2085 | * |
| 2086 | * Implements the XML shell function "dir" |
| 2087 | * dumps informations about the node (namespace, attributes, content). |
| 2088 | * |
| 2089 | * Returns 0 |
| 2090 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2091 | int |
| 2092 | xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 2093 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 2094 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2095 | { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2096 | if (!ctxt) |
| 2097 | return (0); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 2098 | if (node == NULL) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2099 | fprintf(ctxt->output, "NULL\n"); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 2100 | return (0); |
| 2101 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2102 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2103 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2104 | xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2105 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2106 | xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2107 | } else { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2108 | xmlDebugDumpOneNode(ctxt->output, node, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2109 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2110 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 2113 | /** |
| 2114 | * xmlShellSetContent: |
| 2115 | * @ctxt: the shell context |
| 2116 | * @value: the content as a string |
| 2117 | * @node: a node |
| 2118 | * @node2: unused |
| 2119 | * |
| 2120 | * Implements the XML shell function "dir" |
| 2121 | * dumps informations about the node (namespace, attributes, content). |
| 2122 | * |
| 2123 | * Returns 0 |
| 2124 | */ |
| 2125 | static int |
| 2126 | xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 2127 | char *value, xmlNodePtr node, |
| 2128 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2129 | { |
| 2130 | xmlNodePtr results; |
| 2131 | xmlParserErrors ret; |
| 2132 | |
| 2133 | if (!ctxt) |
| 2134 | return (0); |
| 2135 | if (node == NULL) { |
| 2136 | fprintf(ctxt->output, "NULL\n"); |
| 2137 | return (0); |
| 2138 | } |
| 2139 | if (value == NULL) { |
| 2140 | fprintf(ctxt->output, "NULL\n"); |
| 2141 | return (0); |
| 2142 | } |
| 2143 | |
| 2144 | ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results); |
| 2145 | if (ret == XML_ERR_OK) { |
| 2146 | if (node->children != NULL) { |
| 2147 | xmlFreeNodeList(node->children); |
| 2148 | node->children = NULL; |
| 2149 | node->last = NULL; |
| 2150 | } |
| 2151 | xmlAddChildList(node, results); |
| 2152 | } else { |
| 2153 | fprintf(ctxt->output, "failed to parse content\n"); |
| 2154 | } |
| 2155 | return (0); |
| 2156 | } |
| 2157 | |
Daniel Veillard | 522bc60 | 2004-02-21 11:53:09 +0000 | [diff] [blame] | 2158 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 2159 | /** |
| 2160 | * xmlShellRNGValidate: |
| 2161 | * @ctxt: the shell context |
| 2162 | * @schemas: the path to the Relax-NG schemas |
| 2163 | * @node: a node |
| 2164 | * @node2: unused |
| 2165 | * |
| 2166 | * Implements the XML shell function "relaxng" |
| 2167 | * validating the instance against a Relax-NG schemas |
| 2168 | * |
| 2169 | * Returns 0 |
| 2170 | */ |
| 2171 | static int |
| 2172 | xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas, |
| 2173 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 2174 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2175 | { |
| 2176 | xmlRelaxNGPtr relaxngschemas; |
| 2177 | xmlRelaxNGParserCtxtPtr ctxt; |
| 2178 | xmlRelaxNGValidCtxtPtr vctxt; |
| 2179 | int ret; |
| 2180 | |
| 2181 | ctxt = xmlRelaxNGNewParserCtxt(schemas); |
| 2182 | xmlRelaxNGSetParserErrors(ctxt, |
| 2183 | (xmlRelaxNGValidityErrorFunc) fprintf, |
| 2184 | (xmlRelaxNGValidityWarningFunc) fprintf, |
| 2185 | stderr); |
| 2186 | relaxngschemas = xmlRelaxNGParse(ctxt); |
| 2187 | xmlRelaxNGFreeParserCtxt(ctxt); |
| 2188 | if (relaxngschemas == NULL) { |
| 2189 | xmlGenericError(xmlGenericErrorContext, |
| 2190 | "Relax-NG schema %s failed to compile\n", schemas); |
| 2191 | return(-1); |
| 2192 | } |
| 2193 | vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas); |
| 2194 | xmlRelaxNGSetValidErrors(vctxt, |
| 2195 | (xmlRelaxNGValidityErrorFunc) fprintf, |
| 2196 | (xmlRelaxNGValidityWarningFunc) fprintf, |
| 2197 | stderr); |
| 2198 | ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc); |
| 2199 | if (ret == 0) { |
| 2200 | fprintf(stderr, "%s validates\n", sctxt->filename); |
| 2201 | } else if (ret > 0) { |
| 2202 | fprintf(stderr, "%s fails to validate\n", sctxt->filename); |
| 2203 | } else { |
| 2204 | fprintf(stderr, "%s validation generated an internal error\n", |
| 2205 | sctxt->filename); |
| 2206 | } |
| 2207 | xmlRelaxNGFreeValidCtxt(vctxt); |
| 2208 | if (relaxngschemas != NULL) |
| 2209 | xmlRelaxNGFree(relaxngschemas); |
| 2210 | return(0); |
| 2211 | } |
| 2212 | #endif |
| 2213 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2214 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2215 | /** |
| 2216 | * xmlShellCat: |
| 2217 | * @ctxt: the shell context |
| 2218 | * @arg: unused |
| 2219 | * @node: a node |
| 2220 | * @node2: unused |
| 2221 | * |
| 2222 | * Implements the XML shell function "cat" |
| 2223 | * dumps the serialization node content (XML or HTML). |
| 2224 | * |
| 2225 | * Returns 0 |
| 2226 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2227 | int |
| 2228 | xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED, |
| 2229 | xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2230 | { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2231 | if (!ctxt) |
| 2232 | return (0); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 2233 | if (node == NULL) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2234 | fprintf(ctxt->output, "NULL\n"); |
Daniel Veillard | 5e926fa | 2002-01-22 21:44:25 +0000 | [diff] [blame] | 2235 | return (0); |
| 2236 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2237 | if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2238 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2239 | if (node->type == XML_HTML_DOCUMENT_NODE) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2240 | htmlDocDump(ctxt->output, (htmlDocPtr) node); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2241 | else |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2242 | htmlNodeDumpFile(ctxt->output, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2243 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2244 | if (node->type == XML_DOCUMENT_NODE) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2245 | xmlDocDump(ctxt->output, (xmlDocPtr) node); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2246 | else |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2247 | xmlElemDump(ctxt->output, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2248 | #endif /* LIBXML_HTML_ENABLED */ |
| 2249 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2250 | if (node->type == XML_DOCUMENT_NODE) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2251 | xmlDocDump(ctxt->output, (xmlDocPtr) node); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2252 | else |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2253 | xmlElemDump(ctxt->output, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2254 | } |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2255 | fprintf(ctxt->output, "\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2256 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2257 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2258 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2259 | |
| 2260 | /** |
| 2261 | * xmlShellLoad: |
| 2262 | * @ctxt: the shell context |
| 2263 | * @filename: the file name |
| 2264 | * @node: unused |
| 2265 | * @node2: unused |
| 2266 | * |
| 2267 | * Implements the XML shell function "load" |
| 2268 | * loads a new document specified by the filename |
| 2269 | * |
| 2270 | * Returns 0 or -1 if loading failed |
| 2271 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2272 | int |
| 2273 | xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename, |
| 2274 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 2275 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2276 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2277 | xmlDocPtr doc; |
| 2278 | int html = 0; |
| 2279 | |
| 2280 | if (ctxt->doc != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2281 | html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2282 | |
| 2283 | if (html) { |
| 2284 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2285 | doc = htmlParseFile(filename, NULL); |
| 2286 | #else |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2287 | fprintf(ctxt->output, "HTML support not compiled in\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2288 | doc = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2289 | #endif /* LIBXML_HTML_ENABLED */ |
| 2290 | } else { |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 2291 | doc = xmlReadFile(filename,NULL,0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2292 | } |
| 2293 | if (doc != NULL) { |
| 2294 | if (ctxt->loaded == 1) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2295 | xmlFreeDoc(ctxt->doc); |
| 2296 | } |
| 2297 | ctxt->loaded = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2298 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2299 | xmlXPathFreeContext(ctxt->pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2300 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2301 | xmlFree(ctxt->filename); |
| 2302 | ctxt->doc = doc; |
| 2303 | ctxt->node = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2304 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2305 | ctxt->pctxt = xmlXPathNewContext(doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2306 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 85095e2 | 2003-04-23 13:56:44 +0000 | [diff] [blame] | 2307 | ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2308 | } else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2309 | return (-1); |
| 2310 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2313 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2314 | /** |
| 2315 | * xmlShellWrite: |
| 2316 | * @ctxt: the shell context |
| 2317 | * @filename: the file name |
| 2318 | * @node: a node in the tree |
| 2319 | * @node2: unused |
| 2320 | * |
| 2321 | * Implements the XML shell function "write" |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2322 | * Write the current node to the filename, it saves the serialization |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2323 | * of the subtree under the @node specified |
| 2324 | * |
| 2325 | * Returns 0 or -1 in case of error |
| 2326 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2327 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2328 | xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2329 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2330 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2331 | if (node == NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2332 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2333 | if ((filename == NULL) || (filename[0] == 0)) { |
| 2334 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2335 | "Write command requires a filename argument\n"); |
| 2336 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2337 | } |
| 2338 | #ifdef W_OK |
| 2339 | if (access((char *) filename, W_OK)) { |
| 2340 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2341 | "Cannot write to %s\n", filename); |
| 2342 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2344 | #endif |
| 2345 | switch (node->type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2346 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2347 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 2348 | xmlGenericError(xmlGenericErrorContext, |
| 2349 | "Failed to write to %s\n", filename); |
| 2350 | return (-1); |
| 2351 | } |
| 2352 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2353 | case XML_HTML_DOCUMENT_NODE: |
| 2354 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2355 | if (htmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 2356 | xmlGenericError(xmlGenericErrorContext, |
| 2357 | "Failed to write to %s\n", filename); |
| 2358 | return (-1); |
| 2359 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2361 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 2362 | xmlGenericError(xmlGenericErrorContext, |
| 2363 | "Failed to write to %s\n", filename); |
| 2364 | return (-1); |
| 2365 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2366 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2367 | break; |
| 2368 | default:{ |
| 2369 | FILE *f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2370 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2371 | f = fopen((char *) filename, "w"); |
| 2372 | if (f == NULL) { |
| 2373 | xmlGenericError(xmlGenericErrorContext, |
| 2374 | "Failed to write to %s\n", filename); |
| 2375 | return (-1); |
| 2376 | } |
| 2377 | xmlElemDump(f, ctxt->doc, node); |
| 2378 | fclose(f); |
| 2379 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2380 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2381 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | /** |
| 2385 | * xmlShellSave: |
| 2386 | * @ctxt: the shell context |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2387 | * @filename: the file name (optional) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2388 | * @node: unused |
| 2389 | * @node2: unused |
| 2390 | * |
| 2391 | * Implements the XML shell function "save" |
| 2392 | * Write the current document to the filename, or it's original name |
| 2393 | * |
| 2394 | * Returns 0 or -1 in case of error |
| 2395 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2396 | int |
| 2397 | xmlShellSave(xmlShellCtxtPtr ctxt, char *filename, |
| 2398 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 2399 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2400 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2401 | if (ctxt->doc == NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2402 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2403 | if ((filename == NULL) || (filename[0] == 0)) |
| 2404 | filename = ctxt->filename; |
| 2405 | #ifdef W_OK |
| 2406 | if (access((char *) filename, W_OK)) { |
| 2407 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2408 | "Cannot save to %s\n", filename); |
| 2409 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2410 | } |
| 2411 | #endif |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2412 | switch (ctxt->doc->type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2413 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2414 | if (xmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 2415 | xmlGenericError(xmlGenericErrorContext, |
| 2416 | "Failed to save to %s\n", filename); |
| 2417 | } |
| 2418 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2419 | case XML_HTML_DOCUMENT_NODE: |
| 2420 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2421 | if (htmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 2422 | xmlGenericError(xmlGenericErrorContext, |
| 2423 | "Failed to save to %s\n", filename); |
| 2424 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2425 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2426 | if (xmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 2427 | xmlGenericError(xmlGenericErrorContext, |
| 2428 | "Failed to save to %s\n", filename); |
| 2429 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2430 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2431 | break; |
| 2432 | default: |
| 2433 | xmlGenericError(xmlGenericErrorContext, |
| 2434 | "To save to subparts of a document use the 'write' command\n"); |
| 2435 | return (-1); |
| 2436 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2437 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2438 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2439 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2440 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2441 | |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2442 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2443 | /** |
| 2444 | * xmlShellValidate: |
| 2445 | * @ctxt: the shell context |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2446 | * @dtd: the DTD URI (optional) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2447 | * @node: unused |
| 2448 | * @node2: unused |
| 2449 | * |
| 2450 | * Implements the XML shell function "validate" |
| 2451 | * Validate the document, if a DTD path is provided, then the validation |
| 2452 | * is done against the given DTD. |
| 2453 | * |
| 2454 | * Returns 0 or -1 in case of error |
| 2455 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2456 | int |
| 2457 | xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd, |
| 2458 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 2459 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2460 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2461 | xmlValidCtxt vctxt; |
| 2462 | int res = -1; |
| 2463 | |
| 2464 | vctxt.userData = stderr; |
| 2465 | vctxt.error = (xmlValidityErrorFunc) fprintf; |
| 2466 | vctxt.warning = (xmlValidityWarningFunc) fprintf; |
| 2467 | |
| 2468 | if ((dtd == NULL) || (dtd[0] == 0)) { |
| 2469 | res = xmlValidateDocument(&vctxt, ctxt->doc); |
| 2470 | } else { |
| 2471 | xmlDtdPtr subset; |
| 2472 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2473 | subset = xmlParseDTD(NULL, (xmlChar *) dtd); |
| 2474 | if (subset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2475 | res = xmlValidateDtd(&vctxt, ctxt->doc, subset); |
| 2476 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2477 | xmlFreeDtd(subset); |
| 2478 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2479 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2480 | return (res); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2481 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2482 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2483 | |
| 2484 | /** |
| 2485 | * xmlShellDu: |
| 2486 | * @ctxt: the shell context |
| 2487 | * @arg: unused |
| 2488 | * @tree: a node defining a subtree |
| 2489 | * @node2: unused |
| 2490 | * |
| 2491 | * Implements the XML shell function "du" |
| 2492 | * show the structure of the subtree under node @tree |
| 2493 | * If @tree is null, the command works on the current node. |
| 2494 | * |
| 2495 | * Returns 0 or -1 in case of error |
| 2496 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2497 | int |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2498 | xmlShellDu(xmlShellCtxtPtr ctxt, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2499 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree, |
| 2500 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2501 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2502 | xmlNodePtr node; |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2503 | int indent = 0, i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2504 | |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2505 | if (!ctxt) |
| 2506 | return (-1); |
| 2507 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2508 | if (tree == NULL) |
| 2509 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2510 | node = tree; |
| 2511 | while (node != NULL) { |
| 2512 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2513 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2514 | fprintf(ctxt->output, "/\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2515 | } else if (node->type == XML_ELEMENT_NODE) { |
| 2516 | for (i = 0; i < indent; i++) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2517 | fprintf(ctxt->output, " "); |
| 2518 | fprintf(ctxt->output, "%s\n", node->name); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2519 | } else { |
| 2520 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2521 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2522 | /* |
| 2523 | * Browse the full subtree, deep first |
| 2524 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2525 | |
| 2526 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2527 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2528 | node = ((xmlDocPtr) node)->children; |
| 2529 | } else if ((node->children != NULL) |
| 2530 | && (node->type != XML_ENTITY_REF_NODE)) { |
| 2531 | /* deep first */ |
| 2532 | node = node->children; |
| 2533 | indent++; |
| 2534 | } else if ((node != tree) && (node->next != NULL)) { |
| 2535 | /* then siblings */ |
| 2536 | node = node->next; |
| 2537 | } else if (node != tree) { |
| 2538 | /* go up to parents->next if needed */ |
| 2539 | while (node != tree) { |
| 2540 | if (node->parent != NULL) { |
| 2541 | node = node->parent; |
| 2542 | indent--; |
| 2543 | } |
| 2544 | if ((node != tree) && (node->next != NULL)) { |
| 2545 | node = node->next; |
| 2546 | break; |
| 2547 | } |
| 2548 | if (node->parent == NULL) { |
| 2549 | node = NULL; |
| 2550 | break; |
| 2551 | } |
| 2552 | if (node == tree) { |
| 2553 | node = NULL; |
| 2554 | break; |
| 2555 | } |
| 2556 | } |
| 2557 | /* exit condition */ |
| 2558 | if (node == tree) |
| 2559 | node = NULL; |
| 2560 | } else |
| 2561 | node = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2562 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2563 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | /** |
| 2567 | * xmlShellPwd: |
| 2568 | * @ctxt: the shell context |
| 2569 | * @buffer: the output buffer |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 2570 | * @node: a node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2571 | * @node2: unused |
| 2572 | * |
| 2573 | * Implements the XML shell function "pwd" |
| 2574 | * Show the full path from the root to the node, if needed building |
| 2575 | * thumblers when similar elements exists at a given ancestor level. |
| 2576 | * The output is compatible with XPath commands. |
| 2577 | * |
| 2578 | * Returns 0 or -1 in case of error |
| 2579 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2580 | int |
| 2581 | xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer, |
| 2582 | xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2583 | { |
Daniel Veillard | c6e013a | 2001-11-10 10:08:57 +0000 | [diff] [blame] | 2584 | xmlChar *path; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2585 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2586 | if (node == NULL) |
| 2587 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2588 | |
Daniel Veillard | c6e013a | 2001-11-10 10:08:57 +0000 | [diff] [blame] | 2589 | path = xmlGetNodePath(node); |
| 2590 | if (path == NULL) |
| 2591 | return (-1); |
| 2592 | |
| 2593 | /* |
| 2594 | * This test prevents buffer overflow, because this routine |
| 2595 | * is only called by xmlShell, in which the second argument is |
| 2596 | * 500 chars long. |
| 2597 | * It is a dirty hack before a cleaner solution is found. |
| 2598 | * Documentation should mention that the second argument must |
| 2599 | * be at least 500 chars long, and could be stripped if too long. |
| 2600 | */ |
| 2601 | snprintf(buffer, 499, "%s", path); |
| 2602 | buffer[499] = '0'; |
| 2603 | xmlFree(path); |
| 2604 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2605 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2609 | * xmlShell: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2610 | * @doc: the initial document |
| 2611 | * @filename: the output buffer |
| 2612 | * @input: the line reading function |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2613 | * @output: the output FILE*, defaults to stdout if NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2614 | * |
| 2615 | * Implements the XML shell |
| 2616 | * This allow to load, validate, view, modify and save a document |
| 2617 | * using a environment similar to a UNIX commandline. |
| 2618 | */ |
| 2619 | void |
| 2620 | xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2621 | FILE * output) |
| 2622 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2623 | char prompt[500] = "/ > "; |
| 2624 | char *cmdline = NULL, *cur; |
| 2625 | int nbargs; |
| 2626 | char command[100]; |
| 2627 | char arg[400]; |
| 2628 | int i; |
| 2629 | xmlShellCtxtPtr ctxt; |
| 2630 | xmlXPathObjectPtr list; |
| 2631 | |
| 2632 | if (doc == NULL) |
| 2633 | return; |
| 2634 | if (filename == NULL) |
| 2635 | return; |
| 2636 | if (input == NULL) |
| 2637 | return; |
| 2638 | if (output == NULL) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2639 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2640 | ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt)); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2641 | if (ctxt == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | ctxt->loaded = 0; |
| 2644 | ctxt->doc = doc; |
| 2645 | ctxt->input = input; |
| 2646 | ctxt->output = output; |
| 2647 | ctxt->filename = (char *) xmlStrdup((xmlChar *) filename); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2648 | ctxt->node = (xmlNodePtr) ctxt->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2649 | |
| 2650 | #ifdef LIBXML_XPATH_ENABLED |
| 2651 | ctxt->pctxt = xmlXPathNewContext(ctxt->doc); |
| 2652 | if (ctxt->pctxt == NULL) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2653 | xmlFree(ctxt); |
| 2654 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2655 | } |
| 2656 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2657 | while (1) { |
| 2658 | if (ctxt->node == (xmlNodePtr) ctxt->doc) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2659 | snprintf(prompt, sizeof(prompt), "%s > ", "/"); |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 2660 | else if ((ctxt->node != NULL) && (ctxt->node->name)) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2661 | snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2662 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2663 | snprintf(prompt, sizeof(prompt), "? > "); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2664 | prompt[sizeof(prompt) - 1] = 0; |
| 2665 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2666 | /* |
| 2667 | * Get a new command line |
| 2668 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2669 | cmdline = ctxt->input(prompt); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2670 | if (cmdline == NULL) |
| 2671 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2672 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2673 | /* |
| 2674 | * Parse the command itself |
| 2675 | */ |
| 2676 | cur = cmdline; |
| 2677 | nbargs = 0; |
| 2678 | while ((*cur == ' ') || (*cur == '\t')) |
| 2679 | cur++; |
| 2680 | i = 0; |
| 2681 | while ((*cur != ' ') && (*cur != '\t') && |
| 2682 | (*cur != '\n') && (*cur != '\r')) { |
| 2683 | if (*cur == 0) |
| 2684 | break; |
| 2685 | command[i++] = *cur++; |
| 2686 | } |
| 2687 | command[i] = 0; |
| 2688 | if (i == 0) |
| 2689 | continue; |
| 2690 | nbargs++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2691 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2692 | /* |
| 2693 | * Parse the argument |
| 2694 | */ |
| 2695 | while ((*cur == ' ') || (*cur == '\t')) |
| 2696 | cur++; |
| 2697 | i = 0; |
| 2698 | while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) { |
| 2699 | if (*cur == 0) |
| 2700 | break; |
| 2701 | arg[i++] = *cur++; |
| 2702 | } |
| 2703 | arg[i] = 0; |
| 2704 | if (i != 0) |
| 2705 | nbargs++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2706 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2707 | /* |
| 2708 | * start interpreting the command |
| 2709 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | if (!strcmp(command, "exit")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2711 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2712 | if (!strcmp(command, "quit")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2713 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2714 | if (!strcmp(command, "bye")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2715 | break; |
Daniel Veillard | 5004f42 | 2001-11-08 13:53:05 +0000 | [diff] [blame] | 2716 | if (!strcmp(command, "help")) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2717 | fprintf(ctxt->output, "\tbase display XML base of the node\n"); |
| 2718 | fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n"); |
| 2719 | fprintf(ctxt->output, "\tbye leave shell\n"); |
| 2720 | fprintf(ctxt->output, "\tcat [node] display node or current node\n"); |
| 2721 | fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n"); |
| 2722 | fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n"); |
| 2723 | fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n"); |
| 2724 | fprintf(ctxt->output, "\texit leave shell\n"); |
| 2725 | fprintf(ctxt->output, "\thelp display this help\n"); |
| 2726 | fprintf(ctxt->output, "\tfree display memory usage\n"); |
| 2727 | fprintf(ctxt->output, "\tload [name] load a new document with name\n"); |
| 2728 | fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n"); |
Daniel Veillard | c14c389 | 2004-08-16 12:34:50 +0000 | [diff] [blame] | 2729 | fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n"); |
Daniel Veillard | 2070c48 | 2002-01-22 22:12:19 +0000 | [diff] [blame] | 2730 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2731 | fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n"); |
Daniel Veillard | bbaa997 | 2004-06-16 14:08:33 +0000 | [diff] [blame] | 2732 | fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n"); |
| 2733 | fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n"); |
Daniel Veillard | 2070c48 | 2002-01-22 22:12:19 +0000 | [diff] [blame] | 2734 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2735 | fprintf(ctxt->output, "\tpwd display current working directory\n"); |
| 2736 | fprintf(ctxt->output, "\tquit leave shell\n"); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2737 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2738 | fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n"); |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2739 | fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n"); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2740 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2741 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2742 | fprintf(ctxt->output, "\tvalidate check the document for errors\n"); |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2743 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 522bc60 | 2004-02-21 11:53:09 +0000 | [diff] [blame] | 2744 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 2745 | fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n"); |
| 2746 | #endif |
Daniel Veillard | 1e20822 | 2002-10-22 14:25:25 +0000 | [diff] [blame] | 2747 | fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n"); |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2748 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 5004f42 | 2001-11-08 13:53:05 +0000 | [diff] [blame] | 2749 | } else if (!strcmp(command, "validate")) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2750 | xmlShellValidate(ctxt, arg, NULL, NULL); |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2751 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2752 | } else if (!strcmp(command, "load")) { |
| 2753 | xmlShellLoad(ctxt, arg, NULL, NULL); |
Daniel Veillard | 522bc60 | 2004-02-21 11:53:09 +0000 | [diff] [blame] | 2754 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 2755 | } else if (!strcmp(command, "relaxng")) { |
| 2756 | xmlShellRNGValidate(ctxt, arg, NULL, NULL); |
| 2757 | #endif |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2758 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2759 | } else if (!strcmp(command, "save")) { |
| 2760 | xmlShellSave(ctxt, arg, NULL, NULL); |
| 2761 | } else if (!strcmp(command, "write")) { |
| 2762 | xmlShellWrite(ctxt, arg, NULL, NULL); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2763 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 1e20822 | 2002-10-22 14:25:25 +0000 | [diff] [blame] | 2764 | } else if (!strcmp(command, "grep")) { |
| 2765 | xmlShellGrep(ctxt, arg, ctxt->node, NULL); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2766 | } else if (!strcmp(command, "free")) { |
| 2767 | if (arg[0] == 0) { |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2768 | xmlMemShow(ctxt->output, 0); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2769 | } else { |
| 2770 | int len = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2771 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2772 | sscanf(arg, "%d", &len); |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2773 | xmlMemShow(ctxt->output, len); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } else if (!strcmp(command, "pwd")) { |
| 2776 | char dir[500]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2777 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2778 | if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL)) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2779 | fprintf(ctxt->output, "%s\n", dir); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2780 | } else if (!strcmp(command, "du")) { |
| 2781 | xmlShellDu(ctxt, NULL, ctxt->node, NULL); |
| 2782 | } else if (!strcmp(command, "base")) { |
| 2783 | xmlShellBase(ctxt, NULL, ctxt->node, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 2784 | } else if (!strcmp(command, "set")) { |
| 2785 | xmlShellSetContent(ctxt, arg, ctxt->node, NULL); |
Daniel Veillard | 2070c48 | 2002-01-22 22:12:19 +0000 | [diff] [blame] | 2786 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | bbaa997 | 2004-06-16 14:08:33 +0000 | [diff] [blame] | 2787 | } else if (!strcmp(command, "setns")) { |
| 2788 | if (arg[0] == 0) { |
| 2789 | xmlGenericError(xmlGenericErrorContext, |
| 2790 | "setns: prefix=[nsuri] required\n"); |
| 2791 | } else { |
| 2792 | xmlShellRegisterNamespace(ctxt, arg, NULL, NULL); |
| 2793 | } |
Daniel Veillard | 2070c48 | 2002-01-22 22:12:19 +0000 | [diff] [blame] | 2794 | } else if (!strcmp(command, "xpath")) { |
| 2795 | if (arg[0] == 0) { |
| 2796 | xmlGenericError(xmlGenericErrorContext, |
| 2797 | "xpath: expression required\n"); |
| 2798 | } else { |
| 2799 | ctxt->pctxt->node = ctxt->node; |
| 2800 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 2801 | xmlXPathDebugDumpObject(ctxt->output, list, 0); |
Daniel Veillard | 2070c48 | 2002-01-22 22:12:19 +0000 | [diff] [blame] | 2802 | xmlXPathFreeObject(list); |
| 2803 | } |
| 2804 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2805 | #ifdef LIBXML_TREE_ENABLED |
Daniel Veillard | cfa0d81 | 2002-01-17 08:46:58 +0000 | [diff] [blame] | 2806 | } else if (!strcmp(command, "setbase")) { |
| 2807 | xmlShellSetBase(ctxt, arg, ctxt->node, NULL); |
Daniel Veillard | 2156d43 | 2004-03-04 15:59:36 +0000 | [diff] [blame] | 2808 | #endif |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2809 | } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) { |
| 2810 | int dir = (!strcmp(command, "dir")); |
| 2811 | |
| 2812 | if (arg[0] == 0) { |
| 2813 | if (dir) |
| 2814 | xmlShellDir(ctxt, NULL, ctxt->node, NULL); |
| 2815 | else |
| 2816 | xmlShellList(ctxt, NULL, ctxt->node, NULL); |
| 2817 | } else { |
| 2818 | ctxt->pctxt->node = ctxt->node; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 2819 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2820 | ctxt->pctxt->node = ctxt->node; |
| 2821 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2822 | #else |
| 2823 | list = NULL; |
| 2824 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2825 | if (list != NULL) { |
| 2826 | switch (list->type) { |
| 2827 | case XPATH_UNDEFINED: |
| 2828 | xmlGenericError(xmlGenericErrorContext, |
| 2829 | "%s: no such node\n", arg); |
| 2830 | break; |
| 2831 | case XPATH_NODESET:{ |
| 2832 | int indx; |
| 2833 | |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2834 | if (list->nodesetval == NULL) |
| 2835 | break; |
| 2836 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2837 | for (indx = 0; |
| 2838 | indx < list->nodesetval->nodeNr; |
| 2839 | indx++) { |
| 2840 | if (dir) |
| 2841 | xmlShellDir(ctxt, NULL, |
| 2842 | list->nodesetval-> |
| 2843 | nodeTab[indx], NULL); |
| 2844 | else |
| 2845 | xmlShellList(ctxt, NULL, |
| 2846 | list->nodesetval-> |
| 2847 | nodeTab[indx], NULL); |
| 2848 | } |
| 2849 | break; |
| 2850 | } |
| 2851 | case XPATH_BOOLEAN: |
| 2852 | xmlGenericError(xmlGenericErrorContext, |
| 2853 | "%s is a Boolean\n", arg); |
| 2854 | break; |
| 2855 | case XPATH_NUMBER: |
| 2856 | xmlGenericError(xmlGenericErrorContext, |
| 2857 | "%s is a number\n", arg); |
| 2858 | break; |
| 2859 | case XPATH_STRING: |
| 2860 | xmlGenericError(xmlGenericErrorContext, |
| 2861 | "%s is a string\n", arg); |
| 2862 | break; |
| 2863 | case XPATH_POINT: |
| 2864 | xmlGenericError(xmlGenericErrorContext, |
| 2865 | "%s is a point\n", arg); |
| 2866 | break; |
| 2867 | case XPATH_RANGE: |
| 2868 | xmlGenericError(xmlGenericErrorContext, |
| 2869 | "%s is a range\n", arg); |
| 2870 | break; |
| 2871 | case XPATH_LOCATIONSET: |
| 2872 | xmlGenericError(xmlGenericErrorContext, |
| 2873 | "%s is a range\n", arg); |
| 2874 | break; |
| 2875 | case XPATH_USERS: |
| 2876 | xmlGenericError(xmlGenericErrorContext, |
| 2877 | "%s is user-defined\n", arg); |
| 2878 | break; |
| 2879 | case XPATH_XSLT_TREE: |
| 2880 | xmlGenericError(xmlGenericErrorContext, |
| 2881 | "%s is an XSLT value tree\n", |
| 2882 | arg); |
| 2883 | break; |
| 2884 | } |
| 2885 | #ifdef LIBXML_XPATH_ENABLED |
| 2886 | xmlXPathFreeObject(list); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 2887 | #endif |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2888 | } else { |
| 2889 | xmlGenericError(xmlGenericErrorContext, |
| 2890 | "%s: no such node\n", arg); |
| 2891 | } |
| 2892 | ctxt->pctxt->node = NULL; |
| 2893 | } |
| 2894 | } else if (!strcmp(command, "cd")) { |
| 2895 | if (arg[0] == 0) { |
| 2896 | ctxt->node = (xmlNodePtr) ctxt->doc; |
| 2897 | } else { |
| 2898 | #ifdef LIBXML_XPATH_ENABLED |
| 2899 | ctxt->pctxt->node = ctxt->node; |
| 2900 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2901 | #else |
| 2902 | list = NULL; |
| 2903 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2904 | if (list != NULL) { |
| 2905 | switch (list->type) { |
| 2906 | case XPATH_UNDEFINED: |
| 2907 | xmlGenericError(xmlGenericErrorContext, |
| 2908 | "%s: no such node\n", arg); |
| 2909 | break; |
| 2910 | case XPATH_NODESET: |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2911 | if (list->nodesetval != NULL) { |
| 2912 | if (list->nodesetval->nodeNr == 1) { |
| 2913 | ctxt->node = list->nodesetval->nodeTab[0]; |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 2914 | if ((ctxt->node != NULL) && |
| 2915 | (ctxt->node->type == |
| 2916 | XML_NAMESPACE_DECL)) { |
| 2917 | xmlGenericError(xmlGenericErrorContext, |
| 2918 | "cannot cd to namespace\n"); |
| 2919 | ctxt->node = NULL; |
| 2920 | } |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2921 | } else |
| 2922 | xmlGenericError(xmlGenericErrorContext, |
| 2923 | "%s is a %d Node Set\n", |
| 2924 | arg, |
| 2925 | list->nodesetval->nodeNr); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2926 | } else |
| 2927 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2928 | "%s is an empty Node Set\n", |
| 2929 | arg); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2930 | break; |
| 2931 | case XPATH_BOOLEAN: |
| 2932 | xmlGenericError(xmlGenericErrorContext, |
| 2933 | "%s is a Boolean\n", arg); |
| 2934 | break; |
| 2935 | case XPATH_NUMBER: |
| 2936 | xmlGenericError(xmlGenericErrorContext, |
| 2937 | "%s is a number\n", arg); |
| 2938 | break; |
| 2939 | case XPATH_STRING: |
| 2940 | xmlGenericError(xmlGenericErrorContext, |
| 2941 | "%s is a string\n", arg); |
| 2942 | break; |
| 2943 | case XPATH_POINT: |
| 2944 | xmlGenericError(xmlGenericErrorContext, |
| 2945 | "%s is a point\n", arg); |
| 2946 | break; |
| 2947 | case XPATH_RANGE: |
| 2948 | xmlGenericError(xmlGenericErrorContext, |
| 2949 | "%s is a range\n", arg); |
| 2950 | break; |
| 2951 | case XPATH_LOCATIONSET: |
| 2952 | xmlGenericError(xmlGenericErrorContext, |
| 2953 | "%s is a range\n", arg); |
| 2954 | break; |
| 2955 | case XPATH_USERS: |
| 2956 | xmlGenericError(xmlGenericErrorContext, |
| 2957 | "%s is user-defined\n", arg); |
| 2958 | break; |
| 2959 | case XPATH_XSLT_TREE: |
| 2960 | xmlGenericError(xmlGenericErrorContext, |
| 2961 | "%s is an XSLT value tree\n", |
| 2962 | arg); |
| 2963 | break; |
| 2964 | } |
| 2965 | #ifdef LIBXML_XPATH_ENABLED |
| 2966 | xmlXPathFreeObject(list); |
| 2967 | #endif |
| 2968 | } else { |
| 2969 | xmlGenericError(xmlGenericErrorContext, |
| 2970 | "%s: no such node\n", arg); |
| 2971 | } |
| 2972 | ctxt->pctxt->node = NULL; |
| 2973 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2974 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2975 | } else if (!strcmp(command, "cat")) { |
| 2976 | if (arg[0] == 0) { |
| 2977 | xmlShellCat(ctxt, NULL, ctxt->node, NULL); |
| 2978 | } else { |
| 2979 | ctxt->pctxt->node = ctxt->node; |
| 2980 | #ifdef LIBXML_XPATH_ENABLED |
| 2981 | ctxt->pctxt->node = ctxt->node; |
| 2982 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2983 | #else |
| 2984 | list = NULL; |
| 2985 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2986 | if (list != NULL) { |
| 2987 | switch (list->type) { |
| 2988 | case XPATH_UNDEFINED: |
| 2989 | xmlGenericError(xmlGenericErrorContext, |
| 2990 | "%s: no such node\n", arg); |
| 2991 | break; |
| 2992 | case XPATH_NODESET:{ |
| 2993 | int indx; |
| 2994 | |
Daniel Veillard | a6825e8 | 2001-11-07 13:33:59 +0000 | [diff] [blame] | 2995 | if (list->nodesetval == NULL) |
| 2996 | break; |
| 2997 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2998 | for (indx = 0; |
| 2999 | indx < list->nodesetval->nodeNr; |
| 3000 | indx++) { |
| 3001 | if (i > 0) |
Daniel Veillard | 321be0c | 2002-10-08 21:26:42 +0000 | [diff] [blame] | 3002 | fprintf(ctxt->output, " -------\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 3003 | xmlShellCat(ctxt, NULL, |
| 3004 | list->nodesetval-> |
| 3005 | nodeTab[indx], NULL); |
| 3006 | } |
| 3007 | break; |
| 3008 | } |
| 3009 | case XPATH_BOOLEAN: |
| 3010 | xmlGenericError(xmlGenericErrorContext, |
| 3011 | "%s is a Boolean\n", arg); |
| 3012 | break; |
| 3013 | case XPATH_NUMBER: |
| 3014 | xmlGenericError(xmlGenericErrorContext, |
| 3015 | "%s is a number\n", arg); |
| 3016 | break; |
| 3017 | case XPATH_STRING: |
| 3018 | xmlGenericError(xmlGenericErrorContext, |
| 3019 | "%s is a string\n", arg); |
| 3020 | break; |
| 3021 | case XPATH_POINT: |
| 3022 | xmlGenericError(xmlGenericErrorContext, |
| 3023 | "%s is a point\n", arg); |
| 3024 | break; |
| 3025 | case XPATH_RANGE: |
| 3026 | xmlGenericError(xmlGenericErrorContext, |
| 3027 | "%s is a range\n", arg); |
| 3028 | break; |
| 3029 | case XPATH_LOCATIONSET: |
| 3030 | xmlGenericError(xmlGenericErrorContext, |
| 3031 | "%s is a range\n", arg); |
| 3032 | break; |
| 3033 | case XPATH_USERS: |
| 3034 | xmlGenericError(xmlGenericErrorContext, |
| 3035 | "%s is user-defined\n", arg); |
| 3036 | break; |
| 3037 | case XPATH_XSLT_TREE: |
| 3038 | xmlGenericError(xmlGenericErrorContext, |
| 3039 | "%s is an XSLT value tree\n", |
| 3040 | arg); |
| 3041 | break; |
| 3042 | } |
| 3043 | #ifdef LIBXML_XPATH_ENABLED |
| 3044 | xmlXPathFreeObject(list); |
| 3045 | #endif |
| 3046 | } else { |
| 3047 | xmlGenericError(xmlGenericErrorContext, |
| 3048 | "%s: no such node\n", arg); |
| 3049 | } |
| 3050 | ctxt->pctxt->node = NULL; |
| 3051 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 3052 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 3053 | } else { |
| 3054 | xmlGenericError(xmlGenericErrorContext, |
| 3055 | "Unknown command %s\n", command); |
| 3056 | } |
| 3057 | free(cmdline); /* not xmlFree here ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3058 | } |
| 3059 | #ifdef LIBXML_XPATH_ENABLED |
| 3060 | xmlXPathFreeContext(ctxt->pctxt); |
| 3061 | #endif /* LIBXML_XPATH_ENABLED */ |
| 3062 | if (ctxt->loaded) { |
| 3063 | xmlFreeDoc(ctxt->doc); |
| 3064 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 3065 | if (ctxt->filename != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 3066 | xmlFree(ctxt->filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | xmlFree(ctxt); |
| 3068 | if (cmdline != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 3069 | free(cmdline); /* not xmlFree here ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | #endif /* LIBXML_DEBUG_ENABLED */ |