blob: 76134cca10709fcc58a05bdd0020bfda2e58851f [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
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 Veillardc5d64342001-06-24 12:13:24 +00007 * Daniel Veillard <daniel@veillard.com>
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012#ifdef LIBXML_DEBUG_ENABLED
13
Owen Taylor3473f882001-02-23 17:55:21 +000014#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 Veillard567e1b42001-08-01 15:53:47 +000024#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000025#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 Veillard3c01b1d2001-10-17 15:58:35 +000030#include <libxml/globals.h>
Daniel Veillard0ba59232002-02-10 13:20:39 +000031#include <libxml/xpathInternals.h>
Igor Zlatkovicc06bb622003-04-27 15:59:00 +000032#include <libxml/uri.h>
Daniel Veillard522bc602004-02-21 11:53:09 +000033#ifdef LIBXML_SCHEMAS_ENABLED
34#include <libxml/relaxng.h>
35#endif
Owen Taylor3473f882001-02-23 17:55:21 +000036
Daniel Veillardcfa303a2005-08-25 14:03:56 +000037#define DUMP_TEXT_TYPE 1
38
Daniel Veillard22cdb842004-10-04 14:09:17 +000039typedef struct _xmlDebugCtxt xmlDebugCtxt;
40typedef xmlDebugCtxt *xmlDebugCtxtPtr;
41struct _xmlDebugCtxt {
42 FILE *output; /* the output file */
43 char shift[101]; /* used for indenting */
44 int depth; /* current depth */
45 xmlDocPtr doc; /* current document */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000046 xmlNodePtr node; /* current node */
Daniel Veillard03a53c32004-10-26 16:06:51 +000047 xmlDictPtr dict; /* the doc dictionnary */
Daniel Veillard22cdb842004-10-04 14:09:17 +000048 int check; /* do just checkings */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000049 int errors; /* number of errors found */
Daniel Veillard03a53c32004-10-26 16:06:51 +000050 int nodict; /* if the document has no dictionnary */
Daniel Veillardcfa303a2005-08-25 14:03:56 +000051 int options; /* options */
Daniel Veillard22cdb842004-10-04 14:09:17 +000052};
53
54static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
55
56static void
57xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
58{
59 int i;
60
61 ctxt->depth = 0;
62 ctxt->check = 0;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000063 ctxt->errors = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000064 ctxt->output = stdout;
Daniel Veillard03a53c32004-10-26 16:06:51 +000065 ctxt->doc = NULL;
66 ctxt->node = NULL;
67 ctxt->dict = NULL;
Daniel Veillard6927b102004-10-27 17:29:04 +000068 ctxt->nodict = 0;
Daniel Veillardb6580ae2005-08-25 14:18:56 +000069 ctxt->options = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000070 for (i = 0; i < 100; i++)
71 ctxt->shift[i] = ' ';
72 ctxt->shift[100] = 0;
73}
74
75static void
William M. Brack9638d4c2004-10-15 18:25:33 +000076xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
Daniel Veillard76821142004-10-09 20:39:04 +000077{
William M. Brack9638d4c2004-10-15 18:25:33 +000078 /* remove the ATTRIBUTE_UNUSED when this is added */
Daniel Veillard76821142004-10-09 20:39:04 +000079}
80
81/**
Daniel Veillard0d24b112004-10-11 12:28:34 +000082 * xmlNsCheckScope:
83 * @node: the node
84 * @ns: the namespace node
Daniel Veillard76821142004-10-09 20:39:04 +000085 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000086 * Check that a given namespace is in scope on a node.
Daniel Veillard76821142004-10-09 20:39:04 +000087 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000088 * Returns 1 if in scope, -1 in case of argument error,
89 * -2 if the namespace is not in scope, and -3 if not on
90 * an ancestor node.
Daniel Veillard76821142004-10-09 20:39:04 +000091 */
92static int
Daniel Veillard0d24b112004-10-11 12:28:34 +000093xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
Daniel Veillard76821142004-10-09 20:39:04 +000094{
Daniel Veillard0d24b112004-10-11 12:28:34 +000095 xmlNsPtr cur;
Daniel Veillard76821142004-10-09 20:39:04 +000096
Daniel Veillard0d24b112004-10-11 12:28:34 +000097 if ((node == NULL) || (ns == NULL))
98 return(-1);
99
100 if ((node->type != XML_ELEMENT_NODE) &&
101 (node->type != XML_ATTRIBUTE_NODE) &&
102 (node->type != XML_DOCUMENT_NODE) &&
103 (node->type != XML_TEXT_NODE) &&
104 (node->type != XML_HTML_DOCUMENT_NODE) &&
105 (node->type != XML_XINCLUDE_START))
106 return(-2);
107
108 while ((node != NULL) &&
109 ((node->type == XML_ELEMENT_NODE) ||
110 (node->type == XML_ATTRIBUTE_NODE) ||
111 (node->type == XML_TEXT_NODE) ||
112 (node->type == XML_XINCLUDE_START))) {
113 if ((node->type == XML_ELEMENT_NODE) ||
114 (node->type == XML_XINCLUDE_START)) {
115 cur = node->nsDef;
116 while (cur != NULL) {
117 if (cur == ns)
118 return(1);
119 if (xmlStrEqual(cur->prefix, ns->prefix))
120 return(-2);
121 cur = cur->next;
122 }
Daniel Veillard76821142004-10-09 20:39:04 +0000123 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000124 node = node->parent;
Daniel Veillard76821142004-10-09 20:39:04 +0000125 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000126 /* the xml namespace may be declared on the document node */
127 if ((node != NULL) &&
128 ((node->type == XML_DOCUMENT_NODE) ||
129 (node->type == XML_HTML_DOCUMENT_NODE))) {
130 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
131 if (oldNs == ns)
132 return(1);
133 }
134 return(-3);
Daniel Veillard76821142004-10-09 20:39:04 +0000135}
Daniel Veillard76821142004-10-09 20:39:04 +0000136
Daniel Veillard76821142004-10-09 20:39:04 +0000137static void
Daniel Veillard22cdb842004-10-04 14:09:17 +0000138xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
139{
140 if (ctxt->check)
141 return;
142 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
143 if (ctxt->depth < 50)
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200144 fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000145 else
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200146 fprintf(ctxt->output, "%s", ctxt->shift);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000147 }
148}
149
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000150/**
151 * xmlDebugErr:
152 * @ctxt: a debug context
153 * @error: the error code
154 *
155 * Handle a debug error.
156 */
157static void
158xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
159{
160 ctxt->errors++;
161 __xmlRaiseError(NULL, NULL, NULL,
162 NULL, ctxt->node, XML_FROM_CHECK,
163 error, XML_ERR_ERROR, NULL, 0,
164 NULL, NULL, NULL, 0, 0,
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200165 "%s", msg);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000166}
167static void
168xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
169{
170 ctxt->errors++;
171 __xmlRaiseError(NULL, NULL, NULL,
172 NULL, ctxt->node, XML_FROM_CHECK,
173 error, XML_ERR_ERROR, NULL, 0,
174 NULL, NULL, NULL, 0, 0,
175 msg, extra);
176}
177static void
Daniel Veillardc6095782004-10-15 14:50:10 +0000178xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000179{
180 ctxt->errors++;
181 __xmlRaiseError(NULL, NULL, NULL,
182 NULL, ctxt->node, XML_FROM_CHECK,
183 error, XML_ERR_ERROR, NULL, 0,
184 NULL, NULL, NULL, 0, 0,
185 msg, extra);
186}
187
Daniel Veillard0d24b112004-10-11 12:28:34 +0000188/**
189 * xmlCtxtNsCheckScope:
190 * @ctxt: the debugging context
191 * @node: the node
192 * @ns: the namespace node
193 *
194 * Report if a given namespace is is not in scope.
195 */
196static void
197xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
198{
199 int ret;
200
201 ret = xmlNsCheckScope(node, ns);
202 if (ret == -2) {
203 if (ns->prefix == NULL)
204 xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
205 "Reference to default namespace not in scope\n");
206 else
207 xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
208 "Reference to namespace '%s' not in scope\n",
209 (char *) ns->prefix);
210 }
211 if (ret == -3) {
212 if (ns->prefix == NULL)
213 xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
214 "Reference to default namespace not on ancestor\n");
215 else
216 xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
217 "Reference to namespace '%s' not on ancestor\n",
218 (char *) ns->prefix);
219 }
220}
221
Daniel Veillardc6095782004-10-15 14:50:10 +0000222/**
223 * xmlCtxtCheckString:
224 * @ctxt: the debug context
225 * @str: the string
226 *
227 * Do debugging on the string, currently it just checks the UTF-8 content
228 */
229static void
230xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
231{
232 if (str == NULL) return;
233 if (ctxt->check) {
234 if (!xmlCheckUTF8(str)) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000235 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
Daniel Veillardc6095782004-10-15 14:50:10 +0000236 "String is not UTF-8 %s", (const char *) str);
237 }
238 }
239}
240
Daniel Veillard03a53c32004-10-26 16:06:51 +0000241/**
242 * xmlCtxtCheckName:
243 * @ctxt: the debug context
244 * @name: the name
245 *
246 * Do debugging on the name, for example the dictionnary status and
247 * conformance to the Name production.
248 */
249static void
250xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
251{
252 if (ctxt->check) {
253 if (name == NULL) {
254 xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
255 return;
256 }
257 if (xmlValidateName(name, 0)) {
258 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
259 "Name is not an NCName '%s'", (const char *) name);
260 }
261 if ((ctxt->dict != NULL) &&
Daniel Veillard023d0ba2009-07-29 11:34:50 +0200262 (!xmlDictOwns(ctxt->dict, name)) &&
263 ((ctxt->doc == NULL) ||
264 ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000265 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
266 "Name is not from the document dictionnary '%s'",
267 (const char *) name);
268 }
269 }
270}
271
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000272static void
273xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000274 xmlDocPtr doc;
275 xmlDictPtr dict;
276
277 doc = node->doc;
278
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000279 if (node->parent == NULL)
280 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
281 "Node has no parent\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000282 if (node->doc == NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000283 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
284 "Node has no doc\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000285 dict = NULL;
286 } else {
287 dict = doc->dict;
288 if ((dict == NULL) && (ctxt->nodict == 0)) {
Daniel Veillard6927b102004-10-27 17:29:04 +0000289#if 0
290 /* desactivated right now as it raises too many errors */
291 if (doc->type == XML_DOCUMENT_NODE)
292 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
293 "Document has no dictionnary\n");
294#endif
Daniel Veillard03a53c32004-10-26 16:06:51 +0000295 ctxt->nodict = 1;
296 }
297 if (ctxt->doc == NULL)
298 ctxt->doc = doc;
299
300 if (ctxt->dict == NULL) {
301 ctxt->dict = dict;
302 }
303 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000304 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
305 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
306 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
307 "Node doc differs from parent's one\n");
308 if (node->prev == NULL) {
309 if (node->type == XML_ATTRIBUTE_NODE) {
310 if ((node->parent != NULL) &&
311 (node != (xmlNodePtr) node->parent->properties))
312 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
313 "Attr has no prev and not first of attr list\n");
314
315 } else if ((node->parent != NULL) && (node->parent->children != node))
316 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
317 "Node has no prev and not first of parent list\n");
318 } else {
319 if (node->prev->next != node)
320 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
321 "Node prev->next : back link wrong\n");
322 }
323 if (node->next == NULL) {
324 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillard97c9ce22008-03-25 16:52:41 +0000325 (node->parent->last != node) &&
326 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000327 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
328 "Node has no next and not last of parent list\n");
329 } else {
330 if (node->next->prev != node)
331 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
332 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000333 if (node->next->parent != node->parent)
334 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
335 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000336 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000337 if (node->type == XML_ELEMENT_NODE) {
338 xmlNsPtr ns;
339
340 ns = node->nsDef;
341 while (ns != NULL) {
342 xmlCtxtNsCheckScope(ctxt, node, ns);
343 ns = ns->next;
344 }
345 if (node->ns != NULL)
346 xmlCtxtNsCheckScope(ctxt, node, node->ns);
347 } else if (node->type == XML_ATTRIBUTE_NODE) {
348 if (node->ns != NULL)
349 xmlCtxtNsCheckScope(ctxt, node, node->ns);
350 }
351
Daniel Veillardc6095782004-10-15 14:50:10 +0000352 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000353 (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillardf2e066a2005-06-30 13:04:44 +0000354 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000355 (node->type != XML_ATTRIBUTE_DECL) &&
356 (node->type != XML_DTD_NODE) &&
William M. Brack0357a302005-07-06 17:39:14 +0000357 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000358 (node->type != XML_HTML_DOCUMENT_NODE) &&
359 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000360 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000361 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000362 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000363 switch (node->type) {
364 case XML_ELEMENT_NODE:
365 case XML_ATTRIBUTE_NODE:
366 xmlCtxtCheckName(ctxt, node->name);
367 break;
368 case XML_TEXT_NODE:
369 if ((node->name == xmlStringText) ||
370 (node->name == xmlStringTextNoenc))
371 break;
372 /* some case of entity substitution can lead to this */
373 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000374 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
375 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000376 break;
377
378 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
379 "Text node has wrong name '%s'",
380 (const char *) node->name);
381 break;
382 case XML_COMMENT_NODE:
383 if (node->name == xmlStringComment)
384 break;
385 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
386 "Comment node has wrong name '%s'",
387 (const char *) node->name);
388 break;
389 case XML_PI_NODE:
390 xmlCtxtCheckName(ctxt, node->name);
391 break;
392 case XML_CDATA_SECTION_NODE:
393 if (node->name == NULL)
394 break;
395 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
396 "CData section has non NULL name '%s'",
397 (const char *) node->name);
398 break;
399 case XML_ENTITY_REF_NODE:
400 case XML_ENTITY_NODE:
401 case XML_DOCUMENT_TYPE_NODE:
402 case XML_DOCUMENT_FRAG_NODE:
403 case XML_NOTATION_NODE:
404 case XML_DTD_NODE:
405 case XML_ELEMENT_DECL:
406 case XML_ATTRIBUTE_DECL:
407 case XML_ENTITY_DECL:
408 case XML_NAMESPACE_DECL:
409 case XML_XINCLUDE_START:
410 case XML_XINCLUDE_END:
411#ifdef LIBXML_DOCB_ENABLED
412 case XML_DOCB_DOCUMENT_NODE:
413#endif
414 case XML_DOCUMENT_NODE:
415 case XML_HTML_DOCUMENT_NODE:
416 break;
417 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000418}
419
Daniel Veillard22cdb842004-10-04 14:09:17 +0000420static void
421xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
422{
423 int i;
424
Daniel Veillardc6095782004-10-15 14:50:10 +0000425 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000426 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000427 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000428 /* TODO: check UTF8 content of the string */
429 if (str == NULL) {
430 fprintf(ctxt->output, "(NULL)");
431 return;
432 }
433 for (i = 0; i < 40; i++)
434 if (str[i] == 0)
435 return;
436 else if (IS_BLANK_CH(str[i]))
437 fputc(' ', ctxt->output);
438 else if (str[i] >= 0x80)
439 fprintf(ctxt->output, "#%X", str[i]);
440 else
441 fputc(str[i], ctxt->output);
442 fprintf(ctxt->output, "...");
443}
444
445static void
446xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
447{
448 xmlCtxtDumpSpaces(ctxt);
449
450 if (dtd == NULL) {
451 if (!ctxt->check)
452 fprintf(ctxt->output, "DTD node is NULL\n");
453 return;
454 }
455
456 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000457 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
458 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000459 return;
460 }
461 if (!ctxt->check) {
462 if (dtd->name != NULL)
463 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
464 else
465 fprintf(ctxt->output, "DTD");
466 if (dtd->ExternalID != NULL)
467 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
468 if (dtd->SystemID != NULL)
469 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
470 fprintf(ctxt->output, "\n");
471 }
472 /*
473 * Do a bit of checking
474 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000475 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000476}
477
478static void
479xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
480{
481 xmlCtxtDumpSpaces(ctxt);
482
483 if (attr == NULL) {
484 if (!ctxt->check)
485 fprintf(ctxt->output, "Attribute declaration is NULL\n");
486 return;
487 }
488 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000489 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
490 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000491 return;
492 }
493 if (attr->name != NULL) {
494 if (!ctxt->check)
495 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
496 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000497 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
498 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000499 if (attr->elem != NULL) {
500 if (!ctxt->check)
501 fprintf(ctxt->output, " for %s", (char *) attr->elem);
502 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000503 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
504 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000505 if (!ctxt->check) {
506 switch (attr->atype) {
507 case XML_ATTRIBUTE_CDATA:
508 fprintf(ctxt->output, " CDATA");
509 break;
510 case XML_ATTRIBUTE_ID:
511 fprintf(ctxt->output, " ID");
512 break;
513 case XML_ATTRIBUTE_IDREF:
514 fprintf(ctxt->output, " IDREF");
515 break;
516 case XML_ATTRIBUTE_IDREFS:
517 fprintf(ctxt->output, " IDREFS");
518 break;
519 case XML_ATTRIBUTE_ENTITY:
520 fprintf(ctxt->output, " ENTITY");
521 break;
522 case XML_ATTRIBUTE_ENTITIES:
523 fprintf(ctxt->output, " ENTITIES");
524 break;
525 case XML_ATTRIBUTE_NMTOKEN:
526 fprintf(ctxt->output, " NMTOKEN");
527 break;
528 case XML_ATTRIBUTE_NMTOKENS:
529 fprintf(ctxt->output, " NMTOKENS");
530 break;
531 case XML_ATTRIBUTE_ENUMERATION:
532 fprintf(ctxt->output, " ENUMERATION");
533 break;
534 case XML_ATTRIBUTE_NOTATION:
535 fprintf(ctxt->output, " NOTATION ");
536 break;
537 }
538 if (attr->tree != NULL) {
539 int indx;
540 xmlEnumerationPtr cur = attr->tree;
541
542 for (indx = 0; indx < 5; indx++) {
543 if (indx != 0)
544 fprintf(ctxt->output, "|%s", (char *) cur->name);
545 else
546 fprintf(ctxt->output, " (%s", (char *) cur->name);
547 cur = cur->next;
548 if (cur == NULL)
549 break;
550 }
551 if (cur == NULL)
552 fprintf(ctxt->output, ")");
553 else
554 fprintf(ctxt->output, "...)");
555 }
556 switch (attr->def) {
557 case XML_ATTRIBUTE_NONE:
558 break;
559 case XML_ATTRIBUTE_REQUIRED:
560 fprintf(ctxt->output, " REQUIRED");
561 break;
562 case XML_ATTRIBUTE_IMPLIED:
563 fprintf(ctxt->output, " IMPLIED");
564 break;
565 case XML_ATTRIBUTE_FIXED:
566 fprintf(ctxt->output, " FIXED");
567 break;
568 }
569 if (attr->defaultValue != NULL) {
570 fprintf(ctxt->output, "\"");
571 xmlCtxtDumpString(ctxt, attr->defaultValue);
572 fprintf(ctxt->output, "\"");
573 }
574 fprintf(ctxt->output, "\n");
575 }
576
577 /*
578 * Do a bit of checking
579 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000580 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000581}
582
583static void
584xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
585{
586 xmlCtxtDumpSpaces(ctxt);
587
588 if (elem == NULL) {
589 if (!ctxt->check)
590 fprintf(ctxt->output, "Element declaration is NULL\n");
591 return;
592 }
593 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000594 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
595 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000596 return;
597 }
598 if (elem->name != NULL) {
599 if (!ctxt->check) {
600 fprintf(ctxt->output, "ELEMDECL(");
601 xmlCtxtDumpString(ctxt, elem->name);
602 fprintf(ctxt->output, ")");
603 }
604 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000605 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
606 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000607 if (!ctxt->check) {
608 switch (elem->etype) {
609 case XML_ELEMENT_TYPE_UNDEFINED:
610 fprintf(ctxt->output, ", UNDEFINED");
611 break;
612 case XML_ELEMENT_TYPE_EMPTY:
613 fprintf(ctxt->output, ", EMPTY");
614 break;
615 case XML_ELEMENT_TYPE_ANY:
616 fprintf(ctxt->output, ", ANY");
617 break;
618 case XML_ELEMENT_TYPE_MIXED:
619 fprintf(ctxt->output, ", MIXED ");
620 break;
621 case XML_ELEMENT_TYPE_ELEMENT:
622 fprintf(ctxt->output, ", MIXED ");
623 break;
624 }
625 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
626 char buf[5001];
627
628 buf[0] = 0;
629 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
630 buf[5000] = 0;
631 fprintf(ctxt->output, "%s", buf);
632 }
633 fprintf(ctxt->output, "\n");
634 }
635
636 /*
637 * Do a bit of checking
638 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000639 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000640}
641
642static void
643xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
644{
645 xmlCtxtDumpSpaces(ctxt);
646
647 if (ent == NULL) {
648 if (!ctxt->check)
649 fprintf(ctxt->output, "Entity declaration is NULL\n");
650 return;
651 }
652 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000653 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
654 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000655 return;
656 }
657 if (ent->name != NULL) {
658 if (!ctxt->check) {
659 fprintf(ctxt->output, "ENTITYDECL(");
660 xmlCtxtDumpString(ctxt, ent->name);
661 fprintf(ctxt->output, ")");
662 }
663 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000664 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
665 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000666 if (!ctxt->check) {
667 switch (ent->etype) {
668 case XML_INTERNAL_GENERAL_ENTITY:
669 fprintf(ctxt->output, ", internal\n");
670 break;
671 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
672 fprintf(ctxt->output, ", external parsed\n");
673 break;
674 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
675 fprintf(ctxt->output, ", unparsed\n");
676 break;
677 case XML_INTERNAL_PARAMETER_ENTITY:
678 fprintf(ctxt->output, ", parameter\n");
679 break;
680 case XML_EXTERNAL_PARAMETER_ENTITY:
681 fprintf(ctxt->output, ", external parameter\n");
682 break;
683 case XML_INTERNAL_PREDEFINED_ENTITY:
684 fprintf(ctxt->output, ", predefined\n");
685 break;
686 }
687 if (ent->ExternalID) {
688 xmlCtxtDumpSpaces(ctxt);
689 fprintf(ctxt->output, " ExternalID=%s\n",
690 (char *) ent->ExternalID);
691 }
692 if (ent->SystemID) {
693 xmlCtxtDumpSpaces(ctxt);
694 fprintf(ctxt->output, " SystemID=%s\n",
695 (char *) ent->SystemID);
696 }
697 if (ent->URI != NULL) {
698 xmlCtxtDumpSpaces(ctxt);
699 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
700 }
701 if (ent->content) {
702 xmlCtxtDumpSpaces(ctxt);
703 fprintf(ctxt->output, " content=");
704 xmlCtxtDumpString(ctxt, ent->content);
705 fprintf(ctxt->output, "\n");
706 }
707 }
708
709 /*
710 * Do a bit of checking
711 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000712 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000713}
714
715static void
716xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
717{
718 xmlCtxtDumpSpaces(ctxt);
719
720 if (ns == NULL) {
721 if (!ctxt->check)
722 fprintf(ctxt->output, "namespace node is NULL\n");
723 return;
724 }
725 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000726 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
727 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000728 return;
729 }
730 if (ns->href == NULL) {
731 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000732 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
733 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000734 (char *) ns->prefix);
735 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000736 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
737 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000738 } else {
739 if (!ctxt->check) {
740 if (ns->prefix != NULL)
741 fprintf(ctxt->output, "namespace %s href=",
742 (char *) ns->prefix);
743 else
744 fprintf(ctxt->output, "default namespace href=");
745
746 xmlCtxtDumpString(ctxt, ns->href);
747 fprintf(ctxt->output, "\n");
748 }
749 }
750}
751
752static void
753xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
754{
755 while (ns != NULL) {
756 xmlCtxtDumpNamespace(ctxt, ns);
757 ns = ns->next;
758 }
759}
760
761static void
762xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
763{
764 xmlCtxtDumpSpaces(ctxt);
765
766 if (ent == NULL) {
767 if (!ctxt->check)
768 fprintf(ctxt->output, "Entity is NULL\n");
769 return;
770 }
771 if (!ctxt->check) {
772 switch (ent->etype) {
773 case XML_INTERNAL_GENERAL_ENTITY:
774 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
775 break;
776 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
777 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
778 break;
779 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
780 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
781 break;
782 case XML_INTERNAL_PARAMETER_ENTITY:
783 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
784 break;
785 case XML_EXTERNAL_PARAMETER_ENTITY:
786 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
787 break;
788 default:
789 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
790 }
791 fprintf(ctxt->output, "%s\n", ent->name);
792 if (ent->ExternalID) {
793 xmlCtxtDumpSpaces(ctxt);
794 fprintf(ctxt->output, "ExternalID=%s\n",
795 (char *) ent->ExternalID);
796 }
797 if (ent->SystemID) {
798 xmlCtxtDumpSpaces(ctxt);
799 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
800 }
801 if (ent->URI) {
802 xmlCtxtDumpSpaces(ctxt);
803 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
804 }
805 if (ent->content) {
806 xmlCtxtDumpSpaces(ctxt);
807 fprintf(ctxt->output, "content=");
808 xmlCtxtDumpString(ctxt, ent->content);
809 fprintf(ctxt->output, "\n");
810 }
811 }
812}
813
814/**
815 * xmlCtxtDumpAttr:
816 * @output: the FILE * for the output
817 * @attr: the attribute
818 * @depth: the indentation level.
819 *
820 * Dumps debug information for the attribute
821 */
822static void
823xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
824{
825 xmlCtxtDumpSpaces(ctxt);
826
827 if (attr == NULL) {
828 if (!ctxt->check)
829 fprintf(ctxt->output, "Attr is NULL");
830 return;
831 }
832 if (!ctxt->check) {
833 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000834 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000835 fprintf(ctxt->output, "\n");
836 if (attr->children != NULL) {
837 ctxt->depth++;
838 xmlCtxtDumpNodeList(ctxt, attr->children);
839 ctxt->depth--;
840 }
841 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000842 if (attr->name == NULL)
843 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
844 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000845
846 /*
847 * Do a bit of checking
848 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000849 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000850}
851
852/**
853 * xmlCtxtDumpAttrList:
854 * @output: the FILE * for the output
855 * @attr: the attribute list
856 * @depth: the indentation level.
857 *
858 * Dumps debug information for the attribute list
859 */
860static void
861xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
862{
863 while (attr != NULL) {
864 xmlCtxtDumpAttr(ctxt, attr);
865 attr = attr->next;
866 }
867}
868
869/**
870 * xmlCtxtDumpOneNode:
871 * @output: the FILE * for the output
872 * @node: the node
873 * @depth: the indentation level.
874 *
875 * Dumps debug information for the element node, it is not recursive
876 */
877static void
878xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
879{
880 if (node == NULL) {
881 if (!ctxt->check) {
882 xmlCtxtDumpSpaces(ctxt);
883 fprintf(ctxt->output, "node is NULL\n");
884 }
885 return;
886 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000887 ctxt->node = node;
888
Daniel Veillard22cdb842004-10-04 14:09:17 +0000889 switch (node->type) {
890 case XML_ELEMENT_NODE:
891 if (!ctxt->check) {
892 xmlCtxtDumpSpaces(ctxt);
893 fprintf(ctxt->output, "ELEMENT ");
894 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
895 xmlCtxtDumpString(ctxt, node->ns->prefix);
896 fprintf(ctxt->output, ":");
897 }
898 xmlCtxtDumpString(ctxt, node->name);
899 fprintf(ctxt->output, "\n");
900 }
901 break;
902 case XML_ATTRIBUTE_NODE:
903 if (!ctxt->check)
904 xmlCtxtDumpSpaces(ctxt);
905 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000906 xmlCtxtGenericNodeCheck(ctxt, node);
907 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000908 case XML_TEXT_NODE:
909 if (!ctxt->check) {
910 xmlCtxtDumpSpaces(ctxt);
911 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard8874b942005-08-25 13:19:21 +0000912 fprintf(ctxt->output, "TEXT no enc");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000913 else
Daniel Veillard8874b942005-08-25 13:19:21 +0000914 fprintf(ctxt->output, "TEXT");
Daniel Veillardcfa303a2005-08-25 14:03:56 +0000915 if (ctxt->options & DUMP_TEXT_TYPE) {
916 if (node->content == (xmlChar *) &(node->properties))
917 fprintf(ctxt->output, " compact\n");
918 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
919 fprintf(ctxt->output, " interned\n");
920 else
921 fprintf(ctxt->output, "\n");
922 } else
Daniel Veillard8874b942005-08-25 13:19:21 +0000923 fprintf(ctxt->output, "\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000924 }
925 break;
926 case XML_CDATA_SECTION_NODE:
927 if (!ctxt->check) {
928 xmlCtxtDumpSpaces(ctxt);
929 fprintf(ctxt->output, "CDATA_SECTION\n");
930 }
931 break;
932 case XML_ENTITY_REF_NODE:
933 if (!ctxt->check) {
934 xmlCtxtDumpSpaces(ctxt);
935 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
936 (char *) node->name);
937 }
938 break;
939 case XML_ENTITY_NODE:
940 if (!ctxt->check) {
941 xmlCtxtDumpSpaces(ctxt);
942 fprintf(ctxt->output, "ENTITY\n");
943 }
944 break;
945 case XML_PI_NODE:
946 if (!ctxt->check) {
947 xmlCtxtDumpSpaces(ctxt);
948 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
949 }
950 break;
951 case XML_COMMENT_NODE:
952 if (!ctxt->check) {
953 xmlCtxtDumpSpaces(ctxt);
954 fprintf(ctxt->output, "COMMENT\n");
955 }
956 break;
957 case XML_DOCUMENT_NODE:
958 case XML_HTML_DOCUMENT_NODE:
959 if (!ctxt->check) {
960 xmlCtxtDumpSpaces(ctxt);
961 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000962 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
963 xmlCtxtGenericNodeCheck(ctxt, node);
964 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000965 case XML_DOCUMENT_TYPE_NODE:
966 if (!ctxt->check) {
967 xmlCtxtDumpSpaces(ctxt);
968 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
969 }
970 break;
971 case XML_DOCUMENT_FRAG_NODE:
972 if (!ctxt->check) {
973 xmlCtxtDumpSpaces(ctxt);
974 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
975 }
976 break;
977 case XML_NOTATION_NODE:
978 if (!ctxt->check) {
979 xmlCtxtDumpSpaces(ctxt);
980 fprintf(ctxt->output, "NOTATION\n");
981 }
982 break;
983 case XML_DTD_NODE:
984 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
985 return;
986 case XML_ELEMENT_DECL:
987 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
988 return;
989 case XML_ATTRIBUTE_DECL:
990 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
991 return;
992 case XML_ENTITY_DECL:
993 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
994 return;
995 case XML_NAMESPACE_DECL:
996 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
997 return;
998 case XML_XINCLUDE_START:
999 if (!ctxt->check) {
1000 xmlCtxtDumpSpaces(ctxt);
1001 fprintf(ctxt->output, "INCLUDE START\n");
1002 }
1003 return;
1004 case XML_XINCLUDE_END:
1005 if (!ctxt->check) {
1006 xmlCtxtDumpSpaces(ctxt);
1007 fprintf(ctxt->output, "INCLUDE END\n");
1008 }
1009 return;
1010 default:
1011 if (!ctxt->check)
1012 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001013 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1014 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001015 return;
1016 }
1017 if (node->doc == NULL) {
1018 if (!ctxt->check) {
1019 xmlCtxtDumpSpaces(ctxt);
1020 }
1021 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1022 }
1023 ctxt->depth++;
Daniel Veillard8874b942005-08-25 13:19:21 +00001024 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001025 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
Daniel Veillard8874b942005-08-25 13:19:21 +00001026 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001027 xmlCtxtDumpAttrList(ctxt, node->properties);
1028 if (node->type != XML_ENTITY_REF_NODE) {
1029 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1030 if (!ctxt->check) {
1031 xmlCtxtDumpSpaces(ctxt);
1032 fprintf(ctxt->output, "content=");
1033 xmlCtxtDumpString(ctxt, node->content);
1034 fprintf(ctxt->output, "\n");
1035 }
1036 }
1037 } else {
1038 xmlEntityPtr ent;
1039
1040 ent = xmlGetDocEntity(node->doc, node->name);
1041 if (ent != NULL)
1042 xmlCtxtDumpEntity(ctxt, ent);
1043 }
1044 ctxt->depth--;
1045
1046 /*
1047 * Do a bit of checking
1048 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001049 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001050}
1051
1052/**
1053 * xmlCtxtDumpNode:
1054 * @output: the FILE * for the output
1055 * @node: the node
1056 * @depth: the indentation level.
1057 *
1058 * Dumps debug information for the element node, it is recursive
1059 */
1060static void
1061xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1062{
1063 if (node == NULL) {
1064 if (!ctxt->check) {
1065 xmlCtxtDumpSpaces(ctxt);
1066 fprintf(ctxt->output, "node is NULL\n");
1067 }
1068 return;
1069 }
1070 xmlCtxtDumpOneNode(ctxt, node);
Daniel Veillard7837dd82005-09-06 22:16:57 +00001071 if ((node->type != XML_NAMESPACE_DECL) &&
1072 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001073 ctxt->depth++;
1074 xmlCtxtDumpNodeList(ctxt, node->children);
1075 ctxt->depth--;
1076 }
1077}
1078
1079/**
1080 * xmlCtxtDumpNodeList:
1081 * @output: the FILE * for the output
1082 * @node: the node list
1083 * @depth: the indentation level.
1084 *
1085 * Dumps debug information for the list of element node, it is recursive
1086 */
1087static void
1088xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1089{
1090 while (node != NULL) {
1091 xmlCtxtDumpNode(ctxt, node);
1092 node = node->next;
1093 }
1094}
1095
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001096static void
1097xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1098{
1099 if (doc == NULL) {
1100 if (!ctxt->check)
1101 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1102 return;
1103 }
1104 ctxt->node = (xmlNodePtr) doc;
1105
1106 switch (doc->type) {
1107 case XML_ELEMENT_NODE:
1108 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1109 "Misplaced ELEMENT node\n");
1110 break;
1111 case XML_ATTRIBUTE_NODE:
1112 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1113 "Misplaced ATTRIBUTE node\n");
1114 break;
1115 case XML_TEXT_NODE:
1116 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1117 "Misplaced TEXT node\n");
1118 break;
1119 case XML_CDATA_SECTION_NODE:
1120 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1121 "Misplaced CDATA node\n");
1122 break;
1123 case XML_ENTITY_REF_NODE:
1124 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1125 "Misplaced ENTITYREF node\n");
1126 break;
1127 case XML_ENTITY_NODE:
1128 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1129 "Misplaced ENTITY node\n");
1130 break;
1131 case XML_PI_NODE:
1132 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1133 "Misplaced PI node\n");
1134 break;
1135 case XML_COMMENT_NODE:
1136 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1137 "Misplaced COMMENT node\n");
1138 break;
1139 case XML_DOCUMENT_NODE:
1140 if (!ctxt->check)
1141 fprintf(ctxt->output, "DOCUMENT\n");
1142 break;
1143 case XML_HTML_DOCUMENT_NODE:
1144 if (!ctxt->check)
1145 fprintf(ctxt->output, "HTML DOCUMENT\n");
1146 break;
1147 case XML_DOCUMENT_TYPE_NODE:
1148 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1149 "Misplaced DOCTYPE node\n");
1150 break;
1151 case XML_DOCUMENT_FRAG_NODE:
1152 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1153 "Misplaced FRAGMENT node\n");
1154 break;
1155 case XML_NOTATION_NODE:
1156 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1157 "Misplaced NOTATION node\n");
1158 break;
1159 default:
1160 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1161 "Unknown node type %d\n", doc->type);
1162 }
1163}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001164
1165/**
1166 * xmlCtxtDumpDocumentHead:
1167 * @output: the FILE * for the output
1168 * @doc: the document
1169 *
1170 * Dumps debug information cncerning the document, not recursive
1171 */
1172static void
1173xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1174{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001175 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001176 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001177 if (!ctxt->check) {
1178 if (doc->name != NULL) {
1179 fprintf(ctxt->output, "name=");
1180 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1181 fprintf(ctxt->output, "\n");
1182 }
1183 if (doc->version != NULL) {
1184 fprintf(ctxt->output, "version=");
1185 xmlCtxtDumpString(ctxt, doc->version);
1186 fprintf(ctxt->output, "\n");
1187 }
1188 if (doc->encoding != NULL) {
1189 fprintf(ctxt->output, "encoding=");
1190 xmlCtxtDumpString(ctxt, doc->encoding);
1191 fprintf(ctxt->output, "\n");
1192 }
1193 if (doc->URL != NULL) {
1194 fprintf(ctxt->output, "URL=");
1195 xmlCtxtDumpString(ctxt, doc->URL);
1196 fprintf(ctxt->output, "\n");
1197 }
1198 if (doc->standalone)
1199 fprintf(ctxt->output, "standalone=true\n");
1200 }
1201 if (doc->oldNs != NULL)
1202 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1203}
1204
1205/**
1206 * xmlCtxtDumpDocument:
1207 * @output: the FILE * for the output
1208 * @doc: the document
1209 *
1210 * Dumps debug information for the document, it's recursive
1211 */
1212static void
1213xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1214{
1215 if (doc == NULL) {
1216 if (!ctxt->check)
1217 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1218 return;
1219 }
1220 xmlCtxtDumpDocumentHead(ctxt, doc);
1221 if (((doc->type == XML_DOCUMENT_NODE) ||
1222 (doc->type == XML_HTML_DOCUMENT_NODE))
1223 && (doc->children != NULL)) {
1224 ctxt->depth++;
1225 xmlCtxtDumpNodeList(ctxt, doc->children);
1226 ctxt->depth--;
1227 }
1228}
1229
1230static void
1231xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1232{
1233 if (cur == NULL) {
1234 if (!ctxt->check)
1235 fprintf(ctxt->output, "Entity is NULL");
1236 return;
1237 }
1238 if (!ctxt->check) {
1239 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1240 switch (cur->etype) {
1241 case XML_INTERNAL_GENERAL_ENTITY:
1242 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1243 break;
1244 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1245 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1246 break;
1247 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1248 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1249 break;
1250 case XML_INTERNAL_PARAMETER_ENTITY:
1251 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1252 break;
1253 case XML_EXTERNAL_PARAMETER_ENTITY:
1254 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1255 break;
1256 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001257 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1258 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001259 }
1260 if (cur->ExternalID != NULL)
1261 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1262 if (cur->SystemID != NULL)
1263 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1264 if (cur->orig != NULL)
1265 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1266 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1267 fprintf(ctxt->output, "\n content \"%s\"",
1268 (char *) cur->content);
1269 fprintf(ctxt->output, "\n");
1270 }
1271}
1272
1273/**
1274 * xmlCtxtDumpEntities:
1275 * @output: the FILE * for the output
1276 * @doc: the document
1277 *
1278 * Dumps debug information for all the entities in use by the document
1279 */
1280static void
1281xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1282{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001283 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001284 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001285 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1286 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1287 doc->intSubset->entities;
1288
1289 if (!ctxt->check)
1290 fprintf(ctxt->output, "Entities in internal subset\n");
1291 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1292 ctxt);
1293 } else
1294 fprintf(ctxt->output, "No entities in internal subset\n");
1295 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1296 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1297 doc->extSubset->entities;
1298
1299 if (!ctxt->check)
1300 fprintf(ctxt->output, "Entities in external subset\n");
1301 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1302 ctxt);
1303 } else if (!ctxt->check)
1304 fprintf(ctxt->output, "No entities in external subset\n");
1305}
1306
1307/**
1308 * xmlCtxtDumpDTD:
1309 * @output: the FILE * for the output
1310 * @dtd: the DTD
1311 *
1312 * Dumps debug information for the DTD
1313 */
1314static void
1315xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1316{
1317 if (dtd == NULL) {
1318 if (!ctxt->check)
1319 fprintf(ctxt->output, "DTD is NULL\n");
1320 return;
1321 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001322 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001323 if (dtd->children == NULL)
1324 fprintf(ctxt->output, " DTD is empty\n");
1325 else {
1326 ctxt->depth++;
1327 xmlCtxtDumpNodeList(ctxt, dtd->children);
1328 ctxt->depth--;
1329 }
1330}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001331
Daniel Veillard22cdb842004-10-04 14:09:17 +00001332/************************************************************************
1333 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001334 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001335 * *
1336 ************************************************************************/
1337
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001338/**
1339 * xmlDebugDumpString:
1340 * @output: the FILE * for the output
1341 * @str: the string
1342 *
1343 * Dumps informations about the string, shorten it if necessary
1344 */
1345void
1346xmlDebugDumpString(FILE * output, const xmlChar * str)
1347{
Owen Taylor3473f882001-02-23 17:55:21 +00001348 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001349
Daniel Veillard7db38712002-02-07 16:39:11 +00001350 if (output == NULL)
1351 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001352 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001353 fprintf(output, "(NULL)");
1354 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001355 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001356 for (i = 0; i < 40; i++)
1357 if (str[i] == 0)
1358 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001359 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001360 fputc(' ', output);
1361 else if (str[i] >= 0x80)
1362 fprintf(output, "#%X", str[i]);
1363 else
1364 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001365 fprintf(output, "...");
1366}
1367
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001368/**
1369 * xmlDebugDumpAttr:
1370 * @output: the FILE * for the output
1371 * @attr: the attribute
1372 * @depth: the indentation level.
1373 *
1374 * Dumps debug information for the attribute
1375 */
1376void
1377xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001378 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001379
Daniel Veillarda82b1822004-11-08 16:24:57 +00001380 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001381 xmlCtxtDumpInitCtxt(&ctxt);
1382 ctxt.output = output;
1383 ctxt.depth = depth;
1384 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001385 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001386}
Owen Taylor3473f882001-02-23 17:55:21 +00001387
Owen Taylor3473f882001-02-23 17:55:21 +00001388
Daniel Veillard22cdb842004-10-04 14:09:17 +00001389/**
1390 * xmlDebugDumpEntities:
1391 * @output: the FILE * for the output
1392 * @doc: the document
1393 *
1394 * Dumps debug information for all the entities in use by the document
1395 */
1396void
1397xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1398{
1399 xmlDebugCtxt ctxt;
1400
Daniel Veillarda82b1822004-11-08 16:24:57 +00001401 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001402 xmlCtxtDumpInitCtxt(&ctxt);
1403 ctxt.output = output;
1404 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001405 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001406}
1407
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001408/**
1409 * xmlDebugDumpAttrList:
1410 * @output: the FILE * for the output
1411 * @attr: the attribute list
1412 * @depth: the indentation level.
1413 *
1414 * Dumps debug information for the attribute list
1415 */
1416void
1417xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1418{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001419 xmlDebugCtxt ctxt;
1420
Daniel Veillarda82b1822004-11-08 16:24:57 +00001421 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001422 xmlCtxtDumpInitCtxt(&ctxt);
1423 ctxt.output = output;
1424 ctxt.depth = depth;
1425 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001426 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001427}
1428
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001429/**
1430 * xmlDebugDumpOneNode:
1431 * @output: the FILE * for the output
1432 * @node: the node
1433 * @depth: the indentation level.
1434 *
1435 * Dumps debug information for the element node, it is not recursive
1436 */
1437void
1438xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1439{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001440 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001441
Daniel Veillarda82b1822004-11-08 16:24:57 +00001442 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001443 xmlCtxtDumpInitCtxt(&ctxt);
1444 ctxt.output = output;
1445 ctxt.depth = depth;
1446 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001447 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001448}
1449
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001450/**
1451 * xmlDebugDumpNode:
1452 * @output: the FILE * for the output
1453 * @node: the node
1454 * @depth: the indentation level.
1455 *
1456 * Dumps debug information for the element node, it is recursive
1457 */
1458void
1459xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1460{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001461 xmlDebugCtxt ctxt;
1462
Daniel Veillard7db38712002-02-07 16:39:11 +00001463 if (output == NULL)
1464 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001465 xmlCtxtDumpInitCtxt(&ctxt);
1466 ctxt.output = output;
1467 ctxt.depth = depth;
1468 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001469 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001470}
1471
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001472/**
1473 * xmlDebugDumpNodeList:
1474 * @output: the FILE * for the output
1475 * @node: the node list
1476 * @depth: the indentation level.
1477 *
1478 * Dumps debug information for the list of element node, it is recursive
1479 */
1480void
1481xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1482{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001483 xmlDebugCtxt ctxt;
1484
Daniel Veillard7db38712002-02-07 16:39:11 +00001485 if (output == NULL)
1486 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001487 xmlCtxtDumpInitCtxt(&ctxt);
1488 ctxt.output = output;
1489 ctxt.depth = depth;
1490 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001491 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001492}
1493
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001494/**
1495 * xmlDebugDumpDocumentHead:
1496 * @output: the FILE * for the output
1497 * @doc: the document
1498 *
1499 * Dumps debug information cncerning the document, not recursive
1500 */
1501void
1502xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1503{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001504 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001505
Daniel Veillard22cdb842004-10-04 14:09:17 +00001506 if (output == NULL)
1507 output = stdout;
1508 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001509 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001510 ctxt.output = output;
1511 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001512 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001513}
1514
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001515/**
1516 * xmlDebugDumpDocument:
1517 * @output: the FILE * for the output
1518 * @doc: the document
1519 *
1520 * Dumps debug information for the document, it's recursive
1521 */
1522void
1523xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1524{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001525 xmlDebugCtxt ctxt;
1526
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001527 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001528 output = stdout;
1529 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001530 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001531 ctxt.output = output;
1532 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001533 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001534}
Owen Taylor3473f882001-02-23 17:55:21 +00001535
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001536/**
1537 * xmlDebugDumpDTD:
1538 * @output: the FILE * for the output
1539 * @dtd: the DTD
1540 *
1541 * Dumps debug information for the DTD
1542 */
1543void
1544xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1545{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001546 xmlDebugCtxt ctxt;
1547
Daniel Veillard7db38712002-02-07 16:39:11 +00001548 if (output == NULL)
1549 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001550 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001551 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001552 ctxt.output = output;
1553 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001554 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001555}
1556
Daniel Veillard22cdb842004-10-04 14:09:17 +00001557/************************************************************************
1558 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001559 * Public entry points for checkings *
1560 * *
1561 ************************************************************************/
1562
1563/**
1564 * xmlDebugCheckDocument:
1565 * @output: the FILE * for the output
1566 * @doc: the document
1567 *
1568 * Check the document for potential content problems, and output
1569 * the errors to @output
1570 *
1571 * Returns the number of errors found
1572 */
1573int
1574xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1575{
1576 xmlDebugCtxt ctxt;
1577
1578 if (output == NULL)
1579 output = stdout;
1580 xmlCtxtDumpInitCtxt(&ctxt);
1581 ctxt.output = output;
1582 ctxt.check = 1;
1583 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001584 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001585 return(ctxt.errors);
1586}
1587
1588/************************************************************************
1589 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001590 * Helpers for Shell *
1591 * *
1592 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001593
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001594/**
1595 * xmlLsCountNode:
1596 * @node: the node to count
1597 *
1598 * Count the children of @node.
1599 *
1600 * Returns the number of children of @node.
1601 */
1602int
1603xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001604 int ret = 0;
1605 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001606
1607 if (node == NULL)
1608 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001609
1610 switch (node->type) {
1611 case XML_ELEMENT_NODE:
1612 list = node->children;
1613 break;
1614 case XML_DOCUMENT_NODE:
1615 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001616#ifdef LIBXML_DOCB_ENABLED
1617 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001618#endif
1619 list = ((xmlDocPtr) node)->children;
1620 break;
1621 case XML_ATTRIBUTE_NODE:
1622 list = ((xmlAttrPtr) node)->children;
1623 break;
1624 case XML_TEXT_NODE:
1625 case XML_CDATA_SECTION_NODE:
1626 case XML_PI_NODE:
1627 case XML_COMMENT_NODE:
1628 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001629 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001630 }
1631 break;
1632 case XML_ENTITY_REF_NODE:
1633 case XML_DOCUMENT_TYPE_NODE:
1634 case XML_ENTITY_NODE:
1635 case XML_DOCUMENT_FRAG_NODE:
1636 case XML_NOTATION_NODE:
1637 case XML_DTD_NODE:
1638 case XML_ELEMENT_DECL:
1639 case XML_ATTRIBUTE_DECL:
1640 case XML_ENTITY_DECL:
1641 case XML_NAMESPACE_DECL:
1642 case XML_XINCLUDE_START:
1643 case XML_XINCLUDE_END:
1644 ret = 1;
1645 break;
1646 }
1647 for (;list != NULL;ret++)
1648 list = list->next;
1649 return(ret);
1650}
1651
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001652/**
1653 * xmlLsOneNode:
1654 * @output: the FILE * for the output
1655 * @node: the node to dump
1656 *
1657 * Dump to @output the type and name of @node.
1658 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001659void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001660xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001661 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001662 if (node == NULL) {
1663 fprintf(output, "NULL\n");
1664 return;
1665 }
Owen Taylor3473f882001-02-23 17:55:21 +00001666 switch (node->type) {
1667 case XML_ELEMENT_NODE:
1668 fprintf(output, "-");
1669 break;
1670 case XML_ATTRIBUTE_NODE:
1671 fprintf(output, "a");
1672 break;
1673 case XML_TEXT_NODE:
1674 fprintf(output, "t");
1675 break;
1676 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001677 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001678 break;
1679 case XML_ENTITY_REF_NODE:
1680 fprintf(output, "e");
1681 break;
1682 case XML_ENTITY_NODE:
1683 fprintf(output, "E");
1684 break;
1685 case XML_PI_NODE:
1686 fprintf(output, "p");
1687 break;
1688 case XML_COMMENT_NODE:
1689 fprintf(output, "c");
1690 break;
1691 case XML_DOCUMENT_NODE:
1692 fprintf(output, "d");
1693 break;
1694 case XML_HTML_DOCUMENT_NODE:
1695 fprintf(output, "h");
1696 break;
1697 case XML_DOCUMENT_TYPE_NODE:
1698 fprintf(output, "T");
1699 break;
1700 case XML_DOCUMENT_FRAG_NODE:
1701 fprintf(output, "F");
1702 break;
1703 case XML_NOTATION_NODE:
1704 fprintf(output, "N");
1705 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001706 case XML_NAMESPACE_DECL:
1707 fprintf(output, "n");
1708 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001709 default:
1710 fprintf(output, "?");
1711 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001712 if (node->type != XML_NAMESPACE_DECL) {
1713 if (node->properties != NULL)
1714 fprintf(output, "a");
1715 else
1716 fprintf(output, "-");
1717 if (node->nsDef != NULL)
1718 fprintf(output, "n");
1719 else
1720 fprintf(output, "-");
1721 }
Owen Taylor3473f882001-02-23 17:55:21 +00001722
1723 fprintf(output, " %8d ", xmlLsCountNode(node));
1724
1725 switch (node->type) {
1726 case XML_ELEMENT_NODE:
1727 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001728 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001729 break;
1730 case XML_ATTRIBUTE_NODE:
1731 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001732 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001733 break;
1734 case XML_TEXT_NODE:
1735 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001736 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001737 }
1738 break;
1739 case XML_CDATA_SECTION_NODE:
1740 break;
1741 case XML_ENTITY_REF_NODE:
1742 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001743 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001744 break;
1745 case XML_ENTITY_NODE:
1746 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001747 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001748 break;
1749 case XML_PI_NODE:
1750 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001751 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001752 break;
1753 case XML_COMMENT_NODE:
1754 break;
1755 case XML_DOCUMENT_NODE:
1756 break;
1757 case XML_HTML_DOCUMENT_NODE:
1758 break;
1759 case XML_DOCUMENT_TYPE_NODE:
1760 break;
1761 case XML_DOCUMENT_FRAG_NODE:
1762 break;
1763 case XML_NOTATION_NODE:
1764 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001765 case XML_NAMESPACE_DECL: {
1766 xmlNsPtr ns = (xmlNsPtr) node;
1767
1768 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001769 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001770 else
William M. Brack13dfa872004-09-18 04:52:08 +00001771 fprintf(output, "%s -> %s", (char *)ns->prefix,
1772 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001773 break;
1774 }
Owen Taylor3473f882001-02-23 17:55:21 +00001775 default:
1776 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001777 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001778 }
1779 fprintf(output, "\n");
1780}
1781
Daniel Veillard78d12092001-10-11 09:12:24 +00001782/**
1783 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001784 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001785 *
1786 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001787 *
1788 * Returns a pointer to either "True" or "False"
1789 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001790const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001791xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001792{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001793 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001794 return("True");
1795 else
1796 return("False");
1797}
1798
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001799#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001800/****************************************************************
1801 * *
1802 * The XML shell related functions *
1803 * *
1804 ****************************************************************/
1805
Daniel Veillard78d12092001-10-11 09:12:24 +00001806
1807
Owen Taylor3473f882001-02-23 17:55:21 +00001808/*
1809 * TODO: Improvement/cleanups for the XML shell
1810 * - allow to shell out an editor on a subpart
1811 * - cleanup function registrations (with help) and calling
1812 * - provide registration routines
1813 */
1814
1815/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001816 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001817 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001818 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001819 *
1820 * Print the xpath error to libxml default error channel
1821 */
1822void
1823xmlShellPrintXPathError(int errorType, const char *arg)
1824{
1825 const char *default_arg = "Result";
1826
1827 if (!arg)
1828 arg = default_arg;
1829
1830 switch (errorType) {
1831 case XPATH_UNDEFINED:
1832 xmlGenericError(xmlGenericErrorContext,
1833 "%s: no such node\n", arg);
1834 break;
1835
1836 case XPATH_BOOLEAN:
1837 xmlGenericError(xmlGenericErrorContext,
1838 "%s is a Boolean\n", arg);
1839 break;
1840 case XPATH_NUMBER:
1841 xmlGenericError(xmlGenericErrorContext,
1842 "%s is a number\n", arg);
1843 break;
1844 case XPATH_STRING:
1845 xmlGenericError(xmlGenericErrorContext,
1846 "%s is a string\n", arg);
1847 break;
1848 case XPATH_POINT:
1849 xmlGenericError(xmlGenericErrorContext,
1850 "%s is a point\n", arg);
1851 break;
1852 case XPATH_RANGE:
1853 xmlGenericError(xmlGenericErrorContext,
1854 "%s is a range\n", arg);
1855 break;
1856 case XPATH_LOCATIONSET:
1857 xmlGenericError(xmlGenericErrorContext,
1858 "%s is a range\n", arg);
1859 break;
1860 case XPATH_USERS:
1861 xmlGenericError(xmlGenericErrorContext,
1862 "%s is user-defined\n", arg);
1863 break;
1864 case XPATH_XSLT_TREE:
1865 xmlGenericError(xmlGenericErrorContext,
1866 "%s is an XSLT value tree\n", arg);
1867 break;
1868 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001869#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001870 xmlGenericError(xmlGenericErrorContext,
1871 "Try casting the result string function (xpath builtin)\n",
1872 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001873#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001874}
1875
1876
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001877#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001878/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001879 * xmlShellPrintNodeCtxt:
1880 * @ctxt : a non-null shell context
1881 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001882 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001883 * Print node to the output FILE
1884 */
1885static void
1886xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1887{
Daniel Veillard01992e02002-10-09 10:20:30 +00001888 FILE *fp;
1889
1890 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001891 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001892 if (ctxt == NULL)
1893 fp = stdout;
1894 else
1895 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001896
1897 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001898 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001899 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001900 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001901 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001902 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001903
Daniel Veillard01992e02002-10-09 10:20:30 +00001904 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001905}
1906
1907/**
1908 * xmlShellPrintNode:
1909 * @node : a non-null node to print to the output FILE
1910 *
1911 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001912 */
1913void
1914xmlShellPrintNode(xmlNodePtr node)
1915{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001916 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001917}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001918#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001919
Daniel Veillard78d12092001-10-11 09:12:24 +00001920/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001921 * xmlShellPrintXPathResultCtxt:
1922 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001923 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001924 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001925 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001926 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001927static void
1928xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001929{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001930 if (!ctxt)
1931 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001932
1933 if (list != NULL) {
1934 switch (list->type) {
1935 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001936#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001937 int indx;
1938
1939 if (list->nodesetval) {
1940 for (indx = 0; indx < list->nodesetval->nodeNr;
1941 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001942 xmlShellPrintNodeCtxt(ctxt,
1943 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001944 }
1945 } else {
1946 xmlGenericError(xmlGenericErrorContext,
1947 "Empty node set\n");
1948 }
1949 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001950#else
1951 xmlGenericError(xmlGenericErrorContext,
1952 "Node set\n");
1953#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001954 }
1955 case XPATH_BOOLEAN:
1956 xmlGenericError(xmlGenericErrorContext,
1957 "Is a Boolean:%s\n",
1958 xmlBoolToText(list->boolval));
1959 break;
1960 case XPATH_NUMBER:
1961 xmlGenericError(xmlGenericErrorContext,
1962 "Is a number:%0g\n", list->floatval);
1963 break;
1964 case XPATH_STRING:
1965 xmlGenericError(xmlGenericErrorContext,
1966 "Is a string:%s\n", list->stringval);
1967 break;
1968
1969 default:
1970 xmlShellPrintXPathError(list->type, NULL);
1971 }
1972 }
1973}
1974
1975/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001976 * xmlShellPrintXPathResult:
1977 * @list: a valid result generated by an xpath evaluation
1978 *
1979 * Prints result to the output FILE
1980 */
1981void
1982xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1983{
1984 xmlShellPrintXPathResultCtxt(NULL, list);
1985}
1986
1987/**
Owen Taylor3473f882001-02-23 17:55:21 +00001988 * xmlShellList:
1989 * @ctxt: the shell context
1990 * @arg: unused
1991 * @node: a node
1992 * @node2: unused
1993 *
1994 * Implements the XML shell function "ls"
1995 * Does an Unix like listing of the given node (like a directory)
1996 *
1997 * Returns 0
1998 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001999int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002000xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002001 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2002 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2003{
Owen Taylor3473f882001-02-23 17:55:21 +00002004 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002005 if (!ctxt)
2006 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002007 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002008 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002009 return (0);
2010 }
Owen Taylor3473f882001-02-23 17:55:21 +00002011 if ((node->type == XML_DOCUMENT_NODE) ||
2012 (node->type == XML_HTML_DOCUMENT_NODE)) {
2013 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002014 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002015 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00002016 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002017 } else if (node->children != NULL) {
2018 cur = node->children;
2019 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002020 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002021 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002022 }
2023 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002024 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002025 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002026 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002027 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002028}
2029
2030/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002031 * xmlShellBase:
2032 * @ctxt: the shell context
2033 * @arg: unused
2034 * @node: a node
2035 * @node2: unused
2036 *
2037 * Implements the XML shell function "base"
2038 * dumps the current XML base of the node
2039 *
2040 * Returns 0
2041 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002042int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002043xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002044 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2045 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2046{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002047 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002048 if (!ctxt)
2049 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002050 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002051 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002052 return (0);
2053 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002054
2055 base = xmlNodeGetBase(node->doc, node);
2056
2057 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002058 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002059 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002060 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002061 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002062 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002063 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002064}
2065
Daniel Veillardb34321c2004-03-04 17:09:47 +00002066#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002067/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002068 * xmlShellSetBase:
2069 * @ctxt: the shell context
2070 * @arg: the new base
2071 * @node: a node
2072 * @node2: unused
2073 *
2074 * Implements the XML shell function "setbase"
2075 * change the current XML base of the node
2076 *
2077 * Returns 0
2078 */
2079static int
2080xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2081 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2082 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2083{
2084 xmlNodeSetBase(node, (xmlChar*) arg);
2085 return (0);
2086}
Daniel Veillard2156d432004-03-04 15:59:36 +00002087#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002088
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002089#ifdef LIBXML_XPATH_ENABLED
2090/**
2091 * xmlShellRegisterNamespace:
2092 * @ctxt: the shell context
2093 * @arg: a string in prefix=nsuri format
2094 * @node: unused
2095 * @node2: unused
2096 *
2097 * Implements the XML shell function "setns"
2098 * register/unregister a prefix=namespace pair
2099 * on the XPath context
2100 *
2101 * Returns 0 on success and a negative value otherwise.
2102 */
2103static int
2104xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2105 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2106{
2107 xmlChar* nsListDup;
2108 xmlChar* prefix;
2109 xmlChar* href;
2110 xmlChar* next;
2111
2112 nsListDup = xmlStrdup((xmlChar *) arg);
2113 next = nsListDup;
2114 while(next != NULL) {
2115 /* skip spaces */
2116 /*while((*next) == ' ') next++;*/
2117 if((*next) == '\0') break;
2118
2119 /* find prefix */
2120 prefix = next;
2121 next = (xmlChar*)xmlStrchr(next, '=');
2122 if(next == NULL) {
2123 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2124 xmlFree(nsListDup);
2125 return(-1);
2126 }
2127 *(next++) = '\0';
2128
2129 /* find href */
2130 href = next;
2131 next = (xmlChar*)xmlStrchr(next, ' ');
2132 if(next != NULL) {
2133 *(next++) = '\0';
2134 }
2135
2136 /* do register namespace */
2137 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2138 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2139 xmlFree(nsListDup);
2140 return(-1);
2141 }
2142 }
2143
2144 xmlFree(nsListDup);
2145 return(0);
2146}
Daniel Veillard20887ee2005-07-04 09:27:40 +00002147/**
2148 * xmlShellRegisterRootNamespaces:
2149 * @ctxt: the shell context
2150 * @arg: unused
2151 * @node: the root element
2152 * @node2: unused
2153 *
2154 * Implements the XML shell function "setrootns"
2155 * which registers all namespaces declarations found on the root element.
2156 *
2157 * Returns 0 on success and a negative value otherwise.
2158 */
2159static int
2160xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2161 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2162{
2163 xmlNsPtr ns;
2164
2165 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2166 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2167 return(-1);
2168 ns = root->nsDef;
2169 while (ns != NULL) {
2170 if (ns->prefix == NULL)
2171 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2172 else
2173 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2174 ns = ns->next;
2175 }
2176 return(0);
2177}
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002178#endif
2179
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002180/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002181 * xmlShellGrep:
2182 * @ctxt: the shell context
2183 * @arg: the string or regular expression to find
2184 * @node: a node
2185 * @node2: unused
2186 *
2187 * Implements the XML shell function "grep"
2188 * dumps informations about the node (namespace, attributes, content).
2189 *
2190 * Returns 0
2191 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002192static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002193xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2194 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2195{
2196 if (!ctxt)
2197 return (0);
2198 if (node == NULL)
2199 return (0);
2200 if (arg == NULL)
2201 return (0);
2202#ifdef LIBXML_REGEXP_ENABLED
2203 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2204 (xmlStrchr((xmlChar *) arg, '*')) ||
2205 (xmlStrchr((xmlChar *) arg, '.')) ||
2206 (xmlStrchr((xmlChar *) arg, '['))) {
2207 }
2208#endif
2209 while (node != NULL) {
2210 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002211 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002212
2213 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2214 xmlShellList(ctxt, NULL, node, NULL);
2215 }
2216 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002217 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002218
2219 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002220 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002221 }
2222 }
2223
2224 /*
2225 * Browse the full subtree, deep first
2226 */
2227
2228 if ((node->type == XML_DOCUMENT_NODE) ||
2229 (node->type == XML_HTML_DOCUMENT_NODE)) {
2230 node = ((xmlDocPtr) node)->children;
2231 } else if ((node->children != NULL)
2232 && (node->type != XML_ENTITY_REF_NODE)) {
2233 /* deep first */
2234 node = node->children;
2235 } else if (node->next != NULL) {
2236 /* then siblings */
2237 node = node->next;
2238 } else {
2239 /* go up to parents->next if needed */
2240 while (node != NULL) {
2241 if (node->parent != NULL) {
2242 node = node->parent;
2243 }
2244 if (node->next != NULL) {
2245 node = node->next;
2246 break;
2247 }
2248 if (node->parent == NULL) {
2249 node = NULL;
2250 break;
2251 }
2252 }
2253 }
2254 }
2255 return (0);
2256}
2257
2258/**
Owen Taylor3473f882001-02-23 17:55:21 +00002259 * xmlShellDir:
2260 * @ctxt: the shell context
2261 * @arg: unused
2262 * @node: a node
2263 * @node2: unused
2264 *
2265 * Implements the XML shell function "dir"
2266 * dumps informations about the node (namespace, attributes, content).
2267 *
2268 * Returns 0
2269 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002270int
2271xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2272 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2273 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2274{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002275 if (!ctxt)
2276 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002277 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002278 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002279 return (0);
2280 }
Owen Taylor3473f882001-02-23 17:55:21 +00002281 if ((node->type == XML_DOCUMENT_NODE) ||
2282 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002283 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002284 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002285 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002286 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002287 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002288 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002289 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002290}
2291
Daniel Veillard29b17482004-08-16 00:39:03 +00002292/**
2293 * xmlShellSetContent:
2294 * @ctxt: the shell context
2295 * @value: the content as a string
2296 * @node: a node
2297 * @node2: unused
2298 *
2299 * Implements the XML shell function "dir"
2300 * dumps informations about the node (namespace, attributes, content).
2301 *
2302 * Returns 0
2303 */
2304static int
2305xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2306 char *value, xmlNodePtr node,
2307 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2308{
2309 xmlNodePtr results;
2310 xmlParserErrors ret;
2311
2312 if (!ctxt)
2313 return (0);
2314 if (node == NULL) {
2315 fprintf(ctxt->output, "NULL\n");
2316 return (0);
2317 }
2318 if (value == NULL) {
2319 fprintf(ctxt->output, "NULL\n");
2320 return (0);
2321 }
2322
2323 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2324 if (ret == XML_ERR_OK) {
2325 if (node->children != NULL) {
2326 xmlFreeNodeList(node->children);
2327 node->children = NULL;
2328 node->last = NULL;
2329 }
2330 xmlAddChildList(node, results);
2331 } else {
2332 fprintf(ctxt->output, "failed to parse content\n");
2333 }
2334 return (0);
2335}
2336
Daniel Veillard522bc602004-02-21 11:53:09 +00002337#ifdef LIBXML_SCHEMAS_ENABLED
2338/**
2339 * xmlShellRNGValidate:
2340 * @ctxt: the shell context
2341 * @schemas: the path to the Relax-NG schemas
2342 * @node: a node
2343 * @node2: unused
2344 *
2345 * Implements the XML shell function "relaxng"
2346 * validating the instance against a Relax-NG schemas
2347 *
2348 * Returns 0
2349 */
2350static int
2351xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2352 xmlNodePtr node ATTRIBUTE_UNUSED,
2353 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2354{
2355 xmlRelaxNGPtr relaxngschemas;
2356 xmlRelaxNGParserCtxtPtr ctxt;
2357 xmlRelaxNGValidCtxtPtr vctxt;
2358 int ret;
2359
2360 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2361 xmlRelaxNGSetParserErrors(ctxt,
2362 (xmlRelaxNGValidityErrorFunc) fprintf,
2363 (xmlRelaxNGValidityWarningFunc) fprintf,
2364 stderr);
2365 relaxngschemas = xmlRelaxNGParse(ctxt);
2366 xmlRelaxNGFreeParserCtxt(ctxt);
2367 if (relaxngschemas == NULL) {
2368 xmlGenericError(xmlGenericErrorContext,
2369 "Relax-NG schema %s failed to compile\n", schemas);
2370 return(-1);
2371 }
2372 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2373 xmlRelaxNGSetValidErrors(vctxt,
2374 (xmlRelaxNGValidityErrorFunc) fprintf,
2375 (xmlRelaxNGValidityWarningFunc) fprintf,
2376 stderr);
2377 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2378 if (ret == 0) {
2379 fprintf(stderr, "%s validates\n", sctxt->filename);
2380 } else if (ret > 0) {
2381 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2382 } else {
2383 fprintf(stderr, "%s validation generated an internal error\n",
2384 sctxt->filename);
2385 }
2386 xmlRelaxNGFreeValidCtxt(vctxt);
2387 if (relaxngschemas != NULL)
2388 xmlRelaxNGFree(relaxngschemas);
2389 return(0);
2390}
2391#endif
2392
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002393#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002394/**
2395 * xmlShellCat:
2396 * @ctxt: the shell context
2397 * @arg: unused
2398 * @node: a node
2399 * @node2: unused
2400 *
2401 * Implements the XML shell function "cat"
2402 * dumps the serialization node content (XML or HTML).
2403 *
2404 * Returns 0
2405 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002406int
2407xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2408 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2409{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002410 if (!ctxt)
2411 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002412 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002413 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002414 return (0);
2415 }
Owen Taylor3473f882001-02-23 17:55:21 +00002416 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2417#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002418 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002419 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002420 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002421 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002422#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002423 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002424 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002425 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002426 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002427#endif /* LIBXML_HTML_ENABLED */
2428 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002429 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002430 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002431 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002432 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002433 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002434 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002435 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002436}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002437#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002438
2439/**
2440 * xmlShellLoad:
2441 * @ctxt: the shell context
2442 * @filename: the file name
2443 * @node: unused
2444 * @node2: unused
2445 *
2446 * Implements the XML shell function "load"
2447 * loads a new document specified by the filename
2448 *
2449 * Returns 0 or -1 if loading failed
2450 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002451int
2452xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2453 xmlNodePtr node ATTRIBUTE_UNUSED,
2454 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2455{
Owen Taylor3473f882001-02-23 17:55:21 +00002456 xmlDocPtr doc;
2457 int html = 0;
2458
Daniel Veillarda82b1822004-11-08 16:24:57 +00002459 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002460 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002461 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002462
2463 if (html) {
2464#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002465 doc = htmlParseFile(filename, NULL);
2466#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002467 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002468 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002469#endif /* LIBXML_HTML_ENABLED */
2470 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002471 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002472 }
2473 if (doc != NULL) {
2474 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002475 xmlFreeDoc(ctxt->doc);
2476 }
2477 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002478#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002479 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002480#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002481 xmlFree(ctxt->filename);
2482 ctxt->doc = doc;
2483 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002484#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002485 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002486#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002487 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002488 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002489 return (-1);
2490 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002491}
2492
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002493#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002494/**
2495 * xmlShellWrite:
2496 * @ctxt: the shell context
2497 * @filename: the file name
2498 * @node: a node in the tree
2499 * @node2: unused
2500 *
2501 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002502 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002503 * of the subtree under the @node specified
2504 *
2505 * Returns 0 or -1 in case of error
2506 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002507int
Owen Taylor3473f882001-02-23 17:55:21 +00002508xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002509 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2510{
Owen Taylor3473f882001-02-23 17:55:21 +00002511 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002512 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002513 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002514 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002515 }
2516#ifdef W_OK
2517 if (access((char *) filename, W_OK)) {
2518 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002519 "Cannot write to %s\n", filename);
2520 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002521 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002522#endif
2523 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002524 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002525 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2526 xmlGenericError(xmlGenericErrorContext,
2527 "Failed to write to %s\n", filename);
2528 return (-1);
2529 }
2530 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002531 case XML_HTML_DOCUMENT_NODE:
2532#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002533 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2534 xmlGenericError(xmlGenericErrorContext,
2535 "Failed to write to %s\n", filename);
2536 return (-1);
2537 }
Owen Taylor3473f882001-02-23 17:55:21 +00002538#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002539 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2540 xmlGenericError(xmlGenericErrorContext,
2541 "Failed to write to %s\n", filename);
2542 return (-1);
2543 }
Owen Taylor3473f882001-02-23 17:55:21 +00002544#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002545 break;
2546 default:{
2547 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002548
Daniel Veillard78d12092001-10-11 09:12:24 +00002549 f = fopen((char *) filename, "w");
2550 if (f == NULL) {
2551 xmlGenericError(xmlGenericErrorContext,
2552 "Failed to write to %s\n", filename);
2553 return (-1);
2554 }
2555 xmlElemDump(f, ctxt->doc, node);
2556 fclose(f);
2557 }
Owen Taylor3473f882001-02-23 17:55:21 +00002558 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002559 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002560}
2561
2562/**
2563 * xmlShellSave:
2564 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002565 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002566 * @node: unused
2567 * @node2: unused
2568 *
2569 * Implements the XML shell function "save"
2570 * Write the current document to the filename, or it's original name
2571 *
2572 * Returns 0 or -1 in case of error
2573 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002574int
2575xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2576 xmlNodePtr node ATTRIBUTE_UNUSED,
2577 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2578{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002579 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002580 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002581 if ((filename == NULL) || (filename[0] == 0))
2582 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002583 if (filename == NULL)
2584 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002585#ifdef W_OK
2586 if (access((char *) filename, W_OK)) {
2587 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002588 "Cannot save to %s\n", filename);
2589 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002590 }
2591#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002592 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002593 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002594 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2595 xmlGenericError(xmlGenericErrorContext,
2596 "Failed to save to %s\n", filename);
2597 }
2598 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002599 case XML_HTML_DOCUMENT_NODE:
2600#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002601 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2602 xmlGenericError(xmlGenericErrorContext,
2603 "Failed to save to %s\n", filename);
2604 }
Owen Taylor3473f882001-02-23 17:55:21 +00002605#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002606 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2607 xmlGenericError(xmlGenericErrorContext,
2608 "Failed to save to %s\n", filename);
2609 }
Owen Taylor3473f882001-02-23 17:55:21 +00002610#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002611 break;
2612 default:
2613 xmlGenericError(xmlGenericErrorContext,
2614 "To save to subparts of a document use the 'write' command\n");
2615 return (-1);
2616
Owen Taylor3473f882001-02-23 17:55:21 +00002617 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002618 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002619}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002620#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002621
Daniel Veillardf54cd532004-02-25 11:52:31 +00002622#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002623/**
2624 * xmlShellValidate:
2625 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002626 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002627 * @node: unused
2628 * @node2: unused
2629 *
2630 * Implements the XML shell function "validate"
2631 * Validate the document, if a DTD path is provided, then the validation
2632 * is done against the given DTD.
2633 *
2634 * Returns 0 or -1 in case of error
2635 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002636int
2637xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2638 xmlNodePtr node ATTRIBUTE_UNUSED,
2639 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2640{
Owen Taylor3473f882001-02-23 17:55:21 +00002641 xmlValidCtxt vctxt;
2642 int res = -1;
2643
Daniel Veillarda82b1822004-11-08 16:24:57 +00002644 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002645 vctxt.userData = stderr;
2646 vctxt.error = (xmlValidityErrorFunc) fprintf;
2647 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2648
2649 if ((dtd == NULL) || (dtd[0] == 0)) {
2650 res = xmlValidateDocument(&vctxt, ctxt->doc);
2651 } else {
2652 xmlDtdPtr subset;
2653
Daniel Veillard78d12092001-10-11 09:12:24 +00002654 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2655 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002656 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2657
Daniel Veillard78d12092001-10-11 09:12:24 +00002658 xmlFreeDtd(subset);
2659 }
Owen Taylor3473f882001-02-23 17:55:21 +00002660 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002661 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002662}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002663#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002664
2665/**
2666 * xmlShellDu:
2667 * @ctxt: the shell context
2668 * @arg: unused
2669 * @tree: a node defining a subtree
2670 * @node2: unused
2671 *
2672 * Implements the XML shell function "du"
2673 * show the structure of the subtree under node @tree
2674 * If @tree is null, the command works on the current node.
2675 *
2676 * Returns 0 or -1 in case of error
2677 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002678int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002679xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002680 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2681 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2682{
Owen Taylor3473f882001-02-23 17:55:21 +00002683 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002684 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002685
Daniel Veillard321be0c2002-10-08 21:26:42 +00002686 if (!ctxt)
2687 return (-1);
2688
Daniel Veillard78d12092001-10-11 09:12:24 +00002689 if (tree == NULL)
2690 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002691 node = tree;
2692 while (node != NULL) {
2693 if ((node->type == XML_DOCUMENT_NODE) ||
2694 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002695 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002696 } else if (node->type == XML_ELEMENT_NODE) {
2697 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002698 fprintf(ctxt->output, " ");
2699 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002700 } else {
2701 }
Owen Taylor3473f882001-02-23 17:55:21 +00002702
Daniel Veillard78d12092001-10-11 09:12:24 +00002703 /*
2704 * Browse the full subtree, deep first
2705 */
Owen Taylor3473f882001-02-23 17:55:21 +00002706
2707 if ((node->type == XML_DOCUMENT_NODE) ||
2708 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002709 node = ((xmlDocPtr) node)->children;
2710 } else if ((node->children != NULL)
2711 && (node->type != XML_ENTITY_REF_NODE)) {
2712 /* deep first */
2713 node = node->children;
2714 indent++;
2715 } else if ((node != tree) && (node->next != NULL)) {
2716 /* then siblings */
2717 node = node->next;
2718 } else if (node != tree) {
2719 /* go up to parents->next if needed */
2720 while (node != tree) {
2721 if (node->parent != NULL) {
2722 node = node->parent;
2723 indent--;
2724 }
2725 if ((node != tree) && (node->next != NULL)) {
2726 node = node->next;
2727 break;
2728 }
2729 if (node->parent == NULL) {
2730 node = NULL;
2731 break;
2732 }
2733 if (node == tree) {
2734 node = NULL;
2735 break;
2736 }
2737 }
2738 /* exit condition */
2739 if (node == tree)
2740 node = NULL;
2741 } else
2742 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002743 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002744 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002745}
2746
2747/**
2748 * xmlShellPwd:
2749 * @ctxt: the shell context
2750 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002751 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002752 * @node2: unused
2753 *
2754 * Implements the XML shell function "pwd"
2755 * Show the full path from the root to the node, if needed building
2756 * thumblers when similar elements exists at a given ancestor level.
2757 * The output is compatible with XPath commands.
2758 *
2759 * Returns 0 or -1 in case of error
2760 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002761int
2762xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2763 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2764{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002765 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002766
Daniel Veillarda82b1822004-11-08 16:24:57 +00002767 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002768 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002769
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002770 path = xmlGetNodePath(node);
2771 if (path == NULL)
2772 return (-1);
2773
2774 /*
2775 * This test prevents buffer overflow, because this routine
2776 * is only called by xmlShell, in which the second argument is
2777 * 500 chars long.
2778 * It is a dirty hack before a cleaner solution is found.
2779 * Documentation should mention that the second argument must
2780 * be at least 500 chars long, and could be stripped if too long.
2781 */
2782 snprintf(buffer, 499, "%s", path);
2783 buffer[499] = '0';
2784 xmlFree(path);
2785
Daniel Veillard78d12092001-10-11 09:12:24 +00002786 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002787}
2788
2789/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002790 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002791 * @doc: the initial document
2792 * @filename: the output buffer
2793 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002794 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002795 *
2796 * Implements the XML shell
2797 * This allow to load, validate, view, modify and save a document
2798 * using a environment similar to a UNIX commandline.
2799 */
2800void
2801xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002802 FILE * output)
2803{
Owen Taylor3473f882001-02-23 17:55:21 +00002804 char prompt[500] = "/ > ";
2805 char *cmdline = NULL, *cur;
2806 int nbargs;
2807 char command[100];
2808 char arg[400];
2809 int i;
2810 xmlShellCtxtPtr ctxt;
2811 xmlXPathObjectPtr list;
2812
2813 if (doc == NULL)
2814 return;
2815 if (filename == NULL)
2816 return;
2817 if (input == NULL)
2818 return;
2819 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002820 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002821 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002822 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002823 return;
2824 ctxt->loaded = 0;
2825 ctxt->doc = doc;
2826 ctxt->input = input;
2827 ctxt->output = output;
2828 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002829 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002830
2831#ifdef LIBXML_XPATH_ENABLED
2832 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2833 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002834 xmlFree(ctxt);
2835 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002836 }
2837#endif /* LIBXML_XPATH_ENABLED */
2838 while (1) {
2839 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002840 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002841 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002842 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002843 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002844 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002845 prompt[sizeof(prompt) - 1] = 0;
2846
Daniel Veillard78d12092001-10-11 09:12:24 +00002847 /*
2848 * Get a new command line
2849 */
Owen Taylor3473f882001-02-23 17:55:21 +00002850 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002851 if (cmdline == NULL)
2852 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002853
Daniel Veillard78d12092001-10-11 09:12:24 +00002854 /*
2855 * Parse the command itself
2856 */
2857 cur = cmdline;
2858 nbargs = 0;
2859 while ((*cur == ' ') || (*cur == '\t'))
2860 cur++;
2861 i = 0;
2862 while ((*cur != ' ') && (*cur != '\t') &&
2863 (*cur != '\n') && (*cur != '\r')) {
2864 if (*cur == 0)
2865 break;
2866 command[i++] = *cur++;
2867 }
2868 command[i] = 0;
2869 if (i == 0)
2870 continue;
2871 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002872
Daniel Veillard78d12092001-10-11 09:12:24 +00002873 /*
2874 * Parse the argument
2875 */
2876 while ((*cur == ' ') || (*cur == '\t'))
2877 cur++;
2878 i = 0;
2879 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2880 if (*cur == 0)
2881 break;
2882 arg[i++] = *cur++;
2883 }
2884 arg[i] = 0;
2885 if (i != 0)
2886 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002887
Daniel Veillard78d12092001-10-11 09:12:24 +00002888 /*
2889 * start interpreting the command
2890 */
Owen Taylor3473f882001-02-23 17:55:21 +00002891 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002892 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002893 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002894 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002895 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002896 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002897 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002898 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2899 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2900 fprintf(ctxt->output, "\tbye leave shell\n");
2901 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2902 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2903 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2904 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2905 fprintf(ctxt->output, "\texit leave shell\n");
2906 fprintf(ctxt->output, "\thelp display this help\n");
2907 fprintf(ctxt->output, "\tfree display memory usage\n");
2908 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2909 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002910 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002911#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002912 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002913 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2914 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard20887ee2005-07-04 09:27:40 +00002915 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2916 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002917#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002918 fprintf(ctxt->output, "\tpwd display current working directory\n");
2919 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002920#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002921 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002922 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002923#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002924#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002925 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002926#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002927#ifdef LIBXML_SCHEMAS_ENABLED
2928 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2929#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002930 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002931#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002932 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002933 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002934#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002935 } else if (!strcmp(command, "load")) {
2936 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002937#ifdef LIBXML_SCHEMAS_ENABLED
2938 } else if (!strcmp(command, "relaxng")) {
2939 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2940#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002941#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002942 } else if (!strcmp(command, "save")) {
2943 xmlShellSave(ctxt, arg, NULL, NULL);
2944 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002945 if ((arg == NULL) || (arg[0] == 0))
2946 xmlGenericError(xmlGenericErrorContext,
2947 "Write command requires a filename argument\n");
2948 else
2949 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002950#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002951 } else if (!strcmp(command, "grep")) {
2952 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002953 } else if (!strcmp(command, "free")) {
2954 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002955 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002956 } else {
2957 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002958
Daniel Veillard78d12092001-10-11 09:12:24 +00002959 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002960 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002961 }
2962 } else if (!strcmp(command, "pwd")) {
2963 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002964
Daniel Veillard78d12092001-10-11 09:12:24 +00002965 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002966 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002967 } else if (!strcmp(command, "du")) {
2968 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2969 } else if (!strcmp(command, "base")) {
2970 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002971 } else if (!strcmp(command, "set")) {
2972 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002973#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002974 } else if (!strcmp(command, "setns")) {
2975 if (arg[0] == 0) {
2976 xmlGenericError(xmlGenericErrorContext,
2977 "setns: prefix=[nsuri] required\n");
2978 } else {
2979 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2980 }
Daniel Veillard20887ee2005-07-04 09:27:40 +00002981 } else if (!strcmp(command, "setrootns")) {
2982 xmlNodePtr root;
2983
2984 root = xmlDocGetRootElement(ctxt->doc);
2985 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002986 } else if (!strcmp(command, "xpath")) {
2987 if (arg[0] == 0) {
2988 xmlGenericError(xmlGenericErrorContext,
2989 "xpath: expression required\n");
2990 } else {
2991 ctxt->pctxt->node = ctxt->node;
2992 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002993 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002994 xmlXPathFreeObject(list);
2995 }
2996#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002997#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002998 } else if (!strcmp(command, "setbase")) {
2999 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00003000#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003001 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3002 int dir = (!strcmp(command, "dir"));
3003
3004 if (arg[0] == 0) {
3005 if (dir)
3006 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3007 else
3008 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3009 } else {
3010 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00003011#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003012 ctxt->pctxt->node = ctxt->node;
3013 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3014#else
3015 list = NULL;
3016#endif /* LIBXML_XPATH_ENABLED */
3017 if (list != NULL) {
3018 switch (list->type) {
3019 case XPATH_UNDEFINED:
3020 xmlGenericError(xmlGenericErrorContext,
3021 "%s: no such node\n", arg);
3022 break;
3023 case XPATH_NODESET:{
3024 int indx;
3025
Daniel Veillarda6825e82001-11-07 13:33:59 +00003026 if (list->nodesetval == NULL)
3027 break;
3028
Daniel Veillard78d12092001-10-11 09:12:24 +00003029 for (indx = 0;
3030 indx < list->nodesetval->nodeNr;
3031 indx++) {
3032 if (dir)
3033 xmlShellDir(ctxt, NULL,
3034 list->nodesetval->
3035 nodeTab[indx], NULL);
3036 else
3037 xmlShellList(ctxt, NULL,
3038 list->nodesetval->
3039 nodeTab[indx], NULL);
3040 }
3041 break;
3042 }
3043 case XPATH_BOOLEAN:
3044 xmlGenericError(xmlGenericErrorContext,
3045 "%s is a Boolean\n", arg);
3046 break;
3047 case XPATH_NUMBER:
3048 xmlGenericError(xmlGenericErrorContext,
3049 "%s is a number\n", arg);
3050 break;
3051 case XPATH_STRING:
3052 xmlGenericError(xmlGenericErrorContext,
3053 "%s is a string\n", arg);
3054 break;
3055 case XPATH_POINT:
3056 xmlGenericError(xmlGenericErrorContext,
3057 "%s is a point\n", arg);
3058 break;
3059 case XPATH_RANGE:
3060 xmlGenericError(xmlGenericErrorContext,
3061 "%s is a range\n", arg);
3062 break;
3063 case XPATH_LOCATIONSET:
3064 xmlGenericError(xmlGenericErrorContext,
3065 "%s is a range\n", arg);
3066 break;
3067 case XPATH_USERS:
3068 xmlGenericError(xmlGenericErrorContext,
3069 "%s is user-defined\n", arg);
3070 break;
3071 case XPATH_XSLT_TREE:
3072 xmlGenericError(xmlGenericErrorContext,
3073 "%s is an XSLT value tree\n",
3074 arg);
3075 break;
3076 }
3077#ifdef LIBXML_XPATH_ENABLED
3078 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003079#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003080 } else {
3081 xmlGenericError(xmlGenericErrorContext,
3082 "%s: no such node\n", arg);
3083 }
3084 ctxt->pctxt->node = NULL;
3085 }
3086 } else if (!strcmp(command, "cd")) {
3087 if (arg[0] == 0) {
3088 ctxt->node = (xmlNodePtr) ctxt->doc;
3089 } else {
3090#ifdef LIBXML_XPATH_ENABLED
3091 ctxt->pctxt->node = ctxt->node;
3092 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3093#else
3094 list = NULL;
3095#endif /* LIBXML_XPATH_ENABLED */
3096 if (list != NULL) {
3097 switch (list->type) {
3098 case XPATH_UNDEFINED:
3099 xmlGenericError(xmlGenericErrorContext,
3100 "%s: no such node\n", arg);
3101 break;
3102 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003103 if (list->nodesetval != NULL) {
3104 if (list->nodesetval->nodeNr == 1) {
3105 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003106 if ((ctxt->node != NULL) &&
3107 (ctxt->node->type ==
3108 XML_NAMESPACE_DECL)) {
3109 xmlGenericError(xmlGenericErrorContext,
3110 "cannot cd to namespace\n");
3111 ctxt->node = NULL;
3112 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003113 } else
3114 xmlGenericError(xmlGenericErrorContext,
3115 "%s is a %d Node Set\n",
3116 arg,
3117 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003118 } else
3119 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003120 "%s is an empty Node Set\n",
3121 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003122 break;
3123 case XPATH_BOOLEAN:
3124 xmlGenericError(xmlGenericErrorContext,
3125 "%s is a Boolean\n", arg);
3126 break;
3127 case XPATH_NUMBER:
3128 xmlGenericError(xmlGenericErrorContext,
3129 "%s is a number\n", arg);
3130 break;
3131 case XPATH_STRING:
3132 xmlGenericError(xmlGenericErrorContext,
3133 "%s is a string\n", arg);
3134 break;
3135 case XPATH_POINT:
3136 xmlGenericError(xmlGenericErrorContext,
3137 "%s is a point\n", arg);
3138 break;
3139 case XPATH_RANGE:
3140 xmlGenericError(xmlGenericErrorContext,
3141 "%s is a range\n", arg);
3142 break;
3143 case XPATH_LOCATIONSET:
3144 xmlGenericError(xmlGenericErrorContext,
3145 "%s is a range\n", arg);
3146 break;
3147 case XPATH_USERS:
3148 xmlGenericError(xmlGenericErrorContext,
3149 "%s is user-defined\n", arg);
3150 break;
3151 case XPATH_XSLT_TREE:
3152 xmlGenericError(xmlGenericErrorContext,
3153 "%s is an XSLT value tree\n",
3154 arg);
3155 break;
3156 }
3157#ifdef LIBXML_XPATH_ENABLED
3158 xmlXPathFreeObject(list);
3159#endif
3160 } else {
3161 xmlGenericError(xmlGenericErrorContext,
3162 "%s: no such node\n", arg);
3163 }
3164 ctxt->pctxt->node = NULL;
3165 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003166#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003167 } else if (!strcmp(command, "cat")) {
3168 if (arg[0] == 0) {
3169 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3170 } else {
3171 ctxt->pctxt->node = ctxt->node;
3172#ifdef LIBXML_XPATH_ENABLED
3173 ctxt->pctxt->node = ctxt->node;
3174 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3175#else
3176 list = NULL;
3177#endif /* LIBXML_XPATH_ENABLED */
3178 if (list != NULL) {
3179 switch (list->type) {
3180 case XPATH_UNDEFINED:
3181 xmlGenericError(xmlGenericErrorContext,
3182 "%s: no such node\n", arg);
3183 break;
3184 case XPATH_NODESET:{
3185 int indx;
3186
Daniel Veillarda6825e82001-11-07 13:33:59 +00003187 if (list->nodesetval == NULL)
3188 break;
3189
Daniel Veillard78d12092001-10-11 09:12:24 +00003190 for (indx = 0;
3191 indx < list->nodesetval->nodeNr;
3192 indx++) {
3193 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003194 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003195 xmlShellCat(ctxt, NULL,
3196 list->nodesetval->
3197 nodeTab[indx], NULL);
3198 }
3199 break;
3200 }
3201 case XPATH_BOOLEAN:
3202 xmlGenericError(xmlGenericErrorContext,
3203 "%s is a Boolean\n", arg);
3204 break;
3205 case XPATH_NUMBER:
3206 xmlGenericError(xmlGenericErrorContext,
3207 "%s is a number\n", arg);
3208 break;
3209 case XPATH_STRING:
3210 xmlGenericError(xmlGenericErrorContext,
3211 "%s is a string\n", arg);
3212 break;
3213 case XPATH_POINT:
3214 xmlGenericError(xmlGenericErrorContext,
3215 "%s is a point\n", arg);
3216 break;
3217 case XPATH_RANGE:
3218 xmlGenericError(xmlGenericErrorContext,
3219 "%s is a range\n", arg);
3220 break;
3221 case XPATH_LOCATIONSET:
3222 xmlGenericError(xmlGenericErrorContext,
3223 "%s is a range\n", arg);
3224 break;
3225 case XPATH_USERS:
3226 xmlGenericError(xmlGenericErrorContext,
3227 "%s is user-defined\n", arg);
3228 break;
3229 case XPATH_XSLT_TREE:
3230 xmlGenericError(xmlGenericErrorContext,
3231 "%s is an XSLT value tree\n",
3232 arg);
3233 break;
3234 }
3235#ifdef LIBXML_XPATH_ENABLED
3236 xmlXPathFreeObject(list);
3237#endif
3238 } else {
3239 xmlGenericError(xmlGenericErrorContext,
3240 "%s: no such node\n", arg);
3241 }
3242 ctxt->pctxt->node = NULL;
3243 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003244#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003245 } else {
3246 xmlGenericError(xmlGenericErrorContext,
3247 "Unknown command %s\n", command);
3248 }
3249 free(cmdline); /* not xmlFree here ! */
Daniel Veillard30663512008-02-21 22:31:55 +00003250 cmdline = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003251 }
3252#ifdef LIBXML_XPATH_ENABLED
3253 xmlXPathFreeContext(ctxt->pctxt);
3254#endif /* LIBXML_XPATH_ENABLED */
3255 if (ctxt->loaded) {
3256 xmlFreeDoc(ctxt->doc);
3257 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003258 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003259 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003260 xmlFree(ctxt);
3261 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003262 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003263}
3264
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003265#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003266#define bottom_debugXML
3267#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003268#endif /* LIBXML_DEBUG_ENABLED */