2 * debugXML.c : This is a set of routines used for debugging the tree
3 * produced by the XML parser.
5 * See Copyright for the status of this software.
7 * Daniel Veillard <daniel@veillard.com>
12 #ifdef LIBXML_DEBUG_ENABLED
21 #include <libxml/xmlmemory.h>
22 #include <libxml/tree.h>
23 #include <libxml/parser.h>
24 #include <libxml/parserInternals.h>
25 #include <libxml/valid.h>
26 #include <libxml/debugXML.h>
27 #include <libxml/HTMLtree.h>
28 #include <libxml/HTMLparser.h>
29 #include <libxml/xmlerror.h>
30 #include <libxml/globals.h>
31 #include <libxml/xpathInternals.h>
32 #include <libxml/uri.h>
33 #ifdef LIBXML_SCHEMAS_ENABLED
34 #include <libxml/relaxng.h>
37 #define DUMP_TEXT_TYPE 1
39 typedef struct _xmlDebugCtxt xmlDebugCtxt;
40 typedef xmlDebugCtxt *xmlDebugCtxtPtr;
41 struct _xmlDebugCtxt {
42 FILE *output; /* the output file */
43 char shift[101]; /* used for indenting */
44 int depth; /* current depth */
45 xmlDocPtr doc; /* current document */
46 xmlNodePtr node; /* current node */
47 xmlDictPtr dict; /* the doc dictionnary */
48 int check; /* do just checkings */
49 int errors; /* number of errors found */
50 int nodict; /* if the document has no dictionnary */
51 int options; /* options */
54 static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
57 xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
64 ctxt->output = stdout;
70 for (i = 0; i < 100; i++)
76 xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
78 /* remove the ATTRIBUTE_UNUSED when this is added */
84 * @ns: the namespace node
86 * Check that a given namespace is in scope on a node.
88 * 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
93 xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
97 if ((node == NULL) || (ns == NULL))
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))
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)) {
116 while (cur != NULL) {
119 if (xmlStrEqual(cur->prefix, ns->prefix))
126 /* 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;
138 xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
142 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
143 if (ctxt->depth < 50)
144 fprintf(ctxt->output, &ctxt->shift[100 - 2 * ctxt->depth]);
146 fprintf(ctxt->output, ctxt->shift);
152 * @ctxt: a debug context
153 * @error: the error code
155 * Handle a debug error.
158 xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
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,
168 xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
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,
178 xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
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,
189 * xmlCtxtNsCheckScope:
190 * @ctxt: the debugging context
192 * @ns: the namespace node
194 * Report if a given namespace is is not in scope.
197 xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
201 ret = xmlNsCheckScope(node, ns);
203 if (ns->prefix == NULL)
204 xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
205 "Reference to default namespace not in scope\n");
207 xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
208 "Reference to namespace '%s' not in scope\n",
209 (char *) ns->prefix);
212 if (ns->prefix == NULL)
213 xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
214 "Reference to default namespace not on ancestor\n");
216 xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
217 "Reference to namespace '%s' not on ancestor\n",
218 (char *) ns->prefix);
223 * xmlCtxtCheckString:
224 * @ctxt: the debug context
227 * Do debugging on the string, currently it just checks the UTF-8 content
230 xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
232 if (str == NULL) return;
234 if (!xmlCheckUTF8(str)) {
235 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
236 "String is not UTF-8 %s", (const char *) str);
243 * @ctxt: the debug context
246 * Do debugging on the name, for example the dictionnary status and
247 * conformance to the Name production.
250 xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
254 xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
257 if (xmlValidateName(name, 0)) {
258 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
259 "Name is not an NCName '%s'", (const char *) name);
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);
271 xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
277 if (node->parent == NULL)
278 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
279 "Node has no parent\n");
280 if (node->doc == NULL) {
281 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
282 "Node has no doc\n");
286 if ((dict == NULL) && (ctxt->nodict == 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");
295 if (ctxt->doc == NULL)
298 if (ctxt->dict == NULL) {
302 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");
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");
317 if (node->prev->next != node)
318 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
319 "Node prev->next : back link wrong\n");
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");
327 if (node->next->prev != node)
328 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
329 "Node next->prev : forward link wrong\n");
330 if (node->next->parent != node->parent)
331 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
332 "Node next->prev : forward link wrong\n");
334 if (node->type == XML_ELEMENT_NODE) {
339 xmlCtxtNsCheckScope(ctxt, node, ns);
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);
349 if ((node->type != XML_ELEMENT_NODE) &&
350 (node->type != XML_ATTRIBUTE_NODE) &&
351 (node->type != XML_ELEMENT_DECL) &&
352 (node->type != XML_ATTRIBUTE_DECL) &&
353 (node->type != XML_DTD_NODE) &&
354 (node->type != XML_ELEMENT_DECL) &&
355 (node->type != XML_HTML_DOCUMENT_NODE) &&
356 (node->type != XML_DOCUMENT_NODE)) {
357 if (node->content != NULL)
358 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
360 switch (node->type) {
361 case XML_ELEMENT_NODE:
362 case XML_ATTRIBUTE_NODE:
363 xmlCtxtCheckName(ctxt, node->name);
366 if ((node->name == xmlStringText) ||
367 (node->name == xmlStringTextNoenc))
369 /* some case of entity substitution can lead to this */
370 if ((ctxt->dict != NULL) &&
371 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
375 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
376 "Text node has wrong name '%s'",
377 (const char *) node->name);
379 case XML_COMMENT_NODE:
380 if (node->name == xmlStringComment)
382 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
383 "Comment node has wrong name '%s'",
384 (const char *) node->name);
387 xmlCtxtCheckName(ctxt, node->name);
389 case XML_CDATA_SECTION_NODE:
390 if (node->name == NULL)
392 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
393 "CData section has non NULL name '%s'",
394 (const char *) node->name);
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:
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:
411 case XML_DOCUMENT_NODE:
412 case XML_HTML_DOCUMENT_NODE:
418 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
425 /* TODO: check UTF8 content of the string */
427 fprintf(ctxt->output, "(NULL)");
430 for (i = 0; i < 40; i++)
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]);
438 fputc(str[i], ctxt->output);
439 fprintf(ctxt->output, "...");
443 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
445 xmlCtxtDumpSpaces(ctxt);
449 fprintf(ctxt->output, "DTD node is NULL\n");
453 if (dtd->type != XML_DTD_NODE) {
454 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
455 "Node is not a DTD");
459 if (dtd->name != NULL)
460 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
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");
470 * Do a bit of checking
472 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
476 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
478 xmlCtxtDumpSpaces(ctxt);
482 fprintf(ctxt->output, "Attribute declaration is NULL\n");
485 if (attr->type != XML_ATTRIBUTE_DECL) {
486 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
487 "Node is not an attribute declaration");
490 if (attr->name != NULL) {
492 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
494 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
495 "Node attribute declaration has no name");
496 if (attr->elem != NULL) {
498 fprintf(ctxt->output, " for %s", (char *) attr->elem);
500 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
501 "Node attribute declaration has no element name");
503 switch (attr->atype) {
504 case XML_ATTRIBUTE_CDATA:
505 fprintf(ctxt->output, " CDATA");
507 case XML_ATTRIBUTE_ID:
508 fprintf(ctxt->output, " ID");
510 case XML_ATTRIBUTE_IDREF:
511 fprintf(ctxt->output, " IDREF");
513 case XML_ATTRIBUTE_IDREFS:
514 fprintf(ctxt->output, " IDREFS");
516 case XML_ATTRIBUTE_ENTITY:
517 fprintf(ctxt->output, " ENTITY");
519 case XML_ATTRIBUTE_ENTITIES:
520 fprintf(ctxt->output, " ENTITIES");
522 case XML_ATTRIBUTE_NMTOKEN:
523 fprintf(ctxt->output, " NMTOKEN");
525 case XML_ATTRIBUTE_NMTOKENS:
526 fprintf(ctxt->output, " NMTOKENS");
528 case XML_ATTRIBUTE_ENUMERATION:
529 fprintf(ctxt->output, " ENUMERATION");
531 case XML_ATTRIBUTE_NOTATION:
532 fprintf(ctxt->output, " NOTATION ");
535 if (attr->tree != NULL) {
537 xmlEnumerationPtr cur = attr->tree;
539 for (indx = 0; indx < 5; indx++) {
541 fprintf(ctxt->output, "|%s", (char *) cur->name);
543 fprintf(ctxt->output, " (%s", (char *) cur->name);
549 fprintf(ctxt->output, ")");
551 fprintf(ctxt->output, "...)");
554 case XML_ATTRIBUTE_NONE:
556 case XML_ATTRIBUTE_REQUIRED:
557 fprintf(ctxt->output, " REQUIRED");
559 case XML_ATTRIBUTE_IMPLIED:
560 fprintf(ctxt->output, " IMPLIED");
562 case XML_ATTRIBUTE_FIXED:
563 fprintf(ctxt->output, " FIXED");
566 if (attr->defaultValue != NULL) {
567 fprintf(ctxt->output, "\"");
568 xmlCtxtDumpString(ctxt, attr->defaultValue);
569 fprintf(ctxt->output, "\"");
571 fprintf(ctxt->output, "\n");
575 * Do a bit of checking
577 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
581 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
583 xmlCtxtDumpSpaces(ctxt);
587 fprintf(ctxt->output, "Element declaration is NULL\n");
590 if (elem->type != XML_ELEMENT_DECL) {
591 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
592 "Node is not an element declaration");
595 if (elem->name != NULL) {
597 fprintf(ctxt->output, "ELEMDECL(");
598 xmlCtxtDumpString(ctxt, elem->name);
599 fprintf(ctxt->output, ")");
602 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
603 "Element declaration has no name");
605 switch (elem->etype) {
606 case XML_ELEMENT_TYPE_UNDEFINED:
607 fprintf(ctxt->output, ", UNDEFINED");
609 case XML_ELEMENT_TYPE_EMPTY:
610 fprintf(ctxt->output, ", EMPTY");
612 case XML_ELEMENT_TYPE_ANY:
613 fprintf(ctxt->output, ", ANY");
615 case XML_ELEMENT_TYPE_MIXED:
616 fprintf(ctxt->output, ", MIXED ");
618 case XML_ELEMENT_TYPE_ELEMENT:
619 fprintf(ctxt->output, ", MIXED ");
622 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
626 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
628 fprintf(ctxt->output, "%s", buf);
630 fprintf(ctxt->output, "\n");
634 * Do a bit of checking
636 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
640 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
642 xmlCtxtDumpSpaces(ctxt);
646 fprintf(ctxt->output, "Entity declaration is NULL\n");
649 if (ent->type != XML_ENTITY_DECL) {
650 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
651 "Node is not an entity declaration");
654 if (ent->name != NULL) {
656 fprintf(ctxt->output, "ENTITYDECL(");
657 xmlCtxtDumpString(ctxt, ent->name);
658 fprintf(ctxt->output, ")");
661 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
662 "Entity declaration has no name");
664 switch (ent->etype) {
665 case XML_INTERNAL_GENERAL_ENTITY:
666 fprintf(ctxt->output, ", internal\n");
668 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
669 fprintf(ctxt->output, ", external parsed\n");
671 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
672 fprintf(ctxt->output, ", unparsed\n");
674 case XML_INTERNAL_PARAMETER_ENTITY:
675 fprintf(ctxt->output, ", parameter\n");
677 case XML_EXTERNAL_PARAMETER_ENTITY:
678 fprintf(ctxt->output, ", external parameter\n");
680 case XML_INTERNAL_PREDEFINED_ENTITY:
681 fprintf(ctxt->output, ", predefined\n");
684 if (ent->ExternalID) {
685 xmlCtxtDumpSpaces(ctxt);
686 fprintf(ctxt->output, " ExternalID=%s\n",
687 (char *) ent->ExternalID);
690 xmlCtxtDumpSpaces(ctxt);
691 fprintf(ctxt->output, " SystemID=%s\n",
692 (char *) ent->SystemID);
694 if (ent->URI != NULL) {
695 xmlCtxtDumpSpaces(ctxt);
696 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
699 xmlCtxtDumpSpaces(ctxt);
700 fprintf(ctxt->output, " content=");
701 xmlCtxtDumpString(ctxt, ent->content);
702 fprintf(ctxt->output, "\n");
707 * Do a bit of checking
709 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
713 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
715 xmlCtxtDumpSpaces(ctxt);
719 fprintf(ctxt->output, "namespace node is NULL\n");
722 if (ns->type != XML_NAMESPACE_DECL) {
723 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
724 "Node is not a namespace declaration");
727 if (ns->href == NULL) {
728 if (ns->prefix != NULL)
729 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
730 "Incomplete namespace %s href=NULL\n",
731 (char *) ns->prefix);
733 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
734 "Incomplete default namespace href=NULL\n");
737 if (ns->prefix != NULL)
738 fprintf(ctxt->output, "namespace %s href=",
739 (char *) ns->prefix);
741 fprintf(ctxt->output, "default namespace href=");
743 xmlCtxtDumpString(ctxt, ns->href);
744 fprintf(ctxt->output, "\n");
750 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
753 xmlCtxtDumpNamespace(ctxt, ns);
759 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
761 xmlCtxtDumpSpaces(ctxt);
765 fprintf(ctxt->output, "Entity is NULL\n");
769 switch (ent->etype) {
770 case XML_INTERNAL_GENERAL_ENTITY:
771 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
773 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
774 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
776 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
777 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
779 case XML_INTERNAL_PARAMETER_ENTITY:
780 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
782 case XML_EXTERNAL_PARAMETER_ENTITY:
783 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
786 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
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);
795 xmlCtxtDumpSpaces(ctxt);
796 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
799 xmlCtxtDumpSpaces(ctxt);
800 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
803 xmlCtxtDumpSpaces(ctxt);
804 fprintf(ctxt->output, "content=");
805 xmlCtxtDumpString(ctxt, ent->content);
806 fprintf(ctxt->output, "\n");
813 * @output: the FILE * for the output
814 * @attr: the attribute
815 * @depth: the indentation level.
817 * Dumps debug information for the attribute
820 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
822 xmlCtxtDumpSpaces(ctxt);
826 fprintf(ctxt->output, "Attr is NULL");
830 fprintf(ctxt->output, "ATTRIBUTE ");
831 xmlCtxtDumpString(ctxt, attr->name);
832 fprintf(ctxt->output, "\n");
833 if (attr->children != NULL) {
835 xmlCtxtDumpNodeList(ctxt, attr->children);
839 if (attr->name == NULL)
840 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
841 "Attribute has no name");
844 * Do a bit of checking
846 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
850 * xmlCtxtDumpAttrList:
851 * @output: the FILE * for the output
852 * @attr: the attribute list
853 * @depth: the indentation level.
855 * Dumps debug information for the attribute list
858 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
860 while (attr != NULL) {
861 xmlCtxtDumpAttr(ctxt, attr);
867 * xmlCtxtDumpOneNode:
868 * @output: the FILE * for the output
870 * @depth: the indentation level.
872 * Dumps debug information for the element node, it is not recursive
875 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
879 xmlCtxtDumpSpaces(ctxt);
880 fprintf(ctxt->output, "node is NULL\n");
886 switch (node->type) {
887 case XML_ELEMENT_NODE:
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, ":");
895 xmlCtxtDumpString(ctxt, node->name);
896 fprintf(ctxt->output, "\n");
899 case XML_ATTRIBUTE_NODE:
901 xmlCtxtDumpSpaces(ctxt);
902 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
903 xmlCtxtGenericNodeCheck(ctxt, node);
907 xmlCtxtDumpSpaces(ctxt);
908 if (node->name == (const xmlChar *) xmlStringTextNoenc)
909 fprintf(ctxt->output, "TEXT no enc");
911 fprintf(ctxt->output, "TEXT");
912 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");
918 fprintf(ctxt->output, "\n");
920 fprintf(ctxt->output, "\n");
923 case XML_CDATA_SECTION_NODE:
925 xmlCtxtDumpSpaces(ctxt);
926 fprintf(ctxt->output, "CDATA_SECTION\n");
929 case XML_ENTITY_REF_NODE:
931 xmlCtxtDumpSpaces(ctxt);
932 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
933 (char *) node->name);
936 case XML_ENTITY_NODE:
938 xmlCtxtDumpSpaces(ctxt);
939 fprintf(ctxt->output, "ENTITY\n");
944 xmlCtxtDumpSpaces(ctxt);
945 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
948 case XML_COMMENT_NODE:
950 xmlCtxtDumpSpaces(ctxt);
951 fprintf(ctxt->output, "COMMENT\n");
954 case XML_DOCUMENT_NODE:
955 case XML_HTML_DOCUMENT_NODE:
957 xmlCtxtDumpSpaces(ctxt);
959 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
960 xmlCtxtGenericNodeCheck(ctxt, node);
962 case XML_DOCUMENT_TYPE_NODE:
964 xmlCtxtDumpSpaces(ctxt);
965 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
968 case XML_DOCUMENT_FRAG_NODE:
970 xmlCtxtDumpSpaces(ctxt);
971 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
974 case XML_NOTATION_NODE:
976 xmlCtxtDumpSpaces(ctxt);
977 fprintf(ctxt->output, "NOTATION\n");
981 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
983 case XML_ELEMENT_DECL:
984 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
986 case XML_ATTRIBUTE_DECL:
987 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
989 case XML_ENTITY_DECL:
990 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
992 case XML_NAMESPACE_DECL:
993 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
995 case XML_XINCLUDE_START:
997 xmlCtxtDumpSpaces(ctxt);
998 fprintf(ctxt->output, "INCLUDE START\n");
1001 case XML_XINCLUDE_END:
1003 xmlCtxtDumpSpaces(ctxt);
1004 fprintf(ctxt->output, "INCLUDE END\n");
1009 xmlCtxtDumpSpaces(ctxt);
1010 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1011 "Unknown node type %d\n", node->type);
1014 if (node->doc == NULL) {
1016 xmlCtxtDumpSpaces(ctxt);
1018 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1021 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
1022 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1023 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
1024 xmlCtxtDumpAttrList(ctxt, node->properties);
1025 if (node->type != XML_ENTITY_REF_NODE) {
1026 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1028 xmlCtxtDumpSpaces(ctxt);
1029 fprintf(ctxt->output, "content=");
1030 xmlCtxtDumpString(ctxt, node->content);
1031 fprintf(ctxt->output, "\n");
1037 ent = xmlGetDocEntity(node->doc, node->name);
1039 xmlCtxtDumpEntity(ctxt, ent);
1044 * Do a bit of checking
1046 xmlCtxtGenericNodeCheck(ctxt, node);
1051 * @output: the FILE * for the output
1053 * @depth: the indentation level.
1055 * Dumps debug information for the element node, it is recursive
1058 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1062 xmlCtxtDumpSpaces(ctxt);
1063 fprintf(ctxt->output, "node is NULL\n");
1067 xmlCtxtDumpOneNode(ctxt, node);
1068 if ((node->type != XML_NAMESPACE_DECL) &&
1069 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1071 xmlCtxtDumpNodeList(ctxt, node->children);
1077 * xmlCtxtDumpNodeList:
1078 * @output: the FILE * for the output
1079 * @node: the node list
1080 * @depth: the indentation level.
1082 * Dumps debug information for the list of element node, it is recursive
1085 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1087 while (node != NULL) {
1088 xmlCtxtDumpNode(ctxt, node);
1094 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1098 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1101 ctxt->node = (xmlNodePtr) doc;
1103 switch (doc->type) {
1104 case XML_ELEMENT_NODE:
1105 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1106 "Misplaced ELEMENT node\n");
1108 case XML_ATTRIBUTE_NODE:
1109 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1110 "Misplaced ATTRIBUTE node\n");
1113 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1114 "Misplaced TEXT node\n");
1116 case XML_CDATA_SECTION_NODE:
1117 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1118 "Misplaced CDATA node\n");
1120 case XML_ENTITY_REF_NODE:
1121 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1122 "Misplaced ENTITYREF node\n");
1124 case XML_ENTITY_NODE:
1125 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1126 "Misplaced ENTITY node\n");
1129 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1130 "Misplaced PI node\n");
1132 case XML_COMMENT_NODE:
1133 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1134 "Misplaced COMMENT node\n");
1136 case XML_DOCUMENT_NODE:
1138 fprintf(ctxt->output, "DOCUMENT\n");
1140 case XML_HTML_DOCUMENT_NODE:
1142 fprintf(ctxt->output, "HTML DOCUMENT\n");
1144 case XML_DOCUMENT_TYPE_NODE:
1145 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1146 "Misplaced DOCTYPE node\n");
1148 case XML_DOCUMENT_FRAG_NODE:
1149 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1150 "Misplaced FRAGMENT node\n");
1152 case XML_NOTATION_NODE:
1153 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1154 "Misplaced NOTATION node\n");
1157 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1158 "Unknown node type %d\n", doc->type);
1163 * xmlCtxtDumpDocumentHead:
1164 * @output: the FILE * for the output
1165 * @doc: the document
1167 * Dumps debug information cncerning the document, not recursive
1170 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1172 if (doc == NULL) return;
1173 xmlCtxtDumpDocHead(ctxt, doc);
1175 if (doc->name != NULL) {
1176 fprintf(ctxt->output, "name=");
1177 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1178 fprintf(ctxt->output, "\n");
1180 if (doc->version != NULL) {
1181 fprintf(ctxt->output, "version=");
1182 xmlCtxtDumpString(ctxt, doc->version);
1183 fprintf(ctxt->output, "\n");
1185 if (doc->encoding != NULL) {
1186 fprintf(ctxt->output, "encoding=");
1187 xmlCtxtDumpString(ctxt, doc->encoding);
1188 fprintf(ctxt->output, "\n");
1190 if (doc->URL != NULL) {
1191 fprintf(ctxt->output, "URL=");
1192 xmlCtxtDumpString(ctxt, doc->URL);
1193 fprintf(ctxt->output, "\n");
1195 if (doc->standalone)
1196 fprintf(ctxt->output, "standalone=true\n");
1198 if (doc->oldNs != NULL)
1199 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1203 * xmlCtxtDumpDocument:
1204 * @output: the FILE * for the output
1205 * @doc: the document
1207 * Dumps debug information for the document, it's recursive
1210 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1214 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1217 xmlCtxtDumpDocumentHead(ctxt, doc);
1218 if (((doc->type == XML_DOCUMENT_NODE) ||
1219 (doc->type == XML_HTML_DOCUMENT_NODE))
1220 && (doc->children != NULL)) {
1222 xmlCtxtDumpNodeList(ctxt, doc->children);
1228 xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1232 fprintf(ctxt->output, "Entity is NULL");
1236 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1237 switch (cur->etype) {
1238 case XML_INTERNAL_GENERAL_ENTITY:
1239 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1241 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1242 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1244 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1245 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1247 case XML_INTERNAL_PARAMETER_ENTITY:
1248 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1250 case XML_EXTERNAL_PARAMETER_ENTITY:
1251 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1254 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1255 "Unknown entity type %d\n", cur->etype);
1257 if (cur->ExternalID != NULL)
1258 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1259 if (cur->SystemID != NULL)
1260 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1261 if (cur->orig != NULL)
1262 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1263 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1264 fprintf(ctxt->output, "\n content \"%s\"",
1265 (char *) cur->content);
1266 fprintf(ctxt->output, "\n");
1271 * xmlCtxtDumpEntities:
1272 * @output: the FILE * for the output
1273 * @doc: the document
1275 * Dumps debug information for all the entities in use by the document
1278 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1280 if (doc == NULL) return;
1281 xmlCtxtDumpDocHead(ctxt, doc);
1282 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1283 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1284 doc->intSubset->entities;
1287 fprintf(ctxt->output, "Entities in internal subset\n");
1288 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1291 fprintf(ctxt->output, "No entities in internal subset\n");
1292 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1293 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1294 doc->extSubset->entities;
1297 fprintf(ctxt->output, "Entities in external subset\n");
1298 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1300 } else if (!ctxt->check)
1301 fprintf(ctxt->output, "No entities in external subset\n");
1306 * @output: the FILE * for the output
1309 * Dumps debug information for the DTD
1312 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1316 fprintf(ctxt->output, "DTD is NULL\n");
1319 xmlCtxtDumpDtdNode(ctxt, dtd);
1320 if (dtd->children == NULL)
1321 fprintf(ctxt->output, " DTD is empty\n");
1324 xmlCtxtDumpNodeList(ctxt, dtd->children);
1329 /************************************************************************
1331 * Public entry points for dump *
1333 ************************************************************************/
1336 * xmlDebugDumpString:
1337 * @output: the FILE * for the output
1340 * Dumps informations about the string, shorten it if necessary
1343 xmlDebugDumpString(FILE * output, const xmlChar * str)
1350 fprintf(output, "(NULL)");
1353 for (i = 0; i < 40; i++)
1356 else if (IS_BLANK_CH(str[i]))
1358 else if (str[i] >= 0x80)
1359 fprintf(output, "#%X", str[i]);
1361 fputc(str[i], output);
1362 fprintf(output, "...");
1367 * @output: the FILE * for the output
1368 * @attr: the attribute
1369 * @depth: the indentation level.
1371 * Dumps debug information for the attribute
1374 xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
1377 if (output == NULL) return;
1378 xmlCtxtDumpInitCtxt(&ctxt);
1379 ctxt.output = output;
1381 xmlCtxtDumpAttr(&ctxt, attr);
1382 xmlCtxtDumpCleanCtxt(&ctxt);
1387 * xmlDebugDumpEntities:
1388 * @output: the FILE * for the output
1389 * @doc: the document
1391 * Dumps debug information for all the entities in use by the document
1394 xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1398 if (output == NULL) return;
1399 xmlCtxtDumpInitCtxt(&ctxt);
1400 ctxt.output = output;
1401 xmlCtxtDumpEntities(&ctxt, doc);
1402 xmlCtxtDumpCleanCtxt(&ctxt);
1406 * xmlDebugDumpAttrList:
1407 * @output: the FILE * for the output
1408 * @attr: the attribute list
1409 * @depth: the indentation level.
1411 * Dumps debug information for the attribute list
1414 xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1418 if (output == NULL) return;
1419 xmlCtxtDumpInitCtxt(&ctxt);
1420 ctxt.output = output;
1422 xmlCtxtDumpAttrList(&ctxt, attr);
1423 xmlCtxtDumpCleanCtxt(&ctxt);
1427 * xmlDebugDumpOneNode:
1428 * @output: the FILE * for the output
1430 * @depth: the indentation level.
1432 * Dumps debug information for the element node, it is not recursive
1435 xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1439 if (output == NULL) return;
1440 xmlCtxtDumpInitCtxt(&ctxt);
1441 ctxt.output = output;
1443 xmlCtxtDumpOneNode(&ctxt, node);
1444 xmlCtxtDumpCleanCtxt(&ctxt);
1449 * @output: the FILE * for the output
1451 * @depth: the indentation level.
1453 * Dumps debug information for the element node, it is recursive
1456 xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1462 xmlCtxtDumpInitCtxt(&ctxt);
1463 ctxt.output = output;
1465 xmlCtxtDumpNode(&ctxt, node);
1466 xmlCtxtDumpCleanCtxt(&ctxt);
1470 * xmlDebugDumpNodeList:
1471 * @output: the FILE * for the output
1472 * @node: the node list
1473 * @depth: the indentation level.
1475 * Dumps debug information for the list of element node, it is recursive
1478 xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1484 xmlCtxtDumpInitCtxt(&ctxt);
1485 ctxt.output = output;
1487 xmlCtxtDumpNodeList(&ctxt, node);
1488 xmlCtxtDumpCleanCtxt(&ctxt);
1492 * xmlDebugDumpDocumentHead:
1493 * @output: the FILE * for the output
1494 * @doc: the document
1496 * Dumps debug information cncerning the document, not recursive
1499 xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1505 xmlCtxtDumpInitCtxt(&ctxt);
1506 ctxt.options |= DUMP_TEXT_TYPE;
1507 ctxt.output = output;
1508 xmlCtxtDumpDocumentHead(&ctxt, doc);
1509 xmlCtxtDumpCleanCtxt(&ctxt);
1513 * xmlDebugDumpDocument:
1514 * @output: the FILE * for the output
1515 * @doc: the document
1517 * Dumps debug information for the document, it's recursive
1520 xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1526 xmlCtxtDumpInitCtxt(&ctxt);
1527 ctxt.options |= DUMP_TEXT_TYPE;
1528 ctxt.output = output;
1529 xmlCtxtDumpDocument(&ctxt, doc);
1530 xmlCtxtDumpCleanCtxt(&ctxt);
1535 * @output: the FILE * for the output
1538 * Dumps debug information for the DTD
1541 xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1547 xmlCtxtDumpInitCtxt(&ctxt);
1548 ctxt.options |= DUMP_TEXT_TYPE;
1549 ctxt.output = output;
1550 xmlCtxtDumpDTD(&ctxt, dtd);
1551 xmlCtxtDumpCleanCtxt(&ctxt);
1554 /************************************************************************
1556 * Public entry points for checkings *
1558 ************************************************************************/
1561 * xmlDebugCheckDocument:
1562 * @output: the FILE * for the output
1563 * @doc: the document
1565 * Check the document for potential content problems, and output
1566 * the errors to @output
1568 * Returns the number of errors found
1571 xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1577 xmlCtxtDumpInitCtxt(&ctxt);
1578 ctxt.output = output;
1580 xmlCtxtDumpDocument(&ctxt, doc);
1581 xmlCtxtDumpCleanCtxt(&ctxt);
1582 return(ctxt.errors);
1585 /************************************************************************
1587 * Helpers for Shell *
1589 ************************************************************************/
1593 * @node: the node to count
1595 * Count the children of @node.
1597 * Returns the number of children of @node.
1600 xmlLsCountNode(xmlNodePtr node) {
1602 xmlNodePtr list = NULL;
1607 switch (node->type) {
1608 case XML_ELEMENT_NODE:
1609 list = node->children;
1611 case XML_DOCUMENT_NODE:
1612 case XML_HTML_DOCUMENT_NODE:
1613 #ifdef LIBXML_DOCB_ENABLED
1614 case XML_DOCB_DOCUMENT_NODE:
1616 list = ((xmlDocPtr) node)->children;
1618 case XML_ATTRIBUTE_NODE:
1619 list = ((xmlAttrPtr) node)->children;
1622 case XML_CDATA_SECTION_NODE:
1624 case XML_COMMENT_NODE:
1625 if (node->content != NULL) {
1626 ret = xmlStrlen(node->content);
1629 case XML_ENTITY_REF_NODE:
1630 case XML_DOCUMENT_TYPE_NODE:
1631 case XML_ENTITY_NODE:
1632 case XML_DOCUMENT_FRAG_NODE:
1633 case XML_NOTATION_NODE:
1635 case XML_ELEMENT_DECL:
1636 case XML_ATTRIBUTE_DECL:
1637 case XML_ENTITY_DECL:
1638 case XML_NAMESPACE_DECL:
1639 case XML_XINCLUDE_START:
1640 case XML_XINCLUDE_END:
1644 for (;list != NULL;ret++)
1651 * @output: the FILE * for the output
1652 * @node: the node to dump
1654 * Dump to @output the type and name of @node.
1657 xmlLsOneNode(FILE *output, xmlNodePtr node) {
1658 if (output == NULL) return;
1660 fprintf(output, "NULL\n");
1663 switch (node->type) {
1664 case XML_ELEMENT_NODE:
1665 fprintf(output, "-");
1667 case XML_ATTRIBUTE_NODE:
1668 fprintf(output, "a");
1671 fprintf(output, "t");
1673 case XML_CDATA_SECTION_NODE:
1674 fprintf(output, "C");
1676 case XML_ENTITY_REF_NODE:
1677 fprintf(output, "e");
1679 case XML_ENTITY_NODE:
1680 fprintf(output, "E");
1683 fprintf(output, "p");
1685 case XML_COMMENT_NODE:
1686 fprintf(output, "c");
1688 case XML_DOCUMENT_NODE:
1689 fprintf(output, "d");
1691 case XML_HTML_DOCUMENT_NODE:
1692 fprintf(output, "h");
1694 case XML_DOCUMENT_TYPE_NODE:
1695 fprintf(output, "T");
1697 case XML_DOCUMENT_FRAG_NODE:
1698 fprintf(output, "F");
1700 case XML_NOTATION_NODE:
1701 fprintf(output, "N");
1703 case XML_NAMESPACE_DECL:
1704 fprintf(output, "n");
1707 fprintf(output, "?");
1709 if (node->type != XML_NAMESPACE_DECL) {
1710 if (node->properties != NULL)
1711 fprintf(output, "a");
1713 fprintf(output, "-");
1714 if (node->nsDef != NULL)
1715 fprintf(output, "n");
1717 fprintf(output, "-");
1720 fprintf(output, " %8d ", xmlLsCountNode(node));
1722 switch (node->type) {
1723 case XML_ELEMENT_NODE:
1724 if (node->name != NULL)
1725 fprintf(output, "%s", (const char *) node->name);
1727 case XML_ATTRIBUTE_NODE:
1728 if (node->name != NULL)
1729 fprintf(output, "%s", (const char *) node->name);
1732 if (node->content != NULL) {
1733 xmlDebugDumpString(output, node->content);
1736 case XML_CDATA_SECTION_NODE:
1738 case XML_ENTITY_REF_NODE:
1739 if (node->name != NULL)
1740 fprintf(output, "%s", (const char *) node->name);
1742 case XML_ENTITY_NODE:
1743 if (node->name != NULL)
1744 fprintf(output, "%s", (const char *) node->name);
1747 if (node->name != NULL)
1748 fprintf(output, "%s", (const char *) node->name);
1750 case XML_COMMENT_NODE:
1752 case XML_DOCUMENT_NODE:
1754 case XML_HTML_DOCUMENT_NODE:
1756 case XML_DOCUMENT_TYPE_NODE:
1758 case XML_DOCUMENT_FRAG_NODE:
1760 case XML_NOTATION_NODE:
1762 case XML_NAMESPACE_DECL: {
1763 xmlNsPtr ns = (xmlNsPtr) node;
1765 if (ns->prefix == NULL)
1766 fprintf(output, "default -> %s", (char *)ns->href);
1768 fprintf(output, "%s -> %s", (char *)ns->prefix,
1773 if (node->name != NULL)
1774 fprintf(output, "%s", (const char *) node->name);
1776 fprintf(output, "\n");
1781 * @boolval: a bool to turn into text
1783 * Convenient way to turn bool into text
1785 * Returns a pointer to either "True" or "False"
1788 xmlBoolToText(int boolval)
1796 #ifdef LIBXML_XPATH_ENABLED
1797 /****************************************************************
1799 * The XML shell related functions *
1801 ****************************************************************/
1806 * TODO: Improvement/cleanups for the XML shell
1807 * - allow to shell out an editor on a subpart
1808 * - cleanup function registrations (with help) and calling
1809 * - provide registration routines
1813 * xmlShellPrintXPathError:
1814 * @errorType: valid xpath error id
1815 * @arg: the argument that cause xpath to fail
1817 * Print the xpath error to libxml default error channel
1820 xmlShellPrintXPathError(int errorType, const char *arg)
1822 const char *default_arg = "Result";
1827 switch (errorType) {
1828 case XPATH_UNDEFINED:
1829 xmlGenericError(xmlGenericErrorContext,
1830 "%s: no such node\n", arg);
1834 xmlGenericError(xmlGenericErrorContext,
1835 "%s is a Boolean\n", arg);
1838 xmlGenericError(xmlGenericErrorContext,
1839 "%s is a number\n", arg);
1842 xmlGenericError(xmlGenericErrorContext,
1843 "%s is a string\n", arg);
1846 xmlGenericError(xmlGenericErrorContext,
1847 "%s is a point\n", arg);
1850 xmlGenericError(xmlGenericErrorContext,
1851 "%s is a range\n", arg);
1853 case XPATH_LOCATIONSET:
1854 xmlGenericError(xmlGenericErrorContext,
1855 "%s is a range\n", arg);
1858 xmlGenericError(xmlGenericErrorContext,
1859 "%s is user-defined\n", arg);
1861 case XPATH_XSLT_TREE:
1862 xmlGenericError(xmlGenericErrorContext,
1863 "%s is an XSLT value tree\n", arg);
1867 xmlGenericError(xmlGenericErrorContext,
1868 "Try casting the result string function (xpath builtin)\n",
1874 #ifdef LIBXML_OUTPUT_ENABLED
1876 * xmlShellPrintNodeCtxt:
1877 * @ctxt : a non-null shell context
1878 * @node : a non-null node to print to the output FILE
1880 * Print node to the output FILE
1883 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1894 if (node->type == XML_DOCUMENT_NODE)
1895 xmlDocDump(fp, (xmlDocPtr) node);
1896 else if (node->type == XML_ATTRIBUTE_NODE)
1897 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
1899 xmlElemDump(fp, node->doc, node);
1905 * xmlShellPrintNode:
1906 * @node : a non-null node to print to the output FILE
1908 * Print node to the output FILE
1911 xmlShellPrintNode(xmlNodePtr node)
1913 xmlShellPrintNodeCtxt(NULL, node);
1915 #endif /* LIBXML_OUTPUT_ENABLED */
1918 * xmlShellPrintXPathResultCtxt:
1919 * @ctxt: a valid shell context
1920 * @list: a valid result generated by an xpath evaluation
1922 * Prints result to the output FILE
1925 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
1931 switch (list->type) {
1932 case XPATH_NODESET:{
1933 #ifdef LIBXML_OUTPUT_ENABLED
1936 if (list->nodesetval) {
1937 for (indx = 0; indx < list->nodesetval->nodeNr;
1939 xmlShellPrintNodeCtxt(ctxt,
1940 list->nodesetval->nodeTab[indx]);
1943 xmlGenericError(xmlGenericErrorContext,
1944 "Empty node set\n");
1948 xmlGenericError(xmlGenericErrorContext,
1950 #endif /* LIBXML_OUTPUT_ENABLED */
1953 xmlGenericError(xmlGenericErrorContext,
1954 "Is a Boolean:%s\n",
1955 xmlBoolToText(list->boolval));
1958 xmlGenericError(xmlGenericErrorContext,
1959 "Is a number:%0g\n", list->floatval);
1962 xmlGenericError(xmlGenericErrorContext,
1963 "Is a string:%s\n", list->stringval);
1967 xmlShellPrintXPathError(list->type, NULL);
1973 * xmlShellPrintXPathResult:
1974 * @list: a valid result generated by an xpath evaluation
1976 * Prints result to the output FILE
1979 xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1981 xmlShellPrintXPathResultCtxt(NULL, list);
1986 * @ctxt: the shell context
1991 * Implements the XML shell function "ls"
1992 * Does an Unix like listing of the given node (like a directory)
1997 xmlShellList(xmlShellCtxtPtr ctxt,
1998 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1999 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2005 fprintf(ctxt->output, "NULL\n");
2008 if ((node->type == XML_DOCUMENT_NODE) ||
2009 (node->type == XML_HTML_DOCUMENT_NODE)) {
2010 cur = ((xmlDocPtr) node)->children;
2011 } else if (node->type == XML_NAMESPACE_DECL) {
2012 xmlLsOneNode(ctxt->output, node);
2014 } else if (node->children != NULL) {
2015 cur = node->children;
2017 xmlLsOneNode(ctxt->output, node);
2020 while (cur != NULL) {
2021 xmlLsOneNode(ctxt->output, cur);
2029 * @ctxt: the shell context
2034 * Implements the XML shell function "base"
2035 * dumps the current XML base of the node
2040 xmlShellBase(xmlShellCtxtPtr ctxt,
2041 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2042 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2048 fprintf(ctxt->output, "NULL\n");
2052 base = xmlNodeGetBase(node->doc, node);
2055 fprintf(ctxt->output, " No base found !!!\n");
2057 fprintf(ctxt->output, "%s\n", base);
2063 #ifdef LIBXML_TREE_ENABLED
2066 * @ctxt: the shell context
2067 * @arg: the new base
2071 * Implements the XML shell function "setbase"
2072 * change the current XML base of the node
2077 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2078 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2079 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2081 xmlNodeSetBase(node, (xmlChar*) arg);
2086 #ifdef LIBXML_XPATH_ENABLED
2088 * xmlShellRegisterNamespace:
2089 * @ctxt: the shell context
2090 * @arg: a string in prefix=nsuri format
2094 * Implements the XML shell function "setns"
2095 * register/unregister a prefix=namespace pair
2096 * on the XPath context
2098 * Returns 0 on success and a negative value otherwise.
2101 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2102 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2109 nsListDup = xmlStrdup((xmlChar *) arg);
2111 while(next != NULL) {
2113 /*while((*next) == ' ') next++;*/
2114 if((*next) == '\0') break;
2118 next = (xmlChar*)xmlStrchr(next, '=');
2120 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2128 next = (xmlChar*)xmlStrchr(next, ' ');
2133 /* do register namespace */
2134 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2135 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2145 * xmlShellRegisterRootNamespaces:
2146 * @ctxt: the shell context
2148 * @node: the root element
2151 * Implements the XML shell function "setrootns"
2152 * which registers all namespaces declarations found on the root element.
2154 * Returns 0 on success and a negative value otherwise.
2157 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2158 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2162 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2163 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2166 while (ns != NULL) {
2167 if (ns->prefix == NULL)
2168 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2170 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2179 * @ctxt: the shell context
2180 * @arg: the string or regular expression to find
2184 * Implements the XML shell function "grep"
2185 * dumps informations about the node (namespace, attributes, content).
2190 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2191 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2199 #ifdef LIBXML_REGEXP_ENABLED
2200 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2201 (xmlStrchr((xmlChar *) arg, '*')) ||
2202 (xmlStrchr((xmlChar *) arg, '.')) ||
2203 (xmlStrchr((xmlChar *) arg, '['))) {
2206 while (node != NULL) {
2207 if (node->type == XML_COMMENT_NODE) {
2208 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2210 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2211 xmlShellList(ctxt, NULL, node, NULL);
2213 } else if (node->type == XML_TEXT_NODE) {
2214 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2216 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
2217 xmlShellList(ctxt, NULL, node->parent, NULL);
2222 * Browse the full subtree, deep first
2225 if ((node->type == XML_DOCUMENT_NODE) ||
2226 (node->type == XML_HTML_DOCUMENT_NODE)) {
2227 node = ((xmlDocPtr) node)->children;
2228 } else if ((node->children != NULL)
2229 && (node->type != XML_ENTITY_REF_NODE)) {
2231 node = node->children;
2232 } else if (node->next != NULL) {
2236 /* go up to parents->next if needed */
2237 while (node != NULL) {
2238 if (node->parent != NULL) {
2239 node = node->parent;
2241 if (node->next != NULL) {
2245 if (node->parent == NULL) {
2257 * @ctxt: the shell context
2262 * Implements the XML shell function "dir"
2263 * dumps informations about the node (namespace, attributes, content).
2268 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2269 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2270 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2275 fprintf(ctxt->output, "NULL\n");
2278 if ((node->type == XML_DOCUMENT_NODE) ||
2279 (node->type == XML_HTML_DOCUMENT_NODE)) {
2280 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
2281 } else if (node->type == XML_ATTRIBUTE_NODE) {
2282 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
2284 xmlDebugDumpOneNode(ctxt->output, node, 0);
2290 * xmlShellSetContent:
2291 * @ctxt: the shell context
2292 * @value: the content as a string
2296 * Implements the XML shell function "dir"
2297 * dumps informations about the node (namespace, attributes, content).
2302 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2303 char *value, xmlNodePtr node,
2304 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2307 xmlParserErrors ret;
2312 fprintf(ctxt->output, "NULL\n");
2315 if (value == NULL) {
2316 fprintf(ctxt->output, "NULL\n");
2320 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2321 if (ret == XML_ERR_OK) {
2322 if (node->children != NULL) {
2323 xmlFreeNodeList(node->children);
2324 node->children = NULL;
2327 xmlAddChildList(node, results);
2329 fprintf(ctxt->output, "failed to parse content\n");
2334 #ifdef LIBXML_SCHEMAS_ENABLED
2336 * xmlShellRNGValidate:
2337 * @ctxt: the shell context
2338 * @schemas: the path to the Relax-NG schemas
2342 * Implements the XML shell function "relaxng"
2343 * validating the instance against a Relax-NG schemas
2348 xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2349 xmlNodePtr node ATTRIBUTE_UNUSED,
2350 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2352 xmlRelaxNGPtr relaxngschemas;
2353 xmlRelaxNGParserCtxtPtr ctxt;
2354 xmlRelaxNGValidCtxtPtr vctxt;
2357 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2358 xmlRelaxNGSetParserErrors(ctxt,
2359 (xmlRelaxNGValidityErrorFunc) fprintf,
2360 (xmlRelaxNGValidityWarningFunc) fprintf,
2362 relaxngschemas = xmlRelaxNGParse(ctxt);
2363 xmlRelaxNGFreeParserCtxt(ctxt);
2364 if (relaxngschemas == NULL) {
2365 xmlGenericError(xmlGenericErrorContext,
2366 "Relax-NG schema %s failed to compile\n", schemas);
2369 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2370 xmlRelaxNGSetValidErrors(vctxt,
2371 (xmlRelaxNGValidityErrorFunc) fprintf,
2372 (xmlRelaxNGValidityWarningFunc) fprintf,
2374 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2376 fprintf(stderr, "%s validates\n", sctxt->filename);
2377 } else if (ret > 0) {
2378 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2380 fprintf(stderr, "%s validation generated an internal error\n",
2383 xmlRelaxNGFreeValidCtxt(vctxt);
2384 if (relaxngschemas != NULL)
2385 xmlRelaxNGFree(relaxngschemas);
2390 #ifdef LIBXML_OUTPUT_ENABLED
2393 * @ctxt: the shell context
2398 * Implements the XML shell function "cat"
2399 * dumps the serialization node content (XML or HTML).
2404 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2405 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2410 fprintf(ctxt->output, "NULL\n");
2413 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2414 #ifdef LIBXML_HTML_ENABLED
2415 if (node->type == XML_HTML_DOCUMENT_NODE)
2416 htmlDocDump(ctxt->output, (htmlDocPtr) node);
2418 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
2420 if (node->type == XML_DOCUMENT_NODE)
2421 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2423 xmlElemDump(ctxt->output, ctxt->doc, node);
2424 #endif /* LIBXML_HTML_ENABLED */
2426 if (node->type == XML_DOCUMENT_NODE)
2427 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2429 xmlElemDump(ctxt->output, ctxt->doc, node);
2431 fprintf(ctxt->output, "\n");
2434 #endif /* LIBXML_OUTPUT_ENABLED */
2438 * @ctxt: the shell context
2439 * @filename: the file name
2443 * Implements the XML shell function "load"
2444 * loads a new document specified by the filename
2446 * Returns 0 or -1 if loading failed
2449 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2450 xmlNodePtr node ATTRIBUTE_UNUSED,
2451 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2456 if ((ctxt == NULL) || (filename == NULL)) return(-1);
2457 if (ctxt->doc != NULL)
2458 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2461 #ifdef LIBXML_HTML_ENABLED
2462 doc = htmlParseFile(filename, NULL);
2464 fprintf(ctxt->output, "HTML support not compiled in\n");
2466 #endif /* LIBXML_HTML_ENABLED */
2468 doc = xmlReadFile(filename,NULL,0);
2471 if (ctxt->loaded == 1) {
2472 xmlFreeDoc(ctxt->doc);
2475 #ifdef LIBXML_XPATH_ENABLED
2476 xmlXPathFreeContext(ctxt->pctxt);
2477 #endif /* LIBXML_XPATH_ENABLED */
2478 xmlFree(ctxt->filename);
2480 ctxt->node = (xmlNodePtr) doc;
2481 #ifdef LIBXML_XPATH_ENABLED
2482 ctxt->pctxt = xmlXPathNewContext(doc);
2483 #endif /* LIBXML_XPATH_ENABLED */
2484 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2490 #ifdef LIBXML_OUTPUT_ENABLED
2493 * @ctxt: the shell context
2494 * @filename: the file name
2495 * @node: a node in the tree
2498 * Implements the XML shell function "write"
2499 * Write the current node to the filename, it saves the serialization
2500 * of the subtree under the @node specified
2502 * Returns 0 or -1 in case of error
2505 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2506 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2510 if ((filename == NULL) || (filename[0] == 0)) {
2514 if (access((char *) filename, W_OK)) {
2515 xmlGenericError(xmlGenericErrorContext,
2516 "Cannot write to %s\n", filename);
2520 switch (node->type) {
2521 case XML_DOCUMENT_NODE:
2522 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2523 xmlGenericError(xmlGenericErrorContext,
2524 "Failed to write to %s\n", filename);
2528 case XML_HTML_DOCUMENT_NODE:
2529 #ifdef LIBXML_HTML_ENABLED
2530 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2531 xmlGenericError(xmlGenericErrorContext,
2532 "Failed to write to %s\n", filename);
2536 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2537 xmlGenericError(xmlGenericErrorContext,
2538 "Failed to write to %s\n", filename);
2541 #endif /* LIBXML_HTML_ENABLED */
2546 f = fopen((char *) filename, "w");
2548 xmlGenericError(xmlGenericErrorContext,
2549 "Failed to write to %s\n", filename);
2552 xmlElemDump(f, ctxt->doc, node);
2561 * @ctxt: the shell context
2562 * @filename: the file name (optional)
2566 * Implements the XML shell function "save"
2567 * Write the current document to the filename, or it's original name
2569 * Returns 0 or -1 in case of error
2572 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2573 xmlNodePtr node ATTRIBUTE_UNUSED,
2574 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2576 if ((ctxt == NULL) || (ctxt->doc == NULL))
2578 if ((filename == NULL) || (filename[0] == 0))
2579 filename = ctxt->filename;
2580 if (filename == NULL)
2583 if (access((char *) filename, W_OK)) {
2584 xmlGenericError(xmlGenericErrorContext,
2585 "Cannot save to %s\n", filename);
2589 switch (ctxt->doc->type) {
2590 case XML_DOCUMENT_NODE:
2591 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2592 xmlGenericError(xmlGenericErrorContext,
2593 "Failed to save to %s\n", filename);
2596 case XML_HTML_DOCUMENT_NODE:
2597 #ifdef LIBXML_HTML_ENABLED
2598 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2599 xmlGenericError(xmlGenericErrorContext,
2600 "Failed to save to %s\n", filename);
2603 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2604 xmlGenericError(xmlGenericErrorContext,
2605 "Failed to save to %s\n", filename);
2607 #endif /* LIBXML_HTML_ENABLED */
2610 xmlGenericError(xmlGenericErrorContext,
2611 "To save to subparts of a document use the 'write' command\n");
2617 #endif /* LIBXML_OUTPUT_ENABLED */
2619 #ifdef LIBXML_VALID_ENABLED
2622 * @ctxt: the shell context
2623 * @dtd: the DTD URI (optional)
2627 * Implements the XML shell function "validate"
2628 * Validate the document, if a DTD path is provided, then the validation
2629 * is done against the given DTD.
2631 * Returns 0 or -1 in case of error
2634 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2635 xmlNodePtr node ATTRIBUTE_UNUSED,
2636 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2641 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2642 vctxt.userData = stderr;
2643 vctxt.error = (xmlValidityErrorFunc) fprintf;
2644 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2646 if ((dtd == NULL) || (dtd[0] == 0)) {
2647 res = xmlValidateDocument(&vctxt, ctxt->doc);
2651 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2652 if (subset != NULL) {
2653 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2660 #endif /* LIBXML_VALID_ENABLED */
2664 * @ctxt: the shell context
2666 * @tree: a node defining a subtree
2669 * Implements the XML shell function "du"
2670 * show the structure of the subtree under node @tree
2671 * If @tree is null, the command works on the current node.
2673 * Returns 0 or -1 in case of error
2676 xmlShellDu(xmlShellCtxtPtr ctxt,
2677 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2678 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2689 while (node != NULL) {
2690 if ((node->type == XML_DOCUMENT_NODE) ||
2691 (node->type == XML_HTML_DOCUMENT_NODE)) {
2692 fprintf(ctxt->output, "/\n");
2693 } else if (node->type == XML_ELEMENT_NODE) {
2694 for (i = 0; i < indent; i++)
2695 fprintf(ctxt->output, " ");
2696 fprintf(ctxt->output, "%s\n", node->name);
2701 * Browse the full subtree, deep first
2704 if ((node->type == XML_DOCUMENT_NODE) ||
2705 (node->type == XML_HTML_DOCUMENT_NODE)) {
2706 node = ((xmlDocPtr) node)->children;
2707 } else if ((node->children != NULL)
2708 && (node->type != XML_ENTITY_REF_NODE)) {
2710 node = node->children;
2712 } else if ((node != tree) && (node->next != NULL)) {
2715 } else if (node != tree) {
2716 /* go up to parents->next if needed */
2717 while (node != tree) {
2718 if (node->parent != NULL) {
2719 node = node->parent;
2722 if ((node != tree) && (node->next != NULL)) {
2726 if (node->parent == NULL) {
2735 /* exit condition */
2746 * @ctxt: the shell context
2747 * @buffer: the output buffer
2751 * Implements the XML shell function "pwd"
2752 * Show the full path from the root to the node, if needed building
2753 * thumblers when similar elements exists at a given ancestor level.
2754 * The output is compatible with XPath commands.
2756 * Returns 0 or -1 in case of error
2759 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2760 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2764 if ((node == NULL) || (buffer == NULL))
2767 path = xmlGetNodePath(node);
2772 * This test prevents buffer overflow, because this routine
2773 * is only called by xmlShell, in which the second argument is
2775 * It is a dirty hack before a cleaner solution is found.
2776 * Documentation should mention that the second argument must
2777 * be at least 500 chars long, and could be stripped if too long.
2779 snprintf(buffer, 499, "%s", path);
2788 * @doc: the initial document
2789 * @filename: the output buffer
2790 * @input: the line reading function
2791 * @output: the output FILE*, defaults to stdout if NULL
2793 * Implements the XML shell
2794 * This allow to load, validate, view, modify and save a document
2795 * using a environment similar to a UNIX commandline.
2798 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2801 char prompt[500] = "/ > ";
2802 char *cmdline = NULL, *cur;
2807 xmlShellCtxtPtr ctxt;
2808 xmlXPathObjectPtr list;
2812 if (filename == NULL)
2818 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2823 ctxt->input = input;
2824 ctxt->output = output;
2825 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2826 ctxt->node = (xmlNodePtr) ctxt->doc;
2828 #ifdef LIBXML_XPATH_ENABLED
2829 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2830 if (ctxt->pctxt == NULL) {
2834 #endif /* LIBXML_XPATH_ENABLED */
2836 if (ctxt->node == (xmlNodePtr) ctxt->doc)
2837 snprintf(prompt, sizeof(prompt), "%s > ", "/");
2838 else if ((ctxt->node != NULL) && (ctxt->node->name))
2839 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
2841 snprintf(prompt, sizeof(prompt), "? > ");
2842 prompt[sizeof(prompt) - 1] = 0;
2845 * Get a new command line
2847 cmdline = ctxt->input(prompt);
2848 if (cmdline == NULL)
2852 * Parse the command itself
2856 while ((*cur == ' ') || (*cur == '\t'))
2859 while ((*cur != ' ') && (*cur != '\t') &&
2860 (*cur != '\n') && (*cur != '\r')) {
2863 command[i++] = *cur++;
2871 * Parse the argument
2873 while ((*cur == ' ') || (*cur == '\t'))
2876 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2886 * start interpreting the command
2888 if (!strcmp(command, "exit"))
2890 if (!strcmp(command, "quit"))
2892 if (!strcmp(command, "bye"))
2894 if (!strcmp(command, "help")) {
2895 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2896 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2897 fprintf(ctxt->output, "\tbye leave shell\n");
2898 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2899 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2900 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2901 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2902 fprintf(ctxt->output, "\texit leave shell\n");
2903 fprintf(ctxt->output, "\thelp display this help\n");
2904 fprintf(ctxt->output, "\tfree display memory usage\n");
2905 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2906 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
2907 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2908 #ifdef LIBXML_XPATH_ENABLED
2909 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2910 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2911 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2912 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2913 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
2914 #endif /* LIBXML_XPATH_ENABLED */
2915 fprintf(ctxt->output, "\tpwd display current working directory\n");
2916 fprintf(ctxt->output, "\tquit leave shell\n");
2917 #ifdef LIBXML_OUTPUT_ENABLED
2918 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
2919 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2920 #endif /* LIBXML_OUTPUT_ENABLED */
2921 #ifdef LIBXML_VALID_ENABLED
2922 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
2923 #endif /* LIBXML_VALID_ENABLED */
2924 #ifdef LIBXML_SCHEMAS_ENABLED
2925 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2927 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
2928 #ifdef LIBXML_VALID_ENABLED
2929 } else if (!strcmp(command, "validate")) {
2930 xmlShellValidate(ctxt, arg, NULL, NULL);
2931 #endif /* LIBXML_VALID_ENABLED */
2932 } else if (!strcmp(command, "load")) {
2933 xmlShellLoad(ctxt, arg, NULL, NULL);
2934 #ifdef LIBXML_SCHEMAS_ENABLED
2935 } else if (!strcmp(command, "relaxng")) {
2936 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2938 #ifdef LIBXML_OUTPUT_ENABLED
2939 } else if (!strcmp(command, "save")) {
2940 xmlShellSave(ctxt, arg, NULL, NULL);
2941 } else if (!strcmp(command, "write")) {
2942 if ((arg == NULL) || (arg[0] == 0))
2943 xmlGenericError(xmlGenericErrorContext,
2944 "Write command requires a filename argument\n");
2946 xmlShellWrite(ctxt, arg, NULL, NULL);
2947 #endif /* LIBXML_OUTPUT_ENABLED */
2948 } else if (!strcmp(command, "grep")) {
2949 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2950 } else if (!strcmp(command, "free")) {
2952 xmlMemShow(ctxt->output, 0);
2956 sscanf(arg, "%d", &len);
2957 xmlMemShow(ctxt->output, len);
2959 } else if (!strcmp(command, "pwd")) {
2962 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2963 fprintf(ctxt->output, "%s\n", dir);
2964 } else if (!strcmp(command, "du")) {
2965 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2966 } else if (!strcmp(command, "base")) {
2967 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
2968 } else if (!strcmp(command, "set")) {
2969 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
2970 #ifdef LIBXML_XPATH_ENABLED
2971 } else if (!strcmp(command, "setns")) {
2973 xmlGenericError(xmlGenericErrorContext,
2974 "setns: prefix=[nsuri] required\n");
2976 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2978 } else if (!strcmp(command, "setrootns")) {
2981 root = xmlDocGetRootElement(ctxt->doc);
2982 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
2983 } else if (!strcmp(command, "xpath")) {
2985 xmlGenericError(xmlGenericErrorContext,
2986 "xpath: expression required\n");
2988 ctxt->pctxt->node = ctxt->node;
2989 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2990 xmlXPathDebugDumpObject(ctxt->output, list, 0);
2991 xmlXPathFreeObject(list);
2993 #endif /* LIBXML_XPATH_ENABLED */
2994 #ifdef LIBXML_TREE_ENABLED
2995 } else if (!strcmp(command, "setbase")) {
2996 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
2998 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2999 int dir = (!strcmp(command, "dir"));
3003 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3005 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3007 ctxt->pctxt->node = ctxt->node;
3008 #ifdef LIBXML_XPATH_ENABLED
3009 ctxt->pctxt->node = ctxt->node;
3010 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3013 #endif /* LIBXML_XPATH_ENABLED */
3015 switch (list->type) {
3016 case XPATH_UNDEFINED:
3017 xmlGenericError(xmlGenericErrorContext,
3018 "%s: no such node\n", arg);
3020 case XPATH_NODESET:{
3023 if (list->nodesetval == NULL)
3027 indx < list->nodesetval->nodeNr;
3030 xmlShellDir(ctxt, NULL,
3032 nodeTab[indx], NULL);
3034 xmlShellList(ctxt, NULL,
3036 nodeTab[indx], NULL);
3041 xmlGenericError(xmlGenericErrorContext,
3042 "%s is a Boolean\n", arg);
3045 xmlGenericError(xmlGenericErrorContext,
3046 "%s is a number\n", arg);
3049 xmlGenericError(xmlGenericErrorContext,
3050 "%s is a string\n", arg);
3053 xmlGenericError(xmlGenericErrorContext,
3054 "%s is a point\n", arg);
3057 xmlGenericError(xmlGenericErrorContext,
3058 "%s is a range\n", arg);
3060 case XPATH_LOCATIONSET:
3061 xmlGenericError(xmlGenericErrorContext,
3062 "%s is a range\n", arg);
3065 xmlGenericError(xmlGenericErrorContext,
3066 "%s is user-defined\n", arg);
3068 case XPATH_XSLT_TREE:
3069 xmlGenericError(xmlGenericErrorContext,
3070 "%s is an XSLT value tree\n",
3074 #ifdef LIBXML_XPATH_ENABLED
3075 xmlXPathFreeObject(list);
3078 xmlGenericError(xmlGenericErrorContext,
3079 "%s: no such node\n", arg);
3081 ctxt->pctxt->node = NULL;
3083 } else if (!strcmp(command, "cd")) {
3085 ctxt->node = (xmlNodePtr) ctxt->doc;
3087 #ifdef LIBXML_XPATH_ENABLED
3088 ctxt->pctxt->node = ctxt->node;
3089 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3092 #endif /* LIBXML_XPATH_ENABLED */
3094 switch (list->type) {
3095 case XPATH_UNDEFINED:
3096 xmlGenericError(xmlGenericErrorContext,
3097 "%s: no such node\n", arg);
3100 if (list->nodesetval != NULL) {
3101 if (list->nodesetval->nodeNr == 1) {
3102 ctxt->node = list->nodesetval->nodeTab[0];
3103 if ((ctxt->node != NULL) &&
3104 (ctxt->node->type ==
3105 XML_NAMESPACE_DECL)) {
3106 xmlGenericError(xmlGenericErrorContext,
3107 "cannot cd to namespace\n");
3111 xmlGenericError(xmlGenericErrorContext,
3112 "%s is a %d Node Set\n",
3114 list->nodesetval->nodeNr);
3116 xmlGenericError(xmlGenericErrorContext,
3117 "%s is an empty Node Set\n",
3121 xmlGenericError(xmlGenericErrorContext,
3122 "%s is a Boolean\n", arg);
3125 xmlGenericError(xmlGenericErrorContext,
3126 "%s is a number\n", arg);
3129 xmlGenericError(xmlGenericErrorContext,
3130 "%s is a string\n", arg);
3133 xmlGenericError(xmlGenericErrorContext,
3134 "%s is a point\n", arg);
3137 xmlGenericError(xmlGenericErrorContext,
3138 "%s is a range\n", arg);
3140 case XPATH_LOCATIONSET:
3141 xmlGenericError(xmlGenericErrorContext,
3142 "%s is a range\n", arg);
3145 xmlGenericError(xmlGenericErrorContext,
3146 "%s is user-defined\n", arg);
3148 case XPATH_XSLT_TREE:
3149 xmlGenericError(xmlGenericErrorContext,
3150 "%s is an XSLT value tree\n",
3154 #ifdef LIBXML_XPATH_ENABLED
3155 xmlXPathFreeObject(list);
3158 xmlGenericError(xmlGenericErrorContext,
3159 "%s: no such node\n", arg);
3161 ctxt->pctxt->node = NULL;
3163 #ifdef LIBXML_OUTPUT_ENABLED
3164 } else if (!strcmp(command, "cat")) {
3166 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3168 ctxt->pctxt->node = ctxt->node;
3169 #ifdef LIBXML_XPATH_ENABLED
3170 ctxt->pctxt->node = ctxt->node;
3171 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3174 #endif /* LIBXML_XPATH_ENABLED */
3176 switch (list->type) {
3177 case XPATH_UNDEFINED:
3178 xmlGenericError(xmlGenericErrorContext,
3179 "%s: no such node\n", arg);
3181 case XPATH_NODESET:{
3184 if (list->nodesetval == NULL)
3188 indx < list->nodesetval->nodeNr;
3191 fprintf(ctxt->output, " -------\n");
3192 xmlShellCat(ctxt, NULL,
3194 nodeTab[indx], NULL);
3199 xmlGenericError(xmlGenericErrorContext,
3200 "%s is a Boolean\n", arg);
3203 xmlGenericError(xmlGenericErrorContext,
3204 "%s is a number\n", arg);
3207 xmlGenericError(xmlGenericErrorContext,
3208 "%s is a string\n", arg);
3211 xmlGenericError(xmlGenericErrorContext,
3212 "%s is a point\n", arg);
3215 xmlGenericError(xmlGenericErrorContext,
3216 "%s is a range\n", arg);
3218 case XPATH_LOCATIONSET:
3219 xmlGenericError(xmlGenericErrorContext,
3220 "%s is a range\n", arg);
3223 xmlGenericError(xmlGenericErrorContext,
3224 "%s is user-defined\n", arg);
3226 case XPATH_XSLT_TREE:
3227 xmlGenericError(xmlGenericErrorContext,
3228 "%s is an XSLT value tree\n",
3232 #ifdef LIBXML_XPATH_ENABLED
3233 xmlXPathFreeObject(list);
3236 xmlGenericError(xmlGenericErrorContext,
3237 "%s: no such node\n", arg);
3239 ctxt->pctxt->node = NULL;
3241 #endif /* LIBXML_OUTPUT_ENABLED */
3243 xmlGenericError(xmlGenericErrorContext,
3244 "Unknown command %s\n", command);
3246 free(cmdline); /* not xmlFree here ! */
3248 #ifdef LIBXML_XPATH_ENABLED
3249 xmlXPathFreeContext(ctxt->pctxt);
3250 #endif /* LIBXML_XPATH_ENABLED */
3252 xmlFreeDoc(ctxt->doc);
3254 if (ctxt->filename != NULL)
3255 xmlFree(ctxt->filename);
3257 if (cmdline != NULL)
3258 free(cmdline); /* not xmlFree here ! */
3261 #endif /* LIBXML_XPATH_ENABLED */
3262 #define bottom_debugXML
3263 #include "elfgcchack.h"
3264 #endif /* LIBXML_DEBUG_ENABLED */