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_ELEMENT_DECL) &&
358 (node->type != XML_HTML_DOCUMENT_NODE) &&
359 (node->type != XML_DOCUMENT_NODE)) {
360 if (node->content != NULL)
361 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
363 switch (node->type) {
364 case XML_ELEMENT_NODE:
365 case XML_ATTRIBUTE_NODE:
366 xmlCtxtCheckName(ctxt, node->name);
369 if ((node->name == xmlStringText) ||
370 (node->name == xmlStringTextNoenc))
372 /* some case of entity substitution can lead to this */
373 if ((ctxt->dict != NULL) &&
374 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
378 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
379 "Text node has wrong name '%s'",
380 (const char *) node->name);
382 case XML_COMMENT_NODE:
383 if (node->name == xmlStringComment)
385 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
386 "Comment node has wrong name '%s'",
387 (const char *) node->name);
390 xmlCtxtCheckName(ctxt, node->name);
392 case XML_CDATA_SECTION_NODE:
393 if (node->name == NULL)
395 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
396 "CData section has non NULL name '%s'",
397 (const char *) node->name);
399 case XML_ENTITY_REF_NODE:
400 case XML_ENTITY_NODE:
401 case XML_DOCUMENT_TYPE_NODE:
402 case XML_DOCUMENT_FRAG_NODE:
403 case XML_NOTATION_NODE:
405 case XML_ELEMENT_DECL:
406 case XML_ATTRIBUTE_DECL:
407 case XML_ENTITY_DECL:
408 case XML_NAMESPACE_DECL:
409 case XML_XINCLUDE_START:
410 case XML_XINCLUDE_END:
411 #ifdef LIBXML_DOCB_ENABLED
412 case XML_DOCB_DOCUMENT_NODE:
414 case XML_DOCUMENT_NODE:
415 case XML_HTML_DOCUMENT_NODE:
421 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
428 /* TODO: check UTF8 content of the string */
430 fprintf(ctxt->output, "(NULL)");
433 for (i = 0; i < 40; i++)
436 else if (IS_BLANK_CH(str[i]))
437 fputc(' ', ctxt->output);
438 else if (str[i] >= 0x80)
439 fprintf(ctxt->output, "#%X", str[i]);
441 fputc(str[i], ctxt->output);
442 fprintf(ctxt->output, "...");
446 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
448 xmlCtxtDumpSpaces(ctxt);
452 fprintf(ctxt->output, "DTD node is NULL\n");
456 if (dtd->type != XML_DTD_NODE) {
457 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
458 "Node is not a DTD");
462 if (dtd->name != NULL)
463 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
465 fprintf(ctxt->output, "DTD");
466 if (dtd->ExternalID != NULL)
467 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
468 if (dtd->SystemID != NULL)
469 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
470 fprintf(ctxt->output, "\n");
473 * Do a bit of checking
475 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
479 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
481 xmlCtxtDumpSpaces(ctxt);
485 fprintf(ctxt->output, "Attribute declaration is NULL\n");
488 if (attr->type != XML_ATTRIBUTE_DECL) {
489 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
490 "Node is not an attribute declaration");
493 if (attr->name != NULL) {
495 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
497 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
498 "Node attribute declaration has no name");
499 if (attr->elem != NULL) {
501 fprintf(ctxt->output, " for %s", (char *) attr->elem);
503 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
504 "Node attribute declaration has no element name");
506 switch (attr->atype) {
507 case XML_ATTRIBUTE_CDATA:
508 fprintf(ctxt->output, " CDATA");
510 case XML_ATTRIBUTE_ID:
511 fprintf(ctxt->output, " ID");
513 case XML_ATTRIBUTE_IDREF:
514 fprintf(ctxt->output, " IDREF");
516 case XML_ATTRIBUTE_IDREFS:
517 fprintf(ctxt->output, " IDREFS");
519 case XML_ATTRIBUTE_ENTITY:
520 fprintf(ctxt->output, " ENTITY");
522 case XML_ATTRIBUTE_ENTITIES:
523 fprintf(ctxt->output, " ENTITIES");
525 case XML_ATTRIBUTE_NMTOKEN:
526 fprintf(ctxt->output, " NMTOKEN");
528 case XML_ATTRIBUTE_NMTOKENS:
529 fprintf(ctxt->output, " NMTOKENS");
531 case XML_ATTRIBUTE_ENUMERATION:
532 fprintf(ctxt->output, " ENUMERATION");
534 case XML_ATTRIBUTE_NOTATION:
535 fprintf(ctxt->output, " NOTATION ");
538 if (attr->tree != NULL) {
540 xmlEnumerationPtr cur = attr->tree;
542 for (indx = 0; indx < 5; indx++) {
544 fprintf(ctxt->output, "|%s", (char *) cur->name);
546 fprintf(ctxt->output, " (%s", (char *) cur->name);
552 fprintf(ctxt->output, ")");
554 fprintf(ctxt->output, "...)");
557 case XML_ATTRIBUTE_NONE:
559 case XML_ATTRIBUTE_REQUIRED:
560 fprintf(ctxt->output, " REQUIRED");
562 case XML_ATTRIBUTE_IMPLIED:
563 fprintf(ctxt->output, " IMPLIED");
565 case XML_ATTRIBUTE_FIXED:
566 fprintf(ctxt->output, " FIXED");
569 if (attr->defaultValue != NULL) {
570 fprintf(ctxt->output, "\"");
571 xmlCtxtDumpString(ctxt, attr->defaultValue);
572 fprintf(ctxt->output, "\"");
574 fprintf(ctxt->output, "\n");
578 * Do a bit of checking
580 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
584 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
586 xmlCtxtDumpSpaces(ctxt);
590 fprintf(ctxt->output, "Element declaration is NULL\n");
593 if (elem->type != XML_ELEMENT_DECL) {
594 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
595 "Node is not an element declaration");
598 if (elem->name != NULL) {
600 fprintf(ctxt->output, "ELEMDECL(");
601 xmlCtxtDumpString(ctxt, elem->name);
602 fprintf(ctxt->output, ")");
605 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
606 "Element declaration has no name");
608 switch (elem->etype) {
609 case XML_ELEMENT_TYPE_UNDEFINED:
610 fprintf(ctxt->output, ", UNDEFINED");
612 case XML_ELEMENT_TYPE_EMPTY:
613 fprintf(ctxt->output, ", EMPTY");
615 case XML_ELEMENT_TYPE_ANY:
616 fprintf(ctxt->output, ", ANY");
618 case XML_ELEMENT_TYPE_MIXED:
619 fprintf(ctxt->output, ", MIXED ");
621 case XML_ELEMENT_TYPE_ELEMENT:
622 fprintf(ctxt->output, ", MIXED ");
625 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
629 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
631 fprintf(ctxt->output, "%s", buf);
633 fprintf(ctxt->output, "\n");
637 * Do a bit of checking
639 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
643 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
645 xmlCtxtDumpSpaces(ctxt);
649 fprintf(ctxt->output, "Entity declaration is NULL\n");
652 if (ent->type != XML_ENTITY_DECL) {
653 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
654 "Node is not an entity declaration");
657 if (ent->name != NULL) {
659 fprintf(ctxt->output, "ENTITYDECL(");
660 xmlCtxtDumpString(ctxt, ent->name);
661 fprintf(ctxt->output, ")");
664 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
665 "Entity declaration has no name");
667 switch (ent->etype) {
668 case XML_INTERNAL_GENERAL_ENTITY:
669 fprintf(ctxt->output, ", internal\n");
671 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
672 fprintf(ctxt->output, ", external parsed\n");
674 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
675 fprintf(ctxt->output, ", unparsed\n");
677 case XML_INTERNAL_PARAMETER_ENTITY:
678 fprintf(ctxt->output, ", parameter\n");
680 case XML_EXTERNAL_PARAMETER_ENTITY:
681 fprintf(ctxt->output, ", external parameter\n");
683 case XML_INTERNAL_PREDEFINED_ENTITY:
684 fprintf(ctxt->output, ", predefined\n");
687 if (ent->ExternalID) {
688 xmlCtxtDumpSpaces(ctxt);
689 fprintf(ctxt->output, " ExternalID=%s\n",
690 (char *) ent->ExternalID);
693 xmlCtxtDumpSpaces(ctxt);
694 fprintf(ctxt->output, " SystemID=%s\n",
695 (char *) ent->SystemID);
697 if (ent->URI != NULL) {
698 xmlCtxtDumpSpaces(ctxt);
699 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
702 xmlCtxtDumpSpaces(ctxt);
703 fprintf(ctxt->output, " content=");
704 xmlCtxtDumpString(ctxt, ent->content);
705 fprintf(ctxt->output, "\n");
710 * Do a bit of checking
712 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
716 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
718 xmlCtxtDumpSpaces(ctxt);
722 fprintf(ctxt->output, "namespace node is NULL\n");
725 if (ns->type != XML_NAMESPACE_DECL) {
726 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
727 "Node is not a namespace declaration");
730 if (ns->href == NULL) {
731 if (ns->prefix != NULL)
732 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
733 "Incomplete namespace %s href=NULL\n",
734 (char *) ns->prefix);
736 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
737 "Incomplete default namespace href=NULL\n");
740 if (ns->prefix != NULL)
741 fprintf(ctxt->output, "namespace %s href=",
742 (char *) ns->prefix);
744 fprintf(ctxt->output, "default namespace href=");
746 xmlCtxtDumpString(ctxt, ns->href);
747 fprintf(ctxt->output, "\n");
753 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
756 xmlCtxtDumpNamespace(ctxt, ns);
762 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
764 xmlCtxtDumpSpaces(ctxt);
768 fprintf(ctxt->output, "Entity is NULL\n");
772 switch (ent->etype) {
773 case XML_INTERNAL_GENERAL_ENTITY:
774 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
776 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
777 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
779 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
780 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
782 case XML_INTERNAL_PARAMETER_ENTITY:
783 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
785 case XML_EXTERNAL_PARAMETER_ENTITY:
786 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
789 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
791 fprintf(ctxt->output, "%s\n", ent->name);
792 if (ent->ExternalID) {
793 xmlCtxtDumpSpaces(ctxt);
794 fprintf(ctxt->output, "ExternalID=%s\n",
795 (char *) ent->ExternalID);
798 xmlCtxtDumpSpaces(ctxt);
799 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
802 xmlCtxtDumpSpaces(ctxt);
803 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
806 xmlCtxtDumpSpaces(ctxt);
807 fprintf(ctxt->output, "content=");
808 xmlCtxtDumpString(ctxt, ent->content);
809 fprintf(ctxt->output, "\n");
816 * @output: the FILE * for the output
817 * @attr: the attribute
818 * @depth: the indentation level.
820 * Dumps debug information for the attribute
823 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
825 xmlCtxtDumpSpaces(ctxt);
829 fprintf(ctxt->output, "Attr is NULL");
833 fprintf(ctxt->output, "ATTRIBUTE ");
834 xmlCtxtDumpString(ctxt, attr->name);
835 fprintf(ctxt->output, "\n");
836 if (attr->children != NULL) {
838 xmlCtxtDumpNodeList(ctxt, attr->children);
842 if (attr->name == NULL)
843 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
844 "Attribute has no name");
847 * Do a bit of checking
849 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
853 * xmlCtxtDumpAttrList:
854 * @output: the FILE * for the output
855 * @attr: the attribute list
856 * @depth: the indentation level.
858 * Dumps debug information for the attribute list
861 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
863 while (attr != NULL) {
864 xmlCtxtDumpAttr(ctxt, attr);
870 * xmlCtxtDumpOneNode:
871 * @output: the FILE * for the output
873 * @depth: the indentation level.
875 * Dumps debug information for the element node, it is not recursive
878 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
882 xmlCtxtDumpSpaces(ctxt);
883 fprintf(ctxt->output, "node is NULL\n");
889 switch (node->type) {
890 case XML_ELEMENT_NODE:
892 xmlCtxtDumpSpaces(ctxt);
893 fprintf(ctxt->output, "ELEMENT ");
894 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
895 xmlCtxtDumpString(ctxt, node->ns->prefix);
896 fprintf(ctxt->output, ":");
898 xmlCtxtDumpString(ctxt, node->name);
899 fprintf(ctxt->output, "\n");
902 case XML_ATTRIBUTE_NODE:
904 xmlCtxtDumpSpaces(ctxt);
905 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
906 xmlCtxtGenericNodeCheck(ctxt, node);
910 xmlCtxtDumpSpaces(ctxt);
911 if (node->name == (const xmlChar *) xmlStringTextNoenc)
912 fprintf(ctxt->output, "TEXT no enc");
914 fprintf(ctxt->output, "TEXT");
915 if (ctxt->options & DUMP_TEXT_TYPE) {
916 if (node->content == (xmlChar *) &(node->properties))
917 fprintf(ctxt->output, " compact\n");
918 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
919 fprintf(ctxt->output, " interned\n");
921 fprintf(ctxt->output, "\n");
923 fprintf(ctxt->output, "\n");
926 case XML_CDATA_SECTION_NODE:
928 xmlCtxtDumpSpaces(ctxt);
929 fprintf(ctxt->output, "CDATA_SECTION\n");
932 case XML_ENTITY_REF_NODE:
934 xmlCtxtDumpSpaces(ctxt);
935 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
936 (char *) node->name);
939 case XML_ENTITY_NODE:
941 xmlCtxtDumpSpaces(ctxt);
942 fprintf(ctxt->output, "ENTITY\n");
947 xmlCtxtDumpSpaces(ctxt);
948 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
951 case XML_COMMENT_NODE:
953 xmlCtxtDumpSpaces(ctxt);
954 fprintf(ctxt->output, "COMMENT\n");
957 case XML_DOCUMENT_NODE:
958 case XML_HTML_DOCUMENT_NODE:
960 xmlCtxtDumpSpaces(ctxt);
962 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
963 xmlCtxtGenericNodeCheck(ctxt, node);
965 case XML_DOCUMENT_TYPE_NODE:
967 xmlCtxtDumpSpaces(ctxt);
968 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
971 case XML_DOCUMENT_FRAG_NODE:
973 xmlCtxtDumpSpaces(ctxt);
974 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
977 case XML_NOTATION_NODE:
979 xmlCtxtDumpSpaces(ctxt);
980 fprintf(ctxt->output, "NOTATION\n");
984 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
986 case XML_ELEMENT_DECL:
987 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
989 case XML_ATTRIBUTE_DECL:
990 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
992 case XML_ENTITY_DECL:
993 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
995 case XML_NAMESPACE_DECL:
996 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
998 case XML_XINCLUDE_START:
1000 xmlCtxtDumpSpaces(ctxt);
1001 fprintf(ctxt->output, "INCLUDE START\n");
1004 case XML_XINCLUDE_END:
1006 xmlCtxtDumpSpaces(ctxt);
1007 fprintf(ctxt->output, "INCLUDE END\n");
1012 xmlCtxtDumpSpaces(ctxt);
1013 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1014 "Unknown node type %d\n", node->type);
1017 if (node->doc == NULL) {
1019 xmlCtxtDumpSpaces(ctxt);
1021 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1024 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
1025 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1026 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
1027 xmlCtxtDumpAttrList(ctxt, node->properties);
1028 if (node->type != XML_ENTITY_REF_NODE) {
1029 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1031 xmlCtxtDumpSpaces(ctxt);
1032 fprintf(ctxt->output, "content=");
1033 xmlCtxtDumpString(ctxt, node->content);
1034 fprintf(ctxt->output, "\n");
1040 ent = xmlGetDocEntity(node->doc, node->name);
1042 xmlCtxtDumpEntity(ctxt, ent);
1047 * Do a bit of checking
1049 xmlCtxtGenericNodeCheck(ctxt, node);
1054 * @output: the FILE * for the output
1056 * @depth: the indentation level.
1058 * Dumps debug information for the element node, it is recursive
1061 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1065 xmlCtxtDumpSpaces(ctxt);
1066 fprintf(ctxt->output, "node is NULL\n");
1070 xmlCtxtDumpOneNode(ctxt, node);
1071 if ((node->type != XML_NAMESPACE_DECL) &&
1072 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1074 xmlCtxtDumpNodeList(ctxt, node->children);
1080 * xmlCtxtDumpNodeList:
1081 * @output: the FILE * for the output
1082 * @node: the node list
1083 * @depth: the indentation level.
1085 * Dumps debug information for the list of element node, it is recursive
1088 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1090 while (node != NULL) {
1091 xmlCtxtDumpNode(ctxt, node);
1097 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1101 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1104 ctxt->node = (xmlNodePtr) doc;
1106 switch (doc->type) {
1107 case XML_ELEMENT_NODE:
1108 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1109 "Misplaced ELEMENT node\n");
1111 case XML_ATTRIBUTE_NODE:
1112 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1113 "Misplaced ATTRIBUTE node\n");
1116 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1117 "Misplaced TEXT node\n");
1119 case XML_CDATA_SECTION_NODE:
1120 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1121 "Misplaced CDATA node\n");
1123 case XML_ENTITY_REF_NODE:
1124 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1125 "Misplaced ENTITYREF node\n");
1127 case XML_ENTITY_NODE:
1128 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1129 "Misplaced ENTITY node\n");
1132 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1133 "Misplaced PI node\n");
1135 case XML_COMMENT_NODE:
1136 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1137 "Misplaced COMMENT node\n");
1139 case XML_DOCUMENT_NODE:
1141 fprintf(ctxt->output, "DOCUMENT\n");
1143 case XML_HTML_DOCUMENT_NODE:
1145 fprintf(ctxt->output, "HTML DOCUMENT\n");
1147 case XML_DOCUMENT_TYPE_NODE:
1148 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1149 "Misplaced DOCTYPE node\n");
1151 case XML_DOCUMENT_FRAG_NODE:
1152 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1153 "Misplaced FRAGMENT node\n");
1155 case XML_NOTATION_NODE:
1156 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1157 "Misplaced NOTATION node\n");
1160 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1161 "Unknown node type %d\n", doc->type);
1166 * xmlCtxtDumpDocumentHead:
1167 * @output: the FILE * for the output
1168 * @doc: the document
1170 * Dumps debug information cncerning the document, not recursive
1173 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1175 if (doc == NULL) return;
1176 xmlCtxtDumpDocHead(ctxt, doc);
1178 if (doc->name != NULL) {
1179 fprintf(ctxt->output, "name=");
1180 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1181 fprintf(ctxt->output, "\n");
1183 if (doc->version != NULL) {
1184 fprintf(ctxt->output, "version=");
1185 xmlCtxtDumpString(ctxt, doc->version);
1186 fprintf(ctxt->output, "\n");
1188 if (doc->encoding != NULL) {
1189 fprintf(ctxt->output, "encoding=");
1190 xmlCtxtDumpString(ctxt, doc->encoding);
1191 fprintf(ctxt->output, "\n");
1193 if (doc->URL != NULL) {
1194 fprintf(ctxt->output, "URL=");
1195 xmlCtxtDumpString(ctxt, doc->URL);
1196 fprintf(ctxt->output, "\n");
1198 if (doc->standalone)
1199 fprintf(ctxt->output, "standalone=true\n");
1201 if (doc->oldNs != NULL)
1202 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1206 * xmlCtxtDumpDocument:
1207 * @output: the FILE * for the output
1208 * @doc: the document
1210 * Dumps debug information for the document, it's recursive
1213 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1217 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1220 xmlCtxtDumpDocumentHead(ctxt, doc);
1221 if (((doc->type == XML_DOCUMENT_NODE) ||
1222 (doc->type == XML_HTML_DOCUMENT_NODE))
1223 && (doc->children != NULL)) {
1225 xmlCtxtDumpNodeList(ctxt, doc->children);
1231 xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1235 fprintf(ctxt->output, "Entity is NULL");
1239 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1240 switch (cur->etype) {
1241 case XML_INTERNAL_GENERAL_ENTITY:
1242 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1244 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1245 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1247 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1248 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1250 case XML_INTERNAL_PARAMETER_ENTITY:
1251 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1253 case XML_EXTERNAL_PARAMETER_ENTITY:
1254 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1257 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1258 "Unknown entity type %d\n", cur->etype);
1260 if (cur->ExternalID != NULL)
1261 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1262 if (cur->SystemID != NULL)
1263 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1264 if (cur->orig != NULL)
1265 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1266 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1267 fprintf(ctxt->output, "\n content \"%s\"",
1268 (char *) cur->content);
1269 fprintf(ctxt->output, "\n");
1274 * xmlCtxtDumpEntities:
1275 * @output: the FILE * for the output
1276 * @doc: the document
1278 * Dumps debug information for all the entities in use by the document
1281 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1283 if (doc == NULL) return;
1284 xmlCtxtDumpDocHead(ctxt, doc);
1285 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1286 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1287 doc->intSubset->entities;
1290 fprintf(ctxt->output, "Entities in internal subset\n");
1291 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1294 fprintf(ctxt->output, "No entities in internal subset\n");
1295 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1296 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1297 doc->extSubset->entities;
1300 fprintf(ctxt->output, "Entities in external subset\n");
1301 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1303 } else if (!ctxt->check)
1304 fprintf(ctxt->output, "No entities in external subset\n");
1309 * @output: the FILE * for the output
1312 * Dumps debug information for the DTD
1315 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1319 fprintf(ctxt->output, "DTD is NULL\n");
1322 xmlCtxtDumpDtdNode(ctxt, dtd);
1323 if (dtd->children == NULL)
1324 fprintf(ctxt->output, " DTD is empty\n");
1327 xmlCtxtDumpNodeList(ctxt, dtd->children);
1332 /************************************************************************
1334 * Public entry points for dump *
1336 ************************************************************************/
1339 * xmlDebugDumpString:
1340 * @output: the FILE * for the output
1343 * Dumps informations about the string, shorten it if necessary
1346 xmlDebugDumpString(FILE * output, const xmlChar * str)
1353 fprintf(output, "(NULL)");
1356 for (i = 0; i < 40; i++)
1359 else if (IS_BLANK_CH(str[i]))
1361 else if (str[i] >= 0x80)
1362 fprintf(output, "#%X", str[i]);
1364 fputc(str[i], output);
1365 fprintf(output, "...");
1370 * @output: the FILE * for the output
1371 * @attr: the attribute
1372 * @depth: the indentation level.
1374 * Dumps debug information for the attribute
1377 xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
1380 if (output == NULL) return;
1381 xmlCtxtDumpInitCtxt(&ctxt);
1382 ctxt.output = output;
1384 xmlCtxtDumpAttr(&ctxt, attr);
1385 xmlCtxtDumpCleanCtxt(&ctxt);
1390 * xmlDebugDumpEntities:
1391 * @output: the FILE * for the output
1392 * @doc: the document
1394 * Dumps debug information for all the entities in use by the document
1397 xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1401 if (output == NULL) return;
1402 xmlCtxtDumpInitCtxt(&ctxt);
1403 ctxt.output = output;
1404 xmlCtxtDumpEntities(&ctxt, doc);
1405 xmlCtxtDumpCleanCtxt(&ctxt);
1409 * xmlDebugDumpAttrList:
1410 * @output: the FILE * for the output
1411 * @attr: the attribute list
1412 * @depth: the indentation level.
1414 * Dumps debug information for the attribute list
1417 xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1421 if (output == NULL) return;
1422 xmlCtxtDumpInitCtxt(&ctxt);
1423 ctxt.output = output;
1425 xmlCtxtDumpAttrList(&ctxt, attr);
1426 xmlCtxtDumpCleanCtxt(&ctxt);
1430 * xmlDebugDumpOneNode:
1431 * @output: the FILE * for the output
1433 * @depth: the indentation level.
1435 * Dumps debug information for the element node, it is not recursive
1438 xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1442 if (output == NULL) return;
1443 xmlCtxtDumpInitCtxt(&ctxt);
1444 ctxt.output = output;
1446 xmlCtxtDumpOneNode(&ctxt, node);
1447 xmlCtxtDumpCleanCtxt(&ctxt);
1452 * @output: the FILE * for the output
1454 * @depth: the indentation level.
1456 * Dumps debug information for the element node, it is recursive
1459 xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1465 xmlCtxtDumpInitCtxt(&ctxt);
1466 ctxt.output = output;
1468 xmlCtxtDumpNode(&ctxt, node);
1469 xmlCtxtDumpCleanCtxt(&ctxt);
1473 * xmlDebugDumpNodeList:
1474 * @output: the FILE * for the output
1475 * @node: the node list
1476 * @depth: the indentation level.
1478 * Dumps debug information for the list of element node, it is recursive
1481 xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1487 xmlCtxtDumpInitCtxt(&ctxt);
1488 ctxt.output = output;
1490 xmlCtxtDumpNodeList(&ctxt, node);
1491 xmlCtxtDumpCleanCtxt(&ctxt);
1495 * xmlDebugDumpDocumentHead:
1496 * @output: the FILE * for the output
1497 * @doc: the document
1499 * Dumps debug information cncerning the document, not recursive
1502 xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1508 xmlCtxtDumpInitCtxt(&ctxt);
1509 ctxt.options |= DUMP_TEXT_TYPE;
1510 ctxt.output = output;
1511 xmlCtxtDumpDocumentHead(&ctxt, doc);
1512 xmlCtxtDumpCleanCtxt(&ctxt);
1516 * xmlDebugDumpDocument:
1517 * @output: the FILE * for the output
1518 * @doc: the document
1520 * Dumps debug information for the document, it's recursive
1523 xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1529 xmlCtxtDumpInitCtxt(&ctxt);
1530 ctxt.options |= DUMP_TEXT_TYPE;
1531 ctxt.output = output;
1532 xmlCtxtDumpDocument(&ctxt, doc);
1533 xmlCtxtDumpCleanCtxt(&ctxt);
1538 * @output: the FILE * for the output
1541 * Dumps debug information for the DTD
1544 xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1550 xmlCtxtDumpInitCtxt(&ctxt);
1551 ctxt.options |= DUMP_TEXT_TYPE;
1552 ctxt.output = output;
1553 xmlCtxtDumpDTD(&ctxt, dtd);
1554 xmlCtxtDumpCleanCtxt(&ctxt);
1557 /************************************************************************
1559 * Public entry points for checkings *
1561 ************************************************************************/
1564 * xmlDebugCheckDocument:
1565 * @output: the FILE * for the output
1566 * @doc: the document
1568 * Check the document for potential content problems, and output
1569 * the errors to @output
1571 * Returns the number of errors found
1574 xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1580 xmlCtxtDumpInitCtxt(&ctxt);
1581 ctxt.output = output;
1583 xmlCtxtDumpDocument(&ctxt, doc);
1584 xmlCtxtDumpCleanCtxt(&ctxt);
1585 return(ctxt.errors);
1588 /************************************************************************
1590 * Helpers for Shell *
1592 ************************************************************************/
1596 * @node: the node to count
1598 * Count the children of @node.
1600 * Returns the number of children of @node.
1603 xmlLsCountNode(xmlNodePtr node) {
1605 xmlNodePtr list = NULL;
1610 switch (node->type) {
1611 case XML_ELEMENT_NODE:
1612 list = node->children;
1614 case XML_DOCUMENT_NODE:
1615 case XML_HTML_DOCUMENT_NODE:
1616 #ifdef LIBXML_DOCB_ENABLED
1617 case XML_DOCB_DOCUMENT_NODE:
1619 list = ((xmlDocPtr) node)->children;
1621 case XML_ATTRIBUTE_NODE:
1622 list = ((xmlAttrPtr) node)->children;
1625 case XML_CDATA_SECTION_NODE:
1627 case XML_COMMENT_NODE:
1628 if (node->content != NULL) {
1629 ret = xmlStrlen(node->content);
1632 case XML_ENTITY_REF_NODE:
1633 case XML_DOCUMENT_TYPE_NODE:
1634 case XML_ENTITY_NODE:
1635 case XML_DOCUMENT_FRAG_NODE:
1636 case XML_NOTATION_NODE:
1638 case XML_ELEMENT_DECL:
1639 case XML_ATTRIBUTE_DECL:
1640 case XML_ENTITY_DECL:
1641 case XML_NAMESPACE_DECL:
1642 case XML_XINCLUDE_START:
1643 case XML_XINCLUDE_END:
1647 for (;list != NULL;ret++)
1654 * @output: the FILE * for the output
1655 * @node: the node to dump
1657 * Dump to @output the type and name of @node.
1660 xmlLsOneNode(FILE *output, xmlNodePtr node) {
1661 if (output == NULL) return;
1663 fprintf(output, "NULL\n");
1666 switch (node->type) {
1667 case XML_ELEMENT_NODE:
1668 fprintf(output, "-");
1670 case XML_ATTRIBUTE_NODE:
1671 fprintf(output, "a");
1674 fprintf(output, "t");
1676 case XML_CDATA_SECTION_NODE:
1677 fprintf(output, "C");
1679 case XML_ENTITY_REF_NODE:
1680 fprintf(output, "e");
1682 case XML_ENTITY_NODE:
1683 fprintf(output, "E");
1686 fprintf(output, "p");
1688 case XML_COMMENT_NODE:
1689 fprintf(output, "c");
1691 case XML_DOCUMENT_NODE:
1692 fprintf(output, "d");
1694 case XML_HTML_DOCUMENT_NODE:
1695 fprintf(output, "h");
1697 case XML_DOCUMENT_TYPE_NODE:
1698 fprintf(output, "T");
1700 case XML_DOCUMENT_FRAG_NODE:
1701 fprintf(output, "F");
1703 case XML_NOTATION_NODE:
1704 fprintf(output, "N");
1706 case XML_NAMESPACE_DECL:
1707 fprintf(output, "n");
1710 fprintf(output, "?");
1712 if (node->type != XML_NAMESPACE_DECL) {
1713 if (node->properties != NULL)
1714 fprintf(output, "a");
1716 fprintf(output, "-");
1717 if (node->nsDef != NULL)
1718 fprintf(output, "n");
1720 fprintf(output, "-");
1723 fprintf(output, " %8d ", xmlLsCountNode(node));
1725 switch (node->type) {
1726 case XML_ELEMENT_NODE:
1727 if (node->name != NULL)
1728 fprintf(output, "%s", (const char *) node->name);
1730 case XML_ATTRIBUTE_NODE:
1731 if (node->name != NULL)
1732 fprintf(output, "%s", (const char *) node->name);
1735 if (node->content != NULL) {
1736 xmlDebugDumpString(output, node->content);
1739 case XML_CDATA_SECTION_NODE:
1741 case XML_ENTITY_REF_NODE:
1742 if (node->name != NULL)
1743 fprintf(output, "%s", (const char *) node->name);
1745 case XML_ENTITY_NODE:
1746 if (node->name != NULL)
1747 fprintf(output, "%s", (const char *) node->name);
1750 if (node->name != NULL)
1751 fprintf(output, "%s", (const char *) node->name);
1753 case XML_COMMENT_NODE:
1755 case XML_DOCUMENT_NODE:
1757 case XML_HTML_DOCUMENT_NODE:
1759 case XML_DOCUMENT_TYPE_NODE:
1761 case XML_DOCUMENT_FRAG_NODE:
1763 case XML_NOTATION_NODE:
1765 case XML_NAMESPACE_DECL: {
1766 xmlNsPtr ns = (xmlNsPtr) node;
1768 if (ns->prefix == NULL)
1769 fprintf(output, "default -> %s", (char *)ns->href);
1771 fprintf(output, "%s -> %s", (char *)ns->prefix,
1776 if (node->name != NULL)
1777 fprintf(output, "%s", (const char *) node->name);
1779 fprintf(output, "\n");
1784 * @boolval: a bool to turn into text
1786 * Convenient way to turn bool into text
1788 * Returns a pointer to either "True" or "False"
1791 xmlBoolToText(int boolval)
1799 #ifdef LIBXML_XPATH_ENABLED
1800 /****************************************************************
1802 * The XML shell related functions *
1804 ****************************************************************/
1809 * TODO: Improvement/cleanups for the XML shell
1810 * - allow to shell out an editor on a subpart
1811 * - cleanup function registrations (with help) and calling
1812 * - provide registration routines
1816 * xmlShellPrintXPathError:
1817 * @errorType: valid xpath error id
1818 * @arg: the argument that cause xpath to fail
1820 * Print the xpath error to libxml default error channel
1823 xmlShellPrintXPathError(int errorType, const char *arg)
1825 const char *default_arg = "Result";
1830 switch (errorType) {
1831 case XPATH_UNDEFINED:
1832 xmlGenericError(xmlGenericErrorContext,
1833 "%s: no such node\n", arg);
1837 xmlGenericError(xmlGenericErrorContext,
1838 "%s is a Boolean\n", arg);
1841 xmlGenericError(xmlGenericErrorContext,
1842 "%s is a number\n", arg);
1845 xmlGenericError(xmlGenericErrorContext,
1846 "%s is a string\n", arg);
1849 xmlGenericError(xmlGenericErrorContext,
1850 "%s is a point\n", arg);
1853 xmlGenericError(xmlGenericErrorContext,
1854 "%s is a range\n", arg);
1856 case XPATH_LOCATIONSET:
1857 xmlGenericError(xmlGenericErrorContext,
1858 "%s is a range\n", arg);
1861 xmlGenericError(xmlGenericErrorContext,
1862 "%s is user-defined\n", arg);
1864 case XPATH_XSLT_TREE:
1865 xmlGenericError(xmlGenericErrorContext,
1866 "%s is an XSLT value tree\n", arg);
1870 xmlGenericError(xmlGenericErrorContext,
1871 "Try casting the result string function (xpath builtin)\n",
1877 #ifdef LIBXML_OUTPUT_ENABLED
1879 * xmlShellPrintNodeCtxt:
1880 * @ctxt : a non-null shell context
1881 * @node : a non-null node to print to the output FILE
1883 * Print node to the output FILE
1886 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1897 if (node->type == XML_DOCUMENT_NODE)
1898 xmlDocDump(fp, (xmlDocPtr) node);
1899 else if (node->type == XML_ATTRIBUTE_NODE)
1900 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
1902 xmlElemDump(fp, node->doc, node);
1908 * xmlShellPrintNode:
1909 * @node : a non-null node to print to the output FILE
1911 * Print node to the output FILE
1914 xmlShellPrintNode(xmlNodePtr node)
1916 xmlShellPrintNodeCtxt(NULL, node);
1918 #endif /* LIBXML_OUTPUT_ENABLED */
1921 * xmlShellPrintXPathResultCtxt:
1922 * @ctxt: a valid shell context
1923 * @list: a valid result generated by an xpath evaluation
1925 * Prints result to the output FILE
1928 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
1934 switch (list->type) {
1935 case XPATH_NODESET:{
1936 #ifdef LIBXML_OUTPUT_ENABLED
1939 if (list->nodesetval) {
1940 for (indx = 0; indx < list->nodesetval->nodeNr;
1942 xmlShellPrintNodeCtxt(ctxt,
1943 list->nodesetval->nodeTab[indx]);
1946 xmlGenericError(xmlGenericErrorContext,
1947 "Empty node set\n");
1951 xmlGenericError(xmlGenericErrorContext,
1953 #endif /* LIBXML_OUTPUT_ENABLED */
1956 xmlGenericError(xmlGenericErrorContext,
1957 "Is a Boolean:%s\n",
1958 xmlBoolToText(list->boolval));
1961 xmlGenericError(xmlGenericErrorContext,
1962 "Is a number:%0g\n", list->floatval);
1965 xmlGenericError(xmlGenericErrorContext,
1966 "Is a string:%s\n", list->stringval);
1970 xmlShellPrintXPathError(list->type, NULL);
1976 * xmlShellPrintXPathResult:
1977 * @list: a valid result generated by an xpath evaluation
1979 * Prints result to the output FILE
1982 xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1984 xmlShellPrintXPathResultCtxt(NULL, list);
1989 * @ctxt: the shell context
1994 * Implements the XML shell function "ls"
1995 * Does an Unix like listing of the given node (like a directory)
2000 xmlShellList(xmlShellCtxtPtr ctxt,
2001 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2002 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2008 fprintf(ctxt->output, "NULL\n");
2011 if ((node->type == XML_DOCUMENT_NODE) ||
2012 (node->type == XML_HTML_DOCUMENT_NODE)) {
2013 cur = ((xmlDocPtr) node)->children;
2014 } else if (node->type == XML_NAMESPACE_DECL) {
2015 xmlLsOneNode(ctxt->output, node);
2017 } else if (node->children != NULL) {
2018 cur = node->children;
2020 xmlLsOneNode(ctxt->output, node);
2023 while (cur != NULL) {
2024 xmlLsOneNode(ctxt->output, cur);
2032 * @ctxt: the shell context
2037 * Implements the XML shell function "base"
2038 * dumps the current XML base of the node
2043 xmlShellBase(xmlShellCtxtPtr ctxt,
2044 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2045 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2051 fprintf(ctxt->output, "NULL\n");
2055 base = xmlNodeGetBase(node->doc, node);
2058 fprintf(ctxt->output, " No base found !!!\n");
2060 fprintf(ctxt->output, "%s\n", base);
2066 #ifdef LIBXML_TREE_ENABLED
2069 * @ctxt: the shell context
2070 * @arg: the new base
2074 * Implements the XML shell function "setbase"
2075 * change the current XML base of the node
2080 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2081 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2082 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2084 xmlNodeSetBase(node, (xmlChar*) arg);
2089 #ifdef LIBXML_XPATH_ENABLED
2091 * xmlShellRegisterNamespace:
2092 * @ctxt: the shell context
2093 * @arg: a string in prefix=nsuri format
2097 * Implements the XML shell function "setns"
2098 * register/unregister a prefix=namespace pair
2099 * on the XPath context
2101 * Returns 0 on success and a negative value otherwise.
2104 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2105 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2112 nsListDup = xmlStrdup((xmlChar *) arg);
2114 while(next != NULL) {
2116 /*while((*next) == ' ') next++;*/
2117 if((*next) == '\0') break;
2121 next = (xmlChar*)xmlStrchr(next, '=');
2123 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2131 next = (xmlChar*)xmlStrchr(next, ' ');
2136 /* do register namespace */
2137 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2138 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2148 * xmlShellRegisterRootNamespaces:
2149 * @ctxt: the shell context
2151 * @node: the root element
2154 * Implements the XML shell function "setrootns"
2155 * which registers all namespaces declarations found on the root element.
2157 * Returns 0 on success and a negative value otherwise.
2160 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2161 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2165 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2166 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2169 while (ns != NULL) {
2170 if (ns->prefix == NULL)
2171 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2173 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2182 * @ctxt: the shell context
2183 * @arg: the string or regular expression to find
2187 * Implements the XML shell function "grep"
2188 * dumps informations about the node (namespace, attributes, content).
2193 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2194 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2202 #ifdef LIBXML_REGEXP_ENABLED
2203 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2204 (xmlStrchr((xmlChar *) arg, '*')) ||
2205 (xmlStrchr((xmlChar *) arg, '.')) ||
2206 (xmlStrchr((xmlChar *) arg, '['))) {
2209 while (node != NULL) {
2210 if (node->type == XML_COMMENT_NODE) {
2211 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2213 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2214 xmlShellList(ctxt, NULL, node, NULL);
2216 } else if (node->type == XML_TEXT_NODE) {
2217 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2219 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
2220 xmlShellList(ctxt, NULL, node->parent, NULL);
2225 * Browse the full subtree, deep first
2228 if ((node->type == XML_DOCUMENT_NODE) ||
2229 (node->type == XML_HTML_DOCUMENT_NODE)) {
2230 node = ((xmlDocPtr) node)->children;
2231 } else if ((node->children != NULL)
2232 && (node->type != XML_ENTITY_REF_NODE)) {
2234 node = node->children;
2235 } else if (node->next != NULL) {
2239 /* go up to parents->next if needed */
2240 while (node != NULL) {
2241 if (node->parent != NULL) {
2242 node = node->parent;
2244 if (node->next != NULL) {
2248 if (node->parent == NULL) {
2260 * @ctxt: the shell context
2265 * Implements the XML shell function "dir"
2266 * dumps informations about the node (namespace, attributes, content).
2271 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2272 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2273 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2278 fprintf(ctxt->output, "NULL\n");
2281 if ((node->type == XML_DOCUMENT_NODE) ||
2282 (node->type == XML_HTML_DOCUMENT_NODE)) {
2283 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
2284 } else if (node->type == XML_ATTRIBUTE_NODE) {
2285 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
2287 xmlDebugDumpOneNode(ctxt->output, node, 0);
2293 * xmlShellSetContent:
2294 * @ctxt: the shell context
2295 * @value: the content as a string
2299 * Implements the XML shell function "dir"
2300 * dumps informations about the node (namespace, attributes, content).
2305 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2306 char *value, xmlNodePtr node,
2307 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2310 xmlParserErrors ret;
2315 fprintf(ctxt->output, "NULL\n");
2318 if (value == NULL) {
2319 fprintf(ctxt->output, "NULL\n");
2323 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2324 if (ret == XML_ERR_OK) {
2325 if (node->children != NULL) {
2326 xmlFreeNodeList(node->children);
2327 node->children = NULL;
2330 xmlAddChildList(node, results);
2332 fprintf(ctxt->output, "failed to parse content\n");
2337 #ifdef LIBXML_SCHEMAS_ENABLED
2339 * xmlShellRNGValidate:
2340 * @ctxt: the shell context
2341 * @schemas: the path to the Relax-NG schemas
2345 * Implements the XML shell function "relaxng"
2346 * validating the instance against a Relax-NG schemas
2351 xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2352 xmlNodePtr node ATTRIBUTE_UNUSED,
2353 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2355 xmlRelaxNGPtr relaxngschemas;
2356 xmlRelaxNGParserCtxtPtr ctxt;
2357 xmlRelaxNGValidCtxtPtr vctxt;
2360 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2361 xmlRelaxNGSetParserErrors(ctxt,
2362 (xmlRelaxNGValidityErrorFunc) fprintf,
2363 (xmlRelaxNGValidityWarningFunc) fprintf,
2365 relaxngschemas = xmlRelaxNGParse(ctxt);
2366 xmlRelaxNGFreeParserCtxt(ctxt);
2367 if (relaxngschemas == NULL) {
2368 xmlGenericError(xmlGenericErrorContext,
2369 "Relax-NG schema %s failed to compile\n", schemas);
2372 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2373 xmlRelaxNGSetValidErrors(vctxt,
2374 (xmlRelaxNGValidityErrorFunc) fprintf,
2375 (xmlRelaxNGValidityWarningFunc) fprintf,
2377 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2379 fprintf(stderr, "%s validates\n", sctxt->filename);
2380 } else if (ret > 0) {
2381 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2383 fprintf(stderr, "%s validation generated an internal error\n",
2386 xmlRelaxNGFreeValidCtxt(vctxt);
2387 if (relaxngschemas != NULL)
2388 xmlRelaxNGFree(relaxngschemas);
2393 #ifdef LIBXML_OUTPUT_ENABLED
2396 * @ctxt: the shell context
2401 * Implements the XML shell function "cat"
2402 * dumps the serialization node content (XML or HTML).
2407 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2408 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2413 fprintf(ctxt->output, "NULL\n");
2416 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2417 #ifdef LIBXML_HTML_ENABLED
2418 if (node->type == XML_HTML_DOCUMENT_NODE)
2419 htmlDocDump(ctxt->output, (htmlDocPtr) node);
2421 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
2423 if (node->type == XML_DOCUMENT_NODE)
2424 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2426 xmlElemDump(ctxt->output, ctxt->doc, node);
2427 #endif /* LIBXML_HTML_ENABLED */
2429 if (node->type == XML_DOCUMENT_NODE)
2430 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2432 xmlElemDump(ctxt->output, ctxt->doc, node);
2434 fprintf(ctxt->output, "\n");
2437 #endif /* LIBXML_OUTPUT_ENABLED */
2441 * @ctxt: the shell context
2442 * @filename: the file name
2446 * Implements the XML shell function "load"
2447 * loads a new document specified by the filename
2449 * Returns 0 or -1 if loading failed
2452 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2453 xmlNodePtr node ATTRIBUTE_UNUSED,
2454 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2459 if ((ctxt == NULL) || (filename == NULL)) return(-1);
2460 if (ctxt->doc != NULL)
2461 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2464 #ifdef LIBXML_HTML_ENABLED
2465 doc = htmlParseFile(filename, NULL);
2467 fprintf(ctxt->output, "HTML support not compiled in\n");
2469 #endif /* LIBXML_HTML_ENABLED */
2471 doc = xmlReadFile(filename,NULL,0);
2474 if (ctxt->loaded == 1) {
2475 xmlFreeDoc(ctxt->doc);
2478 #ifdef LIBXML_XPATH_ENABLED
2479 xmlXPathFreeContext(ctxt->pctxt);
2480 #endif /* LIBXML_XPATH_ENABLED */
2481 xmlFree(ctxt->filename);
2483 ctxt->node = (xmlNodePtr) doc;
2484 #ifdef LIBXML_XPATH_ENABLED
2485 ctxt->pctxt = xmlXPathNewContext(doc);
2486 #endif /* LIBXML_XPATH_ENABLED */
2487 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2493 #ifdef LIBXML_OUTPUT_ENABLED
2496 * @ctxt: the shell context
2497 * @filename: the file name
2498 * @node: a node in the tree
2501 * Implements the XML shell function "write"
2502 * Write the current node to the filename, it saves the serialization
2503 * of the subtree under the @node specified
2505 * Returns 0 or -1 in case of error
2508 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2509 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2513 if ((filename == NULL) || (filename[0] == 0)) {
2517 if (access((char *) filename, W_OK)) {
2518 xmlGenericError(xmlGenericErrorContext,
2519 "Cannot write to %s\n", filename);
2523 switch (node->type) {
2524 case XML_DOCUMENT_NODE:
2525 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2526 xmlGenericError(xmlGenericErrorContext,
2527 "Failed to write to %s\n", filename);
2531 case XML_HTML_DOCUMENT_NODE:
2532 #ifdef LIBXML_HTML_ENABLED
2533 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2534 xmlGenericError(xmlGenericErrorContext,
2535 "Failed to write to %s\n", filename);
2539 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2540 xmlGenericError(xmlGenericErrorContext,
2541 "Failed to write to %s\n", filename);
2544 #endif /* LIBXML_HTML_ENABLED */
2549 f = fopen((char *) filename, "w");
2551 xmlGenericError(xmlGenericErrorContext,
2552 "Failed to write to %s\n", filename);
2555 xmlElemDump(f, ctxt->doc, node);
2564 * @ctxt: the shell context
2565 * @filename: the file name (optional)
2569 * Implements the XML shell function "save"
2570 * Write the current document to the filename, or it's original name
2572 * Returns 0 or -1 in case of error
2575 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2576 xmlNodePtr node ATTRIBUTE_UNUSED,
2577 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2579 if ((ctxt == NULL) || (ctxt->doc == NULL))
2581 if ((filename == NULL) || (filename[0] == 0))
2582 filename = ctxt->filename;
2583 if (filename == NULL)
2586 if (access((char *) filename, W_OK)) {
2587 xmlGenericError(xmlGenericErrorContext,
2588 "Cannot save to %s\n", filename);
2592 switch (ctxt->doc->type) {
2593 case XML_DOCUMENT_NODE:
2594 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2595 xmlGenericError(xmlGenericErrorContext,
2596 "Failed to save to %s\n", filename);
2599 case XML_HTML_DOCUMENT_NODE:
2600 #ifdef LIBXML_HTML_ENABLED
2601 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2602 xmlGenericError(xmlGenericErrorContext,
2603 "Failed to save to %s\n", filename);
2606 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2607 xmlGenericError(xmlGenericErrorContext,
2608 "Failed to save to %s\n", filename);
2610 #endif /* LIBXML_HTML_ENABLED */
2613 xmlGenericError(xmlGenericErrorContext,
2614 "To save to subparts of a document use the 'write' command\n");
2620 #endif /* LIBXML_OUTPUT_ENABLED */
2622 #ifdef LIBXML_VALID_ENABLED
2625 * @ctxt: the shell context
2626 * @dtd: the DTD URI (optional)
2630 * Implements the XML shell function "validate"
2631 * Validate the document, if a DTD path is provided, then the validation
2632 * is done against the given DTD.
2634 * Returns 0 or -1 in case of error
2637 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2638 xmlNodePtr node ATTRIBUTE_UNUSED,
2639 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2644 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2645 vctxt.userData = stderr;
2646 vctxt.error = (xmlValidityErrorFunc) fprintf;
2647 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2649 if ((dtd == NULL) || (dtd[0] == 0)) {
2650 res = xmlValidateDocument(&vctxt, ctxt->doc);
2654 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2655 if (subset != NULL) {
2656 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2663 #endif /* LIBXML_VALID_ENABLED */
2667 * @ctxt: the shell context
2669 * @tree: a node defining a subtree
2672 * Implements the XML shell function "du"
2673 * show the structure of the subtree under node @tree
2674 * If @tree is null, the command works on the current node.
2676 * Returns 0 or -1 in case of error
2679 xmlShellDu(xmlShellCtxtPtr ctxt,
2680 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2681 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2692 while (node != NULL) {
2693 if ((node->type == XML_DOCUMENT_NODE) ||
2694 (node->type == XML_HTML_DOCUMENT_NODE)) {
2695 fprintf(ctxt->output, "/\n");
2696 } else if (node->type == XML_ELEMENT_NODE) {
2697 for (i = 0; i < indent; i++)
2698 fprintf(ctxt->output, " ");
2699 fprintf(ctxt->output, "%s\n", node->name);
2704 * Browse the full subtree, deep first
2707 if ((node->type == XML_DOCUMENT_NODE) ||
2708 (node->type == XML_HTML_DOCUMENT_NODE)) {
2709 node = ((xmlDocPtr) node)->children;
2710 } else if ((node->children != NULL)
2711 && (node->type != XML_ENTITY_REF_NODE)) {
2713 node = node->children;
2715 } else if ((node != tree) && (node->next != NULL)) {
2718 } else if (node != tree) {
2719 /* go up to parents->next if needed */
2720 while (node != tree) {
2721 if (node->parent != NULL) {
2722 node = node->parent;
2725 if ((node != tree) && (node->next != NULL)) {
2729 if (node->parent == NULL) {
2738 /* exit condition */
2749 * @ctxt: the shell context
2750 * @buffer: the output buffer
2754 * Implements the XML shell function "pwd"
2755 * Show the full path from the root to the node, if needed building
2756 * thumblers when similar elements exists at a given ancestor level.
2757 * The output is compatible with XPath commands.
2759 * Returns 0 or -1 in case of error
2762 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2763 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2767 if ((node == NULL) || (buffer == NULL))
2770 path = xmlGetNodePath(node);
2775 * This test prevents buffer overflow, because this routine
2776 * is only called by xmlShell, in which the second argument is
2778 * It is a dirty hack before a cleaner solution is found.
2779 * Documentation should mention that the second argument must
2780 * be at least 500 chars long, and could be stripped if too long.
2782 snprintf(buffer, 499, "%s", path);
2791 * @doc: the initial document
2792 * @filename: the output buffer
2793 * @input: the line reading function
2794 * @output: the output FILE*, defaults to stdout if NULL
2796 * Implements the XML shell
2797 * This allow to load, validate, view, modify and save a document
2798 * using a environment similar to a UNIX commandline.
2801 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2804 char prompt[500] = "/ > ";
2805 char *cmdline = NULL, *cur;
2809 xmlShellCtxtPtr ctxt;
2810 xmlXPathObjectPtr list;
2814 if (filename == NULL)
2820 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2825 ctxt->input = input;
2826 ctxt->output = output;
2827 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2828 ctxt->node = (xmlNodePtr) ctxt->doc;
2830 #ifdef LIBXML_XPATH_ENABLED
2831 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2832 if (ctxt->pctxt == NULL) {
2836 #endif /* LIBXML_XPATH_ENABLED */
2838 if (ctxt->node == (xmlNodePtr) ctxt->doc)
2839 snprintf(prompt, sizeof(prompt), "%s > ", "/");
2840 else if ((ctxt->node != NULL) && (ctxt->node->name))
2841 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
2843 snprintf(prompt, sizeof(prompt), "? > ");
2844 prompt[sizeof(prompt) - 1] = 0;
2847 * Get a new command line
2849 cmdline = ctxt->input(prompt);
2850 if (cmdline == NULL)
2854 * Parse the command itself
2857 while ((*cur == ' ') || (*cur == '\t'))
2860 while ((*cur != ' ') && (*cur != '\t') &&
2861 (*cur != '\n') && (*cur != '\r')) {
2864 command[i++] = *cur++;
2871 * Parse the argument
2873 while ((*cur == ' ') || (*cur == '\t'))
2876 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2884 * start interpreting the command
2886 if (!strcmp(command, "exit"))
2888 if (!strcmp(command, "quit"))
2890 if (!strcmp(command, "bye"))
2892 if (!strcmp(command, "help")) {
2893 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2894 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2895 fprintf(ctxt->output, "\tbye leave shell\n");
2896 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2897 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2898 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2899 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2900 fprintf(ctxt->output, "\texit leave shell\n");
2901 fprintf(ctxt->output, "\thelp display this help\n");
2902 fprintf(ctxt->output, "\tfree display memory usage\n");
2903 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2904 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
2905 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2906 #ifdef LIBXML_XPATH_ENABLED
2907 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2908 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2909 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2910 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2911 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
2912 #endif /* LIBXML_XPATH_ENABLED */
2913 fprintf(ctxt->output, "\tpwd display current working directory\n");
2914 fprintf(ctxt->output, "\tquit leave shell\n");
2915 #ifdef LIBXML_OUTPUT_ENABLED
2916 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
2917 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2918 #endif /* LIBXML_OUTPUT_ENABLED */
2919 #ifdef LIBXML_VALID_ENABLED
2920 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
2921 #endif /* LIBXML_VALID_ENABLED */
2922 #ifdef LIBXML_SCHEMAS_ENABLED
2923 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2925 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
2926 #ifdef LIBXML_VALID_ENABLED
2927 } else if (!strcmp(command, "validate")) {
2928 xmlShellValidate(ctxt, arg, NULL, NULL);
2929 #endif /* LIBXML_VALID_ENABLED */
2930 } else if (!strcmp(command, "load")) {
2931 xmlShellLoad(ctxt, arg, NULL, NULL);
2932 #ifdef LIBXML_SCHEMAS_ENABLED
2933 } else if (!strcmp(command, "relaxng")) {
2934 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2936 #ifdef LIBXML_OUTPUT_ENABLED
2937 } else if (!strcmp(command, "save")) {
2938 xmlShellSave(ctxt, arg, NULL, NULL);
2939 } else if (!strcmp(command, "write")) {
2940 if ((arg == NULL) || (arg[0] == 0))
2941 xmlGenericError(xmlGenericErrorContext,
2942 "Write command requires a filename argument\n");
2944 xmlShellWrite(ctxt, arg, ctxt->node, NULL);
2945 #endif /* LIBXML_OUTPUT_ENABLED */
2946 } else if (!strcmp(command, "grep")) {
2947 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2948 } else if (!strcmp(command, "free")) {
2950 xmlMemShow(ctxt->output, 0);
2954 sscanf(arg, "%d", &len);
2955 xmlMemShow(ctxt->output, len);
2957 } else if (!strcmp(command, "pwd")) {
2960 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2961 fprintf(ctxt->output, "%s\n", dir);
2962 } else if (!strcmp(command, "du")) {
2963 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2964 } else if (!strcmp(command, "base")) {
2965 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
2966 } else if (!strcmp(command, "set")) {
2967 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
2968 #ifdef LIBXML_XPATH_ENABLED
2969 } else if (!strcmp(command, "setns")) {
2971 xmlGenericError(xmlGenericErrorContext,
2972 "setns: prefix=[nsuri] required\n");
2974 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2976 } else if (!strcmp(command, "setrootns")) {
2979 root = xmlDocGetRootElement(ctxt->doc);
2980 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
2981 } else if (!strcmp(command, "xpath")) {
2983 xmlGenericError(xmlGenericErrorContext,
2984 "xpath: expression required\n");
2986 ctxt->pctxt->node = ctxt->node;
2987 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2988 xmlXPathDebugDumpObject(ctxt->output, list, 0);
2989 xmlXPathFreeObject(list);
2991 #endif /* LIBXML_XPATH_ENABLED */
2992 #ifdef LIBXML_TREE_ENABLED
2993 } else if (!strcmp(command, "setbase")) {
2994 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
2996 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2997 int dir = (!strcmp(command, "dir"));
3001 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3003 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3005 ctxt->pctxt->node = ctxt->node;
3006 #ifdef LIBXML_XPATH_ENABLED
3007 ctxt->pctxt->node = ctxt->node;
3008 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3011 #endif /* LIBXML_XPATH_ENABLED */
3013 switch (list->type) {
3014 case XPATH_UNDEFINED:
3015 xmlGenericError(xmlGenericErrorContext,
3016 "%s: no such node\n", arg);
3018 case XPATH_NODESET:{
3021 if (list->nodesetval == NULL)
3025 indx < list->nodesetval->nodeNr;
3028 xmlShellDir(ctxt, NULL,
3030 nodeTab[indx], NULL);
3032 xmlShellList(ctxt, NULL,
3034 nodeTab[indx], NULL);
3039 xmlGenericError(xmlGenericErrorContext,
3040 "%s is a Boolean\n", arg);
3043 xmlGenericError(xmlGenericErrorContext,
3044 "%s is a number\n", arg);
3047 xmlGenericError(xmlGenericErrorContext,
3048 "%s is a string\n", arg);
3051 xmlGenericError(xmlGenericErrorContext,
3052 "%s is a point\n", arg);
3055 xmlGenericError(xmlGenericErrorContext,
3056 "%s is a range\n", arg);
3058 case XPATH_LOCATIONSET:
3059 xmlGenericError(xmlGenericErrorContext,
3060 "%s is a range\n", arg);
3063 xmlGenericError(xmlGenericErrorContext,
3064 "%s is user-defined\n", arg);
3066 case XPATH_XSLT_TREE:
3067 xmlGenericError(xmlGenericErrorContext,
3068 "%s is an XSLT value tree\n",
3072 #ifdef LIBXML_XPATH_ENABLED
3073 xmlXPathFreeObject(list);
3076 xmlGenericError(xmlGenericErrorContext,
3077 "%s: no such node\n", arg);
3079 ctxt->pctxt->node = NULL;
3081 } else if (!strcmp(command, "cd")) {
3083 ctxt->node = (xmlNodePtr) ctxt->doc;
3085 #ifdef LIBXML_XPATH_ENABLED
3086 ctxt->pctxt->node = ctxt->node;
3087 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3090 #endif /* LIBXML_XPATH_ENABLED */
3092 switch (list->type) {
3093 case XPATH_UNDEFINED:
3094 xmlGenericError(xmlGenericErrorContext,
3095 "%s: no such node\n", arg);
3098 if (list->nodesetval != NULL) {
3099 if (list->nodesetval->nodeNr == 1) {
3100 ctxt->node = list->nodesetval->nodeTab[0];
3101 if ((ctxt->node != NULL) &&
3102 (ctxt->node->type ==
3103 XML_NAMESPACE_DECL)) {
3104 xmlGenericError(xmlGenericErrorContext,
3105 "cannot cd to namespace\n");
3109 xmlGenericError(xmlGenericErrorContext,
3110 "%s is a %d Node Set\n",
3112 list->nodesetval->nodeNr);
3114 xmlGenericError(xmlGenericErrorContext,
3115 "%s is an empty Node Set\n",
3119 xmlGenericError(xmlGenericErrorContext,
3120 "%s is a Boolean\n", arg);
3123 xmlGenericError(xmlGenericErrorContext,
3124 "%s is a number\n", arg);
3127 xmlGenericError(xmlGenericErrorContext,
3128 "%s is a string\n", arg);
3131 xmlGenericError(xmlGenericErrorContext,
3132 "%s is a point\n", arg);
3135 xmlGenericError(xmlGenericErrorContext,
3136 "%s is a range\n", arg);
3138 case XPATH_LOCATIONSET:
3139 xmlGenericError(xmlGenericErrorContext,
3140 "%s is a range\n", arg);
3143 xmlGenericError(xmlGenericErrorContext,
3144 "%s is user-defined\n", arg);
3146 case XPATH_XSLT_TREE:
3147 xmlGenericError(xmlGenericErrorContext,
3148 "%s is an XSLT value tree\n",
3152 #ifdef LIBXML_XPATH_ENABLED
3153 xmlXPathFreeObject(list);
3156 xmlGenericError(xmlGenericErrorContext,
3157 "%s: no such node\n", arg);
3159 ctxt->pctxt->node = NULL;
3161 #ifdef LIBXML_OUTPUT_ENABLED
3162 } else if (!strcmp(command, "cat")) {
3164 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3166 ctxt->pctxt->node = ctxt->node;
3167 #ifdef LIBXML_XPATH_ENABLED
3168 ctxt->pctxt->node = ctxt->node;
3169 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3172 #endif /* LIBXML_XPATH_ENABLED */
3174 switch (list->type) {
3175 case XPATH_UNDEFINED:
3176 xmlGenericError(xmlGenericErrorContext,
3177 "%s: no such node\n", arg);
3179 case XPATH_NODESET:{
3182 if (list->nodesetval == NULL)
3186 indx < list->nodesetval->nodeNr;
3189 fprintf(ctxt->output, " -------\n");
3190 xmlShellCat(ctxt, NULL,
3192 nodeTab[indx], NULL);
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 #endif /* LIBXML_OUTPUT_ENABLED */
3241 xmlGenericError(xmlGenericErrorContext,
3242 "Unknown command %s\n", command);
3244 free(cmdline); /* not xmlFree here ! */
3247 #ifdef LIBXML_XPATH_ENABLED
3248 xmlXPathFreeContext(ctxt->pctxt);
3249 #endif /* LIBXML_XPATH_ENABLED */
3251 xmlFreeDoc(ctxt->doc);
3253 if (ctxt->filename != NULL)
3254 xmlFree(ctxt->filename);
3256 if (cmdline != NULL)
3257 free(cmdline); /* not xmlFree here ! */
3260 #endif /* LIBXML_XPATH_ENABLED */
3261 #define bottom_debugXML
3262 #include "elfgcchack.h"
3263 #endif /* LIBXML_DEBUG_ENABLED */