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