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, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
146 fprintf(ctxt->output, "%s", 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 ((ctxt->doc == NULL) ||
264 ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
265 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
266 "Name is not from the document dictionnary '%s'",
267 (const char *) name);
273 xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
279 if (node->parent == NULL)
280 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
281 "Node has no parent\n");
282 if (node->doc == NULL) {
283 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
284 "Node has no doc\n");
288 if ((dict == NULL) && (ctxt->nodict == 0)) {
290 /* desactivated right now as it raises too many errors */
291 if (doc->type == XML_DOCUMENT_NODE)
292 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
293 "Document has no dictionnary\n");
297 if (ctxt->doc == NULL)
300 if (ctxt->dict == NULL) {
304 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
305 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
306 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
307 "Node doc differs from parent's one\n");
308 if (node->prev == NULL) {
309 if (node->type == XML_ATTRIBUTE_NODE) {
310 if ((node->parent != NULL) &&
311 (node != (xmlNodePtr) node->parent->properties))
312 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
313 "Attr has no prev and not first of attr list\n");
315 } else if ((node->parent != NULL) && (node->parent->children != node))
316 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
317 "Node has no prev and not first of parent list\n");
319 if (node->prev->next != node)
320 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
321 "Node prev->next : back link wrong\n");
323 if (node->next == NULL) {
324 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
325 (node->parent->last != node) &&
326 (node->parent->type == XML_ELEMENT_NODE))
327 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
328 "Node has no next and not last of parent list\n");
330 if (node->next->prev != node)
331 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
332 "Node next->prev : forward link wrong\n");
333 if (node->next->parent != node->parent)
334 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
335 "Node next->prev : forward link wrong\n");
337 if (node->type == XML_ELEMENT_NODE) {
342 xmlCtxtNsCheckScope(ctxt, node, ns);
345 if (node->ns != NULL)
346 xmlCtxtNsCheckScope(ctxt, node, node->ns);
347 } else if (node->type == XML_ATTRIBUTE_NODE) {
348 if (node->ns != NULL)
349 xmlCtxtNsCheckScope(ctxt, node, node->ns);
352 if ((node->type != XML_ELEMENT_NODE) &&
353 (node->type != XML_ATTRIBUTE_NODE) &&
354 (node->type != XML_ELEMENT_DECL) &&
355 (node->type != XML_ATTRIBUTE_DECL) &&
356 (node->type != XML_DTD_NODE) &&
357 (node->type != XML_HTML_DOCUMENT_NODE) &&
358 (node->type != XML_DOCUMENT_NODE)) {
359 if (node->content != NULL)
360 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
362 switch (node->type) {
363 case XML_ELEMENT_NODE:
364 case XML_ATTRIBUTE_NODE:
365 xmlCtxtCheckName(ctxt, node->name);
368 if ((node->name == xmlStringText) ||
369 (node->name == xmlStringTextNoenc))
371 /* some case of entity substitution can lead to this */
372 if ((ctxt->dict != NULL) &&
373 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
377 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
378 "Text node has wrong name '%s'",
379 (const char *) node->name);
381 case XML_COMMENT_NODE:
382 if (node->name == xmlStringComment)
384 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
385 "Comment node has wrong name '%s'",
386 (const char *) node->name);
389 xmlCtxtCheckName(ctxt, node->name);
391 case XML_CDATA_SECTION_NODE:
392 if (node->name == NULL)
394 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
395 "CData section has non NULL name '%s'",
396 (const char *) node->name);
398 case XML_ENTITY_REF_NODE:
399 case XML_ENTITY_NODE:
400 case XML_DOCUMENT_TYPE_NODE:
401 case XML_DOCUMENT_FRAG_NODE:
402 case XML_NOTATION_NODE:
404 case XML_ELEMENT_DECL:
405 case XML_ATTRIBUTE_DECL:
406 case XML_ENTITY_DECL:
407 case XML_NAMESPACE_DECL:
408 case XML_XINCLUDE_START:
409 case XML_XINCLUDE_END:
410 #ifdef LIBXML_DOCB_ENABLED
411 case XML_DOCB_DOCUMENT_NODE:
413 case XML_DOCUMENT_NODE:
414 case XML_HTML_DOCUMENT_NODE:
420 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
427 /* TODO: check UTF8 content of the string */
429 fprintf(ctxt->output, "(NULL)");
432 for (i = 0; i < 40; i++)
435 else if (IS_BLANK_CH(str[i]))
436 fputc(' ', ctxt->output);
437 else if (str[i] >= 0x80)
438 fprintf(ctxt->output, "#%X", str[i]);
440 fputc(str[i], ctxt->output);
441 fprintf(ctxt->output, "...");
445 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
447 xmlCtxtDumpSpaces(ctxt);
451 fprintf(ctxt->output, "DTD node is NULL\n");
455 if (dtd->type != XML_DTD_NODE) {
456 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
457 "Node is not a DTD");
461 if (dtd->name != NULL)
462 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
464 fprintf(ctxt->output, "DTD");
465 if (dtd->ExternalID != NULL)
466 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
467 if (dtd->SystemID != NULL)
468 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
469 fprintf(ctxt->output, "\n");
472 * Do a bit of checking
474 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
478 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
480 xmlCtxtDumpSpaces(ctxt);
484 fprintf(ctxt->output, "Attribute declaration is NULL\n");
487 if (attr->type != XML_ATTRIBUTE_DECL) {
488 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
489 "Node is not an attribute declaration");
492 if (attr->name != NULL) {
494 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
496 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
497 "Node attribute declaration has no name");
498 if (attr->elem != NULL) {
500 fprintf(ctxt->output, " for %s", (char *) attr->elem);
502 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
503 "Node attribute declaration has no element name");
505 switch (attr->atype) {
506 case XML_ATTRIBUTE_CDATA:
507 fprintf(ctxt->output, " CDATA");
509 case XML_ATTRIBUTE_ID:
510 fprintf(ctxt->output, " ID");
512 case XML_ATTRIBUTE_IDREF:
513 fprintf(ctxt->output, " IDREF");
515 case XML_ATTRIBUTE_IDREFS:
516 fprintf(ctxt->output, " IDREFS");
518 case XML_ATTRIBUTE_ENTITY:
519 fprintf(ctxt->output, " ENTITY");
521 case XML_ATTRIBUTE_ENTITIES:
522 fprintf(ctxt->output, " ENTITIES");
524 case XML_ATTRIBUTE_NMTOKEN:
525 fprintf(ctxt->output, " NMTOKEN");
527 case XML_ATTRIBUTE_NMTOKENS:
528 fprintf(ctxt->output, " NMTOKENS");
530 case XML_ATTRIBUTE_ENUMERATION:
531 fprintf(ctxt->output, " ENUMERATION");
533 case XML_ATTRIBUTE_NOTATION:
534 fprintf(ctxt->output, " NOTATION ");
537 if (attr->tree != NULL) {
539 xmlEnumerationPtr cur = attr->tree;
541 for (indx = 0; indx < 5; indx++) {
543 fprintf(ctxt->output, "|%s", (char *) cur->name);
545 fprintf(ctxt->output, " (%s", (char *) cur->name);
551 fprintf(ctxt->output, ")");
553 fprintf(ctxt->output, "...)");
556 case XML_ATTRIBUTE_NONE:
558 case XML_ATTRIBUTE_REQUIRED:
559 fprintf(ctxt->output, " REQUIRED");
561 case XML_ATTRIBUTE_IMPLIED:
562 fprintf(ctxt->output, " IMPLIED");
564 case XML_ATTRIBUTE_FIXED:
565 fprintf(ctxt->output, " FIXED");
568 if (attr->defaultValue != NULL) {
569 fprintf(ctxt->output, "\"");
570 xmlCtxtDumpString(ctxt, attr->defaultValue);
571 fprintf(ctxt->output, "\"");
573 fprintf(ctxt->output, "\n");
577 * Do a bit of checking
579 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
583 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
585 xmlCtxtDumpSpaces(ctxt);
589 fprintf(ctxt->output, "Element declaration is NULL\n");
592 if (elem->type != XML_ELEMENT_DECL) {
593 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
594 "Node is not an element declaration");
597 if (elem->name != NULL) {
599 fprintf(ctxt->output, "ELEMDECL(");
600 xmlCtxtDumpString(ctxt, elem->name);
601 fprintf(ctxt->output, ")");
604 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
605 "Element declaration has no name");
607 switch (elem->etype) {
608 case XML_ELEMENT_TYPE_UNDEFINED:
609 fprintf(ctxt->output, ", UNDEFINED");
611 case XML_ELEMENT_TYPE_EMPTY:
612 fprintf(ctxt->output, ", EMPTY");
614 case XML_ELEMENT_TYPE_ANY:
615 fprintf(ctxt->output, ", ANY");
617 case XML_ELEMENT_TYPE_MIXED:
618 fprintf(ctxt->output, ", MIXED ");
620 case XML_ELEMENT_TYPE_ELEMENT:
621 fprintf(ctxt->output, ", MIXED ");
624 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
628 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
630 fprintf(ctxt->output, "%s", buf);
632 fprintf(ctxt->output, "\n");
636 * Do a bit of checking
638 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
642 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
644 xmlCtxtDumpSpaces(ctxt);
648 fprintf(ctxt->output, "Entity declaration is NULL\n");
651 if (ent->type != XML_ENTITY_DECL) {
652 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
653 "Node is not an entity declaration");
656 if (ent->name != NULL) {
658 fprintf(ctxt->output, "ENTITYDECL(");
659 xmlCtxtDumpString(ctxt, ent->name);
660 fprintf(ctxt->output, ")");
663 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
664 "Entity declaration has no name");
666 switch (ent->etype) {
667 case XML_INTERNAL_GENERAL_ENTITY:
668 fprintf(ctxt->output, ", internal\n");
670 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
671 fprintf(ctxt->output, ", external parsed\n");
673 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
674 fprintf(ctxt->output, ", unparsed\n");
676 case XML_INTERNAL_PARAMETER_ENTITY:
677 fprintf(ctxt->output, ", parameter\n");
679 case XML_EXTERNAL_PARAMETER_ENTITY:
680 fprintf(ctxt->output, ", external parameter\n");
682 case XML_INTERNAL_PREDEFINED_ENTITY:
683 fprintf(ctxt->output, ", predefined\n");
686 if (ent->ExternalID) {
687 xmlCtxtDumpSpaces(ctxt);
688 fprintf(ctxt->output, " ExternalID=%s\n",
689 (char *) ent->ExternalID);
692 xmlCtxtDumpSpaces(ctxt);
693 fprintf(ctxt->output, " SystemID=%s\n",
694 (char *) ent->SystemID);
696 if (ent->URI != NULL) {
697 xmlCtxtDumpSpaces(ctxt);
698 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
701 xmlCtxtDumpSpaces(ctxt);
702 fprintf(ctxt->output, " content=");
703 xmlCtxtDumpString(ctxt, ent->content);
704 fprintf(ctxt->output, "\n");
709 * Do a bit of checking
711 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
715 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
717 xmlCtxtDumpSpaces(ctxt);
721 fprintf(ctxt->output, "namespace node is NULL\n");
724 if (ns->type != XML_NAMESPACE_DECL) {
725 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
726 "Node is not a namespace declaration");
729 if (ns->href == NULL) {
730 if (ns->prefix != NULL)
731 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
732 "Incomplete namespace %s href=NULL\n",
733 (char *) ns->prefix);
735 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
736 "Incomplete default namespace href=NULL\n");
739 if (ns->prefix != NULL)
740 fprintf(ctxt->output, "namespace %s href=",
741 (char *) ns->prefix);
743 fprintf(ctxt->output, "default namespace href=");
745 xmlCtxtDumpString(ctxt, ns->href);
746 fprintf(ctxt->output, "\n");
752 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
755 xmlCtxtDumpNamespace(ctxt, ns);
761 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
763 xmlCtxtDumpSpaces(ctxt);
767 fprintf(ctxt->output, "Entity is NULL\n");
771 switch (ent->etype) {
772 case XML_INTERNAL_GENERAL_ENTITY:
773 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
775 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
776 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
778 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
779 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
781 case XML_INTERNAL_PARAMETER_ENTITY:
782 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
784 case XML_EXTERNAL_PARAMETER_ENTITY:
785 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
788 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
790 fprintf(ctxt->output, "%s\n", ent->name);
791 if (ent->ExternalID) {
792 xmlCtxtDumpSpaces(ctxt);
793 fprintf(ctxt->output, "ExternalID=%s\n",
794 (char *) ent->ExternalID);
797 xmlCtxtDumpSpaces(ctxt);
798 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
801 xmlCtxtDumpSpaces(ctxt);
802 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
805 xmlCtxtDumpSpaces(ctxt);
806 fprintf(ctxt->output, "content=");
807 xmlCtxtDumpString(ctxt, ent->content);
808 fprintf(ctxt->output, "\n");
815 * @output: the FILE * for the output
816 * @attr: the attribute
817 * @depth: the indentation level.
819 * Dumps debug information for the attribute
822 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
824 xmlCtxtDumpSpaces(ctxt);
828 fprintf(ctxt->output, "Attr is NULL");
832 fprintf(ctxt->output, "ATTRIBUTE ");
833 xmlCtxtDumpString(ctxt, attr->name);
834 fprintf(ctxt->output, "\n");
835 if (attr->children != NULL) {
837 xmlCtxtDumpNodeList(ctxt, attr->children);
841 if (attr->name == NULL)
842 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
843 "Attribute has no name");
846 * Do a bit of checking
848 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
852 * xmlCtxtDumpAttrList:
853 * @output: the FILE * for the output
854 * @attr: the attribute list
855 * @depth: the indentation level.
857 * Dumps debug information for the attribute list
860 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
862 while (attr != NULL) {
863 xmlCtxtDumpAttr(ctxt, attr);
869 * xmlCtxtDumpOneNode:
870 * @output: the FILE * for the output
872 * @depth: the indentation level.
874 * Dumps debug information for the element node, it is not recursive
877 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
881 xmlCtxtDumpSpaces(ctxt);
882 fprintf(ctxt->output, "node is NULL\n");
888 switch (node->type) {
889 case XML_ELEMENT_NODE:
891 xmlCtxtDumpSpaces(ctxt);
892 fprintf(ctxt->output, "ELEMENT ");
893 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
894 xmlCtxtDumpString(ctxt, node->ns->prefix);
895 fprintf(ctxt->output, ":");
897 xmlCtxtDumpString(ctxt, node->name);
898 fprintf(ctxt->output, "\n");
901 case XML_ATTRIBUTE_NODE:
903 xmlCtxtDumpSpaces(ctxt);
904 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
905 xmlCtxtGenericNodeCheck(ctxt, node);
909 xmlCtxtDumpSpaces(ctxt);
910 if (node->name == (const xmlChar *) xmlStringTextNoenc)
911 fprintf(ctxt->output, "TEXT no enc");
913 fprintf(ctxt->output, "TEXT");
914 if (ctxt->options & DUMP_TEXT_TYPE) {
915 if (node->content == (xmlChar *) &(node->properties))
916 fprintf(ctxt->output, " compact\n");
917 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
918 fprintf(ctxt->output, " interned\n");
920 fprintf(ctxt->output, "\n");
922 fprintf(ctxt->output, "\n");
925 case XML_CDATA_SECTION_NODE:
927 xmlCtxtDumpSpaces(ctxt);
928 fprintf(ctxt->output, "CDATA_SECTION\n");
931 case XML_ENTITY_REF_NODE:
933 xmlCtxtDumpSpaces(ctxt);
934 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
935 (char *) node->name);
938 case XML_ENTITY_NODE:
940 xmlCtxtDumpSpaces(ctxt);
941 fprintf(ctxt->output, "ENTITY\n");
946 xmlCtxtDumpSpaces(ctxt);
947 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
950 case XML_COMMENT_NODE:
952 xmlCtxtDumpSpaces(ctxt);
953 fprintf(ctxt->output, "COMMENT\n");
956 case XML_DOCUMENT_NODE:
957 case XML_HTML_DOCUMENT_NODE:
959 xmlCtxtDumpSpaces(ctxt);
961 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
962 xmlCtxtGenericNodeCheck(ctxt, node);
964 case XML_DOCUMENT_TYPE_NODE:
966 xmlCtxtDumpSpaces(ctxt);
967 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
970 case XML_DOCUMENT_FRAG_NODE:
972 xmlCtxtDumpSpaces(ctxt);
973 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
976 case XML_NOTATION_NODE:
978 xmlCtxtDumpSpaces(ctxt);
979 fprintf(ctxt->output, "NOTATION\n");
983 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
985 case XML_ELEMENT_DECL:
986 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
988 case XML_ATTRIBUTE_DECL:
989 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
991 case XML_ENTITY_DECL:
992 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
994 case XML_NAMESPACE_DECL:
995 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
997 case XML_XINCLUDE_START:
999 xmlCtxtDumpSpaces(ctxt);
1000 fprintf(ctxt->output, "INCLUDE START\n");
1003 case XML_XINCLUDE_END:
1005 xmlCtxtDumpSpaces(ctxt);
1006 fprintf(ctxt->output, "INCLUDE END\n");
1011 xmlCtxtDumpSpaces(ctxt);
1012 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1013 "Unknown node type %d\n", node->type);
1016 if (node->doc == NULL) {
1018 xmlCtxtDumpSpaces(ctxt);
1020 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1023 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
1024 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1025 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
1026 xmlCtxtDumpAttrList(ctxt, node->properties);
1027 if (node->type != XML_ENTITY_REF_NODE) {
1028 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1030 xmlCtxtDumpSpaces(ctxt);
1031 fprintf(ctxt->output, "content=");
1032 xmlCtxtDumpString(ctxt, node->content);
1033 fprintf(ctxt->output, "\n");
1039 ent = xmlGetDocEntity(node->doc, node->name);
1041 xmlCtxtDumpEntity(ctxt, ent);
1046 * Do a bit of checking
1048 xmlCtxtGenericNodeCheck(ctxt, node);
1053 * @output: the FILE * for the output
1055 * @depth: the indentation level.
1057 * Dumps debug information for the element node, it is recursive
1060 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1064 xmlCtxtDumpSpaces(ctxt);
1065 fprintf(ctxt->output, "node is NULL\n");
1069 xmlCtxtDumpOneNode(ctxt, node);
1070 if ((node->type != XML_NAMESPACE_DECL) &&
1071 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1073 xmlCtxtDumpNodeList(ctxt, node->children);
1079 * xmlCtxtDumpNodeList:
1080 * @output: the FILE * for the output
1081 * @node: the node list
1082 * @depth: the indentation level.
1084 * Dumps debug information for the list of element node, it is recursive
1087 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1089 while (node != NULL) {
1090 xmlCtxtDumpNode(ctxt, node);
1096 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1100 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1103 ctxt->node = (xmlNodePtr) doc;
1105 switch (doc->type) {
1106 case XML_ELEMENT_NODE:
1107 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1108 "Misplaced ELEMENT node\n");
1110 case XML_ATTRIBUTE_NODE:
1111 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1112 "Misplaced ATTRIBUTE node\n");
1115 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1116 "Misplaced TEXT node\n");
1118 case XML_CDATA_SECTION_NODE:
1119 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1120 "Misplaced CDATA node\n");
1122 case XML_ENTITY_REF_NODE:
1123 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1124 "Misplaced ENTITYREF node\n");
1126 case XML_ENTITY_NODE:
1127 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1128 "Misplaced ENTITY node\n");
1131 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1132 "Misplaced PI node\n");
1134 case XML_COMMENT_NODE:
1135 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1136 "Misplaced COMMENT node\n");
1138 case XML_DOCUMENT_NODE:
1140 fprintf(ctxt->output, "DOCUMENT\n");
1142 case XML_HTML_DOCUMENT_NODE:
1144 fprintf(ctxt->output, "HTML DOCUMENT\n");
1146 case XML_DOCUMENT_TYPE_NODE:
1147 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1148 "Misplaced DOCTYPE node\n");
1150 case XML_DOCUMENT_FRAG_NODE:
1151 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1152 "Misplaced FRAGMENT node\n");
1154 case XML_NOTATION_NODE:
1155 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1156 "Misplaced NOTATION node\n");
1159 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1160 "Unknown node type %d\n", doc->type);
1165 * xmlCtxtDumpDocumentHead:
1166 * @output: the FILE * for the output
1167 * @doc: the document
1169 * Dumps debug information cncerning the document, not recursive
1172 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1174 if (doc == NULL) return;
1175 xmlCtxtDumpDocHead(ctxt, doc);
1177 if (doc->name != NULL) {
1178 fprintf(ctxt->output, "name=");
1179 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1180 fprintf(ctxt->output, "\n");
1182 if (doc->version != NULL) {
1183 fprintf(ctxt->output, "version=");
1184 xmlCtxtDumpString(ctxt, doc->version);
1185 fprintf(ctxt->output, "\n");
1187 if (doc->encoding != NULL) {
1188 fprintf(ctxt->output, "encoding=");
1189 xmlCtxtDumpString(ctxt, doc->encoding);
1190 fprintf(ctxt->output, "\n");
1192 if (doc->URL != NULL) {
1193 fprintf(ctxt->output, "URL=");
1194 xmlCtxtDumpString(ctxt, doc->URL);
1195 fprintf(ctxt->output, "\n");
1197 if (doc->standalone)
1198 fprintf(ctxt->output, "standalone=true\n");
1200 if (doc->oldNs != NULL)
1201 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1205 * xmlCtxtDumpDocument:
1206 * @output: the FILE * for the output
1207 * @doc: the document
1209 * Dumps debug information for the document, it's recursive
1212 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1216 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1219 xmlCtxtDumpDocumentHead(ctxt, doc);
1220 if (((doc->type == XML_DOCUMENT_NODE) ||
1221 (doc->type == XML_HTML_DOCUMENT_NODE))
1222 && (doc->children != NULL)) {
1224 xmlCtxtDumpNodeList(ctxt, doc->children);
1230 xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1234 fprintf(ctxt->output, "Entity is NULL");
1238 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1239 switch (cur->etype) {
1240 case XML_INTERNAL_GENERAL_ENTITY:
1241 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1243 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1244 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1246 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1247 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1249 case XML_INTERNAL_PARAMETER_ENTITY:
1250 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1252 case XML_EXTERNAL_PARAMETER_ENTITY:
1253 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1256 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1257 "Unknown entity type %d\n", cur->etype);
1259 if (cur->ExternalID != NULL)
1260 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1261 if (cur->SystemID != NULL)
1262 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1263 if (cur->orig != NULL)
1264 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1265 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1266 fprintf(ctxt->output, "\n content \"%s\"",
1267 (char *) cur->content);
1268 fprintf(ctxt->output, "\n");
1273 * xmlCtxtDumpEntities:
1274 * @output: the FILE * for the output
1275 * @doc: the document
1277 * Dumps debug information for all the entities in use by the document
1280 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1282 if (doc == NULL) return;
1283 xmlCtxtDumpDocHead(ctxt, doc);
1284 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1285 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1286 doc->intSubset->entities;
1289 fprintf(ctxt->output, "Entities in internal subset\n");
1290 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1293 fprintf(ctxt->output, "No entities in internal subset\n");
1294 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1295 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1296 doc->extSubset->entities;
1299 fprintf(ctxt->output, "Entities in external subset\n");
1300 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1302 } else if (!ctxt->check)
1303 fprintf(ctxt->output, "No entities in external subset\n");
1308 * @output: the FILE * for the output
1311 * Dumps debug information for the DTD
1314 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1318 fprintf(ctxt->output, "DTD is NULL\n");
1321 xmlCtxtDumpDtdNode(ctxt, dtd);
1322 if (dtd->children == NULL)
1323 fprintf(ctxt->output, " DTD is empty\n");
1326 xmlCtxtDumpNodeList(ctxt, dtd->children);
1331 /************************************************************************
1333 * Public entry points for dump *
1335 ************************************************************************/
1338 * xmlDebugDumpString:
1339 * @output: the FILE * for the output
1342 * Dumps informations about the string, shorten it if necessary
1345 xmlDebugDumpString(FILE * output, const xmlChar * str)
1352 fprintf(output, "(NULL)");
1355 for (i = 0; i < 40; i++)
1358 else if (IS_BLANK_CH(str[i]))
1360 else if (str[i] >= 0x80)
1361 fprintf(output, "#%X", str[i]);
1363 fputc(str[i], output);
1364 fprintf(output, "...");
1369 * @output: the FILE * for the output
1370 * @attr: the attribute
1371 * @depth: the indentation level.
1373 * Dumps debug information for the attribute
1376 xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
1379 if (output == NULL) return;
1380 xmlCtxtDumpInitCtxt(&ctxt);
1381 ctxt.output = output;
1383 xmlCtxtDumpAttr(&ctxt, attr);
1384 xmlCtxtDumpCleanCtxt(&ctxt);
1389 * xmlDebugDumpEntities:
1390 * @output: the FILE * for the output
1391 * @doc: the document
1393 * Dumps debug information for all the entities in use by the document
1396 xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1400 if (output == NULL) return;
1401 xmlCtxtDumpInitCtxt(&ctxt);
1402 ctxt.output = output;
1403 xmlCtxtDumpEntities(&ctxt, doc);
1404 xmlCtxtDumpCleanCtxt(&ctxt);
1408 * xmlDebugDumpAttrList:
1409 * @output: the FILE * for the output
1410 * @attr: the attribute list
1411 * @depth: the indentation level.
1413 * Dumps debug information for the attribute list
1416 xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1420 if (output == NULL) return;
1421 xmlCtxtDumpInitCtxt(&ctxt);
1422 ctxt.output = output;
1424 xmlCtxtDumpAttrList(&ctxt, attr);
1425 xmlCtxtDumpCleanCtxt(&ctxt);
1429 * xmlDebugDumpOneNode:
1430 * @output: the FILE * for the output
1432 * @depth: the indentation level.
1434 * Dumps debug information for the element node, it is not recursive
1437 xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1441 if (output == NULL) return;
1442 xmlCtxtDumpInitCtxt(&ctxt);
1443 ctxt.output = output;
1445 xmlCtxtDumpOneNode(&ctxt, node);
1446 xmlCtxtDumpCleanCtxt(&ctxt);
1451 * @output: the FILE * for the output
1453 * @depth: the indentation level.
1455 * Dumps debug information for the element node, it is recursive
1458 xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1464 xmlCtxtDumpInitCtxt(&ctxt);
1465 ctxt.output = output;
1467 xmlCtxtDumpNode(&ctxt, node);
1468 xmlCtxtDumpCleanCtxt(&ctxt);
1472 * xmlDebugDumpNodeList:
1473 * @output: the FILE * for the output
1474 * @node: the node list
1475 * @depth: the indentation level.
1477 * Dumps debug information for the list of element node, it is recursive
1480 xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1486 xmlCtxtDumpInitCtxt(&ctxt);
1487 ctxt.output = output;
1489 xmlCtxtDumpNodeList(&ctxt, node);
1490 xmlCtxtDumpCleanCtxt(&ctxt);
1494 * xmlDebugDumpDocumentHead:
1495 * @output: the FILE * for the output
1496 * @doc: the document
1498 * Dumps debug information cncerning the document, not recursive
1501 xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1507 xmlCtxtDumpInitCtxt(&ctxt);
1508 ctxt.options |= DUMP_TEXT_TYPE;
1509 ctxt.output = output;
1510 xmlCtxtDumpDocumentHead(&ctxt, doc);
1511 xmlCtxtDumpCleanCtxt(&ctxt);
1515 * xmlDebugDumpDocument:
1516 * @output: the FILE * for the output
1517 * @doc: the document
1519 * Dumps debug information for the document, it's recursive
1522 xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1528 xmlCtxtDumpInitCtxt(&ctxt);
1529 ctxt.options |= DUMP_TEXT_TYPE;
1530 ctxt.output = output;
1531 xmlCtxtDumpDocument(&ctxt, doc);
1532 xmlCtxtDumpCleanCtxt(&ctxt);
1537 * @output: the FILE * for the output
1540 * Dumps debug information for the DTD
1543 xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1549 xmlCtxtDumpInitCtxt(&ctxt);
1550 ctxt.options |= DUMP_TEXT_TYPE;
1551 ctxt.output = output;
1552 xmlCtxtDumpDTD(&ctxt, dtd);
1553 xmlCtxtDumpCleanCtxt(&ctxt);
1556 /************************************************************************
1558 * Public entry points for checkings *
1560 ************************************************************************/
1563 * xmlDebugCheckDocument:
1564 * @output: the FILE * for the output
1565 * @doc: the document
1567 * Check the document for potential content problems, and output
1568 * the errors to @output
1570 * Returns the number of errors found
1573 xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1579 xmlCtxtDumpInitCtxt(&ctxt);
1580 ctxt.output = output;
1582 xmlCtxtDumpDocument(&ctxt, doc);
1583 xmlCtxtDumpCleanCtxt(&ctxt);
1584 return(ctxt.errors);
1587 /************************************************************************
1589 * Helpers for Shell *
1591 ************************************************************************/
1595 * @node: the node to count
1597 * Count the children of @node.
1599 * Returns the number of children of @node.
1602 xmlLsCountNode(xmlNodePtr node) {
1604 xmlNodePtr list = NULL;
1609 switch (node->type) {
1610 case XML_ELEMENT_NODE:
1611 list = node->children;
1613 case XML_DOCUMENT_NODE:
1614 case XML_HTML_DOCUMENT_NODE:
1615 #ifdef LIBXML_DOCB_ENABLED
1616 case XML_DOCB_DOCUMENT_NODE:
1618 list = ((xmlDocPtr) node)->children;
1620 case XML_ATTRIBUTE_NODE:
1621 list = ((xmlAttrPtr) node)->children;
1624 case XML_CDATA_SECTION_NODE:
1626 case XML_COMMENT_NODE:
1627 if (node->content != NULL) {
1628 ret = xmlStrlen(node->content);
1631 case XML_ENTITY_REF_NODE:
1632 case XML_DOCUMENT_TYPE_NODE:
1633 case XML_ENTITY_NODE:
1634 case XML_DOCUMENT_FRAG_NODE:
1635 case XML_NOTATION_NODE:
1637 case XML_ELEMENT_DECL:
1638 case XML_ATTRIBUTE_DECL:
1639 case XML_ENTITY_DECL:
1640 case XML_NAMESPACE_DECL:
1641 case XML_XINCLUDE_START:
1642 case XML_XINCLUDE_END:
1646 for (;list != NULL;ret++)
1653 * @output: the FILE * for the output
1654 * @node: the node to dump
1656 * Dump to @output the type and name of @node.
1659 xmlLsOneNode(FILE *output, xmlNodePtr node) {
1660 if (output == NULL) return;
1662 fprintf(output, "NULL\n");
1665 switch (node->type) {
1666 case XML_ELEMENT_NODE:
1667 fprintf(output, "-");
1669 case XML_ATTRIBUTE_NODE:
1670 fprintf(output, "a");
1673 fprintf(output, "t");
1675 case XML_CDATA_SECTION_NODE:
1676 fprintf(output, "C");
1678 case XML_ENTITY_REF_NODE:
1679 fprintf(output, "e");
1681 case XML_ENTITY_NODE:
1682 fprintf(output, "E");
1685 fprintf(output, "p");
1687 case XML_COMMENT_NODE:
1688 fprintf(output, "c");
1690 case XML_DOCUMENT_NODE:
1691 fprintf(output, "d");
1693 case XML_HTML_DOCUMENT_NODE:
1694 fprintf(output, "h");
1696 case XML_DOCUMENT_TYPE_NODE:
1697 fprintf(output, "T");
1699 case XML_DOCUMENT_FRAG_NODE:
1700 fprintf(output, "F");
1702 case XML_NOTATION_NODE:
1703 fprintf(output, "N");
1705 case XML_NAMESPACE_DECL:
1706 fprintf(output, "n");
1709 fprintf(output, "?");
1711 if (node->type != XML_NAMESPACE_DECL) {
1712 if (node->properties != NULL)
1713 fprintf(output, "a");
1715 fprintf(output, "-");
1716 if (node->nsDef != NULL)
1717 fprintf(output, "n");
1719 fprintf(output, "-");
1722 fprintf(output, " %8d ", xmlLsCountNode(node));
1724 switch (node->type) {
1725 case XML_ELEMENT_NODE:
1726 if (node->name != NULL) {
1727 if ((node->ns != NULL) && (node->ns->prefix != NULL))
1728 fprintf(output, "%s:", node->ns->prefix);
1729 fprintf(output, "%s", (const char *) node->name);
1732 case XML_ATTRIBUTE_NODE:
1733 if (node->name != NULL)
1734 fprintf(output, "%s", (const char *) node->name);
1737 if (node->content != NULL) {
1738 xmlDebugDumpString(output, node->content);
1741 case XML_CDATA_SECTION_NODE:
1743 case XML_ENTITY_REF_NODE:
1744 if (node->name != NULL)
1745 fprintf(output, "%s", (const char *) node->name);
1747 case XML_ENTITY_NODE:
1748 if (node->name != NULL)
1749 fprintf(output, "%s", (const char *) node->name);
1752 if (node->name != NULL)
1753 fprintf(output, "%s", (const char *) node->name);
1755 case XML_COMMENT_NODE:
1757 case XML_DOCUMENT_NODE:
1759 case XML_HTML_DOCUMENT_NODE:
1761 case XML_DOCUMENT_TYPE_NODE:
1763 case XML_DOCUMENT_FRAG_NODE:
1765 case XML_NOTATION_NODE:
1767 case XML_NAMESPACE_DECL: {
1768 xmlNsPtr ns = (xmlNsPtr) node;
1770 if (ns->prefix == NULL)
1771 fprintf(output, "default -> %s", (char *)ns->href);
1773 fprintf(output, "%s -> %s", (char *)ns->prefix,
1778 if (node->name != NULL)
1779 fprintf(output, "%s", (const char *) node->name);
1781 fprintf(output, "\n");
1786 * @boolval: a bool to turn into text
1788 * Convenient way to turn bool into text
1790 * Returns a pointer to either "True" or "False"
1793 xmlBoolToText(int boolval)
1801 #ifdef LIBXML_XPATH_ENABLED
1802 /****************************************************************
1804 * The XML shell related functions *
1806 ****************************************************************/
1811 * TODO: Improvement/cleanups for the XML shell
1812 * - allow to shell out an editor on a subpart
1813 * - cleanup function registrations (with help) and calling
1814 * - provide registration routines
1818 * xmlShellPrintXPathError:
1819 * @errorType: valid xpath error id
1820 * @arg: the argument that cause xpath to fail
1822 * Print the xpath error to libxml default error channel
1825 xmlShellPrintXPathError(int errorType, const char *arg)
1827 const char *default_arg = "Result";
1832 switch (errorType) {
1833 case XPATH_UNDEFINED:
1834 xmlGenericError(xmlGenericErrorContext,
1835 "%s: no such node\n", arg);
1839 xmlGenericError(xmlGenericErrorContext,
1840 "%s is a Boolean\n", arg);
1843 xmlGenericError(xmlGenericErrorContext,
1844 "%s is a number\n", arg);
1847 xmlGenericError(xmlGenericErrorContext,
1848 "%s is a string\n", arg);
1851 xmlGenericError(xmlGenericErrorContext,
1852 "%s is a point\n", arg);
1855 xmlGenericError(xmlGenericErrorContext,
1856 "%s is a range\n", arg);
1858 case XPATH_LOCATIONSET:
1859 xmlGenericError(xmlGenericErrorContext,
1860 "%s is a range\n", arg);
1863 xmlGenericError(xmlGenericErrorContext,
1864 "%s is user-defined\n", arg);
1866 case XPATH_XSLT_TREE:
1867 xmlGenericError(xmlGenericErrorContext,
1868 "%s is an XSLT value tree\n", arg);
1872 xmlGenericError(xmlGenericErrorContext,
1873 "Try casting the result string function (xpath builtin)\n",
1879 #ifdef LIBXML_OUTPUT_ENABLED
1881 * xmlShellPrintNodeCtxt:
1882 * @ctxt : a non-null shell context
1883 * @node : a non-null node to print to the output FILE
1885 * Print node to the output FILE
1888 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1899 if (node->type == XML_DOCUMENT_NODE)
1900 xmlDocDump(fp, (xmlDocPtr) node);
1901 else if (node->type == XML_ATTRIBUTE_NODE)
1902 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
1904 xmlElemDump(fp, node->doc, node);
1910 * xmlShellPrintNode:
1911 * @node : a non-null node to print to the output FILE
1913 * Print node to the output FILE
1916 xmlShellPrintNode(xmlNodePtr node)
1918 xmlShellPrintNodeCtxt(NULL, node);
1920 #endif /* LIBXML_OUTPUT_ENABLED */
1923 * xmlShellPrintXPathResultCtxt:
1924 * @ctxt: a valid shell context
1925 * @list: a valid result generated by an xpath evaluation
1927 * Prints result to the output FILE
1930 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
1936 switch (list->type) {
1937 case XPATH_NODESET:{
1938 #ifdef LIBXML_OUTPUT_ENABLED
1941 if (list->nodesetval) {
1942 for (indx = 0; indx < list->nodesetval->nodeNr;
1944 xmlShellPrintNodeCtxt(ctxt,
1945 list->nodesetval->nodeTab[indx]);
1948 xmlGenericError(xmlGenericErrorContext,
1949 "Empty node set\n");
1953 xmlGenericError(xmlGenericErrorContext,
1955 #endif /* LIBXML_OUTPUT_ENABLED */
1958 xmlGenericError(xmlGenericErrorContext,
1959 "Is a Boolean:%s\n",
1960 xmlBoolToText(list->boolval));
1963 xmlGenericError(xmlGenericErrorContext,
1964 "Is a number:%0g\n", list->floatval);
1967 xmlGenericError(xmlGenericErrorContext,
1968 "Is a string:%s\n", list->stringval);
1972 xmlShellPrintXPathError(list->type, NULL);
1978 * xmlShellPrintXPathResult:
1979 * @list: a valid result generated by an xpath evaluation
1981 * Prints result to the output FILE
1984 xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1986 xmlShellPrintXPathResultCtxt(NULL, list);
1991 * @ctxt: the shell context
1996 * Implements the XML shell function "ls"
1997 * Does an Unix like listing of the given node (like a directory)
2002 xmlShellList(xmlShellCtxtPtr ctxt,
2003 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2004 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2010 fprintf(ctxt->output, "NULL\n");
2013 if ((node->type == XML_DOCUMENT_NODE) ||
2014 (node->type == XML_HTML_DOCUMENT_NODE)) {
2015 cur = ((xmlDocPtr) node)->children;
2016 } else if (node->type == XML_NAMESPACE_DECL) {
2017 xmlLsOneNode(ctxt->output, node);
2019 } else if (node->children != NULL) {
2020 cur = node->children;
2022 xmlLsOneNode(ctxt->output, node);
2025 while (cur != NULL) {
2026 xmlLsOneNode(ctxt->output, cur);
2034 * @ctxt: the shell context
2039 * Implements the XML shell function "base"
2040 * dumps the current XML base of the node
2045 xmlShellBase(xmlShellCtxtPtr ctxt,
2046 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2047 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2053 fprintf(ctxt->output, "NULL\n");
2057 base = xmlNodeGetBase(node->doc, node);
2060 fprintf(ctxt->output, " No base found !!!\n");
2062 fprintf(ctxt->output, "%s\n", base);
2068 #ifdef LIBXML_TREE_ENABLED
2071 * @ctxt: the shell context
2072 * @arg: the new base
2076 * Implements the XML shell function "setbase"
2077 * change the current XML base of the node
2082 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2083 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2084 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2086 xmlNodeSetBase(node, (xmlChar*) arg);
2091 #ifdef LIBXML_XPATH_ENABLED
2093 * xmlShellRegisterNamespace:
2094 * @ctxt: the shell context
2095 * @arg: a string in prefix=nsuri format
2099 * Implements the XML shell function "setns"
2100 * register/unregister a prefix=namespace pair
2101 * on the XPath context
2103 * Returns 0 on success and a negative value otherwise.
2106 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2107 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2114 nsListDup = xmlStrdup((xmlChar *) arg);
2116 while(next != NULL) {
2118 /*while((*next) == ' ') next++;*/
2119 if((*next) == '\0') break;
2123 next = (xmlChar*)xmlStrchr(next, '=');
2125 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2133 next = (xmlChar*)xmlStrchr(next, ' ');
2138 /* do register namespace */
2139 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2140 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2150 * xmlShellRegisterRootNamespaces:
2151 * @ctxt: the shell context
2153 * @node: the root element
2156 * Implements the XML shell function "setrootns"
2157 * which registers all namespaces declarations found on the root element.
2159 * Returns 0 on success and a negative value otherwise.
2162 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2163 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2167 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2168 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2171 while (ns != NULL) {
2172 if (ns->prefix == NULL)
2173 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2175 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2184 * @ctxt: the shell context
2185 * @arg: the string or regular expression to find
2189 * Implements the XML shell function "grep"
2190 * dumps informations about the node (namespace, attributes, content).
2195 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2196 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2204 #ifdef LIBXML_REGEXP_ENABLED
2205 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2206 (xmlStrchr((xmlChar *) arg, '*')) ||
2207 (xmlStrchr((xmlChar *) arg, '.')) ||
2208 (xmlStrchr((xmlChar *) arg, '['))) {
2211 while (node != NULL) {
2212 if (node->type == XML_COMMENT_NODE) {
2213 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2215 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2216 xmlShellList(ctxt, NULL, node, NULL);
2218 } else if (node->type == XML_TEXT_NODE) {
2219 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2221 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
2222 xmlShellList(ctxt, NULL, node->parent, NULL);
2227 * Browse the full subtree, deep first
2230 if ((node->type == XML_DOCUMENT_NODE) ||
2231 (node->type == XML_HTML_DOCUMENT_NODE)) {
2232 node = ((xmlDocPtr) node)->children;
2233 } else if ((node->children != NULL)
2234 && (node->type != XML_ENTITY_REF_NODE)) {
2236 node = node->children;
2237 } else if (node->next != NULL) {
2241 /* go up to parents->next if needed */
2242 while (node != NULL) {
2243 if (node->parent != NULL) {
2244 node = node->parent;
2246 if (node->next != NULL) {
2250 if (node->parent == NULL) {
2262 * @ctxt: the shell context
2267 * Implements the XML shell function "dir"
2268 * dumps informations about the node (namespace, attributes, content).
2273 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2274 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2275 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2280 fprintf(ctxt->output, "NULL\n");
2283 if ((node->type == XML_DOCUMENT_NODE) ||
2284 (node->type == XML_HTML_DOCUMENT_NODE)) {
2285 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
2286 } else if (node->type == XML_ATTRIBUTE_NODE) {
2287 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
2289 xmlDebugDumpOneNode(ctxt->output, node, 0);
2295 * xmlShellSetContent:
2296 * @ctxt: the shell context
2297 * @value: the content as a string
2301 * Implements the XML shell function "dir"
2302 * dumps informations about the node (namespace, attributes, content).
2307 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2308 char *value, xmlNodePtr node,
2309 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2312 xmlParserErrors ret;
2317 fprintf(ctxt->output, "NULL\n");
2320 if (value == NULL) {
2321 fprintf(ctxt->output, "NULL\n");
2325 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2326 if (ret == XML_ERR_OK) {
2327 if (node->children != NULL) {
2328 xmlFreeNodeList(node->children);
2329 node->children = NULL;
2332 xmlAddChildList(node, results);
2334 fprintf(ctxt->output, "failed to parse content\n");
2339 #ifdef LIBXML_SCHEMAS_ENABLED
2341 * xmlShellRNGValidate:
2342 * @ctxt: the shell context
2343 * @schemas: the path to the Relax-NG schemas
2347 * Implements the XML shell function "relaxng"
2348 * validating the instance against a Relax-NG schemas
2353 xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2354 xmlNodePtr node ATTRIBUTE_UNUSED,
2355 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2357 xmlRelaxNGPtr relaxngschemas;
2358 xmlRelaxNGParserCtxtPtr ctxt;
2359 xmlRelaxNGValidCtxtPtr vctxt;
2362 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2363 xmlRelaxNGSetParserErrors(ctxt,
2364 (xmlRelaxNGValidityErrorFunc) fprintf,
2365 (xmlRelaxNGValidityWarningFunc) fprintf,
2367 relaxngschemas = xmlRelaxNGParse(ctxt);
2368 xmlRelaxNGFreeParserCtxt(ctxt);
2369 if (relaxngschemas == NULL) {
2370 xmlGenericError(xmlGenericErrorContext,
2371 "Relax-NG schema %s failed to compile\n", schemas);
2374 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2375 xmlRelaxNGSetValidErrors(vctxt,
2376 (xmlRelaxNGValidityErrorFunc) fprintf,
2377 (xmlRelaxNGValidityWarningFunc) fprintf,
2379 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2381 fprintf(stderr, "%s validates\n", sctxt->filename);
2382 } else if (ret > 0) {
2383 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2385 fprintf(stderr, "%s validation generated an internal error\n",
2388 xmlRelaxNGFreeValidCtxt(vctxt);
2389 if (relaxngschemas != NULL)
2390 xmlRelaxNGFree(relaxngschemas);
2395 #ifdef LIBXML_OUTPUT_ENABLED
2398 * @ctxt: the shell context
2403 * Implements the XML shell function "cat"
2404 * dumps the serialization node content (XML or HTML).
2409 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2410 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2415 fprintf(ctxt->output, "NULL\n");
2418 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2419 #ifdef LIBXML_HTML_ENABLED
2420 if (node->type == XML_HTML_DOCUMENT_NODE)
2421 htmlDocDump(ctxt->output, (htmlDocPtr) node);
2423 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
2425 if (node->type == XML_DOCUMENT_NODE)
2426 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2428 xmlElemDump(ctxt->output, ctxt->doc, node);
2429 #endif /* LIBXML_HTML_ENABLED */
2431 if (node->type == XML_DOCUMENT_NODE)
2432 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2434 xmlElemDump(ctxt->output, ctxt->doc, node);
2436 fprintf(ctxt->output, "\n");
2439 #endif /* LIBXML_OUTPUT_ENABLED */
2443 * @ctxt: the shell context
2444 * @filename: the file name
2448 * Implements the XML shell function "load"
2449 * loads a new document specified by the filename
2451 * Returns 0 or -1 if loading failed
2454 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2455 xmlNodePtr node ATTRIBUTE_UNUSED,
2456 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2461 if ((ctxt == NULL) || (filename == NULL)) return(-1);
2462 if (ctxt->doc != NULL)
2463 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2466 #ifdef LIBXML_HTML_ENABLED
2467 doc = htmlParseFile(filename, NULL);
2469 fprintf(ctxt->output, "HTML support not compiled in\n");
2471 #endif /* LIBXML_HTML_ENABLED */
2473 doc = xmlReadFile(filename,NULL,0);
2476 if (ctxt->loaded == 1) {
2477 xmlFreeDoc(ctxt->doc);
2480 #ifdef LIBXML_XPATH_ENABLED
2481 xmlXPathFreeContext(ctxt->pctxt);
2482 #endif /* LIBXML_XPATH_ENABLED */
2483 xmlFree(ctxt->filename);
2485 ctxt->node = (xmlNodePtr) doc;
2486 #ifdef LIBXML_XPATH_ENABLED
2487 ctxt->pctxt = xmlXPathNewContext(doc);
2488 #endif /* LIBXML_XPATH_ENABLED */
2489 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2495 #ifdef LIBXML_OUTPUT_ENABLED
2498 * @ctxt: the shell context
2499 * @filename: the file name
2500 * @node: a node in the tree
2503 * Implements the XML shell function "write"
2504 * Write the current node to the filename, it saves the serialization
2505 * of the subtree under the @node specified
2507 * Returns 0 or -1 in case of error
2510 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2511 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2515 if ((filename == NULL) || (filename[0] == 0)) {
2519 if (access((char *) filename, W_OK)) {
2520 xmlGenericError(xmlGenericErrorContext,
2521 "Cannot write to %s\n", filename);
2525 switch (node->type) {
2526 case XML_DOCUMENT_NODE:
2527 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2528 xmlGenericError(xmlGenericErrorContext,
2529 "Failed to write to %s\n", filename);
2533 case XML_HTML_DOCUMENT_NODE:
2534 #ifdef LIBXML_HTML_ENABLED
2535 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2536 xmlGenericError(xmlGenericErrorContext,
2537 "Failed to write to %s\n", filename);
2541 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2542 xmlGenericError(xmlGenericErrorContext,
2543 "Failed to write to %s\n", filename);
2546 #endif /* LIBXML_HTML_ENABLED */
2551 f = fopen((char *) filename, "w");
2553 xmlGenericError(xmlGenericErrorContext,
2554 "Failed to write to %s\n", filename);
2557 xmlElemDump(f, ctxt->doc, node);
2566 * @ctxt: the shell context
2567 * @filename: the file name (optional)
2571 * Implements the XML shell function "save"
2572 * Write the current document to the filename, or it's original name
2574 * Returns 0 or -1 in case of error
2577 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2578 xmlNodePtr node ATTRIBUTE_UNUSED,
2579 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2581 if ((ctxt == NULL) || (ctxt->doc == NULL))
2583 if ((filename == NULL) || (filename[0] == 0))
2584 filename = ctxt->filename;
2585 if (filename == NULL)
2588 if (access((char *) filename, W_OK)) {
2589 xmlGenericError(xmlGenericErrorContext,
2590 "Cannot save to %s\n", filename);
2594 switch (ctxt->doc->type) {
2595 case XML_DOCUMENT_NODE:
2596 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2597 xmlGenericError(xmlGenericErrorContext,
2598 "Failed to save to %s\n", filename);
2601 case XML_HTML_DOCUMENT_NODE:
2602 #ifdef LIBXML_HTML_ENABLED
2603 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2604 xmlGenericError(xmlGenericErrorContext,
2605 "Failed to save to %s\n", filename);
2608 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2609 xmlGenericError(xmlGenericErrorContext,
2610 "Failed to save to %s\n", filename);
2612 #endif /* LIBXML_HTML_ENABLED */
2615 xmlGenericError(xmlGenericErrorContext,
2616 "To save to subparts of a document use the 'write' command\n");
2622 #endif /* LIBXML_OUTPUT_ENABLED */
2624 #ifdef LIBXML_VALID_ENABLED
2627 * @ctxt: the shell context
2628 * @dtd: the DTD URI (optional)
2632 * Implements the XML shell function "validate"
2633 * Validate the document, if a DTD path is provided, then the validation
2634 * is done against the given DTD.
2636 * Returns 0 or -1 in case of error
2639 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2640 xmlNodePtr node ATTRIBUTE_UNUSED,
2641 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2646 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2647 vctxt.userData = stderr;
2648 vctxt.error = (xmlValidityErrorFunc) fprintf;
2649 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2651 if ((dtd == NULL) || (dtd[0] == 0)) {
2652 res = xmlValidateDocument(&vctxt, ctxt->doc);
2656 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2657 if (subset != NULL) {
2658 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2665 #endif /* LIBXML_VALID_ENABLED */
2669 * @ctxt: the shell context
2671 * @tree: a node defining a subtree
2674 * Implements the XML shell function "du"
2675 * show the structure of the subtree under node @tree
2676 * If @tree is null, the command works on the current node.
2678 * Returns 0 or -1 in case of error
2681 xmlShellDu(xmlShellCtxtPtr ctxt,
2682 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2683 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2694 while (node != NULL) {
2695 if ((node->type == XML_DOCUMENT_NODE) ||
2696 (node->type == XML_HTML_DOCUMENT_NODE)) {
2697 fprintf(ctxt->output, "/\n");
2698 } else if (node->type == XML_ELEMENT_NODE) {
2699 for (i = 0; i < indent; i++)
2700 fprintf(ctxt->output, " ");
2701 if ((node->ns) && (node->ns->prefix))
2702 fprintf(ctxt->output, "%s:", node->ns->prefix);
2703 fprintf(ctxt->output, "%s\n", node->name);
2708 * Browse the full subtree, deep first
2711 if ((node->type == XML_DOCUMENT_NODE) ||
2712 (node->type == XML_HTML_DOCUMENT_NODE)) {
2713 node = ((xmlDocPtr) node)->children;
2714 } else if ((node->children != NULL)
2715 && (node->type != XML_ENTITY_REF_NODE)) {
2717 node = node->children;
2719 } else if ((node != tree) && (node->next != NULL)) {
2722 } else if (node != tree) {
2723 /* go up to parents->next if needed */
2724 while (node != tree) {
2725 if (node->parent != NULL) {
2726 node = node->parent;
2729 if ((node != tree) && (node->next != NULL)) {
2733 if (node->parent == NULL) {
2742 /* exit condition */
2753 * @ctxt: the shell context
2754 * @buffer: the output buffer
2758 * Implements the XML shell function "pwd"
2759 * Show the full path from the root to the node, if needed building
2760 * thumblers when similar elements exists at a given ancestor level.
2761 * The output is compatible with XPath commands.
2763 * Returns 0 or -1 in case of error
2766 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2767 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2771 if ((node == NULL) || (buffer == NULL))
2774 path = xmlGetNodePath(node);
2779 * This test prevents buffer overflow, because this routine
2780 * is only called by xmlShell, in which the second argument is
2782 * It is a dirty hack before a cleaner solution is found.
2783 * Documentation should mention that the second argument must
2784 * be at least 500 chars long, and could be stripped if too long.
2786 snprintf(buffer, 499, "%s", path);
2795 * @doc: the initial document
2796 * @filename: the output buffer
2797 * @input: the line reading function
2798 * @output: the output FILE*, defaults to stdout if NULL
2800 * Implements the XML shell
2801 * This allow to load, validate, view, modify and save a document
2802 * using a environment similar to a UNIX commandline.
2805 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2808 char prompt[500] = "/ > ";
2809 char *cmdline = NULL, *cur;
2813 xmlShellCtxtPtr ctxt;
2814 xmlXPathObjectPtr list;
2818 if (filename == NULL)
2824 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2829 ctxt->input = input;
2830 ctxt->output = output;
2831 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2832 ctxt->node = (xmlNodePtr) ctxt->doc;
2834 #ifdef LIBXML_XPATH_ENABLED
2835 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2836 if (ctxt->pctxt == NULL) {
2840 #endif /* LIBXML_XPATH_ENABLED */
2842 if (ctxt->node == (xmlNodePtr) ctxt->doc)
2843 snprintf(prompt, sizeof(prompt), "%s > ", "/");
2844 else if ((ctxt->node != NULL) && (ctxt->node->name) &&
2845 (ctxt->node->ns) && (ctxt->node->ns->prefix))
2846 snprintf(prompt, sizeof(prompt), "%s:%s > ",
2847 (ctxt->node->ns->prefix), ctxt->node->name);
2848 else if ((ctxt->node != NULL) && (ctxt->node->name))
2849 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
2851 snprintf(prompt, sizeof(prompt), "? > ");
2852 prompt[sizeof(prompt) - 1] = 0;
2855 * Get a new command line
2857 cmdline = ctxt->input(prompt);
2858 if (cmdline == NULL)
2862 * Parse the command itself
2865 while ((*cur == ' ') || (*cur == '\t'))
2868 while ((*cur != ' ') && (*cur != '\t') &&
2869 (*cur != '\n') && (*cur != '\r')) {
2872 command[i++] = *cur++;
2879 * Parse the argument
2881 while ((*cur == ' ') || (*cur == '\t'))
2884 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2892 * start interpreting the command
2894 if (!strcmp(command, "exit"))
2896 if (!strcmp(command, "quit"))
2898 if (!strcmp(command, "bye"))
2900 if (!strcmp(command, "help")) {
2901 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2902 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2903 fprintf(ctxt->output, "\tbye leave shell\n");
2904 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2905 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2906 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2907 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2908 fprintf(ctxt->output, "\texit leave shell\n");
2909 fprintf(ctxt->output, "\thelp display this help\n");
2910 fprintf(ctxt->output, "\tfree display memory usage\n");
2911 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2912 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
2913 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2914 #ifdef LIBXML_XPATH_ENABLED
2915 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2916 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2917 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2918 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2919 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
2920 #endif /* LIBXML_XPATH_ENABLED */
2921 fprintf(ctxt->output, "\tpwd display current working directory\n");
2922 fprintf(ctxt->output, "\twhereis display absolute path of [path] or current working directory\n");
2923 fprintf(ctxt->output, "\tquit leave shell\n");
2924 #ifdef LIBXML_OUTPUT_ENABLED
2925 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
2926 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2927 #endif /* LIBXML_OUTPUT_ENABLED */
2928 #ifdef LIBXML_VALID_ENABLED
2929 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
2930 #endif /* LIBXML_VALID_ENABLED */
2931 #ifdef LIBXML_SCHEMAS_ENABLED
2932 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2934 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
2935 #ifdef LIBXML_VALID_ENABLED
2936 } else if (!strcmp(command, "validate")) {
2937 xmlShellValidate(ctxt, arg, NULL, NULL);
2938 #endif /* LIBXML_VALID_ENABLED */
2939 } else if (!strcmp(command, "load")) {
2940 xmlShellLoad(ctxt, arg, NULL, NULL);
2941 #ifdef LIBXML_SCHEMAS_ENABLED
2942 } else if (!strcmp(command, "relaxng")) {
2943 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2945 #ifdef LIBXML_OUTPUT_ENABLED
2946 } else if (!strcmp(command, "save")) {
2947 xmlShellSave(ctxt, arg, NULL, NULL);
2948 } else if (!strcmp(command, "write")) {
2949 if ((arg == NULL) || (arg[0] == 0))
2950 xmlGenericError(xmlGenericErrorContext,
2951 "Write command requires a filename argument\n");
2953 xmlShellWrite(ctxt, arg, ctxt->node, NULL);
2954 #endif /* LIBXML_OUTPUT_ENABLED */
2955 } else if (!strcmp(command, "grep")) {
2956 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2957 } else if (!strcmp(command, "free")) {
2959 xmlMemShow(ctxt->output, 0);
2963 sscanf(arg, "%d", &len);
2964 xmlMemShow(ctxt->output, len);
2966 } else if (!strcmp(command, "pwd")) {
2969 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2970 fprintf(ctxt->output, "%s\n", dir);
2971 } else if (!strcmp(command, "du")) {
2973 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2975 ctxt->pctxt->node = ctxt->node;
2976 #ifdef LIBXML_XPATH_ENABLED
2977 ctxt->pctxt->node = ctxt->node;
2978 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2981 #endif /* LIBXML_XPATH_ENABLED */
2983 switch (list->type) {
2984 case XPATH_UNDEFINED:
2985 xmlGenericError(xmlGenericErrorContext,
2986 "%s: no such node\n", arg);
2988 case XPATH_NODESET:{
2991 if (list->nodesetval == NULL)
2995 indx < list->nodesetval->nodeNr;
2997 xmlShellDu(ctxt, NULL,
2999 nodeTab[indx], NULL);
3003 xmlGenericError(xmlGenericErrorContext,
3004 "%s is a Boolean\n", arg);
3007 xmlGenericError(xmlGenericErrorContext,
3008 "%s is a number\n", arg);
3011 xmlGenericError(xmlGenericErrorContext,
3012 "%s is a string\n", arg);
3015 xmlGenericError(xmlGenericErrorContext,
3016 "%s is a point\n", arg);
3019 xmlGenericError(xmlGenericErrorContext,
3020 "%s is a range\n", arg);
3022 case XPATH_LOCATIONSET:
3023 xmlGenericError(xmlGenericErrorContext,
3024 "%s is a range\n", arg);
3027 xmlGenericError(xmlGenericErrorContext,
3028 "%s is user-defined\n", arg);
3030 case XPATH_XSLT_TREE:
3031 xmlGenericError(xmlGenericErrorContext,
3032 "%s is an XSLT value tree\n",
3036 #ifdef LIBXML_XPATH_ENABLED
3037 xmlXPathFreeObject(list);
3040 xmlGenericError(xmlGenericErrorContext,
3041 "%s: no such node\n", arg);
3043 ctxt->pctxt->node = NULL;
3045 } else if (!strcmp(command, "base")) {
3046 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
3047 } else if (!strcmp(command, "set")) {
3048 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
3049 #ifdef LIBXML_XPATH_ENABLED
3050 } else if (!strcmp(command, "setns")) {
3052 xmlGenericError(xmlGenericErrorContext,
3053 "setns: prefix=[nsuri] required\n");
3055 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
3057 } else if (!strcmp(command, "setrootns")) {
3060 root = xmlDocGetRootElement(ctxt->doc);
3061 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
3062 } else if (!strcmp(command, "xpath")) {
3064 xmlGenericError(xmlGenericErrorContext,
3065 "xpath: expression required\n");
3067 ctxt->pctxt->node = ctxt->node;
3068 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3069 xmlXPathDebugDumpObject(ctxt->output, list, 0);
3070 xmlXPathFreeObject(list);
3072 #endif /* LIBXML_XPATH_ENABLED */
3073 #ifdef LIBXML_TREE_ENABLED
3074 } else if (!strcmp(command, "setbase")) {
3075 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
3077 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3078 int dir = (!strcmp(command, "dir"));
3082 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3084 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3086 ctxt->pctxt->node = ctxt->node;
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);
3099 case XPATH_NODESET:{
3102 if (list->nodesetval == NULL)
3106 indx < list->nodesetval->nodeNr;
3109 xmlShellDir(ctxt, NULL,
3111 nodeTab[indx], NULL);
3113 xmlShellList(ctxt, NULL,
3115 nodeTab[indx], NULL);
3120 xmlGenericError(xmlGenericErrorContext,
3121 "%s is a Boolean\n", arg);
3124 xmlGenericError(xmlGenericErrorContext,
3125 "%s is a number\n", arg);
3128 xmlGenericError(xmlGenericErrorContext,
3129 "%s is a string\n", arg);
3132 xmlGenericError(xmlGenericErrorContext,
3133 "%s is a point\n", arg);
3136 xmlGenericError(xmlGenericErrorContext,
3137 "%s is a range\n", arg);
3139 case XPATH_LOCATIONSET:
3140 xmlGenericError(xmlGenericErrorContext,
3141 "%s is a range\n", arg);
3144 xmlGenericError(xmlGenericErrorContext,
3145 "%s is user-defined\n", arg);
3147 case XPATH_XSLT_TREE:
3148 xmlGenericError(xmlGenericErrorContext,
3149 "%s is an XSLT value tree\n",
3153 #ifdef LIBXML_XPATH_ENABLED
3154 xmlXPathFreeObject(list);
3157 xmlGenericError(xmlGenericErrorContext,
3158 "%s: no such node\n", arg);
3160 ctxt->pctxt->node = NULL;
3162 } else if (!strcmp(command, "whereis")) {
3166 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
3167 fprintf(ctxt->output, "%s\n", dir);
3169 ctxt->pctxt->node = ctxt->node;
3170 #ifdef LIBXML_XPATH_ENABLED
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;
3190 if (!xmlShellPwd(ctxt, dir, list->nodesetval->
3191 nodeTab[indx], NULL))
3192 fprintf(ctxt->output, "%s\n", dir);
3197 xmlGenericError(xmlGenericErrorContext,
3198 "%s is a Boolean\n", arg);
3201 xmlGenericError(xmlGenericErrorContext,
3202 "%s is a number\n", arg);
3205 xmlGenericError(xmlGenericErrorContext,
3206 "%s is a string\n", arg);
3209 xmlGenericError(xmlGenericErrorContext,
3210 "%s is a point\n", arg);
3213 xmlGenericError(xmlGenericErrorContext,
3214 "%s is a range\n", arg);
3216 case XPATH_LOCATIONSET:
3217 xmlGenericError(xmlGenericErrorContext,
3218 "%s is a range\n", arg);
3221 xmlGenericError(xmlGenericErrorContext,
3222 "%s is user-defined\n", arg);
3224 case XPATH_XSLT_TREE:
3225 xmlGenericError(xmlGenericErrorContext,
3226 "%s is an XSLT value tree\n",
3230 #ifdef LIBXML_XPATH_ENABLED
3231 xmlXPathFreeObject(list);
3234 xmlGenericError(xmlGenericErrorContext,
3235 "%s: no such node\n", arg);
3237 ctxt->pctxt->node = NULL;
3239 } else if (!strcmp(command, "cd")) {
3241 ctxt->node = (xmlNodePtr) ctxt->doc;
3243 #ifdef LIBXML_XPATH_ENABLED
3244 ctxt->pctxt->node = ctxt->node;
3245 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3248 #endif /* LIBXML_XPATH_ENABLED */
3250 switch (list->type) {
3251 case XPATH_UNDEFINED:
3252 xmlGenericError(xmlGenericErrorContext,
3253 "%s: no such node\n", arg);
3256 if (list->nodesetval != NULL) {
3257 if (list->nodesetval->nodeNr == 1) {
3258 ctxt->node = list->nodesetval->nodeTab[0];
3259 if ((ctxt->node != NULL) &&
3260 (ctxt->node->type ==
3261 XML_NAMESPACE_DECL)) {
3262 xmlGenericError(xmlGenericErrorContext,
3263 "cannot cd to namespace\n");
3267 xmlGenericError(xmlGenericErrorContext,
3268 "%s is a %d Node Set\n",
3270 list->nodesetval->nodeNr);
3272 xmlGenericError(xmlGenericErrorContext,
3273 "%s is an empty Node Set\n",
3277 xmlGenericError(xmlGenericErrorContext,
3278 "%s is a Boolean\n", arg);
3281 xmlGenericError(xmlGenericErrorContext,
3282 "%s is a number\n", arg);
3285 xmlGenericError(xmlGenericErrorContext,
3286 "%s is a string\n", arg);
3289 xmlGenericError(xmlGenericErrorContext,
3290 "%s is a point\n", arg);
3293 xmlGenericError(xmlGenericErrorContext,
3294 "%s is a range\n", arg);
3296 case XPATH_LOCATIONSET:
3297 xmlGenericError(xmlGenericErrorContext,
3298 "%s is a range\n", arg);
3301 xmlGenericError(xmlGenericErrorContext,
3302 "%s is user-defined\n", arg);
3304 case XPATH_XSLT_TREE:
3305 xmlGenericError(xmlGenericErrorContext,
3306 "%s is an XSLT value tree\n",
3310 #ifdef LIBXML_XPATH_ENABLED
3311 xmlXPathFreeObject(list);
3314 xmlGenericError(xmlGenericErrorContext,
3315 "%s: no such node\n", arg);
3317 ctxt->pctxt->node = NULL;
3319 #ifdef LIBXML_OUTPUT_ENABLED
3320 } else if (!strcmp(command, "cat")) {
3322 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3324 ctxt->pctxt->node = ctxt->node;
3325 #ifdef LIBXML_XPATH_ENABLED
3326 ctxt->pctxt->node = ctxt->node;
3327 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3330 #endif /* LIBXML_XPATH_ENABLED */
3332 switch (list->type) {
3333 case XPATH_UNDEFINED:
3334 xmlGenericError(xmlGenericErrorContext,
3335 "%s: no such node\n", arg);
3337 case XPATH_NODESET:{
3340 if (list->nodesetval == NULL)
3344 indx < list->nodesetval->nodeNr;
3347 fprintf(ctxt->output, " -------\n");
3348 xmlShellCat(ctxt, NULL,
3350 nodeTab[indx], NULL);
3355 xmlGenericError(xmlGenericErrorContext,
3356 "%s is a Boolean\n", arg);
3359 xmlGenericError(xmlGenericErrorContext,
3360 "%s is a number\n", arg);
3363 xmlGenericError(xmlGenericErrorContext,
3364 "%s is a string\n", arg);
3367 xmlGenericError(xmlGenericErrorContext,
3368 "%s is a point\n", arg);
3371 xmlGenericError(xmlGenericErrorContext,
3372 "%s is a range\n", arg);
3374 case XPATH_LOCATIONSET:
3375 xmlGenericError(xmlGenericErrorContext,
3376 "%s is a range\n", arg);
3379 xmlGenericError(xmlGenericErrorContext,
3380 "%s is user-defined\n", arg);
3382 case XPATH_XSLT_TREE:
3383 xmlGenericError(xmlGenericErrorContext,
3384 "%s is an XSLT value tree\n",
3388 #ifdef LIBXML_XPATH_ENABLED
3389 xmlXPathFreeObject(list);
3392 xmlGenericError(xmlGenericErrorContext,
3393 "%s: no such node\n", arg);
3395 ctxt->pctxt->node = NULL;
3397 #endif /* LIBXML_OUTPUT_ENABLED */
3399 xmlGenericError(xmlGenericErrorContext,
3400 "Unknown command %s\n", command);
3402 free(cmdline); /* not xmlFree here ! */
3405 #ifdef LIBXML_XPATH_ENABLED
3406 xmlXPathFreeContext(ctxt->pctxt);
3407 #endif /* LIBXML_XPATH_ENABLED */
3409 xmlFreeDoc(ctxt->doc);
3411 if (ctxt->filename != NULL)
3412 xmlFree(ctxt->filename);
3414 if (cmdline != NULL)
3415 free(cmdline); /* not xmlFree here ! */
3418 #endif /* LIBXML_XPATH_ENABLED */
3419 #define bottom_debugXML
3420 #include "elfgcchack.h"
3421 #endif /* LIBXML_DEBUG_ENABLED */