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 if ((node->ns != NULL) && (node->ns->prefix != NULL))
1729 fprintf(output, "%s:", node->ns->prefix);
1730 fprintf(output, "%s", (const char *) node->name);
1733 case XML_ATTRIBUTE_NODE:
1734 if (node->name != NULL)
1735 fprintf(output, "%s", (const char *) node->name);
1738 if (node->content != NULL) {
1739 xmlDebugDumpString(output, node->content);
1742 case XML_CDATA_SECTION_NODE:
1744 case XML_ENTITY_REF_NODE:
1745 if (node->name != NULL)
1746 fprintf(output, "%s", (const char *) node->name);
1748 case XML_ENTITY_NODE:
1749 if (node->name != NULL)
1750 fprintf(output, "%s", (const char *) node->name);
1753 if (node->name != NULL)
1754 fprintf(output, "%s", (const char *) node->name);
1756 case XML_COMMENT_NODE:
1758 case XML_DOCUMENT_NODE:
1760 case XML_HTML_DOCUMENT_NODE:
1762 case XML_DOCUMENT_TYPE_NODE:
1764 case XML_DOCUMENT_FRAG_NODE:
1766 case XML_NOTATION_NODE:
1768 case XML_NAMESPACE_DECL: {
1769 xmlNsPtr ns = (xmlNsPtr) node;
1771 if (ns->prefix == NULL)
1772 fprintf(output, "default -> %s", (char *)ns->href);
1774 fprintf(output, "%s -> %s", (char *)ns->prefix,
1779 if (node->name != NULL)
1780 fprintf(output, "%s", (const char *) node->name);
1782 fprintf(output, "\n");
1787 * @boolval: a bool to turn into text
1789 * Convenient way to turn bool into text
1791 * Returns a pointer to either "True" or "False"
1794 xmlBoolToText(int boolval)
1802 #ifdef LIBXML_XPATH_ENABLED
1803 /****************************************************************
1805 * The XML shell related functions *
1807 ****************************************************************/
1812 * TODO: Improvement/cleanups for the XML shell
1813 * - allow to shell out an editor on a subpart
1814 * - cleanup function registrations (with help) and calling
1815 * - provide registration routines
1819 * xmlShellPrintXPathError:
1820 * @errorType: valid xpath error id
1821 * @arg: the argument that cause xpath to fail
1823 * Print the xpath error to libxml default error channel
1826 xmlShellPrintXPathError(int errorType, const char *arg)
1828 const char *default_arg = "Result";
1833 switch (errorType) {
1834 case XPATH_UNDEFINED:
1835 xmlGenericError(xmlGenericErrorContext,
1836 "%s: no such node\n", arg);
1840 xmlGenericError(xmlGenericErrorContext,
1841 "%s is a Boolean\n", arg);
1844 xmlGenericError(xmlGenericErrorContext,
1845 "%s is a number\n", arg);
1848 xmlGenericError(xmlGenericErrorContext,
1849 "%s is a string\n", arg);
1852 xmlGenericError(xmlGenericErrorContext,
1853 "%s is a point\n", arg);
1856 xmlGenericError(xmlGenericErrorContext,
1857 "%s is a range\n", arg);
1859 case XPATH_LOCATIONSET:
1860 xmlGenericError(xmlGenericErrorContext,
1861 "%s is a range\n", arg);
1864 xmlGenericError(xmlGenericErrorContext,
1865 "%s is user-defined\n", arg);
1867 case XPATH_XSLT_TREE:
1868 xmlGenericError(xmlGenericErrorContext,
1869 "%s is an XSLT value tree\n", arg);
1873 xmlGenericError(xmlGenericErrorContext,
1874 "Try casting the result string function (xpath builtin)\n",
1880 #ifdef LIBXML_OUTPUT_ENABLED
1882 * xmlShellPrintNodeCtxt:
1883 * @ctxt : a non-null shell context
1884 * @node : a non-null node to print to the output FILE
1886 * Print node to the output FILE
1889 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1900 if (node->type == XML_DOCUMENT_NODE)
1901 xmlDocDump(fp, (xmlDocPtr) node);
1902 else if (node->type == XML_ATTRIBUTE_NODE)
1903 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
1905 xmlElemDump(fp, node->doc, node);
1911 * xmlShellPrintNode:
1912 * @node : a non-null node to print to the output FILE
1914 * Print node to the output FILE
1917 xmlShellPrintNode(xmlNodePtr node)
1919 xmlShellPrintNodeCtxt(NULL, node);
1921 #endif /* LIBXML_OUTPUT_ENABLED */
1924 * xmlShellPrintXPathResultCtxt:
1925 * @ctxt: a valid shell context
1926 * @list: a valid result generated by an xpath evaluation
1928 * Prints result to the output FILE
1931 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
1937 switch (list->type) {
1938 case XPATH_NODESET:{
1939 #ifdef LIBXML_OUTPUT_ENABLED
1942 if (list->nodesetval) {
1943 for (indx = 0; indx < list->nodesetval->nodeNr;
1945 xmlShellPrintNodeCtxt(ctxt,
1946 list->nodesetval->nodeTab[indx]);
1949 xmlGenericError(xmlGenericErrorContext,
1950 "Empty node set\n");
1954 xmlGenericError(xmlGenericErrorContext,
1956 #endif /* LIBXML_OUTPUT_ENABLED */
1959 xmlGenericError(xmlGenericErrorContext,
1960 "Is a Boolean:%s\n",
1961 xmlBoolToText(list->boolval));
1964 xmlGenericError(xmlGenericErrorContext,
1965 "Is a number:%0g\n", list->floatval);
1968 xmlGenericError(xmlGenericErrorContext,
1969 "Is a string:%s\n", list->stringval);
1973 xmlShellPrintXPathError(list->type, NULL);
1979 * xmlShellPrintXPathResult:
1980 * @list: a valid result generated by an xpath evaluation
1982 * Prints result to the output FILE
1985 xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1987 xmlShellPrintXPathResultCtxt(NULL, list);
1992 * @ctxt: the shell context
1997 * Implements the XML shell function "ls"
1998 * Does an Unix like listing of the given node (like a directory)
2003 xmlShellList(xmlShellCtxtPtr ctxt,
2004 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2005 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2011 fprintf(ctxt->output, "NULL\n");
2014 if ((node->type == XML_DOCUMENT_NODE) ||
2015 (node->type == XML_HTML_DOCUMENT_NODE)) {
2016 cur = ((xmlDocPtr) node)->children;
2017 } else if (node->type == XML_NAMESPACE_DECL) {
2018 xmlLsOneNode(ctxt->output, node);
2020 } else if (node->children != NULL) {
2021 cur = node->children;
2023 xmlLsOneNode(ctxt->output, node);
2026 while (cur != NULL) {
2027 xmlLsOneNode(ctxt->output, cur);
2035 * @ctxt: the shell context
2040 * Implements the XML shell function "base"
2041 * dumps the current XML base of the node
2046 xmlShellBase(xmlShellCtxtPtr ctxt,
2047 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2048 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2054 fprintf(ctxt->output, "NULL\n");
2058 base = xmlNodeGetBase(node->doc, node);
2061 fprintf(ctxt->output, " No base found !!!\n");
2063 fprintf(ctxt->output, "%s\n", base);
2069 #ifdef LIBXML_TREE_ENABLED
2072 * @ctxt: the shell context
2073 * @arg: the new base
2077 * Implements the XML shell function "setbase"
2078 * change the current XML base of the node
2083 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2084 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2085 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2087 xmlNodeSetBase(node, (xmlChar*) arg);
2092 #ifdef LIBXML_XPATH_ENABLED
2094 * xmlShellRegisterNamespace:
2095 * @ctxt: the shell context
2096 * @arg: a string in prefix=nsuri format
2100 * Implements the XML shell function "setns"
2101 * register/unregister a prefix=namespace pair
2102 * on the XPath context
2104 * Returns 0 on success and a negative value otherwise.
2107 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2108 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2115 nsListDup = xmlStrdup((xmlChar *) arg);
2117 while(next != NULL) {
2119 /*while((*next) == ' ') next++;*/
2120 if((*next) == '\0') break;
2124 next = (xmlChar*)xmlStrchr(next, '=');
2126 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2134 next = (xmlChar*)xmlStrchr(next, ' ');
2139 /* do register namespace */
2140 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2141 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2151 * xmlShellRegisterRootNamespaces:
2152 * @ctxt: the shell context
2154 * @node: the root element
2157 * Implements the XML shell function "setrootns"
2158 * which registers all namespaces declarations found on the root element.
2160 * Returns 0 on success and a negative value otherwise.
2163 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2164 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2168 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2169 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2172 while (ns != NULL) {
2173 if (ns->prefix == NULL)
2174 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2176 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2185 * @ctxt: the shell context
2186 * @arg: the string or regular expression to find
2190 * Implements the XML shell function "grep"
2191 * dumps informations about the node (namespace, attributes, content).
2196 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2197 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2205 #ifdef LIBXML_REGEXP_ENABLED
2206 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2207 (xmlStrchr((xmlChar *) arg, '*')) ||
2208 (xmlStrchr((xmlChar *) arg, '.')) ||
2209 (xmlStrchr((xmlChar *) arg, '['))) {
2212 while (node != NULL) {
2213 if (node->type == XML_COMMENT_NODE) {
2214 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2216 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2217 xmlShellList(ctxt, NULL, node, NULL);
2219 } else if (node->type == XML_TEXT_NODE) {
2220 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2222 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
2223 xmlShellList(ctxt, NULL, node->parent, NULL);
2228 * Browse the full subtree, deep first
2231 if ((node->type == XML_DOCUMENT_NODE) ||
2232 (node->type == XML_HTML_DOCUMENT_NODE)) {
2233 node = ((xmlDocPtr) node)->children;
2234 } else if ((node->children != NULL)
2235 && (node->type != XML_ENTITY_REF_NODE)) {
2237 node = node->children;
2238 } else if (node->next != NULL) {
2242 /* go up to parents->next if needed */
2243 while (node != NULL) {
2244 if (node->parent != NULL) {
2245 node = node->parent;
2247 if (node->next != NULL) {
2251 if (node->parent == NULL) {
2263 * @ctxt: the shell context
2268 * Implements the XML shell function "dir"
2269 * dumps informations about the node (namespace, attributes, content).
2274 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2275 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2276 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2281 fprintf(ctxt->output, "NULL\n");
2284 if ((node->type == XML_DOCUMENT_NODE) ||
2285 (node->type == XML_HTML_DOCUMENT_NODE)) {
2286 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
2287 } else if (node->type == XML_ATTRIBUTE_NODE) {
2288 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
2290 xmlDebugDumpOneNode(ctxt->output, node, 0);
2296 * xmlShellSetContent:
2297 * @ctxt: the shell context
2298 * @value: the content as a string
2302 * Implements the XML shell function "dir"
2303 * dumps informations about the node (namespace, attributes, content).
2308 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2309 char *value, xmlNodePtr node,
2310 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2313 xmlParserErrors ret;
2318 fprintf(ctxt->output, "NULL\n");
2321 if (value == NULL) {
2322 fprintf(ctxt->output, "NULL\n");
2326 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2327 if (ret == XML_ERR_OK) {
2328 if (node->children != NULL) {
2329 xmlFreeNodeList(node->children);
2330 node->children = NULL;
2333 xmlAddChildList(node, results);
2335 fprintf(ctxt->output, "failed to parse content\n");
2340 #ifdef LIBXML_SCHEMAS_ENABLED
2342 * xmlShellRNGValidate:
2343 * @ctxt: the shell context
2344 * @schemas: the path to the Relax-NG schemas
2348 * Implements the XML shell function "relaxng"
2349 * validating the instance against a Relax-NG schemas
2354 xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2355 xmlNodePtr node ATTRIBUTE_UNUSED,
2356 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2358 xmlRelaxNGPtr relaxngschemas;
2359 xmlRelaxNGParserCtxtPtr ctxt;
2360 xmlRelaxNGValidCtxtPtr vctxt;
2363 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2364 xmlRelaxNGSetParserErrors(ctxt,
2365 (xmlRelaxNGValidityErrorFunc) fprintf,
2366 (xmlRelaxNGValidityWarningFunc) fprintf,
2368 relaxngschemas = xmlRelaxNGParse(ctxt);
2369 xmlRelaxNGFreeParserCtxt(ctxt);
2370 if (relaxngschemas == NULL) {
2371 xmlGenericError(xmlGenericErrorContext,
2372 "Relax-NG schema %s failed to compile\n", schemas);
2375 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2376 xmlRelaxNGSetValidErrors(vctxt,
2377 (xmlRelaxNGValidityErrorFunc) fprintf,
2378 (xmlRelaxNGValidityWarningFunc) fprintf,
2380 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2382 fprintf(stderr, "%s validates\n", sctxt->filename);
2383 } else if (ret > 0) {
2384 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2386 fprintf(stderr, "%s validation generated an internal error\n",
2389 xmlRelaxNGFreeValidCtxt(vctxt);
2390 if (relaxngschemas != NULL)
2391 xmlRelaxNGFree(relaxngschemas);
2396 #ifdef LIBXML_OUTPUT_ENABLED
2399 * @ctxt: the shell context
2404 * Implements the XML shell function "cat"
2405 * dumps the serialization node content (XML or HTML).
2410 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2411 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2416 fprintf(ctxt->output, "NULL\n");
2419 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2420 #ifdef LIBXML_HTML_ENABLED
2421 if (node->type == XML_HTML_DOCUMENT_NODE)
2422 htmlDocDump(ctxt->output, (htmlDocPtr) node);
2424 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
2426 if (node->type == XML_DOCUMENT_NODE)
2427 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2429 xmlElemDump(ctxt->output, ctxt->doc, node);
2430 #endif /* LIBXML_HTML_ENABLED */
2432 if (node->type == XML_DOCUMENT_NODE)
2433 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2435 xmlElemDump(ctxt->output, ctxt->doc, node);
2437 fprintf(ctxt->output, "\n");
2440 #endif /* LIBXML_OUTPUT_ENABLED */
2444 * @ctxt: the shell context
2445 * @filename: the file name
2449 * Implements the XML shell function "load"
2450 * loads a new document specified by the filename
2452 * Returns 0 or -1 if loading failed
2455 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2456 xmlNodePtr node ATTRIBUTE_UNUSED,
2457 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2462 if ((ctxt == NULL) || (filename == NULL)) return(-1);
2463 if (ctxt->doc != NULL)
2464 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2467 #ifdef LIBXML_HTML_ENABLED
2468 doc = htmlParseFile(filename, NULL);
2470 fprintf(ctxt->output, "HTML support not compiled in\n");
2472 #endif /* LIBXML_HTML_ENABLED */
2474 doc = xmlReadFile(filename,NULL,0);
2477 if (ctxt->loaded == 1) {
2478 xmlFreeDoc(ctxt->doc);
2481 #ifdef LIBXML_XPATH_ENABLED
2482 xmlXPathFreeContext(ctxt->pctxt);
2483 #endif /* LIBXML_XPATH_ENABLED */
2484 xmlFree(ctxt->filename);
2486 ctxt->node = (xmlNodePtr) doc;
2487 #ifdef LIBXML_XPATH_ENABLED
2488 ctxt->pctxt = xmlXPathNewContext(doc);
2489 #endif /* LIBXML_XPATH_ENABLED */
2490 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2496 #ifdef LIBXML_OUTPUT_ENABLED
2499 * @ctxt: the shell context
2500 * @filename: the file name
2501 * @node: a node in the tree
2504 * Implements the XML shell function "write"
2505 * Write the current node to the filename, it saves the serialization
2506 * of the subtree under the @node specified
2508 * Returns 0 or -1 in case of error
2511 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2512 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2516 if ((filename == NULL) || (filename[0] == 0)) {
2520 if (access((char *) filename, W_OK)) {
2521 xmlGenericError(xmlGenericErrorContext,
2522 "Cannot write to %s\n", filename);
2526 switch (node->type) {
2527 case XML_DOCUMENT_NODE:
2528 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2529 xmlGenericError(xmlGenericErrorContext,
2530 "Failed to write to %s\n", filename);
2534 case XML_HTML_DOCUMENT_NODE:
2535 #ifdef LIBXML_HTML_ENABLED
2536 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2537 xmlGenericError(xmlGenericErrorContext,
2538 "Failed to write to %s\n", filename);
2542 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2543 xmlGenericError(xmlGenericErrorContext,
2544 "Failed to write to %s\n", filename);
2547 #endif /* LIBXML_HTML_ENABLED */
2552 f = fopen((char *) filename, "w");
2554 xmlGenericError(xmlGenericErrorContext,
2555 "Failed to write to %s\n", filename);
2558 xmlElemDump(f, ctxt->doc, node);
2567 * @ctxt: the shell context
2568 * @filename: the file name (optional)
2572 * Implements the XML shell function "save"
2573 * Write the current document to the filename, or it's original name
2575 * Returns 0 or -1 in case of error
2578 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2579 xmlNodePtr node ATTRIBUTE_UNUSED,
2580 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2582 if ((ctxt == NULL) || (ctxt->doc == NULL))
2584 if ((filename == NULL) || (filename[0] == 0))
2585 filename = ctxt->filename;
2586 if (filename == NULL)
2589 if (access((char *) filename, W_OK)) {
2590 xmlGenericError(xmlGenericErrorContext,
2591 "Cannot save to %s\n", filename);
2595 switch (ctxt->doc->type) {
2596 case XML_DOCUMENT_NODE:
2597 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2598 xmlGenericError(xmlGenericErrorContext,
2599 "Failed to save to %s\n", filename);
2602 case XML_HTML_DOCUMENT_NODE:
2603 #ifdef LIBXML_HTML_ENABLED
2604 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2605 xmlGenericError(xmlGenericErrorContext,
2606 "Failed to save to %s\n", filename);
2609 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2610 xmlGenericError(xmlGenericErrorContext,
2611 "Failed to save to %s\n", filename);
2613 #endif /* LIBXML_HTML_ENABLED */
2616 xmlGenericError(xmlGenericErrorContext,
2617 "To save to subparts of a document use the 'write' command\n");
2623 #endif /* LIBXML_OUTPUT_ENABLED */
2625 #ifdef LIBXML_VALID_ENABLED
2628 * @ctxt: the shell context
2629 * @dtd: the DTD URI (optional)
2633 * Implements the XML shell function "validate"
2634 * Validate the document, if a DTD path is provided, then the validation
2635 * is done against the given DTD.
2637 * Returns 0 or -1 in case of error
2640 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2641 xmlNodePtr node ATTRIBUTE_UNUSED,
2642 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2647 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2648 vctxt.userData = stderr;
2649 vctxt.error = (xmlValidityErrorFunc) fprintf;
2650 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2652 if ((dtd == NULL) || (dtd[0] == 0)) {
2653 res = xmlValidateDocument(&vctxt, ctxt->doc);
2657 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2658 if (subset != NULL) {
2659 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2666 #endif /* LIBXML_VALID_ENABLED */
2670 * @ctxt: the shell context
2672 * @tree: a node defining a subtree
2675 * Implements the XML shell function "du"
2676 * show the structure of the subtree under node @tree
2677 * If @tree is null, the command works on the current node.
2679 * Returns 0 or -1 in case of error
2682 xmlShellDu(xmlShellCtxtPtr ctxt,
2683 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2684 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2695 while (node != NULL) {
2696 if ((node->type == XML_DOCUMENT_NODE) ||
2697 (node->type == XML_HTML_DOCUMENT_NODE)) {
2698 fprintf(ctxt->output, "/\n");
2699 } else if (node->type == XML_ELEMENT_NODE) {
2700 for (i = 0; i < indent; i++)
2701 fprintf(ctxt->output, " ");
2702 if ((node->ns) && (node->ns->prefix))
2703 fprintf(ctxt->output, "%s:", node->ns->prefix);
2704 fprintf(ctxt->output, "%s\n", node->name);
2709 * Browse the full subtree, deep first
2712 if ((node->type == XML_DOCUMENT_NODE) ||
2713 (node->type == XML_HTML_DOCUMENT_NODE)) {
2714 node = ((xmlDocPtr) node)->children;
2715 } else if ((node->children != NULL)
2716 && (node->type != XML_ENTITY_REF_NODE)) {
2718 node = node->children;
2720 } else if ((node != tree) && (node->next != NULL)) {
2723 } else if (node != tree) {
2724 /* go up to parents->next if needed */
2725 while (node != tree) {
2726 if (node->parent != NULL) {
2727 node = node->parent;
2730 if ((node != tree) && (node->next != NULL)) {
2734 if (node->parent == NULL) {
2743 /* exit condition */
2754 * @ctxt: the shell context
2755 * @buffer: the output buffer
2759 * Implements the XML shell function "pwd"
2760 * Show the full path from the root to the node, if needed building
2761 * thumblers when similar elements exists at a given ancestor level.
2762 * The output is compatible with XPath commands.
2764 * Returns 0 or -1 in case of error
2767 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2768 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2772 if ((node == NULL) || (buffer == NULL))
2775 path = xmlGetNodePath(node);
2780 * This test prevents buffer overflow, because this routine
2781 * is only called by xmlShell, in which the second argument is
2783 * It is a dirty hack before a cleaner solution is found.
2784 * Documentation should mention that the second argument must
2785 * be at least 500 chars long, and could be stripped if too long.
2787 snprintf(buffer, 499, "%s", path);
2796 * @doc: the initial document
2797 * @filename: the output buffer
2798 * @input: the line reading function
2799 * @output: the output FILE*, defaults to stdout if NULL
2801 * Implements the XML shell
2802 * This allow to load, validate, view, modify and save a document
2803 * using a environment similar to a UNIX commandline.
2806 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2809 char prompt[500] = "/ > ";
2810 char *cmdline = NULL, *cur;
2814 xmlShellCtxtPtr ctxt;
2815 xmlXPathObjectPtr list;
2819 if (filename == NULL)
2825 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2830 ctxt->input = input;
2831 ctxt->output = output;
2832 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2833 ctxt->node = (xmlNodePtr) ctxt->doc;
2835 #ifdef LIBXML_XPATH_ENABLED
2836 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2837 if (ctxt->pctxt == NULL) {
2841 #endif /* LIBXML_XPATH_ENABLED */
2843 if (ctxt->node == (xmlNodePtr) ctxt->doc)
2844 snprintf(prompt, sizeof(prompt), "%s > ", "/");
2845 else if ((ctxt->node != NULL) && (ctxt->node->name) &&
2846 (ctxt->node->ns) && (ctxt->node->ns->prefix))
2847 snprintf(prompt, sizeof(prompt), "%s:%s > ",
2848 (ctxt->node->ns->prefix), ctxt->node->name);
2849 else if ((ctxt->node != NULL) && (ctxt->node->name))
2850 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
2852 snprintf(prompt, sizeof(prompt), "? > ");
2853 prompt[sizeof(prompt) - 1] = 0;
2856 * Get a new command line
2858 cmdline = ctxt->input(prompt);
2859 if (cmdline == NULL)
2863 * Parse the command itself
2866 while ((*cur == ' ') || (*cur == '\t'))
2869 while ((*cur != ' ') && (*cur != '\t') &&
2870 (*cur != '\n') && (*cur != '\r')) {
2873 command[i++] = *cur++;
2880 * Parse the argument
2882 while ((*cur == ' ') || (*cur == '\t'))
2885 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2893 * start interpreting the command
2895 if (!strcmp(command, "exit"))
2897 if (!strcmp(command, "quit"))
2899 if (!strcmp(command, "bye"))
2901 if (!strcmp(command, "help")) {
2902 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2903 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2904 fprintf(ctxt->output, "\tbye leave shell\n");
2905 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2906 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2907 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2908 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2909 fprintf(ctxt->output, "\texit leave shell\n");
2910 fprintf(ctxt->output, "\thelp display this help\n");
2911 fprintf(ctxt->output, "\tfree display memory usage\n");
2912 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2913 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
2914 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2915 #ifdef LIBXML_XPATH_ENABLED
2916 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2917 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2918 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2919 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2920 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
2921 #endif /* LIBXML_XPATH_ENABLED */
2922 fprintf(ctxt->output, "\tpwd display current working directory\n");
2923 fprintf(ctxt->output, "\twhereis display absolute path of [path] or current working directory\n");
2924 fprintf(ctxt->output, "\tquit leave shell\n");
2925 #ifdef LIBXML_OUTPUT_ENABLED
2926 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
2927 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2928 #endif /* LIBXML_OUTPUT_ENABLED */
2929 #ifdef LIBXML_VALID_ENABLED
2930 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
2931 #endif /* LIBXML_VALID_ENABLED */
2932 #ifdef LIBXML_SCHEMAS_ENABLED
2933 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2935 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
2936 #ifdef LIBXML_VALID_ENABLED
2937 } else if (!strcmp(command, "validate")) {
2938 xmlShellValidate(ctxt, arg, NULL, NULL);
2939 #endif /* LIBXML_VALID_ENABLED */
2940 } else if (!strcmp(command, "load")) {
2941 xmlShellLoad(ctxt, arg, NULL, NULL);
2942 #ifdef LIBXML_SCHEMAS_ENABLED
2943 } else if (!strcmp(command, "relaxng")) {
2944 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2946 #ifdef LIBXML_OUTPUT_ENABLED
2947 } else if (!strcmp(command, "save")) {
2948 xmlShellSave(ctxt, arg, NULL, NULL);
2949 } else if (!strcmp(command, "write")) {
2950 if ((arg == NULL) || (arg[0] == 0))
2951 xmlGenericError(xmlGenericErrorContext,
2952 "Write command requires a filename argument\n");
2954 xmlShellWrite(ctxt, arg, ctxt->node, NULL);
2955 #endif /* LIBXML_OUTPUT_ENABLED */
2956 } else if (!strcmp(command, "grep")) {
2957 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2958 } else if (!strcmp(command, "free")) {
2960 xmlMemShow(ctxt->output, 0);
2964 sscanf(arg, "%d", &len);
2965 xmlMemShow(ctxt->output, len);
2967 } else if (!strcmp(command, "pwd")) {
2970 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2971 fprintf(ctxt->output, "%s\n", dir);
2972 } else if (!strcmp(command, "du")) {
2974 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2976 ctxt->pctxt->node = ctxt->node;
2977 #ifdef LIBXML_XPATH_ENABLED
2978 ctxt->pctxt->node = ctxt->node;
2979 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2982 #endif /* LIBXML_XPATH_ENABLED */
2984 switch (list->type) {
2985 case XPATH_UNDEFINED:
2986 xmlGenericError(xmlGenericErrorContext,
2987 "%s: no such node\n", arg);
2989 case XPATH_NODESET:{
2992 if (list->nodesetval == NULL)
2996 indx < list->nodesetval->nodeNr;
2998 xmlShellDu(ctxt, NULL,
3000 nodeTab[indx], NULL);
3004 xmlGenericError(xmlGenericErrorContext,
3005 "%s is a Boolean\n", arg);
3008 xmlGenericError(xmlGenericErrorContext,
3009 "%s is a number\n", arg);
3012 xmlGenericError(xmlGenericErrorContext,
3013 "%s is a string\n", arg);
3016 xmlGenericError(xmlGenericErrorContext,
3017 "%s is a point\n", arg);
3020 xmlGenericError(xmlGenericErrorContext,
3021 "%s is a range\n", arg);
3023 case XPATH_LOCATIONSET:
3024 xmlGenericError(xmlGenericErrorContext,
3025 "%s is a range\n", arg);
3028 xmlGenericError(xmlGenericErrorContext,
3029 "%s is user-defined\n", arg);
3031 case XPATH_XSLT_TREE:
3032 xmlGenericError(xmlGenericErrorContext,
3033 "%s is an XSLT value tree\n",
3037 #ifdef LIBXML_XPATH_ENABLED
3038 xmlXPathFreeObject(list);
3041 xmlGenericError(xmlGenericErrorContext,
3042 "%s: no such node\n", arg);
3044 ctxt->pctxt->node = NULL;
3046 } else if (!strcmp(command, "base")) {
3047 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
3048 } else if (!strcmp(command, "set")) {
3049 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
3050 #ifdef LIBXML_XPATH_ENABLED
3051 } else if (!strcmp(command, "setns")) {
3053 xmlGenericError(xmlGenericErrorContext,
3054 "setns: prefix=[nsuri] required\n");
3056 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
3058 } else if (!strcmp(command, "setrootns")) {
3061 root = xmlDocGetRootElement(ctxt->doc);
3062 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
3063 } else if (!strcmp(command, "xpath")) {
3065 xmlGenericError(xmlGenericErrorContext,
3066 "xpath: expression required\n");
3068 ctxt->pctxt->node = ctxt->node;
3069 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3070 xmlXPathDebugDumpObject(ctxt->output, list, 0);
3071 xmlXPathFreeObject(list);
3073 #endif /* LIBXML_XPATH_ENABLED */
3074 #ifdef LIBXML_TREE_ENABLED
3075 } else if (!strcmp(command, "setbase")) {
3076 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
3078 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3079 int dir = (!strcmp(command, "dir"));
3083 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3085 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3087 ctxt->pctxt->node = ctxt->node;
3088 #ifdef LIBXML_XPATH_ENABLED
3089 ctxt->pctxt->node = ctxt->node;
3090 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3093 #endif /* LIBXML_XPATH_ENABLED */
3095 switch (list->type) {
3096 case XPATH_UNDEFINED:
3097 xmlGenericError(xmlGenericErrorContext,
3098 "%s: no such node\n", arg);
3100 case XPATH_NODESET:{
3103 if (list->nodesetval == NULL)
3107 indx < list->nodesetval->nodeNr;
3110 xmlShellDir(ctxt, NULL,
3112 nodeTab[indx], NULL);
3114 xmlShellList(ctxt, NULL,
3116 nodeTab[indx], NULL);
3121 xmlGenericError(xmlGenericErrorContext,
3122 "%s is a Boolean\n", arg);
3125 xmlGenericError(xmlGenericErrorContext,
3126 "%s is a number\n", arg);
3129 xmlGenericError(xmlGenericErrorContext,
3130 "%s is a string\n", arg);
3133 xmlGenericError(xmlGenericErrorContext,
3134 "%s is a point\n", arg);
3137 xmlGenericError(xmlGenericErrorContext,
3138 "%s is a range\n", arg);
3140 case XPATH_LOCATIONSET:
3141 xmlGenericError(xmlGenericErrorContext,
3142 "%s is a range\n", arg);
3145 xmlGenericError(xmlGenericErrorContext,
3146 "%s is user-defined\n", arg);
3148 case XPATH_XSLT_TREE:
3149 xmlGenericError(xmlGenericErrorContext,
3150 "%s is an XSLT value tree\n",
3154 #ifdef LIBXML_XPATH_ENABLED
3155 xmlXPathFreeObject(list);
3158 xmlGenericError(xmlGenericErrorContext,
3159 "%s: no such node\n", arg);
3161 ctxt->pctxt->node = NULL;
3163 } else if (!strcmp(command, "whereis")) {
3167 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
3168 fprintf(ctxt->output, "%s\n", dir);
3170 ctxt->pctxt->node = ctxt->node;
3171 #ifdef LIBXML_XPATH_ENABLED
3172 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3175 #endif /* LIBXML_XPATH_ENABLED */
3177 switch (list->type) {
3178 case XPATH_UNDEFINED:
3179 xmlGenericError(xmlGenericErrorContext,
3180 "%s: no such node\n", arg);
3182 case XPATH_NODESET:{
3185 if (list->nodesetval == NULL)
3189 indx < list->nodesetval->nodeNr;
3191 if (!xmlShellPwd(ctxt, dir, list->nodesetval->
3192 nodeTab[indx], NULL))
3193 fprintf(ctxt->output, "%s\n", dir);
3198 xmlGenericError(xmlGenericErrorContext,
3199 "%s is a Boolean\n", arg);
3202 xmlGenericError(xmlGenericErrorContext,
3203 "%s is a number\n", arg);
3206 xmlGenericError(xmlGenericErrorContext,
3207 "%s is a string\n", arg);
3210 xmlGenericError(xmlGenericErrorContext,
3211 "%s is a point\n", arg);
3214 xmlGenericError(xmlGenericErrorContext,
3215 "%s is a range\n", arg);
3217 case XPATH_LOCATIONSET:
3218 xmlGenericError(xmlGenericErrorContext,
3219 "%s is a range\n", arg);
3222 xmlGenericError(xmlGenericErrorContext,
3223 "%s is user-defined\n", arg);
3225 case XPATH_XSLT_TREE:
3226 xmlGenericError(xmlGenericErrorContext,
3227 "%s is an XSLT value tree\n",
3231 #ifdef LIBXML_XPATH_ENABLED
3232 xmlXPathFreeObject(list);
3235 xmlGenericError(xmlGenericErrorContext,
3236 "%s: no such node\n", arg);
3238 ctxt->pctxt->node = NULL;
3240 } else if (!strcmp(command, "cd")) {
3242 ctxt->node = (xmlNodePtr) ctxt->doc;
3244 #ifdef LIBXML_XPATH_ENABLED
3245 ctxt->pctxt->node = ctxt->node;
3246 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3249 #endif /* LIBXML_XPATH_ENABLED */
3251 switch (list->type) {
3252 case XPATH_UNDEFINED:
3253 xmlGenericError(xmlGenericErrorContext,
3254 "%s: no such node\n", arg);
3257 if (list->nodesetval != NULL) {
3258 if (list->nodesetval->nodeNr == 1) {
3259 ctxt->node = list->nodesetval->nodeTab[0];
3260 if ((ctxt->node != NULL) &&
3261 (ctxt->node->type ==
3262 XML_NAMESPACE_DECL)) {
3263 xmlGenericError(xmlGenericErrorContext,
3264 "cannot cd to namespace\n");
3268 xmlGenericError(xmlGenericErrorContext,
3269 "%s is a %d Node Set\n",
3271 list->nodesetval->nodeNr);
3273 xmlGenericError(xmlGenericErrorContext,
3274 "%s is an empty Node Set\n",
3278 xmlGenericError(xmlGenericErrorContext,
3279 "%s is a Boolean\n", arg);
3282 xmlGenericError(xmlGenericErrorContext,
3283 "%s is a number\n", arg);
3286 xmlGenericError(xmlGenericErrorContext,
3287 "%s is a string\n", arg);
3290 xmlGenericError(xmlGenericErrorContext,
3291 "%s is a point\n", arg);
3294 xmlGenericError(xmlGenericErrorContext,
3295 "%s is a range\n", arg);
3297 case XPATH_LOCATIONSET:
3298 xmlGenericError(xmlGenericErrorContext,
3299 "%s is a range\n", arg);
3302 xmlGenericError(xmlGenericErrorContext,
3303 "%s is user-defined\n", arg);
3305 case XPATH_XSLT_TREE:
3306 xmlGenericError(xmlGenericErrorContext,
3307 "%s is an XSLT value tree\n",
3311 #ifdef LIBXML_XPATH_ENABLED
3312 xmlXPathFreeObject(list);
3315 xmlGenericError(xmlGenericErrorContext,
3316 "%s: no such node\n", arg);
3318 ctxt->pctxt->node = NULL;
3320 #ifdef LIBXML_OUTPUT_ENABLED
3321 } else if (!strcmp(command, "cat")) {
3323 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3325 ctxt->pctxt->node = ctxt->node;
3326 #ifdef LIBXML_XPATH_ENABLED
3327 ctxt->pctxt->node = ctxt->node;
3328 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3331 #endif /* LIBXML_XPATH_ENABLED */
3333 switch (list->type) {
3334 case XPATH_UNDEFINED:
3335 xmlGenericError(xmlGenericErrorContext,
3336 "%s: no such node\n", arg);
3338 case XPATH_NODESET:{
3341 if (list->nodesetval == NULL)
3345 indx < list->nodesetval->nodeNr;
3348 fprintf(ctxt->output, " -------\n");
3349 xmlShellCat(ctxt, NULL,
3351 nodeTab[indx], NULL);
3356 xmlGenericError(xmlGenericErrorContext,
3357 "%s is a Boolean\n", arg);
3360 xmlGenericError(xmlGenericErrorContext,
3361 "%s is a number\n", arg);
3364 xmlGenericError(xmlGenericErrorContext,
3365 "%s is a string\n", arg);
3368 xmlGenericError(xmlGenericErrorContext,
3369 "%s is a point\n", arg);
3372 xmlGenericError(xmlGenericErrorContext,
3373 "%s is a range\n", arg);
3375 case XPATH_LOCATIONSET:
3376 xmlGenericError(xmlGenericErrorContext,
3377 "%s is a range\n", arg);
3380 xmlGenericError(xmlGenericErrorContext,
3381 "%s is user-defined\n", arg);
3383 case XPATH_XSLT_TREE:
3384 xmlGenericError(xmlGenericErrorContext,
3385 "%s is an XSLT value tree\n",
3389 #ifdef LIBXML_XPATH_ENABLED
3390 xmlXPathFreeObject(list);
3393 xmlGenericError(xmlGenericErrorContext,
3394 "%s: no such node\n", arg);
3396 ctxt->pctxt->node = NULL;
3398 #endif /* LIBXML_OUTPUT_ENABLED */
3400 xmlGenericError(xmlGenericErrorContext,
3401 "Unknown command %s\n", command);
3403 free(cmdline); /* not xmlFree here ! */
3406 #ifdef LIBXML_XPATH_ENABLED
3407 xmlXPathFreeContext(ctxt->pctxt);
3408 #endif /* LIBXML_XPATH_ENABLED */
3410 xmlFreeDoc(ctxt->doc);
3412 if (ctxt->filename != NULL)
3413 xmlFree(ctxt->filename);
3415 if (cmdline != NULL)
3416 free(cmdline); /* not xmlFree here ! */
3419 #endif /* LIBXML_XPATH_ENABLED */
3420 #define bottom_debugXML
3421 #include "elfgcchack.h"
3422 #endif /* LIBXML_DEBUG_ENABLED */