blob: 59529d6ec43d83d46af187521722cdb07a487410 [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)
144 fprintf(ctxt->output, &ctxt->shift[100 - 2 * ctxt->depth]);
145 else
146 fprintf(ctxt->output, ctxt->shift);
147 }
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,
165 msg);
166}
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) &&
262 (!xmlDictOwns(ctxt->dict, name))) {
263 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
264 "Name is not from the document dictionnary '%s'",
265 (const char *) name);
266 }
267 }
268}
269
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000270static void
271xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000272 xmlDocPtr doc;
273 xmlDictPtr dict;
274
275 doc = node->doc;
276
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000277 if (node->parent == NULL)
278 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
279 "Node has no parent\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000280 if (node->doc == NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000281 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
282 "Node has no doc\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000283 dict = NULL;
284 } else {
285 dict = doc->dict;
286 if ((dict == NULL) && (ctxt->nodict == 0)) {
Daniel Veillard6927b102004-10-27 17:29:04 +0000287#if 0
288 /* desactivated right now as it raises too many errors */
289 if (doc->type == XML_DOCUMENT_NODE)
290 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
291 "Document has no dictionnary\n");
292#endif
Daniel Veillard03a53c32004-10-26 16:06:51 +0000293 ctxt->nodict = 1;
294 }
295 if (ctxt->doc == NULL)
296 ctxt->doc = doc;
297
298 if (ctxt->dict == NULL) {
299 ctxt->dict = dict;
300 }
301 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000302 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
303 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
304 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
305 "Node doc differs from parent's one\n");
306 if (node->prev == NULL) {
307 if (node->type == XML_ATTRIBUTE_NODE) {
308 if ((node->parent != NULL) &&
309 (node != (xmlNodePtr) node->parent->properties))
310 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
311 "Attr has no prev and not first of attr list\n");
312
313 } else if ((node->parent != NULL) && (node->parent->children != node))
314 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
315 "Node has no prev and not first of parent list\n");
316 } else {
317 if (node->prev->next != node)
318 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
319 "Node prev->next : back link wrong\n");
320 }
321 if (node->next == NULL) {
322 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
323 (node->parent->last != node))
324 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
325 "Node has no next and not last of parent list\n");
326 } else {
327 if (node->next->prev != node)
328 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
329 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000330 if (node->next->parent != node->parent)
331 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
332 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000333 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000334 if (node->type == XML_ELEMENT_NODE) {
335 xmlNsPtr ns;
336
337 ns = node->nsDef;
338 while (ns != NULL) {
339 xmlCtxtNsCheckScope(ctxt, node, ns);
340 ns = ns->next;
341 }
342 if (node->ns != NULL)
343 xmlCtxtNsCheckScope(ctxt, node, node->ns);
344 } else if (node->type == XML_ATTRIBUTE_NODE) {
345 if (node->ns != NULL)
346 xmlCtxtNsCheckScope(ctxt, node, node->ns);
347 }
348
Daniel Veillardc6095782004-10-15 14:50:10 +0000349 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000350 (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillardf2e066a2005-06-30 13:04:44 +0000351 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000352 (node->type != XML_ATTRIBUTE_DECL) &&
353 (node->type != XML_DTD_NODE) &&
William M. Brack0357a302005-07-06 17:39:14 +0000354 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000355 (node->type != XML_HTML_DOCUMENT_NODE) &&
356 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000357 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000358 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000359 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000360 switch (node->type) {
361 case XML_ELEMENT_NODE:
362 case XML_ATTRIBUTE_NODE:
363 xmlCtxtCheckName(ctxt, node->name);
364 break;
365 case XML_TEXT_NODE:
366 if ((node->name == xmlStringText) ||
367 (node->name == xmlStringTextNoenc))
368 break;
369 /* some case of entity substitution can lead to this */
370 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000371 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
372 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000373 break;
374
375 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
376 "Text node has wrong name '%s'",
377 (const char *) node->name);
378 break;
379 case XML_COMMENT_NODE:
380 if (node->name == xmlStringComment)
381 break;
382 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
383 "Comment node has wrong name '%s'",
384 (const char *) node->name);
385 break;
386 case XML_PI_NODE:
387 xmlCtxtCheckName(ctxt, node->name);
388 break;
389 case XML_CDATA_SECTION_NODE:
390 if (node->name == NULL)
391 break;
392 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
393 "CData section has non NULL name '%s'",
394 (const char *) node->name);
395 break;
396 case XML_ENTITY_REF_NODE:
397 case XML_ENTITY_NODE:
398 case XML_DOCUMENT_TYPE_NODE:
399 case XML_DOCUMENT_FRAG_NODE:
400 case XML_NOTATION_NODE:
401 case XML_DTD_NODE:
402 case XML_ELEMENT_DECL:
403 case XML_ATTRIBUTE_DECL:
404 case XML_ENTITY_DECL:
405 case XML_NAMESPACE_DECL:
406 case XML_XINCLUDE_START:
407 case XML_XINCLUDE_END:
408#ifdef LIBXML_DOCB_ENABLED
409 case XML_DOCB_DOCUMENT_NODE:
410#endif
411 case XML_DOCUMENT_NODE:
412 case XML_HTML_DOCUMENT_NODE:
413 break;
414 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000415}
416
Daniel Veillard22cdb842004-10-04 14:09:17 +0000417static void
418xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
419{
420 int i;
421
Daniel Veillardc6095782004-10-15 14:50:10 +0000422 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000423 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000424 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000425 /* TODO: check UTF8 content of the string */
426 if (str == NULL) {
427 fprintf(ctxt->output, "(NULL)");
428 return;
429 }
430 for (i = 0; i < 40; i++)
431 if (str[i] == 0)
432 return;
433 else if (IS_BLANK_CH(str[i]))
434 fputc(' ', ctxt->output);
435 else if (str[i] >= 0x80)
436 fprintf(ctxt->output, "#%X", str[i]);
437 else
438 fputc(str[i], ctxt->output);
439 fprintf(ctxt->output, "...");
440}
441
442static void
443xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
444{
445 xmlCtxtDumpSpaces(ctxt);
446
447 if (dtd == NULL) {
448 if (!ctxt->check)
449 fprintf(ctxt->output, "DTD node is NULL\n");
450 return;
451 }
452
453 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000454 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
455 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000456 return;
457 }
458 if (!ctxt->check) {
459 if (dtd->name != NULL)
460 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
461 else
462 fprintf(ctxt->output, "DTD");
463 if (dtd->ExternalID != NULL)
464 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
465 if (dtd->SystemID != NULL)
466 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
467 fprintf(ctxt->output, "\n");
468 }
469 /*
470 * Do a bit of checking
471 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000472 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000473}
474
475static void
476xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
477{
478 xmlCtxtDumpSpaces(ctxt);
479
480 if (attr == NULL) {
481 if (!ctxt->check)
482 fprintf(ctxt->output, "Attribute declaration is NULL\n");
483 return;
484 }
485 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000486 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
487 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000488 return;
489 }
490 if (attr->name != NULL) {
491 if (!ctxt->check)
492 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
493 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000494 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
495 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000496 if (attr->elem != NULL) {
497 if (!ctxt->check)
498 fprintf(ctxt->output, " for %s", (char *) attr->elem);
499 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000500 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
501 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000502 if (!ctxt->check) {
503 switch (attr->atype) {
504 case XML_ATTRIBUTE_CDATA:
505 fprintf(ctxt->output, " CDATA");
506 break;
507 case XML_ATTRIBUTE_ID:
508 fprintf(ctxt->output, " ID");
509 break;
510 case XML_ATTRIBUTE_IDREF:
511 fprintf(ctxt->output, " IDREF");
512 break;
513 case XML_ATTRIBUTE_IDREFS:
514 fprintf(ctxt->output, " IDREFS");
515 break;
516 case XML_ATTRIBUTE_ENTITY:
517 fprintf(ctxt->output, " ENTITY");
518 break;
519 case XML_ATTRIBUTE_ENTITIES:
520 fprintf(ctxt->output, " ENTITIES");
521 break;
522 case XML_ATTRIBUTE_NMTOKEN:
523 fprintf(ctxt->output, " NMTOKEN");
524 break;
525 case XML_ATTRIBUTE_NMTOKENS:
526 fprintf(ctxt->output, " NMTOKENS");
527 break;
528 case XML_ATTRIBUTE_ENUMERATION:
529 fprintf(ctxt->output, " ENUMERATION");
530 break;
531 case XML_ATTRIBUTE_NOTATION:
532 fprintf(ctxt->output, " NOTATION ");
533 break;
534 }
535 if (attr->tree != NULL) {
536 int indx;
537 xmlEnumerationPtr cur = attr->tree;
538
539 for (indx = 0; indx < 5; indx++) {
540 if (indx != 0)
541 fprintf(ctxt->output, "|%s", (char *) cur->name);
542 else
543 fprintf(ctxt->output, " (%s", (char *) cur->name);
544 cur = cur->next;
545 if (cur == NULL)
546 break;
547 }
548 if (cur == NULL)
549 fprintf(ctxt->output, ")");
550 else
551 fprintf(ctxt->output, "...)");
552 }
553 switch (attr->def) {
554 case XML_ATTRIBUTE_NONE:
555 break;
556 case XML_ATTRIBUTE_REQUIRED:
557 fprintf(ctxt->output, " REQUIRED");
558 break;
559 case XML_ATTRIBUTE_IMPLIED:
560 fprintf(ctxt->output, " IMPLIED");
561 break;
562 case XML_ATTRIBUTE_FIXED:
563 fprintf(ctxt->output, " FIXED");
564 break;
565 }
566 if (attr->defaultValue != NULL) {
567 fprintf(ctxt->output, "\"");
568 xmlCtxtDumpString(ctxt, attr->defaultValue);
569 fprintf(ctxt->output, "\"");
570 }
571 fprintf(ctxt->output, "\n");
572 }
573
574 /*
575 * Do a bit of checking
576 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000577 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000578}
579
580static void
581xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
582{
583 xmlCtxtDumpSpaces(ctxt);
584
585 if (elem == NULL) {
586 if (!ctxt->check)
587 fprintf(ctxt->output, "Element declaration is NULL\n");
588 return;
589 }
590 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000591 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
592 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000593 return;
594 }
595 if (elem->name != NULL) {
596 if (!ctxt->check) {
597 fprintf(ctxt->output, "ELEMDECL(");
598 xmlCtxtDumpString(ctxt, elem->name);
599 fprintf(ctxt->output, ")");
600 }
601 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000602 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
603 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000604 if (!ctxt->check) {
605 switch (elem->etype) {
606 case XML_ELEMENT_TYPE_UNDEFINED:
607 fprintf(ctxt->output, ", UNDEFINED");
608 break;
609 case XML_ELEMENT_TYPE_EMPTY:
610 fprintf(ctxt->output, ", EMPTY");
611 break;
612 case XML_ELEMENT_TYPE_ANY:
613 fprintf(ctxt->output, ", ANY");
614 break;
615 case XML_ELEMENT_TYPE_MIXED:
616 fprintf(ctxt->output, ", MIXED ");
617 break;
618 case XML_ELEMENT_TYPE_ELEMENT:
619 fprintf(ctxt->output, ", MIXED ");
620 break;
621 }
622 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
623 char buf[5001];
624
625 buf[0] = 0;
626 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
627 buf[5000] = 0;
628 fprintf(ctxt->output, "%s", buf);
629 }
630 fprintf(ctxt->output, "\n");
631 }
632
633 /*
634 * Do a bit of checking
635 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000636 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000637}
638
639static void
640xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
641{
642 xmlCtxtDumpSpaces(ctxt);
643
644 if (ent == NULL) {
645 if (!ctxt->check)
646 fprintf(ctxt->output, "Entity declaration is NULL\n");
647 return;
648 }
649 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000650 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
651 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000652 return;
653 }
654 if (ent->name != NULL) {
655 if (!ctxt->check) {
656 fprintf(ctxt->output, "ENTITYDECL(");
657 xmlCtxtDumpString(ctxt, ent->name);
658 fprintf(ctxt->output, ")");
659 }
660 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000661 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
662 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000663 if (!ctxt->check) {
664 switch (ent->etype) {
665 case XML_INTERNAL_GENERAL_ENTITY:
666 fprintf(ctxt->output, ", internal\n");
667 break;
668 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
669 fprintf(ctxt->output, ", external parsed\n");
670 break;
671 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
672 fprintf(ctxt->output, ", unparsed\n");
673 break;
674 case XML_INTERNAL_PARAMETER_ENTITY:
675 fprintf(ctxt->output, ", parameter\n");
676 break;
677 case XML_EXTERNAL_PARAMETER_ENTITY:
678 fprintf(ctxt->output, ", external parameter\n");
679 break;
680 case XML_INTERNAL_PREDEFINED_ENTITY:
681 fprintf(ctxt->output, ", predefined\n");
682 break;
683 }
684 if (ent->ExternalID) {
685 xmlCtxtDumpSpaces(ctxt);
686 fprintf(ctxt->output, " ExternalID=%s\n",
687 (char *) ent->ExternalID);
688 }
689 if (ent->SystemID) {
690 xmlCtxtDumpSpaces(ctxt);
691 fprintf(ctxt->output, " SystemID=%s\n",
692 (char *) ent->SystemID);
693 }
694 if (ent->URI != NULL) {
695 xmlCtxtDumpSpaces(ctxt);
696 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
697 }
698 if (ent->content) {
699 xmlCtxtDumpSpaces(ctxt);
700 fprintf(ctxt->output, " content=");
701 xmlCtxtDumpString(ctxt, ent->content);
702 fprintf(ctxt->output, "\n");
703 }
704 }
705
706 /*
707 * Do a bit of checking
708 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000709 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000710}
711
712static void
713xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
714{
715 xmlCtxtDumpSpaces(ctxt);
716
717 if (ns == NULL) {
718 if (!ctxt->check)
719 fprintf(ctxt->output, "namespace node is NULL\n");
720 return;
721 }
722 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000723 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
724 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000725 return;
726 }
727 if (ns->href == NULL) {
728 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000729 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
730 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000731 (char *) ns->prefix);
732 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000733 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
734 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000735 } else {
736 if (!ctxt->check) {
737 if (ns->prefix != NULL)
738 fprintf(ctxt->output, "namespace %s href=",
739 (char *) ns->prefix);
740 else
741 fprintf(ctxt->output, "default namespace href=");
742
743 xmlCtxtDumpString(ctxt, ns->href);
744 fprintf(ctxt->output, "\n");
745 }
746 }
747}
748
749static void
750xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
751{
752 while (ns != NULL) {
753 xmlCtxtDumpNamespace(ctxt, ns);
754 ns = ns->next;
755 }
756}
757
758static void
759xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
760{
761 xmlCtxtDumpSpaces(ctxt);
762
763 if (ent == NULL) {
764 if (!ctxt->check)
765 fprintf(ctxt->output, "Entity is NULL\n");
766 return;
767 }
768 if (!ctxt->check) {
769 switch (ent->etype) {
770 case XML_INTERNAL_GENERAL_ENTITY:
771 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
772 break;
773 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
774 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
775 break;
776 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
777 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
778 break;
779 case XML_INTERNAL_PARAMETER_ENTITY:
780 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
781 break;
782 case XML_EXTERNAL_PARAMETER_ENTITY:
783 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
784 break;
785 default:
786 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
787 }
788 fprintf(ctxt->output, "%s\n", ent->name);
789 if (ent->ExternalID) {
790 xmlCtxtDumpSpaces(ctxt);
791 fprintf(ctxt->output, "ExternalID=%s\n",
792 (char *) ent->ExternalID);
793 }
794 if (ent->SystemID) {
795 xmlCtxtDumpSpaces(ctxt);
796 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
797 }
798 if (ent->URI) {
799 xmlCtxtDumpSpaces(ctxt);
800 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
801 }
802 if (ent->content) {
803 xmlCtxtDumpSpaces(ctxt);
804 fprintf(ctxt->output, "content=");
805 xmlCtxtDumpString(ctxt, ent->content);
806 fprintf(ctxt->output, "\n");
807 }
808 }
809}
810
811/**
812 * xmlCtxtDumpAttr:
813 * @output: the FILE * for the output
814 * @attr: the attribute
815 * @depth: the indentation level.
816 *
817 * Dumps debug information for the attribute
818 */
819static void
820xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
821{
822 xmlCtxtDumpSpaces(ctxt);
823
824 if (attr == NULL) {
825 if (!ctxt->check)
826 fprintf(ctxt->output, "Attr is NULL");
827 return;
828 }
829 if (!ctxt->check) {
830 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000831 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000832 fprintf(ctxt->output, "\n");
833 if (attr->children != NULL) {
834 ctxt->depth++;
835 xmlCtxtDumpNodeList(ctxt, attr->children);
836 ctxt->depth--;
837 }
838 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000839 if (attr->name == NULL)
840 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
841 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000842
843 /*
844 * Do a bit of checking
845 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000846 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000847}
848
849/**
850 * xmlCtxtDumpAttrList:
851 * @output: the FILE * for the output
852 * @attr: the attribute list
853 * @depth: the indentation level.
854 *
855 * Dumps debug information for the attribute list
856 */
857static void
858xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
859{
860 while (attr != NULL) {
861 xmlCtxtDumpAttr(ctxt, attr);
862 attr = attr->next;
863 }
864}
865
866/**
867 * xmlCtxtDumpOneNode:
868 * @output: the FILE * for the output
869 * @node: the node
870 * @depth: the indentation level.
871 *
872 * Dumps debug information for the element node, it is not recursive
873 */
874static void
875xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
876{
877 if (node == NULL) {
878 if (!ctxt->check) {
879 xmlCtxtDumpSpaces(ctxt);
880 fprintf(ctxt->output, "node is NULL\n");
881 }
882 return;
883 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000884 ctxt->node = node;
885
Daniel Veillard22cdb842004-10-04 14:09:17 +0000886 switch (node->type) {
887 case XML_ELEMENT_NODE:
888 if (!ctxt->check) {
889 xmlCtxtDumpSpaces(ctxt);
890 fprintf(ctxt->output, "ELEMENT ");
891 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
892 xmlCtxtDumpString(ctxt, node->ns->prefix);
893 fprintf(ctxt->output, ":");
894 }
895 xmlCtxtDumpString(ctxt, node->name);
896 fprintf(ctxt->output, "\n");
897 }
898 break;
899 case XML_ATTRIBUTE_NODE:
900 if (!ctxt->check)
901 xmlCtxtDumpSpaces(ctxt);
902 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000903 xmlCtxtGenericNodeCheck(ctxt, node);
904 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000905 case XML_TEXT_NODE:
906 if (!ctxt->check) {
907 xmlCtxtDumpSpaces(ctxt);
908 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard8874b942005-08-25 13:19:21 +0000909 fprintf(ctxt->output, "TEXT no enc");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000910 else
Daniel Veillard8874b942005-08-25 13:19:21 +0000911 fprintf(ctxt->output, "TEXT");
Daniel Veillardcfa303a2005-08-25 14:03:56 +0000912 if (ctxt->options & DUMP_TEXT_TYPE) {
913 if (node->content == (xmlChar *) &(node->properties))
914 fprintf(ctxt->output, " compact\n");
915 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
916 fprintf(ctxt->output, " interned\n");
917 else
918 fprintf(ctxt->output, "\n");
919 } else
Daniel Veillard8874b942005-08-25 13:19:21 +0000920 fprintf(ctxt->output, "\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000921 }
922 break;
923 case XML_CDATA_SECTION_NODE:
924 if (!ctxt->check) {
925 xmlCtxtDumpSpaces(ctxt);
926 fprintf(ctxt->output, "CDATA_SECTION\n");
927 }
928 break;
929 case XML_ENTITY_REF_NODE:
930 if (!ctxt->check) {
931 xmlCtxtDumpSpaces(ctxt);
932 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
933 (char *) node->name);
934 }
935 break;
936 case XML_ENTITY_NODE:
937 if (!ctxt->check) {
938 xmlCtxtDumpSpaces(ctxt);
939 fprintf(ctxt->output, "ENTITY\n");
940 }
941 break;
942 case XML_PI_NODE:
943 if (!ctxt->check) {
944 xmlCtxtDumpSpaces(ctxt);
945 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
946 }
947 break;
948 case XML_COMMENT_NODE:
949 if (!ctxt->check) {
950 xmlCtxtDumpSpaces(ctxt);
951 fprintf(ctxt->output, "COMMENT\n");
952 }
953 break;
954 case XML_DOCUMENT_NODE:
955 case XML_HTML_DOCUMENT_NODE:
956 if (!ctxt->check) {
957 xmlCtxtDumpSpaces(ctxt);
958 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000959 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
960 xmlCtxtGenericNodeCheck(ctxt, node);
961 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000962 case XML_DOCUMENT_TYPE_NODE:
963 if (!ctxt->check) {
964 xmlCtxtDumpSpaces(ctxt);
965 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
966 }
967 break;
968 case XML_DOCUMENT_FRAG_NODE:
969 if (!ctxt->check) {
970 xmlCtxtDumpSpaces(ctxt);
971 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
972 }
973 break;
974 case XML_NOTATION_NODE:
975 if (!ctxt->check) {
976 xmlCtxtDumpSpaces(ctxt);
977 fprintf(ctxt->output, "NOTATION\n");
978 }
979 break;
980 case XML_DTD_NODE:
981 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
982 return;
983 case XML_ELEMENT_DECL:
984 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
985 return;
986 case XML_ATTRIBUTE_DECL:
987 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
988 return;
989 case XML_ENTITY_DECL:
990 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
991 return;
992 case XML_NAMESPACE_DECL:
993 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
994 return;
995 case XML_XINCLUDE_START:
996 if (!ctxt->check) {
997 xmlCtxtDumpSpaces(ctxt);
998 fprintf(ctxt->output, "INCLUDE START\n");
999 }
1000 return;
1001 case XML_XINCLUDE_END:
1002 if (!ctxt->check) {
1003 xmlCtxtDumpSpaces(ctxt);
1004 fprintf(ctxt->output, "INCLUDE END\n");
1005 }
1006 return;
1007 default:
1008 if (!ctxt->check)
1009 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001010 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1011 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001012 return;
1013 }
1014 if (node->doc == NULL) {
1015 if (!ctxt->check) {
1016 xmlCtxtDumpSpaces(ctxt);
1017 }
1018 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1019 }
1020 ctxt->depth++;
Daniel Veillard8874b942005-08-25 13:19:21 +00001021 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001022 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
Daniel Veillard8874b942005-08-25 13:19:21 +00001023 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001024 xmlCtxtDumpAttrList(ctxt, node->properties);
1025 if (node->type != XML_ENTITY_REF_NODE) {
1026 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1027 if (!ctxt->check) {
1028 xmlCtxtDumpSpaces(ctxt);
1029 fprintf(ctxt->output, "content=");
1030 xmlCtxtDumpString(ctxt, node->content);
1031 fprintf(ctxt->output, "\n");
1032 }
1033 }
1034 } else {
1035 xmlEntityPtr ent;
1036
1037 ent = xmlGetDocEntity(node->doc, node->name);
1038 if (ent != NULL)
1039 xmlCtxtDumpEntity(ctxt, ent);
1040 }
1041 ctxt->depth--;
1042
1043 /*
1044 * Do a bit of checking
1045 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001046 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001047}
1048
1049/**
1050 * xmlCtxtDumpNode:
1051 * @output: the FILE * for the output
1052 * @node: the node
1053 * @depth: the indentation level.
1054 *
1055 * Dumps debug information for the element node, it is recursive
1056 */
1057static void
1058xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1059{
1060 if (node == NULL) {
1061 if (!ctxt->check) {
1062 xmlCtxtDumpSpaces(ctxt);
1063 fprintf(ctxt->output, "node is NULL\n");
1064 }
1065 return;
1066 }
1067 xmlCtxtDumpOneNode(ctxt, node);
1068 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1069 ctxt->depth++;
1070 xmlCtxtDumpNodeList(ctxt, node->children);
1071 ctxt->depth--;
1072 }
1073}
1074
1075/**
1076 * xmlCtxtDumpNodeList:
1077 * @output: the FILE * for the output
1078 * @node: the node list
1079 * @depth: the indentation level.
1080 *
1081 * Dumps debug information for the list of element node, it is recursive
1082 */
1083static void
1084xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1085{
1086 while (node != NULL) {
1087 xmlCtxtDumpNode(ctxt, node);
1088 node = node->next;
1089 }
1090}
1091
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001092static void
1093xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1094{
1095 if (doc == NULL) {
1096 if (!ctxt->check)
1097 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1098 return;
1099 }
1100 ctxt->node = (xmlNodePtr) doc;
1101
1102 switch (doc->type) {
1103 case XML_ELEMENT_NODE:
1104 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1105 "Misplaced ELEMENT node\n");
1106 break;
1107 case XML_ATTRIBUTE_NODE:
1108 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1109 "Misplaced ATTRIBUTE node\n");
1110 break;
1111 case XML_TEXT_NODE:
1112 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1113 "Misplaced TEXT node\n");
1114 break;
1115 case XML_CDATA_SECTION_NODE:
1116 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1117 "Misplaced CDATA node\n");
1118 break;
1119 case XML_ENTITY_REF_NODE:
1120 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1121 "Misplaced ENTITYREF node\n");
1122 break;
1123 case XML_ENTITY_NODE:
1124 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1125 "Misplaced ENTITY node\n");
1126 break;
1127 case XML_PI_NODE:
1128 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1129 "Misplaced PI node\n");
1130 break;
1131 case XML_COMMENT_NODE:
1132 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1133 "Misplaced COMMENT node\n");
1134 break;
1135 case XML_DOCUMENT_NODE:
1136 if (!ctxt->check)
1137 fprintf(ctxt->output, "DOCUMENT\n");
1138 break;
1139 case XML_HTML_DOCUMENT_NODE:
1140 if (!ctxt->check)
1141 fprintf(ctxt->output, "HTML DOCUMENT\n");
1142 break;
1143 case XML_DOCUMENT_TYPE_NODE:
1144 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1145 "Misplaced DOCTYPE node\n");
1146 break;
1147 case XML_DOCUMENT_FRAG_NODE:
1148 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1149 "Misplaced FRAGMENT node\n");
1150 break;
1151 case XML_NOTATION_NODE:
1152 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1153 "Misplaced NOTATION node\n");
1154 break;
1155 default:
1156 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1157 "Unknown node type %d\n", doc->type);
1158 }
1159}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001160
1161/**
1162 * xmlCtxtDumpDocumentHead:
1163 * @output: the FILE * for the output
1164 * @doc: the document
1165 *
1166 * Dumps debug information cncerning the document, not recursive
1167 */
1168static void
1169xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1170{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001171 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001172 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001173 if (!ctxt->check) {
1174 if (doc->name != NULL) {
1175 fprintf(ctxt->output, "name=");
1176 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1177 fprintf(ctxt->output, "\n");
1178 }
1179 if (doc->version != NULL) {
1180 fprintf(ctxt->output, "version=");
1181 xmlCtxtDumpString(ctxt, doc->version);
1182 fprintf(ctxt->output, "\n");
1183 }
1184 if (doc->encoding != NULL) {
1185 fprintf(ctxt->output, "encoding=");
1186 xmlCtxtDumpString(ctxt, doc->encoding);
1187 fprintf(ctxt->output, "\n");
1188 }
1189 if (doc->URL != NULL) {
1190 fprintf(ctxt->output, "URL=");
1191 xmlCtxtDumpString(ctxt, doc->URL);
1192 fprintf(ctxt->output, "\n");
1193 }
1194 if (doc->standalone)
1195 fprintf(ctxt->output, "standalone=true\n");
1196 }
1197 if (doc->oldNs != NULL)
1198 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1199}
1200
1201/**
1202 * xmlCtxtDumpDocument:
1203 * @output: the FILE * for the output
1204 * @doc: the document
1205 *
1206 * Dumps debug information for the document, it's recursive
1207 */
1208static void
1209xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1210{
1211 if (doc == NULL) {
1212 if (!ctxt->check)
1213 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1214 return;
1215 }
1216 xmlCtxtDumpDocumentHead(ctxt, doc);
1217 if (((doc->type == XML_DOCUMENT_NODE) ||
1218 (doc->type == XML_HTML_DOCUMENT_NODE))
1219 && (doc->children != NULL)) {
1220 ctxt->depth++;
1221 xmlCtxtDumpNodeList(ctxt, doc->children);
1222 ctxt->depth--;
1223 }
1224}
1225
1226static void
1227xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1228{
1229 if (cur == NULL) {
1230 if (!ctxt->check)
1231 fprintf(ctxt->output, "Entity is NULL");
1232 return;
1233 }
1234 if (!ctxt->check) {
1235 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1236 switch (cur->etype) {
1237 case XML_INTERNAL_GENERAL_ENTITY:
1238 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1239 break;
1240 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1241 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1242 break;
1243 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1244 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1245 break;
1246 case XML_INTERNAL_PARAMETER_ENTITY:
1247 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1248 break;
1249 case XML_EXTERNAL_PARAMETER_ENTITY:
1250 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1251 break;
1252 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001253 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1254 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001255 }
1256 if (cur->ExternalID != NULL)
1257 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1258 if (cur->SystemID != NULL)
1259 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1260 if (cur->orig != NULL)
1261 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1262 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1263 fprintf(ctxt->output, "\n content \"%s\"",
1264 (char *) cur->content);
1265 fprintf(ctxt->output, "\n");
1266 }
1267}
1268
1269/**
1270 * xmlCtxtDumpEntities:
1271 * @output: the FILE * for the output
1272 * @doc: the document
1273 *
1274 * Dumps debug information for all the entities in use by the document
1275 */
1276static void
1277xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1278{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001279 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001280 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001281 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1282 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1283 doc->intSubset->entities;
1284
1285 if (!ctxt->check)
1286 fprintf(ctxt->output, "Entities in internal subset\n");
1287 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1288 ctxt);
1289 } else
1290 fprintf(ctxt->output, "No entities in internal subset\n");
1291 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1292 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1293 doc->extSubset->entities;
1294
1295 if (!ctxt->check)
1296 fprintf(ctxt->output, "Entities in external subset\n");
1297 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1298 ctxt);
1299 } else if (!ctxt->check)
1300 fprintf(ctxt->output, "No entities in external subset\n");
1301}
1302
1303/**
1304 * xmlCtxtDumpDTD:
1305 * @output: the FILE * for the output
1306 * @dtd: the DTD
1307 *
1308 * Dumps debug information for the DTD
1309 */
1310static void
1311xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1312{
1313 if (dtd == NULL) {
1314 if (!ctxt->check)
1315 fprintf(ctxt->output, "DTD is NULL\n");
1316 return;
1317 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001318 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001319 if (dtd->children == NULL)
1320 fprintf(ctxt->output, " DTD is empty\n");
1321 else {
1322 ctxt->depth++;
1323 xmlCtxtDumpNodeList(ctxt, dtd->children);
1324 ctxt->depth--;
1325 }
1326}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001327
Daniel Veillard22cdb842004-10-04 14:09:17 +00001328/************************************************************************
1329 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001330 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001331 * *
1332 ************************************************************************/
1333
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001334/**
1335 * xmlDebugDumpString:
1336 * @output: the FILE * for the output
1337 * @str: the string
1338 *
1339 * Dumps informations about the string, shorten it if necessary
1340 */
1341void
1342xmlDebugDumpString(FILE * output, const xmlChar * str)
1343{
Owen Taylor3473f882001-02-23 17:55:21 +00001344 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001345
Daniel Veillard7db38712002-02-07 16:39:11 +00001346 if (output == NULL)
1347 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001348 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001349 fprintf(output, "(NULL)");
1350 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001351 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001352 for (i = 0; i < 40; i++)
1353 if (str[i] == 0)
1354 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001355 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001356 fputc(' ', output);
1357 else if (str[i] >= 0x80)
1358 fprintf(output, "#%X", str[i]);
1359 else
1360 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001361 fprintf(output, "...");
1362}
1363
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001364/**
1365 * xmlDebugDumpAttr:
1366 * @output: the FILE * for the output
1367 * @attr: the attribute
1368 * @depth: the indentation level.
1369 *
1370 * Dumps debug information for the attribute
1371 */
1372void
1373xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001374 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001375
Daniel Veillarda82b1822004-11-08 16:24:57 +00001376 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001377 xmlCtxtDumpInitCtxt(&ctxt);
1378 ctxt.output = output;
1379 ctxt.depth = depth;
1380 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001381 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001382}
Owen Taylor3473f882001-02-23 17:55:21 +00001383
Owen Taylor3473f882001-02-23 17:55:21 +00001384
Daniel Veillard22cdb842004-10-04 14:09:17 +00001385/**
1386 * xmlDebugDumpEntities:
1387 * @output: the FILE * for the output
1388 * @doc: the document
1389 *
1390 * Dumps debug information for all the entities in use by the document
1391 */
1392void
1393xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1394{
1395 xmlDebugCtxt ctxt;
1396
Daniel Veillarda82b1822004-11-08 16:24:57 +00001397 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001398 xmlCtxtDumpInitCtxt(&ctxt);
1399 ctxt.output = output;
1400 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001401 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001402}
1403
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001404/**
1405 * xmlDebugDumpAttrList:
1406 * @output: the FILE * for the output
1407 * @attr: the attribute list
1408 * @depth: the indentation level.
1409 *
1410 * Dumps debug information for the attribute list
1411 */
1412void
1413xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1414{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001415 xmlDebugCtxt ctxt;
1416
Daniel Veillarda82b1822004-11-08 16:24:57 +00001417 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001418 xmlCtxtDumpInitCtxt(&ctxt);
1419 ctxt.output = output;
1420 ctxt.depth = depth;
1421 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001422 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001423}
1424
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001425/**
1426 * xmlDebugDumpOneNode:
1427 * @output: the FILE * for the output
1428 * @node: the node
1429 * @depth: the indentation level.
1430 *
1431 * Dumps debug information for the element node, it is not recursive
1432 */
1433void
1434xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1435{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001436 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001437
Daniel Veillarda82b1822004-11-08 16:24:57 +00001438 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001439 xmlCtxtDumpInitCtxt(&ctxt);
1440 ctxt.output = output;
1441 ctxt.depth = depth;
1442 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001443 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001444}
1445
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001446/**
1447 * xmlDebugDumpNode:
1448 * @output: the FILE * for the output
1449 * @node: the node
1450 * @depth: the indentation level.
1451 *
1452 * Dumps debug information for the element node, it is recursive
1453 */
1454void
1455xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1456{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001457 xmlDebugCtxt ctxt;
1458
Daniel Veillard7db38712002-02-07 16:39:11 +00001459 if (output == NULL)
1460 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001461 xmlCtxtDumpInitCtxt(&ctxt);
1462 ctxt.output = output;
1463 ctxt.depth = depth;
1464 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001465 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001466}
1467
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001468/**
1469 * xmlDebugDumpNodeList:
1470 * @output: the FILE * for the output
1471 * @node: the node list
1472 * @depth: the indentation level.
1473 *
1474 * Dumps debug information for the list of element node, it is recursive
1475 */
1476void
1477xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1478{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001479 xmlDebugCtxt ctxt;
1480
Daniel Veillard7db38712002-02-07 16:39:11 +00001481 if (output == NULL)
1482 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001483 xmlCtxtDumpInitCtxt(&ctxt);
1484 ctxt.output = output;
1485 ctxt.depth = depth;
1486 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001487 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001488}
1489
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001490/**
1491 * xmlDebugDumpDocumentHead:
1492 * @output: the FILE * for the output
1493 * @doc: the document
1494 *
1495 * Dumps debug information cncerning the document, not recursive
1496 */
1497void
1498xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1499{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001500 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001501
Daniel Veillard22cdb842004-10-04 14:09:17 +00001502 if (output == NULL)
1503 output = stdout;
1504 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001505 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001506 ctxt.output = output;
1507 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001508 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001509}
1510
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001511/**
1512 * xmlDebugDumpDocument:
1513 * @output: the FILE * for the output
1514 * @doc: the document
1515 *
1516 * Dumps debug information for the document, it's recursive
1517 */
1518void
1519xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1520{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001521 xmlDebugCtxt ctxt;
1522
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001523 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001524 output = stdout;
1525 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001526 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001527 ctxt.output = output;
1528 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001529 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001530}
Owen Taylor3473f882001-02-23 17:55:21 +00001531
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001532/**
1533 * xmlDebugDumpDTD:
1534 * @output: the FILE * for the output
1535 * @dtd: the DTD
1536 *
1537 * Dumps debug information for the DTD
1538 */
1539void
1540xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1541{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001542 xmlDebugCtxt ctxt;
1543
Daniel Veillard7db38712002-02-07 16:39:11 +00001544 if (output == NULL)
1545 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001546 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001547 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001548 ctxt.output = output;
1549 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001550 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001551}
1552
Daniel Veillard22cdb842004-10-04 14:09:17 +00001553/************************************************************************
1554 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001555 * Public entry points for checkings *
1556 * *
1557 ************************************************************************/
1558
1559/**
1560 * xmlDebugCheckDocument:
1561 * @output: the FILE * for the output
1562 * @doc: the document
1563 *
1564 * Check the document for potential content problems, and output
1565 * the errors to @output
1566 *
1567 * Returns the number of errors found
1568 */
1569int
1570xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1571{
1572 xmlDebugCtxt ctxt;
1573
1574 if (output == NULL)
1575 output = stdout;
1576 xmlCtxtDumpInitCtxt(&ctxt);
1577 ctxt.output = output;
1578 ctxt.check = 1;
1579 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001580 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001581 return(ctxt.errors);
1582}
1583
1584/************************************************************************
1585 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001586 * Helpers for Shell *
1587 * *
1588 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001589
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001590/**
1591 * xmlLsCountNode:
1592 * @node: the node to count
1593 *
1594 * Count the children of @node.
1595 *
1596 * Returns the number of children of @node.
1597 */
1598int
1599xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001600 int ret = 0;
1601 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001602
1603 if (node == NULL)
1604 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001605
1606 switch (node->type) {
1607 case XML_ELEMENT_NODE:
1608 list = node->children;
1609 break;
1610 case XML_DOCUMENT_NODE:
1611 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001612#ifdef LIBXML_DOCB_ENABLED
1613 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001614#endif
1615 list = ((xmlDocPtr) node)->children;
1616 break;
1617 case XML_ATTRIBUTE_NODE:
1618 list = ((xmlAttrPtr) node)->children;
1619 break;
1620 case XML_TEXT_NODE:
1621 case XML_CDATA_SECTION_NODE:
1622 case XML_PI_NODE:
1623 case XML_COMMENT_NODE:
1624 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001625 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001626 }
1627 break;
1628 case XML_ENTITY_REF_NODE:
1629 case XML_DOCUMENT_TYPE_NODE:
1630 case XML_ENTITY_NODE:
1631 case XML_DOCUMENT_FRAG_NODE:
1632 case XML_NOTATION_NODE:
1633 case XML_DTD_NODE:
1634 case XML_ELEMENT_DECL:
1635 case XML_ATTRIBUTE_DECL:
1636 case XML_ENTITY_DECL:
1637 case XML_NAMESPACE_DECL:
1638 case XML_XINCLUDE_START:
1639 case XML_XINCLUDE_END:
1640 ret = 1;
1641 break;
1642 }
1643 for (;list != NULL;ret++)
1644 list = list->next;
1645 return(ret);
1646}
1647
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001648/**
1649 * xmlLsOneNode:
1650 * @output: the FILE * for the output
1651 * @node: the node to dump
1652 *
1653 * Dump to @output the type and name of @node.
1654 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001655void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001656xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001657 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001658 if (node == NULL) {
1659 fprintf(output, "NULL\n");
1660 return;
1661 }
Owen Taylor3473f882001-02-23 17:55:21 +00001662 switch (node->type) {
1663 case XML_ELEMENT_NODE:
1664 fprintf(output, "-");
1665 break;
1666 case XML_ATTRIBUTE_NODE:
1667 fprintf(output, "a");
1668 break;
1669 case XML_TEXT_NODE:
1670 fprintf(output, "t");
1671 break;
1672 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001673 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001674 break;
1675 case XML_ENTITY_REF_NODE:
1676 fprintf(output, "e");
1677 break;
1678 case XML_ENTITY_NODE:
1679 fprintf(output, "E");
1680 break;
1681 case XML_PI_NODE:
1682 fprintf(output, "p");
1683 break;
1684 case XML_COMMENT_NODE:
1685 fprintf(output, "c");
1686 break;
1687 case XML_DOCUMENT_NODE:
1688 fprintf(output, "d");
1689 break;
1690 case XML_HTML_DOCUMENT_NODE:
1691 fprintf(output, "h");
1692 break;
1693 case XML_DOCUMENT_TYPE_NODE:
1694 fprintf(output, "T");
1695 break;
1696 case XML_DOCUMENT_FRAG_NODE:
1697 fprintf(output, "F");
1698 break;
1699 case XML_NOTATION_NODE:
1700 fprintf(output, "N");
1701 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001702 case XML_NAMESPACE_DECL:
1703 fprintf(output, "n");
1704 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001705 default:
1706 fprintf(output, "?");
1707 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001708 if (node->type != XML_NAMESPACE_DECL) {
1709 if (node->properties != NULL)
1710 fprintf(output, "a");
1711 else
1712 fprintf(output, "-");
1713 if (node->nsDef != NULL)
1714 fprintf(output, "n");
1715 else
1716 fprintf(output, "-");
1717 }
Owen Taylor3473f882001-02-23 17:55:21 +00001718
1719 fprintf(output, " %8d ", xmlLsCountNode(node));
1720
1721 switch (node->type) {
1722 case XML_ELEMENT_NODE:
1723 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001724 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001725 break;
1726 case XML_ATTRIBUTE_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_TEXT_NODE:
1731 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001732 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001733 }
1734 break;
1735 case XML_CDATA_SECTION_NODE:
1736 break;
1737 case XML_ENTITY_REF_NODE:
1738 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001739 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001740 break;
1741 case XML_ENTITY_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_PI_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_COMMENT_NODE:
1750 break;
1751 case XML_DOCUMENT_NODE:
1752 break;
1753 case XML_HTML_DOCUMENT_NODE:
1754 break;
1755 case XML_DOCUMENT_TYPE_NODE:
1756 break;
1757 case XML_DOCUMENT_FRAG_NODE:
1758 break;
1759 case XML_NOTATION_NODE:
1760 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001761 case XML_NAMESPACE_DECL: {
1762 xmlNsPtr ns = (xmlNsPtr) node;
1763
1764 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001765 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001766 else
William M. Brack13dfa872004-09-18 04:52:08 +00001767 fprintf(output, "%s -> %s", (char *)ns->prefix,
1768 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001769 break;
1770 }
Owen Taylor3473f882001-02-23 17:55:21 +00001771 default:
1772 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001773 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001774 }
1775 fprintf(output, "\n");
1776}
1777
Daniel Veillard78d12092001-10-11 09:12:24 +00001778/**
1779 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001780 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001781 *
1782 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001783 *
1784 * Returns a pointer to either "True" or "False"
1785 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001786const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001787xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001788{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001789 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001790 return("True");
1791 else
1792 return("False");
1793}
1794
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001795#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001796/****************************************************************
1797 * *
1798 * The XML shell related functions *
1799 * *
1800 ****************************************************************/
1801
Daniel Veillard78d12092001-10-11 09:12:24 +00001802
1803
Owen Taylor3473f882001-02-23 17:55:21 +00001804/*
1805 * TODO: Improvement/cleanups for the XML shell
1806 * - allow to shell out an editor on a subpart
1807 * - cleanup function registrations (with help) and calling
1808 * - provide registration routines
1809 */
1810
1811/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001812 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001813 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001814 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001815 *
1816 * Print the xpath error to libxml default error channel
1817 */
1818void
1819xmlShellPrintXPathError(int errorType, const char *arg)
1820{
1821 const char *default_arg = "Result";
1822
1823 if (!arg)
1824 arg = default_arg;
1825
1826 switch (errorType) {
1827 case XPATH_UNDEFINED:
1828 xmlGenericError(xmlGenericErrorContext,
1829 "%s: no such node\n", arg);
1830 break;
1831
1832 case XPATH_BOOLEAN:
1833 xmlGenericError(xmlGenericErrorContext,
1834 "%s is a Boolean\n", arg);
1835 break;
1836 case XPATH_NUMBER:
1837 xmlGenericError(xmlGenericErrorContext,
1838 "%s is a number\n", arg);
1839 break;
1840 case XPATH_STRING:
1841 xmlGenericError(xmlGenericErrorContext,
1842 "%s is a string\n", arg);
1843 break;
1844 case XPATH_POINT:
1845 xmlGenericError(xmlGenericErrorContext,
1846 "%s is a point\n", arg);
1847 break;
1848 case XPATH_RANGE:
1849 xmlGenericError(xmlGenericErrorContext,
1850 "%s is a range\n", arg);
1851 break;
1852 case XPATH_LOCATIONSET:
1853 xmlGenericError(xmlGenericErrorContext,
1854 "%s is a range\n", arg);
1855 break;
1856 case XPATH_USERS:
1857 xmlGenericError(xmlGenericErrorContext,
1858 "%s is user-defined\n", arg);
1859 break;
1860 case XPATH_XSLT_TREE:
1861 xmlGenericError(xmlGenericErrorContext,
1862 "%s is an XSLT value tree\n", arg);
1863 break;
1864 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001865#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001866 xmlGenericError(xmlGenericErrorContext,
1867 "Try casting the result string function (xpath builtin)\n",
1868 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001869#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001870}
1871
1872
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001873#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001874/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001875 * xmlShellPrintNodeCtxt:
1876 * @ctxt : a non-null shell context
1877 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001878 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001879 * Print node to the output FILE
1880 */
1881static void
1882xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1883{
Daniel Veillard01992e02002-10-09 10:20:30 +00001884 FILE *fp;
1885
1886 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001887 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001888 if (ctxt == NULL)
1889 fp = stdout;
1890 else
1891 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001892
1893 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001894 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001895 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001896 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001897 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001898 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001899
Daniel Veillard01992e02002-10-09 10:20:30 +00001900 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001901}
1902
1903/**
1904 * xmlShellPrintNode:
1905 * @node : a non-null node to print to the output FILE
1906 *
1907 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001908 */
1909void
1910xmlShellPrintNode(xmlNodePtr node)
1911{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001912 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001913}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001914#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001915
Daniel Veillard78d12092001-10-11 09:12:24 +00001916/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001917 * xmlShellPrintXPathResultCtxt:
1918 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001919 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001920 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001921 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001922 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001923static void
1924xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001925{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001926 if (!ctxt)
1927 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001928
1929 if (list != NULL) {
1930 switch (list->type) {
1931 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001932#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001933 int indx;
1934
1935 if (list->nodesetval) {
1936 for (indx = 0; indx < list->nodesetval->nodeNr;
1937 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001938 xmlShellPrintNodeCtxt(ctxt,
1939 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001940 }
1941 } else {
1942 xmlGenericError(xmlGenericErrorContext,
1943 "Empty node set\n");
1944 }
1945 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001946#else
1947 xmlGenericError(xmlGenericErrorContext,
1948 "Node set\n");
1949#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001950 }
1951 case XPATH_BOOLEAN:
1952 xmlGenericError(xmlGenericErrorContext,
1953 "Is a Boolean:%s\n",
1954 xmlBoolToText(list->boolval));
1955 break;
1956 case XPATH_NUMBER:
1957 xmlGenericError(xmlGenericErrorContext,
1958 "Is a number:%0g\n", list->floatval);
1959 break;
1960 case XPATH_STRING:
1961 xmlGenericError(xmlGenericErrorContext,
1962 "Is a string:%s\n", list->stringval);
1963 break;
1964
1965 default:
1966 xmlShellPrintXPathError(list->type, NULL);
1967 }
1968 }
1969}
1970
1971/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001972 * xmlShellPrintXPathResult:
1973 * @list: a valid result generated by an xpath evaluation
1974 *
1975 * Prints result to the output FILE
1976 */
1977void
1978xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1979{
1980 xmlShellPrintXPathResultCtxt(NULL, list);
1981}
1982
1983/**
Owen Taylor3473f882001-02-23 17:55:21 +00001984 * xmlShellList:
1985 * @ctxt: the shell context
1986 * @arg: unused
1987 * @node: a node
1988 * @node2: unused
1989 *
1990 * Implements the XML shell function "ls"
1991 * Does an Unix like listing of the given node (like a directory)
1992 *
1993 * Returns 0
1994 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001995int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001996xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001997 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1998 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1999{
Owen Taylor3473f882001-02-23 17:55:21 +00002000 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002001 if (!ctxt)
2002 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002003 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002004 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002005 return (0);
2006 }
Owen Taylor3473f882001-02-23 17:55:21 +00002007 if ((node->type == XML_DOCUMENT_NODE) ||
2008 (node->type == XML_HTML_DOCUMENT_NODE)) {
2009 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002010 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002011 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00002012 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002013 } else if (node->children != NULL) {
2014 cur = node->children;
2015 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002016 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002017 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002018 }
2019 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002020 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002021 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002022 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002023 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002024}
2025
2026/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002027 * xmlShellBase:
2028 * @ctxt: the shell context
2029 * @arg: unused
2030 * @node: a node
2031 * @node2: unused
2032 *
2033 * Implements the XML shell function "base"
2034 * dumps the current XML base of the node
2035 *
2036 * Returns 0
2037 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002038int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002039xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002040 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2041 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2042{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002043 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002044 if (!ctxt)
2045 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002046 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002047 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002048 return (0);
2049 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002050
2051 base = xmlNodeGetBase(node->doc, node);
2052
2053 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002054 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002055 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002056 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002057 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002058 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002059 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002060}
2061
Daniel Veillardb34321c2004-03-04 17:09:47 +00002062#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002063/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002064 * xmlShellSetBase:
2065 * @ctxt: the shell context
2066 * @arg: the new base
2067 * @node: a node
2068 * @node2: unused
2069 *
2070 * Implements the XML shell function "setbase"
2071 * change the current XML base of the node
2072 *
2073 * Returns 0
2074 */
2075static int
2076xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2077 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2078 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2079{
2080 xmlNodeSetBase(node, (xmlChar*) arg);
2081 return (0);
2082}
Daniel Veillard2156d432004-03-04 15:59:36 +00002083#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002084
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002085#ifdef LIBXML_XPATH_ENABLED
2086/**
2087 * xmlShellRegisterNamespace:
2088 * @ctxt: the shell context
2089 * @arg: a string in prefix=nsuri format
2090 * @node: unused
2091 * @node2: unused
2092 *
2093 * Implements the XML shell function "setns"
2094 * register/unregister a prefix=namespace pair
2095 * on the XPath context
2096 *
2097 * Returns 0 on success and a negative value otherwise.
2098 */
2099static int
2100xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2101 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2102{
2103 xmlChar* nsListDup;
2104 xmlChar* prefix;
2105 xmlChar* href;
2106 xmlChar* next;
2107
2108 nsListDup = xmlStrdup((xmlChar *) arg);
2109 next = nsListDup;
2110 while(next != NULL) {
2111 /* skip spaces */
2112 /*while((*next) == ' ') next++;*/
2113 if((*next) == '\0') break;
2114
2115 /* find prefix */
2116 prefix = next;
2117 next = (xmlChar*)xmlStrchr(next, '=');
2118 if(next == NULL) {
2119 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2120 xmlFree(nsListDup);
2121 return(-1);
2122 }
2123 *(next++) = '\0';
2124
2125 /* find href */
2126 href = next;
2127 next = (xmlChar*)xmlStrchr(next, ' ');
2128 if(next != NULL) {
2129 *(next++) = '\0';
2130 }
2131
2132 /* do register namespace */
2133 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2134 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2135 xmlFree(nsListDup);
2136 return(-1);
2137 }
2138 }
2139
2140 xmlFree(nsListDup);
2141 return(0);
2142}
Daniel Veillard20887ee2005-07-04 09:27:40 +00002143/**
2144 * xmlShellRegisterRootNamespaces:
2145 * @ctxt: the shell context
2146 * @arg: unused
2147 * @node: the root element
2148 * @node2: unused
2149 *
2150 * Implements the XML shell function "setrootns"
2151 * which registers all namespaces declarations found on the root element.
2152 *
2153 * Returns 0 on success and a negative value otherwise.
2154 */
2155static int
2156xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2157 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2158{
2159 xmlNsPtr ns;
2160
2161 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2162 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2163 return(-1);
2164 ns = root->nsDef;
2165 while (ns != NULL) {
2166 if (ns->prefix == NULL)
2167 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2168 else
2169 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2170 ns = ns->next;
2171 }
2172 return(0);
2173}
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002174#endif
2175
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002176/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002177 * xmlShellGrep:
2178 * @ctxt: the shell context
2179 * @arg: the string or regular expression to find
2180 * @node: a node
2181 * @node2: unused
2182 *
2183 * Implements the XML shell function "grep"
2184 * dumps informations about the node (namespace, attributes, content).
2185 *
2186 * Returns 0
2187 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002188static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002189xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2190 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2191{
2192 if (!ctxt)
2193 return (0);
2194 if (node == NULL)
2195 return (0);
2196 if (arg == NULL)
2197 return (0);
2198#ifdef LIBXML_REGEXP_ENABLED
2199 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2200 (xmlStrchr((xmlChar *) arg, '*')) ||
2201 (xmlStrchr((xmlChar *) arg, '.')) ||
2202 (xmlStrchr((xmlChar *) arg, '['))) {
2203 }
2204#endif
2205 while (node != NULL) {
2206 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002207 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002208
2209 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2210 xmlShellList(ctxt, NULL, node, NULL);
2211 }
2212 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002213 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002214
2215 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002216 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002217 }
2218 }
2219
2220 /*
2221 * Browse the full subtree, deep first
2222 */
2223
2224 if ((node->type == XML_DOCUMENT_NODE) ||
2225 (node->type == XML_HTML_DOCUMENT_NODE)) {
2226 node = ((xmlDocPtr) node)->children;
2227 } else if ((node->children != NULL)
2228 && (node->type != XML_ENTITY_REF_NODE)) {
2229 /* deep first */
2230 node = node->children;
2231 } else if (node->next != NULL) {
2232 /* then siblings */
2233 node = node->next;
2234 } else {
2235 /* go up to parents->next if needed */
2236 while (node != NULL) {
2237 if (node->parent != NULL) {
2238 node = node->parent;
2239 }
2240 if (node->next != NULL) {
2241 node = node->next;
2242 break;
2243 }
2244 if (node->parent == NULL) {
2245 node = NULL;
2246 break;
2247 }
2248 }
2249 }
2250 }
2251 return (0);
2252}
2253
2254/**
Owen Taylor3473f882001-02-23 17:55:21 +00002255 * xmlShellDir:
2256 * @ctxt: the shell context
2257 * @arg: unused
2258 * @node: a node
2259 * @node2: unused
2260 *
2261 * Implements the XML shell function "dir"
2262 * dumps informations about the node (namespace, attributes, content).
2263 *
2264 * Returns 0
2265 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002266int
2267xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2268 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2269 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2270{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002271 if (!ctxt)
2272 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002273 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002274 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002275 return (0);
2276 }
Owen Taylor3473f882001-02-23 17:55:21 +00002277 if ((node->type == XML_DOCUMENT_NODE) ||
2278 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002279 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002280 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002281 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002282 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002283 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002284 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002285 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002286}
2287
Daniel Veillard29b17482004-08-16 00:39:03 +00002288/**
2289 * xmlShellSetContent:
2290 * @ctxt: the shell context
2291 * @value: the content as a string
2292 * @node: a node
2293 * @node2: unused
2294 *
2295 * Implements the XML shell function "dir"
2296 * dumps informations about the node (namespace, attributes, content).
2297 *
2298 * Returns 0
2299 */
2300static int
2301xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2302 char *value, xmlNodePtr node,
2303 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2304{
2305 xmlNodePtr results;
2306 xmlParserErrors ret;
2307
2308 if (!ctxt)
2309 return (0);
2310 if (node == NULL) {
2311 fprintf(ctxt->output, "NULL\n");
2312 return (0);
2313 }
2314 if (value == NULL) {
2315 fprintf(ctxt->output, "NULL\n");
2316 return (0);
2317 }
2318
2319 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2320 if (ret == XML_ERR_OK) {
2321 if (node->children != NULL) {
2322 xmlFreeNodeList(node->children);
2323 node->children = NULL;
2324 node->last = NULL;
2325 }
2326 xmlAddChildList(node, results);
2327 } else {
2328 fprintf(ctxt->output, "failed to parse content\n");
2329 }
2330 return (0);
2331}
2332
Daniel Veillard522bc602004-02-21 11:53:09 +00002333#ifdef LIBXML_SCHEMAS_ENABLED
2334/**
2335 * xmlShellRNGValidate:
2336 * @ctxt: the shell context
2337 * @schemas: the path to the Relax-NG schemas
2338 * @node: a node
2339 * @node2: unused
2340 *
2341 * Implements the XML shell function "relaxng"
2342 * validating the instance against a Relax-NG schemas
2343 *
2344 * Returns 0
2345 */
2346static int
2347xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2348 xmlNodePtr node ATTRIBUTE_UNUSED,
2349 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2350{
2351 xmlRelaxNGPtr relaxngschemas;
2352 xmlRelaxNGParserCtxtPtr ctxt;
2353 xmlRelaxNGValidCtxtPtr vctxt;
2354 int ret;
2355
2356 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2357 xmlRelaxNGSetParserErrors(ctxt,
2358 (xmlRelaxNGValidityErrorFunc) fprintf,
2359 (xmlRelaxNGValidityWarningFunc) fprintf,
2360 stderr);
2361 relaxngschemas = xmlRelaxNGParse(ctxt);
2362 xmlRelaxNGFreeParserCtxt(ctxt);
2363 if (relaxngschemas == NULL) {
2364 xmlGenericError(xmlGenericErrorContext,
2365 "Relax-NG schema %s failed to compile\n", schemas);
2366 return(-1);
2367 }
2368 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2369 xmlRelaxNGSetValidErrors(vctxt,
2370 (xmlRelaxNGValidityErrorFunc) fprintf,
2371 (xmlRelaxNGValidityWarningFunc) fprintf,
2372 stderr);
2373 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2374 if (ret == 0) {
2375 fprintf(stderr, "%s validates\n", sctxt->filename);
2376 } else if (ret > 0) {
2377 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2378 } else {
2379 fprintf(stderr, "%s validation generated an internal error\n",
2380 sctxt->filename);
2381 }
2382 xmlRelaxNGFreeValidCtxt(vctxt);
2383 if (relaxngschemas != NULL)
2384 xmlRelaxNGFree(relaxngschemas);
2385 return(0);
2386}
2387#endif
2388
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002389#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002390/**
2391 * xmlShellCat:
2392 * @ctxt: the shell context
2393 * @arg: unused
2394 * @node: a node
2395 * @node2: unused
2396 *
2397 * Implements the XML shell function "cat"
2398 * dumps the serialization node content (XML or HTML).
2399 *
2400 * Returns 0
2401 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002402int
2403xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2404 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2405{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002406 if (!ctxt)
2407 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002408 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002409 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002410 return (0);
2411 }
Owen Taylor3473f882001-02-23 17:55:21 +00002412 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2413#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002414 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002415 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002416 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002417 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002418#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002419 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002420 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002421 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002422 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002423#endif /* LIBXML_HTML_ENABLED */
2424 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002425 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002426 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002427 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002428 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002429 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002430 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002431 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002432}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002433#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002434
2435/**
2436 * xmlShellLoad:
2437 * @ctxt: the shell context
2438 * @filename: the file name
2439 * @node: unused
2440 * @node2: unused
2441 *
2442 * Implements the XML shell function "load"
2443 * loads a new document specified by the filename
2444 *
2445 * Returns 0 or -1 if loading failed
2446 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002447int
2448xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2449 xmlNodePtr node ATTRIBUTE_UNUSED,
2450 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2451{
Owen Taylor3473f882001-02-23 17:55:21 +00002452 xmlDocPtr doc;
2453 int html = 0;
2454
Daniel Veillarda82b1822004-11-08 16:24:57 +00002455 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002456 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002457 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002458
2459 if (html) {
2460#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002461 doc = htmlParseFile(filename, NULL);
2462#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002463 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002464 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002465#endif /* LIBXML_HTML_ENABLED */
2466 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002467 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002468 }
2469 if (doc != NULL) {
2470 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002471 xmlFreeDoc(ctxt->doc);
2472 }
2473 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002474#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002475 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002476#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002477 xmlFree(ctxt->filename);
2478 ctxt->doc = doc;
2479 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002480#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002481 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002482#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002483 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002484 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002485 return (-1);
2486 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002487}
2488
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002489#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002490/**
2491 * xmlShellWrite:
2492 * @ctxt: the shell context
2493 * @filename: the file name
2494 * @node: a node in the tree
2495 * @node2: unused
2496 *
2497 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002498 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002499 * of the subtree under the @node specified
2500 *
2501 * Returns 0 or -1 in case of error
2502 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002503int
Owen Taylor3473f882001-02-23 17:55:21 +00002504xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002505 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2506{
Owen Taylor3473f882001-02-23 17:55:21 +00002507 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002508 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002509 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002510 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002511 }
2512#ifdef W_OK
2513 if (access((char *) filename, W_OK)) {
2514 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002515 "Cannot write to %s\n", filename);
2516 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002517 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002518#endif
2519 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002520 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002521 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2522 xmlGenericError(xmlGenericErrorContext,
2523 "Failed to write to %s\n", filename);
2524 return (-1);
2525 }
2526 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002527 case XML_HTML_DOCUMENT_NODE:
2528#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002529 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2530 xmlGenericError(xmlGenericErrorContext,
2531 "Failed to write to %s\n", filename);
2532 return (-1);
2533 }
Owen Taylor3473f882001-02-23 17:55:21 +00002534#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002535 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2536 xmlGenericError(xmlGenericErrorContext,
2537 "Failed to write to %s\n", filename);
2538 return (-1);
2539 }
Owen Taylor3473f882001-02-23 17:55:21 +00002540#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002541 break;
2542 default:{
2543 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002544
Daniel Veillard78d12092001-10-11 09:12:24 +00002545 f = fopen((char *) filename, "w");
2546 if (f == NULL) {
2547 xmlGenericError(xmlGenericErrorContext,
2548 "Failed to write to %s\n", filename);
2549 return (-1);
2550 }
2551 xmlElemDump(f, ctxt->doc, node);
2552 fclose(f);
2553 }
Owen Taylor3473f882001-02-23 17:55:21 +00002554 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002555 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002556}
2557
2558/**
2559 * xmlShellSave:
2560 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002561 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002562 * @node: unused
2563 * @node2: unused
2564 *
2565 * Implements the XML shell function "save"
2566 * Write the current document to the filename, or it's original name
2567 *
2568 * Returns 0 or -1 in case of error
2569 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002570int
2571xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2572 xmlNodePtr node ATTRIBUTE_UNUSED,
2573 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2574{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002575 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002576 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002577 if ((filename == NULL) || (filename[0] == 0))
2578 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002579 if (filename == NULL)
2580 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002581#ifdef W_OK
2582 if (access((char *) filename, W_OK)) {
2583 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002584 "Cannot save to %s\n", filename);
2585 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002586 }
2587#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002588 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002589 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002590 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2591 xmlGenericError(xmlGenericErrorContext,
2592 "Failed to save to %s\n", filename);
2593 }
2594 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002595 case XML_HTML_DOCUMENT_NODE:
2596#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002597 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2598 xmlGenericError(xmlGenericErrorContext,
2599 "Failed to save to %s\n", filename);
2600 }
Owen Taylor3473f882001-02-23 17:55:21 +00002601#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002602 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2603 xmlGenericError(xmlGenericErrorContext,
2604 "Failed to save to %s\n", filename);
2605 }
Owen Taylor3473f882001-02-23 17:55:21 +00002606#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002607 break;
2608 default:
2609 xmlGenericError(xmlGenericErrorContext,
2610 "To save to subparts of a document use the 'write' command\n");
2611 return (-1);
2612
Owen Taylor3473f882001-02-23 17:55:21 +00002613 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002614 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002615}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002616#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002617
Daniel Veillardf54cd532004-02-25 11:52:31 +00002618#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002619/**
2620 * xmlShellValidate:
2621 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002622 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002623 * @node: unused
2624 * @node2: unused
2625 *
2626 * Implements the XML shell function "validate"
2627 * Validate the document, if a DTD path is provided, then the validation
2628 * is done against the given DTD.
2629 *
2630 * Returns 0 or -1 in case of error
2631 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002632int
2633xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2634 xmlNodePtr node ATTRIBUTE_UNUSED,
2635 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2636{
Owen Taylor3473f882001-02-23 17:55:21 +00002637 xmlValidCtxt vctxt;
2638 int res = -1;
2639
Daniel Veillarda82b1822004-11-08 16:24:57 +00002640 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002641 vctxt.userData = stderr;
2642 vctxt.error = (xmlValidityErrorFunc) fprintf;
2643 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2644
2645 if ((dtd == NULL) || (dtd[0] == 0)) {
2646 res = xmlValidateDocument(&vctxt, ctxt->doc);
2647 } else {
2648 xmlDtdPtr subset;
2649
Daniel Veillard78d12092001-10-11 09:12:24 +00002650 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2651 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002652 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2653
Daniel Veillard78d12092001-10-11 09:12:24 +00002654 xmlFreeDtd(subset);
2655 }
Owen Taylor3473f882001-02-23 17:55:21 +00002656 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002657 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002658}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002659#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002660
2661/**
2662 * xmlShellDu:
2663 * @ctxt: the shell context
2664 * @arg: unused
2665 * @tree: a node defining a subtree
2666 * @node2: unused
2667 *
2668 * Implements the XML shell function "du"
2669 * show the structure of the subtree under node @tree
2670 * If @tree is null, the command works on the current node.
2671 *
2672 * Returns 0 or -1 in case of error
2673 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002674int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002675xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002676 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2677 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2678{
Owen Taylor3473f882001-02-23 17:55:21 +00002679 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002680 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002681
Daniel Veillard321be0c2002-10-08 21:26:42 +00002682 if (!ctxt)
2683 return (-1);
2684
Daniel Veillard78d12092001-10-11 09:12:24 +00002685 if (tree == NULL)
2686 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002687 node = tree;
2688 while (node != NULL) {
2689 if ((node->type == XML_DOCUMENT_NODE) ||
2690 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002691 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002692 } else if (node->type == XML_ELEMENT_NODE) {
2693 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002694 fprintf(ctxt->output, " ");
2695 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002696 } else {
2697 }
Owen Taylor3473f882001-02-23 17:55:21 +00002698
Daniel Veillard78d12092001-10-11 09:12:24 +00002699 /*
2700 * Browse the full subtree, deep first
2701 */
Owen Taylor3473f882001-02-23 17:55:21 +00002702
2703 if ((node->type == XML_DOCUMENT_NODE) ||
2704 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002705 node = ((xmlDocPtr) node)->children;
2706 } else if ((node->children != NULL)
2707 && (node->type != XML_ENTITY_REF_NODE)) {
2708 /* deep first */
2709 node = node->children;
2710 indent++;
2711 } else if ((node != tree) && (node->next != NULL)) {
2712 /* then siblings */
2713 node = node->next;
2714 } else if (node != tree) {
2715 /* go up to parents->next if needed */
2716 while (node != tree) {
2717 if (node->parent != NULL) {
2718 node = node->parent;
2719 indent--;
2720 }
2721 if ((node != tree) && (node->next != NULL)) {
2722 node = node->next;
2723 break;
2724 }
2725 if (node->parent == NULL) {
2726 node = NULL;
2727 break;
2728 }
2729 if (node == tree) {
2730 node = NULL;
2731 break;
2732 }
2733 }
2734 /* exit condition */
2735 if (node == tree)
2736 node = NULL;
2737 } else
2738 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002739 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002740 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002741}
2742
2743/**
2744 * xmlShellPwd:
2745 * @ctxt: the shell context
2746 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002747 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002748 * @node2: unused
2749 *
2750 * Implements the XML shell function "pwd"
2751 * Show the full path from the root to the node, if needed building
2752 * thumblers when similar elements exists at a given ancestor level.
2753 * The output is compatible with XPath commands.
2754 *
2755 * Returns 0 or -1 in case of error
2756 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002757int
2758xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2759 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2760{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002761 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002762
Daniel Veillarda82b1822004-11-08 16:24:57 +00002763 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002764 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002765
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002766 path = xmlGetNodePath(node);
2767 if (path == NULL)
2768 return (-1);
2769
2770 /*
2771 * This test prevents buffer overflow, because this routine
2772 * is only called by xmlShell, in which the second argument is
2773 * 500 chars long.
2774 * It is a dirty hack before a cleaner solution is found.
2775 * Documentation should mention that the second argument must
2776 * be at least 500 chars long, and could be stripped if too long.
2777 */
2778 snprintf(buffer, 499, "%s", path);
2779 buffer[499] = '0';
2780 xmlFree(path);
2781
Daniel Veillard78d12092001-10-11 09:12:24 +00002782 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002783}
2784
2785/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002786 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002787 * @doc: the initial document
2788 * @filename: the output buffer
2789 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002790 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002791 *
2792 * Implements the XML shell
2793 * This allow to load, validate, view, modify and save a document
2794 * using a environment similar to a UNIX commandline.
2795 */
2796void
2797xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002798 FILE * output)
2799{
Owen Taylor3473f882001-02-23 17:55:21 +00002800 char prompt[500] = "/ > ";
2801 char *cmdline = NULL, *cur;
2802 int nbargs;
2803 char command[100];
2804 char arg[400];
2805 int i;
2806 xmlShellCtxtPtr ctxt;
2807 xmlXPathObjectPtr list;
2808
2809 if (doc == NULL)
2810 return;
2811 if (filename == NULL)
2812 return;
2813 if (input == NULL)
2814 return;
2815 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002816 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002817 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002818 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002819 return;
2820 ctxt->loaded = 0;
2821 ctxt->doc = doc;
2822 ctxt->input = input;
2823 ctxt->output = output;
2824 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002825 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002826
2827#ifdef LIBXML_XPATH_ENABLED
2828 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2829 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002830 xmlFree(ctxt);
2831 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002832 }
2833#endif /* LIBXML_XPATH_ENABLED */
2834 while (1) {
2835 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002836 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002837 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002838 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002839 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002840 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002841 prompt[sizeof(prompt) - 1] = 0;
2842
Daniel Veillard78d12092001-10-11 09:12:24 +00002843 /*
2844 * Get a new command line
2845 */
Owen Taylor3473f882001-02-23 17:55:21 +00002846 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002847 if (cmdline == NULL)
2848 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002849
Daniel Veillard78d12092001-10-11 09:12:24 +00002850 /*
2851 * Parse the command itself
2852 */
2853 cur = cmdline;
2854 nbargs = 0;
2855 while ((*cur == ' ') || (*cur == '\t'))
2856 cur++;
2857 i = 0;
2858 while ((*cur != ' ') && (*cur != '\t') &&
2859 (*cur != '\n') && (*cur != '\r')) {
2860 if (*cur == 0)
2861 break;
2862 command[i++] = *cur++;
2863 }
2864 command[i] = 0;
2865 if (i == 0)
2866 continue;
2867 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002868
Daniel Veillard78d12092001-10-11 09:12:24 +00002869 /*
2870 * Parse the argument
2871 */
2872 while ((*cur == ' ') || (*cur == '\t'))
2873 cur++;
2874 i = 0;
2875 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2876 if (*cur == 0)
2877 break;
2878 arg[i++] = *cur++;
2879 }
2880 arg[i] = 0;
2881 if (i != 0)
2882 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002883
Daniel Veillard78d12092001-10-11 09:12:24 +00002884 /*
2885 * start interpreting the command
2886 */
Owen Taylor3473f882001-02-23 17:55:21 +00002887 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002888 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002889 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002890 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002891 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002892 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002893 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002894 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2895 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2896 fprintf(ctxt->output, "\tbye leave shell\n");
2897 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2898 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2899 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2900 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2901 fprintf(ctxt->output, "\texit leave shell\n");
2902 fprintf(ctxt->output, "\thelp display this help\n");
2903 fprintf(ctxt->output, "\tfree display memory usage\n");
2904 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2905 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002906 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 +00002907#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002908 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002909 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2910 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard20887ee2005-07-04 09:27:40 +00002911 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2912 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002913#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002914 fprintf(ctxt->output, "\tpwd display current working directory\n");
2915 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002916#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002917 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002918 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002919#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002920#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002921 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002922#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002923#ifdef LIBXML_SCHEMAS_ENABLED
2924 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2925#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002926 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002927#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002928 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002929 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002930#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002931 } else if (!strcmp(command, "load")) {
2932 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002933#ifdef LIBXML_SCHEMAS_ENABLED
2934 } else if (!strcmp(command, "relaxng")) {
2935 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2936#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002937#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002938 } else if (!strcmp(command, "save")) {
2939 xmlShellSave(ctxt, arg, NULL, NULL);
2940 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002941 if ((arg == NULL) || (arg[0] == 0))
2942 xmlGenericError(xmlGenericErrorContext,
2943 "Write command requires a filename argument\n");
2944 else
2945 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002946#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002947 } else if (!strcmp(command, "grep")) {
2948 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002949 } else if (!strcmp(command, "free")) {
2950 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002951 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002952 } else {
2953 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002954
Daniel Veillard78d12092001-10-11 09:12:24 +00002955 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002956 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002957 }
2958 } else if (!strcmp(command, "pwd")) {
2959 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002960
Daniel Veillard78d12092001-10-11 09:12:24 +00002961 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002962 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002963 } else if (!strcmp(command, "du")) {
2964 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2965 } else if (!strcmp(command, "base")) {
2966 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002967 } else if (!strcmp(command, "set")) {
2968 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002969#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002970 } else if (!strcmp(command, "setns")) {
2971 if (arg[0] == 0) {
2972 xmlGenericError(xmlGenericErrorContext,
2973 "setns: prefix=[nsuri] required\n");
2974 } else {
2975 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2976 }
Daniel Veillard20887ee2005-07-04 09:27:40 +00002977 } else if (!strcmp(command, "setrootns")) {
2978 xmlNodePtr root;
2979
2980 root = xmlDocGetRootElement(ctxt->doc);
2981 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002982 } else if (!strcmp(command, "xpath")) {
2983 if (arg[0] == 0) {
2984 xmlGenericError(xmlGenericErrorContext,
2985 "xpath: expression required\n");
2986 } else {
2987 ctxt->pctxt->node = ctxt->node;
2988 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002989 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002990 xmlXPathFreeObject(list);
2991 }
2992#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002993#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002994 } else if (!strcmp(command, "setbase")) {
2995 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002996#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002997 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2998 int dir = (!strcmp(command, "dir"));
2999
3000 if (arg[0] == 0) {
3001 if (dir)
3002 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3003 else
3004 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3005 } else {
3006 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00003007#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003008 ctxt->pctxt->node = ctxt->node;
3009 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3010#else
3011 list = NULL;
3012#endif /* LIBXML_XPATH_ENABLED */
3013 if (list != NULL) {
3014 switch (list->type) {
3015 case XPATH_UNDEFINED:
3016 xmlGenericError(xmlGenericErrorContext,
3017 "%s: no such node\n", arg);
3018 break;
3019 case XPATH_NODESET:{
3020 int indx;
3021
Daniel Veillarda6825e82001-11-07 13:33:59 +00003022 if (list->nodesetval == NULL)
3023 break;
3024
Daniel Veillard78d12092001-10-11 09:12:24 +00003025 for (indx = 0;
3026 indx < list->nodesetval->nodeNr;
3027 indx++) {
3028 if (dir)
3029 xmlShellDir(ctxt, NULL,
3030 list->nodesetval->
3031 nodeTab[indx], NULL);
3032 else
3033 xmlShellList(ctxt, NULL,
3034 list->nodesetval->
3035 nodeTab[indx], NULL);
3036 }
3037 break;
3038 }
3039 case XPATH_BOOLEAN:
3040 xmlGenericError(xmlGenericErrorContext,
3041 "%s is a Boolean\n", arg);
3042 break;
3043 case XPATH_NUMBER:
3044 xmlGenericError(xmlGenericErrorContext,
3045 "%s is a number\n", arg);
3046 break;
3047 case XPATH_STRING:
3048 xmlGenericError(xmlGenericErrorContext,
3049 "%s is a string\n", arg);
3050 break;
3051 case XPATH_POINT:
3052 xmlGenericError(xmlGenericErrorContext,
3053 "%s is a point\n", arg);
3054 break;
3055 case XPATH_RANGE:
3056 xmlGenericError(xmlGenericErrorContext,
3057 "%s is a range\n", arg);
3058 break;
3059 case XPATH_LOCATIONSET:
3060 xmlGenericError(xmlGenericErrorContext,
3061 "%s is a range\n", arg);
3062 break;
3063 case XPATH_USERS:
3064 xmlGenericError(xmlGenericErrorContext,
3065 "%s is user-defined\n", arg);
3066 break;
3067 case XPATH_XSLT_TREE:
3068 xmlGenericError(xmlGenericErrorContext,
3069 "%s is an XSLT value tree\n",
3070 arg);
3071 break;
3072 }
3073#ifdef LIBXML_XPATH_ENABLED
3074 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003075#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003076 } else {
3077 xmlGenericError(xmlGenericErrorContext,
3078 "%s: no such node\n", arg);
3079 }
3080 ctxt->pctxt->node = NULL;
3081 }
3082 } else if (!strcmp(command, "cd")) {
3083 if (arg[0] == 0) {
3084 ctxt->node = (xmlNodePtr) ctxt->doc;
3085 } else {
3086#ifdef LIBXML_XPATH_ENABLED
3087 ctxt->pctxt->node = ctxt->node;
3088 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3089#else
3090 list = NULL;
3091#endif /* LIBXML_XPATH_ENABLED */
3092 if (list != NULL) {
3093 switch (list->type) {
3094 case XPATH_UNDEFINED:
3095 xmlGenericError(xmlGenericErrorContext,
3096 "%s: no such node\n", arg);
3097 break;
3098 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003099 if (list->nodesetval != NULL) {
3100 if (list->nodesetval->nodeNr == 1) {
3101 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003102 if ((ctxt->node != NULL) &&
3103 (ctxt->node->type ==
3104 XML_NAMESPACE_DECL)) {
3105 xmlGenericError(xmlGenericErrorContext,
3106 "cannot cd to namespace\n");
3107 ctxt->node = NULL;
3108 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003109 } else
3110 xmlGenericError(xmlGenericErrorContext,
3111 "%s is a %d Node Set\n",
3112 arg,
3113 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003114 } else
3115 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003116 "%s is an empty Node Set\n",
3117 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003118 break;
3119 case XPATH_BOOLEAN:
3120 xmlGenericError(xmlGenericErrorContext,
3121 "%s is a Boolean\n", arg);
3122 break;
3123 case XPATH_NUMBER:
3124 xmlGenericError(xmlGenericErrorContext,
3125 "%s is a number\n", arg);
3126 break;
3127 case XPATH_STRING:
3128 xmlGenericError(xmlGenericErrorContext,
3129 "%s is a string\n", arg);
3130 break;
3131 case XPATH_POINT:
3132 xmlGenericError(xmlGenericErrorContext,
3133 "%s is a point\n", arg);
3134 break;
3135 case XPATH_RANGE:
3136 xmlGenericError(xmlGenericErrorContext,
3137 "%s is a range\n", arg);
3138 break;
3139 case XPATH_LOCATIONSET:
3140 xmlGenericError(xmlGenericErrorContext,
3141 "%s is a range\n", arg);
3142 break;
3143 case XPATH_USERS:
3144 xmlGenericError(xmlGenericErrorContext,
3145 "%s is user-defined\n", arg);
3146 break;
3147 case XPATH_XSLT_TREE:
3148 xmlGenericError(xmlGenericErrorContext,
3149 "%s is an XSLT value tree\n",
3150 arg);
3151 break;
3152 }
3153#ifdef LIBXML_XPATH_ENABLED
3154 xmlXPathFreeObject(list);
3155#endif
3156 } else {
3157 xmlGenericError(xmlGenericErrorContext,
3158 "%s: no such node\n", arg);
3159 }
3160 ctxt->pctxt->node = NULL;
3161 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003162#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003163 } else if (!strcmp(command, "cat")) {
3164 if (arg[0] == 0) {
3165 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3166 } else {
3167 ctxt->pctxt->node = ctxt->node;
3168#ifdef LIBXML_XPATH_ENABLED
3169 ctxt->pctxt->node = ctxt->node;
3170 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3171#else
3172 list = NULL;
3173#endif /* LIBXML_XPATH_ENABLED */
3174 if (list != NULL) {
3175 switch (list->type) {
3176 case XPATH_UNDEFINED:
3177 xmlGenericError(xmlGenericErrorContext,
3178 "%s: no such node\n", arg);
3179 break;
3180 case XPATH_NODESET:{
3181 int indx;
3182
Daniel Veillarda6825e82001-11-07 13:33:59 +00003183 if (list->nodesetval == NULL)
3184 break;
3185
Daniel Veillard78d12092001-10-11 09:12:24 +00003186 for (indx = 0;
3187 indx < list->nodesetval->nodeNr;
3188 indx++) {
3189 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003190 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003191 xmlShellCat(ctxt, NULL,
3192 list->nodesetval->
3193 nodeTab[indx], NULL);
3194 }
3195 break;
3196 }
3197 case XPATH_BOOLEAN:
3198 xmlGenericError(xmlGenericErrorContext,
3199 "%s is a Boolean\n", arg);
3200 break;
3201 case XPATH_NUMBER:
3202 xmlGenericError(xmlGenericErrorContext,
3203 "%s is a number\n", arg);
3204 break;
3205 case XPATH_STRING:
3206 xmlGenericError(xmlGenericErrorContext,
3207 "%s is a string\n", arg);
3208 break;
3209 case XPATH_POINT:
3210 xmlGenericError(xmlGenericErrorContext,
3211 "%s is a point\n", arg);
3212 break;
3213 case XPATH_RANGE:
3214 xmlGenericError(xmlGenericErrorContext,
3215 "%s is a range\n", arg);
3216 break;
3217 case XPATH_LOCATIONSET:
3218 xmlGenericError(xmlGenericErrorContext,
3219 "%s is a range\n", arg);
3220 break;
3221 case XPATH_USERS:
3222 xmlGenericError(xmlGenericErrorContext,
3223 "%s is user-defined\n", arg);
3224 break;
3225 case XPATH_XSLT_TREE:
3226 xmlGenericError(xmlGenericErrorContext,
3227 "%s is an XSLT value tree\n",
3228 arg);
3229 break;
3230 }
3231#ifdef LIBXML_XPATH_ENABLED
3232 xmlXPathFreeObject(list);
3233#endif
3234 } else {
3235 xmlGenericError(xmlGenericErrorContext,
3236 "%s: no such node\n", arg);
3237 }
3238 ctxt->pctxt->node = NULL;
3239 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003240#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003241 } else {
3242 xmlGenericError(xmlGenericErrorContext,
3243 "Unknown command %s\n", command);
3244 }
3245 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003246 }
3247#ifdef LIBXML_XPATH_ENABLED
3248 xmlXPathFreeContext(ctxt->pctxt);
3249#endif /* LIBXML_XPATH_ENABLED */
3250 if (ctxt->loaded) {
3251 xmlFreeDoc(ctxt->doc);
3252 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003253 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003254 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003255 xmlFree(ctxt);
3256 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003257 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003258}
3259
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003260#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003261#define bottom_debugXML
3262#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003263#endif /* LIBXML_DEBUG_ENABLED */