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