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