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 dictionary */
48 int check; /* do just checkings */
49 int errors; /* number of errors found */
50 int nodict; /* if the document has no dictionary */
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,
167 static void LIBXML_ATTR_FORMAT(3,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,
177 static void LIBXML_ATTR_FORMAT(3,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 dictionary 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 defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
258 if (xmlValidateName(name, 0)) {
259 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
260 "Name is not an NCName '%s'", (const char *) name);
263 if ((ctxt->dict != NULL) &&
264 (!xmlDictOwns(ctxt->dict, name)) &&
265 ((ctxt->doc == NULL) ||
266 ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
267 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
268 "Name is not from the document dictionary '%s'",
269 (const char *) name);
275 xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
281 if (node->parent == NULL)
282 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
283 "Node has no parent\n");
284 if (node->doc == NULL) {
285 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
286 "Node has no doc\n");
290 if ((dict == NULL) && (ctxt->nodict == 0)) {
292 /* desactivated right now as it raises too many errors */
293 if (doc->type == XML_DOCUMENT_NODE)
294 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
295 "Document has no dictionary\n");
299 if (ctxt->doc == NULL)
302 if (ctxt->dict == NULL) {
306 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
307 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
308 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
309 "Node doc differs from parent's one\n");
310 if (node->prev == NULL) {
311 if (node->type == XML_ATTRIBUTE_NODE) {
312 if ((node->parent != NULL) &&
313 (node != (xmlNodePtr) node->parent->properties))
314 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
315 "Attr has no prev and not first of attr list\n");
317 } else if ((node->parent != NULL) && (node->parent->children != node))
318 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
319 "Node has no prev and not first of parent list\n");
321 if (node->prev->next != node)
322 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
323 "Node prev->next : back link wrong\n");
325 if (node->next == NULL) {
326 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
327 (node->parent->last != node) &&
328 (node->parent->type == XML_ELEMENT_NODE))
329 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
330 "Node has no next and not last of parent list\n");
332 if (node->next->prev != node)
333 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
334 "Node next->prev : forward link wrong\n");
335 if (node->next->parent != node->parent)
336 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
337 "Node next->prev : forward link wrong\n");
339 if (node->type == XML_ELEMENT_NODE) {
344 xmlCtxtNsCheckScope(ctxt, node, ns);
347 if (node->ns != NULL)
348 xmlCtxtNsCheckScope(ctxt, node, node->ns);
349 } else if (node->type == XML_ATTRIBUTE_NODE) {
350 if (node->ns != NULL)
351 xmlCtxtNsCheckScope(ctxt, node, node->ns);
354 if ((node->type != XML_ELEMENT_NODE) &&
355 (node->type != XML_ATTRIBUTE_NODE) &&
356 (node->type != XML_ELEMENT_DECL) &&
357 (node->type != XML_ATTRIBUTE_DECL) &&
358 (node->type != XML_DTD_NODE) &&
359 (node->type != XML_HTML_DOCUMENT_NODE) &&
360 (node->type != XML_DOCUMENT_NODE)) {
361 if (node->content != NULL)
362 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
364 switch (node->type) {
365 case XML_ELEMENT_NODE:
366 case XML_ATTRIBUTE_NODE:
367 xmlCtxtCheckName(ctxt, node->name);
370 if ((node->name == xmlStringText) ||
371 (node->name == xmlStringTextNoenc))
373 /* some case of entity substitution can lead to this */
374 if ((ctxt->dict != NULL) &&
375 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
379 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
380 "Text node has wrong name '%s'",
381 (const char *) node->name);
383 case XML_COMMENT_NODE:
384 if (node->name == xmlStringComment)
386 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
387 "Comment node has wrong name '%s'",
388 (const char *) node->name);
391 xmlCtxtCheckName(ctxt, node->name);
393 case XML_CDATA_SECTION_NODE:
394 if (node->name == NULL)
396 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
397 "CData section has non NULL name '%s'",
398 (const char *) node->name);
400 case XML_ENTITY_REF_NODE:
401 case XML_ENTITY_NODE:
402 case XML_DOCUMENT_TYPE_NODE:
403 case XML_DOCUMENT_FRAG_NODE:
404 case XML_NOTATION_NODE:
406 case XML_ELEMENT_DECL:
407 case XML_ATTRIBUTE_DECL:
408 case XML_ENTITY_DECL:
409 case XML_NAMESPACE_DECL:
410 case XML_XINCLUDE_START:
411 case XML_XINCLUDE_END:
412 #ifdef LIBXML_DOCB_ENABLED
413 case XML_DOCB_DOCUMENT_NODE:
415 case XML_DOCUMENT_NODE:
416 case XML_HTML_DOCUMENT_NODE:
422 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
429 /* TODO: check UTF8 content of the string */
431 fprintf(ctxt->output, "(NULL)");
434 for (i = 0; i < 40; i++)
437 else if (IS_BLANK_CH(str[i]))
438 fputc(' ', ctxt->output);
439 else if (str[i] >= 0x80)
440 fprintf(ctxt->output, "#%X", str[i]);
442 fputc(str[i], ctxt->output);
443 fprintf(ctxt->output, "...");
447 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
449 xmlCtxtDumpSpaces(ctxt);
453 fprintf(ctxt->output, "DTD node is NULL\n");
457 if (dtd->type != XML_DTD_NODE) {
458 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
459 "Node is not a DTD");
463 if (dtd->name != NULL)
464 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
466 fprintf(ctxt->output, "DTD");
467 if (dtd->ExternalID != NULL)
468 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
469 if (dtd->SystemID != NULL)
470 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
471 fprintf(ctxt->output, "\n");
474 * Do a bit of checking
476 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
480 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
482 xmlCtxtDumpSpaces(ctxt);
486 fprintf(ctxt->output, "Attribute declaration is NULL\n");
489 if (attr->type != XML_ATTRIBUTE_DECL) {
490 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
491 "Node is not an attribute declaration");
494 if (attr->name != NULL) {
496 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
498 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
499 "Node attribute declaration has no name");
500 if (attr->elem != NULL) {
502 fprintf(ctxt->output, " for %s", (char *) attr->elem);
504 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
505 "Node attribute declaration has no element name");
507 switch (attr->atype) {
508 case XML_ATTRIBUTE_CDATA:
509 fprintf(ctxt->output, " CDATA");
511 case XML_ATTRIBUTE_ID:
512 fprintf(ctxt->output, " ID");
514 case XML_ATTRIBUTE_IDREF:
515 fprintf(ctxt->output, " IDREF");
517 case XML_ATTRIBUTE_IDREFS:
518 fprintf(ctxt->output, " IDREFS");
520 case XML_ATTRIBUTE_ENTITY:
521 fprintf(ctxt->output, " ENTITY");
523 case XML_ATTRIBUTE_ENTITIES:
524 fprintf(ctxt->output, " ENTITIES");
526 case XML_ATTRIBUTE_NMTOKEN:
527 fprintf(ctxt->output, " NMTOKEN");
529 case XML_ATTRIBUTE_NMTOKENS:
530 fprintf(ctxt->output, " NMTOKENS");
532 case XML_ATTRIBUTE_ENUMERATION:
533 fprintf(ctxt->output, " ENUMERATION");
535 case XML_ATTRIBUTE_NOTATION:
536 fprintf(ctxt->output, " NOTATION ");
539 if (attr->tree != NULL) {
541 xmlEnumerationPtr cur = attr->tree;
543 for (indx = 0; indx < 5; indx++) {
545 fprintf(ctxt->output, "|%s", (char *) cur->name);
547 fprintf(ctxt->output, " (%s", (char *) cur->name);
553 fprintf(ctxt->output, ")");
555 fprintf(ctxt->output, "...)");
558 case XML_ATTRIBUTE_NONE:
560 case XML_ATTRIBUTE_REQUIRED:
561 fprintf(ctxt->output, " REQUIRED");
563 case XML_ATTRIBUTE_IMPLIED:
564 fprintf(ctxt->output, " IMPLIED");
566 case XML_ATTRIBUTE_FIXED:
567 fprintf(ctxt->output, " FIXED");
570 if (attr->defaultValue != NULL) {
571 fprintf(ctxt->output, "\"");
572 xmlCtxtDumpString(ctxt, attr->defaultValue);
573 fprintf(ctxt->output, "\"");
575 fprintf(ctxt->output, "\n");
579 * Do a bit of checking
581 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
585 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
587 xmlCtxtDumpSpaces(ctxt);
591 fprintf(ctxt->output, "Element declaration is NULL\n");
594 if (elem->type != XML_ELEMENT_DECL) {
595 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
596 "Node is not an element declaration");
599 if (elem->name != NULL) {
601 fprintf(ctxt->output, "ELEMDECL(");
602 xmlCtxtDumpString(ctxt, elem->name);
603 fprintf(ctxt->output, ")");
606 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
607 "Element declaration has no name");
609 switch (elem->etype) {
610 case XML_ELEMENT_TYPE_UNDEFINED:
611 fprintf(ctxt->output, ", UNDEFINED");
613 case XML_ELEMENT_TYPE_EMPTY:
614 fprintf(ctxt->output, ", EMPTY");
616 case XML_ELEMENT_TYPE_ANY:
617 fprintf(ctxt->output, ", ANY");
619 case XML_ELEMENT_TYPE_MIXED:
620 fprintf(ctxt->output, ", MIXED ");
622 case XML_ELEMENT_TYPE_ELEMENT:
623 fprintf(ctxt->output, ", MIXED ");
626 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
630 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
632 fprintf(ctxt->output, "%s", buf);
634 fprintf(ctxt->output, "\n");
638 * Do a bit of checking
640 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
644 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
646 xmlCtxtDumpSpaces(ctxt);
650 fprintf(ctxt->output, "Entity declaration is NULL\n");
653 if (ent->type != XML_ENTITY_DECL) {
654 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
655 "Node is not an entity declaration");
658 if (ent->name != NULL) {
660 fprintf(ctxt->output, "ENTITYDECL(");
661 xmlCtxtDumpString(ctxt, ent->name);
662 fprintf(ctxt->output, ")");
665 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
666 "Entity declaration has no name");
668 switch (ent->etype) {
669 case XML_INTERNAL_GENERAL_ENTITY:
670 fprintf(ctxt->output, ", internal\n");
672 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
673 fprintf(ctxt->output, ", external parsed\n");
675 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
676 fprintf(ctxt->output, ", unparsed\n");
678 case XML_INTERNAL_PARAMETER_ENTITY:
679 fprintf(ctxt->output, ", parameter\n");
681 case XML_EXTERNAL_PARAMETER_ENTITY:
682 fprintf(ctxt->output, ", external parameter\n");
684 case XML_INTERNAL_PREDEFINED_ENTITY:
685 fprintf(ctxt->output, ", predefined\n");
688 if (ent->ExternalID) {
689 xmlCtxtDumpSpaces(ctxt);
690 fprintf(ctxt->output, " ExternalID=%s\n",
691 (char *) ent->ExternalID);
694 xmlCtxtDumpSpaces(ctxt);
695 fprintf(ctxt->output, " SystemID=%s\n",
696 (char *) ent->SystemID);
698 if (ent->URI != NULL) {
699 xmlCtxtDumpSpaces(ctxt);
700 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
703 xmlCtxtDumpSpaces(ctxt);
704 fprintf(ctxt->output, " content=");
705 xmlCtxtDumpString(ctxt, ent->content);
706 fprintf(ctxt->output, "\n");
711 * Do a bit of checking
713 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
717 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
719 xmlCtxtDumpSpaces(ctxt);
723 fprintf(ctxt->output, "namespace node is NULL\n");
726 if (ns->type != XML_NAMESPACE_DECL) {
727 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
728 "Node is not a namespace declaration");
731 if (ns->href == NULL) {
732 if (ns->prefix != NULL)
733 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
734 "Incomplete namespace %s href=NULL\n",
735 (char *) ns->prefix);
737 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
738 "Incomplete default namespace href=NULL\n");
741 if (ns->prefix != NULL)
742 fprintf(ctxt->output, "namespace %s href=",
743 (char *) ns->prefix);
745 fprintf(ctxt->output, "default namespace href=");
747 xmlCtxtDumpString(ctxt, ns->href);
748 fprintf(ctxt->output, "\n");
754 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
757 xmlCtxtDumpNamespace(ctxt, ns);
763 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
765 xmlCtxtDumpSpaces(ctxt);
769 fprintf(ctxt->output, "Entity is NULL\n");
773 switch (ent->etype) {
774 case XML_INTERNAL_GENERAL_ENTITY:
775 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
777 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
778 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
780 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
781 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
783 case XML_INTERNAL_PARAMETER_ENTITY:
784 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
786 case XML_EXTERNAL_PARAMETER_ENTITY:
787 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
790 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
792 fprintf(ctxt->output, "%s\n", ent->name);
793 if (ent->ExternalID) {
794 xmlCtxtDumpSpaces(ctxt);
795 fprintf(ctxt->output, "ExternalID=%s\n",
796 (char *) ent->ExternalID);
799 xmlCtxtDumpSpaces(ctxt);
800 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
803 xmlCtxtDumpSpaces(ctxt);
804 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
807 xmlCtxtDumpSpaces(ctxt);
808 fprintf(ctxt->output, "content=");
809 xmlCtxtDumpString(ctxt, ent->content);
810 fprintf(ctxt->output, "\n");
817 * @output: the FILE * for the output
818 * @attr: the attribute
819 * @depth: the indentation level.
821 * Dumps debug information for the attribute
824 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
826 xmlCtxtDumpSpaces(ctxt);
830 fprintf(ctxt->output, "Attr is NULL");
834 fprintf(ctxt->output, "ATTRIBUTE ");
835 xmlCtxtDumpString(ctxt, attr->name);
836 fprintf(ctxt->output, "\n");
837 if (attr->children != NULL) {
839 xmlCtxtDumpNodeList(ctxt, attr->children);
843 if (attr->name == NULL)
844 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
845 "Attribute has no name");
848 * Do a bit of checking
850 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
854 * xmlCtxtDumpAttrList:
855 * @output: the FILE * for the output
856 * @attr: the attribute list
857 * @depth: the indentation level.
859 * Dumps debug information for the attribute list
862 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
864 while (attr != NULL) {
865 xmlCtxtDumpAttr(ctxt, attr);
871 * xmlCtxtDumpOneNode:
872 * @output: the FILE * for the output
874 * @depth: the indentation level.
876 * Dumps debug information for the element node, it is not recursive
879 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
883 xmlCtxtDumpSpaces(ctxt);
884 fprintf(ctxt->output, "node is NULL\n");
890 switch (node->type) {
891 case XML_ELEMENT_NODE:
893 xmlCtxtDumpSpaces(ctxt);
894 fprintf(ctxt->output, "ELEMENT ");
895 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
896 xmlCtxtDumpString(ctxt, node->ns->prefix);
897 fprintf(ctxt->output, ":");
899 xmlCtxtDumpString(ctxt, node->name);
900 fprintf(ctxt->output, "\n");
903 case XML_ATTRIBUTE_NODE:
905 xmlCtxtDumpSpaces(ctxt);
906 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
907 xmlCtxtGenericNodeCheck(ctxt, node);
911 xmlCtxtDumpSpaces(ctxt);
912 if (node->name == (const xmlChar *) xmlStringTextNoenc)
913 fprintf(ctxt->output, "TEXT no enc");
915 fprintf(ctxt->output, "TEXT");
916 if (ctxt->options & DUMP_TEXT_TYPE) {
917 if (node->content == (xmlChar *) &(node->properties))
918 fprintf(ctxt->output, " compact\n");
919 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
920 fprintf(ctxt->output, " interned\n");
922 fprintf(ctxt->output, "\n");
924 fprintf(ctxt->output, "\n");
927 case XML_CDATA_SECTION_NODE:
929 xmlCtxtDumpSpaces(ctxt);
930 fprintf(ctxt->output, "CDATA_SECTION\n");
933 case XML_ENTITY_REF_NODE:
935 xmlCtxtDumpSpaces(ctxt);
936 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
937 (char *) node->name);
940 case XML_ENTITY_NODE:
942 xmlCtxtDumpSpaces(ctxt);
943 fprintf(ctxt->output, "ENTITY\n");
948 xmlCtxtDumpSpaces(ctxt);
949 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
952 case XML_COMMENT_NODE:
954 xmlCtxtDumpSpaces(ctxt);
955 fprintf(ctxt->output, "COMMENT\n");
958 case XML_DOCUMENT_NODE:
959 case XML_HTML_DOCUMENT_NODE:
961 xmlCtxtDumpSpaces(ctxt);
963 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
964 xmlCtxtGenericNodeCheck(ctxt, node);
966 case XML_DOCUMENT_TYPE_NODE:
968 xmlCtxtDumpSpaces(ctxt);
969 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
972 case XML_DOCUMENT_FRAG_NODE:
974 xmlCtxtDumpSpaces(ctxt);
975 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
978 case XML_NOTATION_NODE:
980 xmlCtxtDumpSpaces(ctxt);
981 fprintf(ctxt->output, "NOTATION\n");
985 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
987 case XML_ELEMENT_DECL:
988 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
990 case XML_ATTRIBUTE_DECL:
991 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
993 case XML_ENTITY_DECL:
994 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
996 case XML_NAMESPACE_DECL:
997 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
999 case XML_XINCLUDE_START:
1001 xmlCtxtDumpSpaces(ctxt);
1002 fprintf(ctxt->output, "INCLUDE START\n");
1005 case XML_XINCLUDE_END:
1007 xmlCtxtDumpSpaces(ctxt);
1008 fprintf(ctxt->output, "INCLUDE END\n");
1013 xmlCtxtDumpSpaces(ctxt);
1014 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1015 "Unknown node type %d\n", node->type);
1018 if (node->doc == NULL) {
1020 xmlCtxtDumpSpaces(ctxt);
1022 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1025 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
1026 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1027 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
1028 xmlCtxtDumpAttrList(ctxt, node->properties);
1029 if (node->type != XML_ENTITY_REF_NODE) {
1030 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1032 xmlCtxtDumpSpaces(ctxt);
1033 fprintf(ctxt->output, "content=");
1034 xmlCtxtDumpString(ctxt, node->content);
1035 fprintf(ctxt->output, "\n");
1041 ent = xmlGetDocEntity(node->doc, node->name);
1043 xmlCtxtDumpEntity(ctxt, ent);
1048 * Do a bit of checking
1050 xmlCtxtGenericNodeCheck(ctxt, node);
1055 * @output: the FILE * for the output
1057 * @depth: the indentation level.
1059 * Dumps debug information for the element node, it is recursive
1062 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1066 xmlCtxtDumpSpaces(ctxt);
1067 fprintf(ctxt->output, "node is NULL\n");
1071 xmlCtxtDumpOneNode(ctxt, node);
1072 if ((node->type != XML_NAMESPACE_DECL) &&
1073 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1075 xmlCtxtDumpNodeList(ctxt, node->children);
1081 * xmlCtxtDumpNodeList:
1082 * @output: the FILE * for the output
1083 * @node: the node list
1084 * @depth: the indentation level.
1086 * Dumps debug information for the list of element node, it is recursive
1089 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1091 while (node != NULL) {
1092 xmlCtxtDumpNode(ctxt, node);
1098 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1102 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1105 ctxt->node = (xmlNodePtr) doc;
1107 switch (doc->type) {
1108 case XML_ELEMENT_NODE:
1109 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1110 "Misplaced ELEMENT node\n");
1112 case XML_ATTRIBUTE_NODE:
1113 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1114 "Misplaced ATTRIBUTE node\n");
1117 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1118 "Misplaced TEXT node\n");
1120 case XML_CDATA_SECTION_NODE:
1121 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1122 "Misplaced CDATA node\n");
1124 case XML_ENTITY_REF_NODE:
1125 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1126 "Misplaced ENTITYREF node\n");
1128 case XML_ENTITY_NODE:
1129 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1130 "Misplaced ENTITY node\n");
1133 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1134 "Misplaced PI node\n");
1136 case XML_COMMENT_NODE:
1137 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1138 "Misplaced COMMENT node\n");
1140 case XML_DOCUMENT_NODE:
1142 fprintf(ctxt->output, "DOCUMENT\n");
1144 case XML_HTML_DOCUMENT_NODE:
1146 fprintf(ctxt->output, "HTML DOCUMENT\n");
1148 case XML_DOCUMENT_TYPE_NODE:
1149 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1150 "Misplaced DOCTYPE node\n");
1152 case XML_DOCUMENT_FRAG_NODE:
1153 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1154 "Misplaced FRAGMENT node\n");
1156 case XML_NOTATION_NODE:
1157 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1158 "Misplaced NOTATION node\n");
1161 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1162 "Unknown node type %d\n", doc->type);
1167 * xmlCtxtDumpDocumentHead:
1168 * @output: the FILE * for the output
1169 * @doc: the document
1171 * Dumps debug information cncerning the document, not recursive
1174 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1176 if (doc == NULL) return;
1177 xmlCtxtDumpDocHead(ctxt, doc);
1179 if (doc->name != NULL) {
1180 fprintf(ctxt->output, "name=");
1181 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1182 fprintf(ctxt->output, "\n");
1184 if (doc->version != NULL) {
1185 fprintf(ctxt->output, "version=");
1186 xmlCtxtDumpString(ctxt, doc->version);
1187 fprintf(ctxt->output, "\n");
1189 if (doc->encoding != NULL) {
1190 fprintf(ctxt->output, "encoding=");
1191 xmlCtxtDumpString(ctxt, doc->encoding);
1192 fprintf(ctxt->output, "\n");
1194 if (doc->URL != NULL) {
1195 fprintf(ctxt->output, "URL=");
1196 xmlCtxtDumpString(ctxt, doc->URL);
1197 fprintf(ctxt->output, "\n");
1199 if (doc->standalone)
1200 fprintf(ctxt->output, "standalone=true\n");
1202 if (doc->oldNs != NULL)
1203 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1207 * xmlCtxtDumpDocument:
1208 * @output: the FILE * for the output
1209 * @doc: the document
1211 * Dumps debug information for the document, it's recursive
1214 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1218 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1221 xmlCtxtDumpDocumentHead(ctxt, doc);
1222 if (((doc->type == XML_DOCUMENT_NODE) ||
1223 (doc->type == XML_HTML_DOCUMENT_NODE))
1224 && (doc->children != NULL)) {
1226 xmlCtxtDumpNodeList(ctxt, doc->children);
1232 xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1236 fprintf(ctxt->output, "Entity is NULL");
1240 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1241 switch (cur->etype) {
1242 case XML_INTERNAL_GENERAL_ENTITY:
1243 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1245 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1246 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1248 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1249 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1251 case XML_INTERNAL_PARAMETER_ENTITY:
1252 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1254 case XML_EXTERNAL_PARAMETER_ENTITY:
1255 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1258 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1259 "Unknown entity type %d\n", cur->etype);
1261 if (cur->ExternalID != NULL)
1262 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1263 if (cur->SystemID != NULL)
1264 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1265 if (cur->orig != NULL)
1266 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1267 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1268 fprintf(ctxt->output, "\n content \"%s\"",
1269 (char *) cur->content);
1270 fprintf(ctxt->output, "\n");
1275 * xmlCtxtDumpEntities:
1276 * @output: the FILE * for the output
1277 * @doc: the document
1279 * Dumps debug information for all the entities in use by the document
1282 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1284 if (doc == NULL) return;
1285 xmlCtxtDumpDocHead(ctxt, doc);
1286 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1287 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1288 doc->intSubset->entities;
1291 fprintf(ctxt->output, "Entities in internal subset\n");
1292 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1295 fprintf(ctxt->output, "No entities in internal subset\n");
1296 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1297 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1298 doc->extSubset->entities;
1301 fprintf(ctxt->output, "Entities in external subset\n");
1302 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1304 } else if (!ctxt->check)
1305 fprintf(ctxt->output, "No entities in external subset\n");
1310 * @output: the FILE * for the output
1313 * Dumps debug information for the DTD
1316 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1320 fprintf(ctxt->output, "DTD is NULL\n");
1323 xmlCtxtDumpDtdNode(ctxt, dtd);
1324 if (dtd->children == NULL)
1325 fprintf(ctxt->output, " DTD is empty\n");
1328 xmlCtxtDumpNodeList(ctxt, dtd->children);
1333 /************************************************************************
1335 * Public entry points for dump *
1337 ************************************************************************/
1340 * xmlDebugDumpString:
1341 * @output: the FILE * for the output
1344 * Dumps informations about the string, shorten it if necessary
1347 xmlDebugDumpString(FILE * output, const xmlChar * str)
1354 fprintf(output, "(NULL)");
1357 for (i = 0; i < 40; i++)
1360 else if (IS_BLANK_CH(str[i]))
1362 else if (str[i] >= 0x80)
1363 fprintf(output, "#%X", str[i]);
1365 fputc(str[i], output);
1366 fprintf(output, "...");
1371 * @output: the FILE * for the output
1372 * @attr: the attribute
1373 * @depth: the indentation level.
1375 * Dumps debug information for the attribute
1378 xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
1381 if (output == NULL) return;
1382 xmlCtxtDumpInitCtxt(&ctxt);
1383 ctxt.output = output;
1385 xmlCtxtDumpAttr(&ctxt, attr);
1386 xmlCtxtDumpCleanCtxt(&ctxt);
1391 * xmlDebugDumpEntities:
1392 * @output: the FILE * for the output
1393 * @doc: the document
1395 * Dumps debug information for all the entities in use by the document
1398 xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1402 if (output == NULL) return;
1403 xmlCtxtDumpInitCtxt(&ctxt);
1404 ctxt.output = output;
1405 xmlCtxtDumpEntities(&ctxt, doc);
1406 xmlCtxtDumpCleanCtxt(&ctxt);
1410 * xmlDebugDumpAttrList:
1411 * @output: the FILE * for the output
1412 * @attr: the attribute list
1413 * @depth: the indentation level.
1415 * Dumps debug information for the attribute list
1418 xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1422 if (output == NULL) return;
1423 xmlCtxtDumpInitCtxt(&ctxt);
1424 ctxt.output = output;
1426 xmlCtxtDumpAttrList(&ctxt, attr);
1427 xmlCtxtDumpCleanCtxt(&ctxt);
1431 * xmlDebugDumpOneNode:
1432 * @output: the FILE * for the output
1434 * @depth: the indentation level.
1436 * Dumps debug information for the element node, it is not recursive
1439 xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1443 if (output == NULL) return;
1444 xmlCtxtDumpInitCtxt(&ctxt);
1445 ctxt.output = output;
1447 xmlCtxtDumpOneNode(&ctxt, node);
1448 xmlCtxtDumpCleanCtxt(&ctxt);
1453 * @output: the FILE * for the output
1455 * @depth: the indentation level.
1457 * Dumps debug information for the element node, it is recursive
1460 xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1466 xmlCtxtDumpInitCtxt(&ctxt);
1467 ctxt.output = output;
1469 xmlCtxtDumpNode(&ctxt, node);
1470 xmlCtxtDumpCleanCtxt(&ctxt);
1474 * xmlDebugDumpNodeList:
1475 * @output: the FILE * for the output
1476 * @node: the node list
1477 * @depth: the indentation level.
1479 * Dumps debug information for the list of element node, it is recursive
1482 xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1488 xmlCtxtDumpInitCtxt(&ctxt);
1489 ctxt.output = output;
1491 xmlCtxtDumpNodeList(&ctxt, node);
1492 xmlCtxtDumpCleanCtxt(&ctxt);
1496 * xmlDebugDumpDocumentHead:
1497 * @output: the FILE * for the output
1498 * @doc: the document
1500 * Dumps debug information cncerning the document, not recursive
1503 xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1509 xmlCtxtDumpInitCtxt(&ctxt);
1510 ctxt.options |= DUMP_TEXT_TYPE;
1511 ctxt.output = output;
1512 xmlCtxtDumpDocumentHead(&ctxt, doc);
1513 xmlCtxtDumpCleanCtxt(&ctxt);
1517 * xmlDebugDumpDocument:
1518 * @output: the FILE * for the output
1519 * @doc: the document
1521 * Dumps debug information for the document, it's recursive
1524 xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1530 xmlCtxtDumpInitCtxt(&ctxt);
1531 ctxt.options |= DUMP_TEXT_TYPE;
1532 ctxt.output = output;
1533 xmlCtxtDumpDocument(&ctxt, doc);
1534 xmlCtxtDumpCleanCtxt(&ctxt);
1539 * @output: the FILE * for the output
1542 * Dumps debug information for the DTD
1545 xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1551 xmlCtxtDumpInitCtxt(&ctxt);
1552 ctxt.options |= DUMP_TEXT_TYPE;
1553 ctxt.output = output;
1554 xmlCtxtDumpDTD(&ctxt, dtd);
1555 xmlCtxtDumpCleanCtxt(&ctxt);
1558 /************************************************************************
1560 * Public entry points for checkings *
1562 ************************************************************************/
1565 * xmlDebugCheckDocument:
1566 * @output: the FILE * for the output
1567 * @doc: the document
1569 * Check the document for potential content problems, and output
1570 * the errors to @output
1572 * Returns the number of errors found
1575 xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1581 xmlCtxtDumpInitCtxt(&ctxt);
1582 ctxt.output = output;
1584 xmlCtxtDumpDocument(&ctxt, doc);
1585 xmlCtxtDumpCleanCtxt(&ctxt);
1586 return(ctxt.errors);
1589 /************************************************************************
1591 * Helpers for Shell *
1593 ************************************************************************/
1597 * @node: the node to count
1599 * Count the children of @node.
1601 * Returns the number of children of @node.
1604 xmlLsCountNode(xmlNodePtr node) {
1606 xmlNodePtr list = NULL;
1611 switch (node->type) {
1612 case XML_ELEMENT_NODE:
1613 list = node->children;
1615 case XML_DOCUMENT_NODE:
1616 case XML_HTML_DOCUMENT_NODE:
1617 #ifdef LIBXML_DOCB_ENABLED
1618 case XML_DOCB_DOCUMENT_NODE:
1620 list = ((xmlDocPtr) node)->children;
1622 case XML_ATTRIBUTE_NODE:
1623 list = ((xmlAttrPtr) node)->children;
1626 case XML_CDATA_SECTION_NODE:
1628 case XML_COMMENT_NODE:
1629 if (node->content != NULL) {
1630 ret = xmlStrlen(node->content);
1633 case XML_ENTITY_REF_NODE:
1634 case XML_DOCUMENT_TYPE_NODE:
1635 case XML_ENTITY_NODE:
1636 case XML_DOCUMENT_FRAG_NODE:
1637 case XML_NOTATION_NODE:
1639 case XML_ELEMENT_DECL:
1640 case XML_ATTRIBUTE_DECL:
1641 case XML_ENTITY_DECL:
1642 case XML_NAMESPACE_DECL:
1643 case XML_XINCLUDE_START:
1644 case XML_XINCLUDE_END:
1648 for (;list != NULL;ret++)
1655 * @output: the FILE * for the output
1656 * @node: the node to dump
1658 * Dump to @output the type and name of @node.
1661 xmlLsOneNode(FILE *output, xmlNodePtr node) {
1662 if (output == NULL) return;
1664 fprintf(output, "NULL\n");
1667 switch (node->type) {
1668 case XML_ELEMENT_NODE:
1669 fprintf(output, "-");
1671 case XML_ATTRIBUTE_NODE:
1672 fprintf(output, "a");
1675 fprintf(output, "t");
1677 case XML_CDATA_SECTION_NODE:
1678 fprintf(output, "C");
1680 case XML_ENTITY_REF_NODE:
1681 fprintf(output, "e");
1683 case XML_ENTITY_NODE:
1684 fprintf(output, "E");
1687 fprintf(output, "p");
1689 case XML_COMMENT_NODE:
1690 fprintf(output, "c");
1692 case XML_DOCUMENT_NODE:
1693 fprintf(output, "d");
1695 case XML_HTML_DOCUMENT_NODE:
1696 fprintf(output, "h");
1698 case XML_DOCUMENT_TYPE_NODE:
1699 fprintf(output, "T");
1701 case XML_DOCUMENT_FRAG_NODE:
1702 fprintf(output, "F");
1704 case XML_NOTATION_NODE:
1705 fprintf(output, "N");
1707 case XML_NAMESPACE_DECL:
1708 fprintf(output, "n");
1711 fprintf(output, "?");
1713 if (node->type != XML_NAMESPACE_DECL) {
1714 if (node->properties != NULL)
1715 fprintf(output, "a");
1717 fprintf(output, "-");
1718 if (node->nsDef != NULL)
1719 fprintf(output, "n");
1721 fprintf(output, "-");
1724 fprintf(output, " %8d ", xmlLsCountNode(node));
1726 switch (node->type) {
1727 case XML_ELEMENT_NODE:
1728 if (node->name != NULL) {
1729 if ((node->ns != NULL) && (node->ns->prefix != NULL))
1730 fprintf(output, "%s:", node->ns->prefix);
1731 fprintf(output, "%s", (const char *) node->name);
1734 case XML_ATTRIBUTE_NODE:
1735 if (node->name != NULL)
1736 fprintf(output, "%s", (const char *) node->name);
1739 if (node->content != NULL) {
1740 xmlDebugDumpString(output, node->content);
1743 case XML_CDATA_SECTION_NODE:
1745 case XML_ENTITY_REF_NODE:
1746 if (node->name != NULL)
1747 fprintf(output, "%s", (const char *) node->name);
1749 case XML_ENTITY_NODE:
1750 if (node->name != NULL)
1751 fprintf(output, "%s", (const char *) node->name);
1754 if (node->name != NULL)
1755 fprintf(output, "%s", (const char *) node->name);
1757 case XML_COMMENT_NODE:
1759 case XML_DOCUMENT_NODE:
1761 case XML_HTML_DOCUMENT_NODE:
1763 case XML_DOCUMENT_TYPE_NODE:
1765 case XML_DOCUMENT_FRAG_NODE:
1767 case XML_NOTATION_NODE:
1769 case XML_NAMESPACE_DECL: {
1770 xmlNsPtr ns = (xmlNsPtr) node;
1772 if (ns->prefix == NULL)
1773 fprintf(output, "default -> %s", (char *)ns->href);
1775 fprintf(output, "%s -> %s", (char *)ns->prefix,
1780 if (node->name != NULL)
1781 fprintf(output, "%s", (const char *) node->name);
1783 fprintf(output, "\n");
1788 * @boolval: a bool to turn into text
1790 * Convenient way to turn bool into text
1792 * Returns a pointer to either "True" or "False"
1795 xmlBoolToText(int boolval)
1803 #ifdef LIBXML_XPATH_ENABLED
1804 /****************************************************************
1806 * The XML shell related functions *
1808 ****************************************************************/
1813 * TODO: Improvement/cleanups for the XML shell
1814 * - allow to shell out an editor on a subpart
1815 * - cleanup function registrations (with help) and calling
1816 * - provide registration routines
1820 * xmlShellPrintXPathError:
1821 * @errorType: valid xpath error id
1822 * @arg: the argument that cause xpath to fail
1824 * Print the xpath error to libxml default error channel
1827 xmlShellPrintXPathError(int errorType, const char *arg)
1829 const char *default_arg = "Result";
1834 switch (errorType) {
1835 case XPATH_UNDEFINED:
1836 xmlGenericError(xmlGenericErrorContext,
1837 "%s: no such node\n", arg);
1841 xmlGenericError(xmlGenericErrorContext,
1842 "%s is a Boolean\n", arg);
1845 xmlGenericError(xmlGenericErrorContext,
1846 "%s is a number\n", arg);
1849 xmlGenericError(xmlGenericErrorContext,
1850 "%s is a string\n", arg);
1853 xmlGenericError(xmlGenericErrorContext,
1854 "%s is a point\n", arg);
1857 xmlGenericError(xmlGenericErrorContext,
1858 "%s is a range\n", arg);
1860 case XPATH_LOCATIONSET:
1861 xmlGenericError(xmlGenericErrorContext,
1862 "%s is a range\n", arg);
1865 xmlGenericError(xmlGenericErrorContext,
1866 "%s is user-defined\n", arg);
1868 case XPATH_XSLT_TREE:
1869 xmlGenericError(xmlGenericErrorContext,
1870 "%s is an XSLT value tree\n", arg);
1874 xmlGenericError(xmlGenericErrorContext,
1875 "Try casting the result string function (xpath builtin)\n",
1881 #ifdef LIBXML_OUTPUT_ENABLED
1883 * xmlShellPrintNodeCtxt:
1884 * @ctxt : a non-null shell context
1885 * @node : a non-null node to print to the output FILE
1887 * Print node to the output FILE
1890 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1901 if (node->type == XML_DOCUMENT_NODE)
1902 xmlDocDump(fp, (xmlDocPtr) node);
1903 else if (node->type == XML_ATTRIBUTE_NODE)
1904 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
1906 xmlElemDump(fp, node->doc, node);
1912 * xmlShellPrintNode:
1913 * @node : a non-null node to print to the output FILE
1915 * Print node to the output FILE
1918 xmlShellPrintNode(xmlNodePtr node)
1920 xmlShellPrintNodeCtxt(NULL, node);
1922 #endif /* LIBXML_OUTPUT_ENABLED */
1925 * xmlShellPrintXPathResultCtxt:
1926 * @ctxt: a valid shell context
1927 * @list: a valid result generated by an xpath evaluation
1929 * Prints result to the output FILE
1932 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
1938 switch (list->type) {
1939 case XPATH_NODESET:{
1940 #ifdef LIBXML_OUTPUT_ENABLED
1943 if (list->nodesetval) {
1944 for (indx = 0; indx < list->nodesetval->nodeNr;
1946 xmlShellPrintNodeCtxt(ctxt,
1947 list->nodesetval->nodeTab[indx]);
1950 xmlGenericError(xmlGenericErrorContext,
1951 "Empty node set\n");
1955 xmlGenericError(xmlGenericErrorContext,
1957 #endif /* LIBXML_OUTPUT_ENABLED */
1960 xmlGenericError(xmlGenericErrorContext,
1961 "Is a Boolean:%s\n",
1962 xmlBoolToText(list->boolval));
1965 xmlGenericError(xmlGenericErrorContext,
1966 "Is a number:%0g\n", list->floatval);
1969 xmlGenericError(xmlGenericErrorContext,
1970 "Is a string:%s\n", list->stringval);
1974 xmlShellPrintXPathError(list->type, NULL);
1980 * xmlShellPrintXPathResult:
1981 * @list: a valid result generated by an xpath evaluation
1983 * Prints result to the output FILE
1986 xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1988 xmlShellPrintXPathResultCtxt(NULL, list);
1993 * @ctxt: the shell context
1998 * Implements the XML shell function "ls"
1999 * Does an Unix like listing of the given node (like a directory)
2004 xmlShellList(xmlShellCtxtPtr ctxt,
2005 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2006 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2012 fprintf(ctxt->output, "NULL\n");
2015 if ((node->type == XML_DOCUMENT_NODE) ||
2016 (node->type == XML_HTML_DOCUMENT_NODE)) {
2017 cur = ((xmlDocPtr) node)->children;
2018 } else if (node->type == XML_NAMESPACE_DECL) {
2019 xmlLsOneNode(ctxt->output, node);
2021 } else if (node->children != NULL) {
2022 cur = node->children;
2024 xmlLsOneNode(ctxt->output, node);
2027 while (cur != NULL) {
2028 xmlLsOneNode(ctxt->output, cur);
2036 * @ctxt: the shell context
2041 * Implements the XML shell function "base"
2042 * dumps the current XML base of the node
2047 xmlShellBase(xmlShellCtxtPtr ctxt,
2048 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2049 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2055 fprintf(ctxt->output, "NULL\n");
2059 base = xmlNodeGetBase(node->doc, node);
2062 fprintf(ctxt->output, " No base found !!!\n");
2064 fprintf(ctxt->output, "%s\n", base);
2070 #ifdef LIBXML_TREE_ENABLED
2073 * @ctxt: the shell context
2074 * @arg: the new base
2078 * Implements the XML shell function "setbase"
2079 * change the current XML base of the node
2084 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2085 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2086 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2088 xmlNodeSetBase(node, (xmlChar*) arg);
2093 #ifdef LIBXML_XPATH_ENABLED
2095 * xmlShellRegisterNamespace:
2096 * @ctxt: the shell context
2097 * @arg: a string in prefix=nsuri format
2101 * Implements the XML shell function "setns"
2102 * register/unregister a prefix=namespace pair
2103 * on the XPath context
2105 * Returns 0 on success and a negative value otherwise.
2108 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2109 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2116 nsListDup = xmlStrdup((xmlChar *) arg);
2118 while(next != NULL) {
2120 /*while((*next) == ' ') next++;*/
2121 if((*next) == '\0') break;
2125 next = (xmlChar*)xmlStrchr(next, '=');
2127 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2135 next = (xmlChar*)xmlStrchr(next, ' ');
2140 /* do register namespace */
2141 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2142 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2152 * xmlShellRegisterRootNamespaces:
2153 * @ctxt: the shell context
2155 * @node: the root element
2158 * Implements the XML shell function "setrootns"
2159 * which registers all namespaces declarations found on the root element.
2161 * Returns 0 on success and a negative value otherwise.
2164 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2165 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2169 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2170 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2173 while (ns != NULL) {
2174 if (ns->prefix == NULL)
2175 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2177 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2186 * @ctxt: the shell context
2187 * @arg: the string or regular expression to find
2191 * Implements the XML shell function "grep"
2192 * dumps informations about the node (namespace, attributes, content).
2197 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2198 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2206 #ifdef LIBXML_REGEXP_ENABLED
2207 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2208 (xmlStrchr((xmlChar *) arg, '*')) ||
2209 (xmlStrchr((xmlChar *) arg, '.')) ||
2210 (xmlStrchr((xmlChar *) arg, '['))) {
2213 while (node != NULL) {
2214 if (node->type == XML_COMMENT_NODE) {
2215 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2217 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2218 xmlShellList(ctxt, NULL, node, NULL);
2220 } else if (node->type == XML_TEXT_NODE) {
2221 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2223 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
2224 xmlShellList(ctxt, NULL, node->parent, NULL);
2229 * Browse the full subtree, deep first
2232 if ((node->type == XML_DOCUMENT_NODE) ||
2233 (node->type == XML_HTML_DOCUMENT_NODE)) {
2234 node = ((xmlDocPtr) node)->children;
2235 } else if ((node->children != NULL)
2236 && (node->type != XML_ENTITY_REF_NODE)) {
2238 node = node->children;
2239 } else if (node->next != NULL) {
2243 /* go up to parents->next if needed */
2244 while (node != NULL) {
2245 if (node->parent != NULL) {
2246 node = node->parent;
2248 if (node->next != NULL) {
2252 if (node->parent == NULL) {
2264 * @ctxt: the shell context
2269 * Implements the XML shell function "dir"
2270 * dumps informations about the node (namespace, attributes, content).
2275 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2276 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2277 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2282 fprintf(ctxt->output, "NULL\n");
2285 if ((node->type == XML_DOCUMENT_NODE) ||
2286 (node->type == XML_HTML_DOCUMENT_NODE)) {
2287 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
2288 } else if (node->type == XML_ATTRIBUTE_NODE) {
2289 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
2291 xmlDebugDumpOneNode(ctxt->output, node, 0);
2297 * xmlShellSetContent:
2298 * @ctxt: the shell context
2299 * @value: the content as a string
2303 * Implements the XML shell function "dir"
2304 * dumps informations about the node (namespace, attributes, content).
2309 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2310 char *value, xmlNodePtr node,
2311 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2314 xmlParserErrors ret;
2319 fprintf(ctxt->output, "NULL\n");
2322 if (value == NULL) {
2323 fprintf(ctxt->output, "NULL\n");
2327 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2328 if (ret == XML_ERR_OK) {
2329 if (node->children != NULL) {
2330 xmlFreeNodeList(node->children);
2331 node->children = NULL;
2334 xmlAddChildList(node, results);
2336 fprintf(ctxt->output, "failed to parse content\n");
2341 #ifdef LIBXML_SCHEMAS_ENABLED
2343 * xmlShellRNGValidate:
2344 * @ctxt: the shell context
2345 * @schemas: the path to the Relax-NG schemas
2349 * Implements the XML shell function "relaxng"
2350 * validating the instance against a Relax-NG schemas
2355 xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2356 xmlNodePtr node ATTRIBUTE_UNUSED,
2357 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2359 xmlRelaxNGPtr relaxngschemas;
2360 xmlRelaxNGParserCtxtPtr ctxt;
2361 xmlRelaxNGValidCtxtPtr vctxt;
2364 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2365 xmlRelaxNGSetParserErrors(ctxt,
2366 (xmlRelaxNGValidityErrorFunc) fprintf,
2367 (xmlRelaxNGValidityWarningFunc) fprintf,
2369 relaxngschemas = xmlRelaxNGParse(ctxt);
2370 xmlRelaxNGFreeParserCtxt(ctxt);
2371 if (relaxngschemas == NULL) {
2372 xmlGenericError(xmlGenericErrorContext,
2373 "Relax-NG schema %s failed to compile\n", schemas);
2376 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2377 xmlRelaxNGSetValidErrors(vctxt,
2378 (xmlRelaxNGValidityErrorFunc) fprintf,
2379 (xmlRelaxNGValidityWarningFunc) fprintf,
2381 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2383 fprintf(stderr, "%s validates\n", sctxt->filename);
2384 } else if (ret > 0) {
2385 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2387 fprintf(stderr, "%s validation generated an internal error\n",
2390 xmlRelaxNGFreeValidCtxt(vctxt);
2391 if (relaxngschemas != NULL)
2392 xmlRelaxNGFree(relaxngschemas);
2397 #ifdef LIBXML_OUTPUT_ENABLED
2400 * @ctxt: the shell context
2405 * Implements the XML shell function "cat"
2406 * dumps the serialization node content (XML or HTML).
2411 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2412 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2417 fprintf(ctxt->output, "NULL\n");
2420 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2421 #ifdef LIBXML_HTML_ENABLED
2422 if (node->type == XML_HTML_DOCUMENT_NODE)
2423 htmlDocDump(ctxt->output, (htmlDocPtr) node);
2425 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
2427 if (node->type == XML_DOCUMENT_NODE)
2428 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2430 xmlElemDump(ctxt->output, ctxt->doc, node);
2431 #endif /* LIBXML_HTML_ENABLED */
2433 if (node->type == XML_DOCUMENT_NODE)
2434 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2436 xmlElemDump(ctxt->output, ctxt->doc, node);
2438 fprintf(ctxt->output, "\n");
2441 #endif /* LIBXML_OUTPUT_ENABLED */
2445 * @ctxt: the shell context
2446 * @filename: the file name
2450 * Implements the XML shell function "load"
2451 * loads a new document specified by the filename
2453 * Returns 0 or -1 if loading failed
2456 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2457 xmlNodePtr node ATTRIBUTE_UNUSED,
2458 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2463 if ((ctxt == NULL) || (filename == NULL)) return(-1);
2464 if (ctxt->doc != NULL)
2465 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2468 #ifdef LIBXML_HTML_ENABLED
2469 doc = htmlParseFile(filename, NULL);
2471 fprintf(ctxt->output, "HTML support not compiled in\n");
2473 #endif /* LIBXML_HTML_ENABLED */
2475 doc = xmlReadFile(filename,NULL,0);
2478 if (ctxt->loaded == 1) {
2479 xmlFreeDoc(ctxt->doc);
2482 #ifdef LIBXML_XPATH_ENABLED
2483 xmlXPathFreeContext(ctxt->pctxt);
2484 #endif /* LIBXML_XPATH_ENABLED */
2485 xmlFree(ctxt->filename);
2487 ctxt->node = (xmlNodePtr) doc;
2488 #ifdef LIBXML_XPATH_ENABLED
2489 ctxt->pctxt = xmlXPathNewContext(doc);
2490 #endif /* LIBXML_XPATH_ENABLED */
2491 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2497 #ifdef LIBXML_OUTPUT_ENABLED
2500 * @ctxt: the shell context
2501 * @filename: the file name
2502 * @node: a node in the tree
2505 * Implements the XML shell function "write"
2506 * Write the current node to the filename, it saves the serialization
2507 * of the subtree under the @node specified
2509 * Returns 0 or -1 in case of error
2512 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2513 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2517 if ((filename == NULL) || (filename[0] == 0)) {
2521 if (access((char *) filename, W_OK)) {
2522 xmlGenericError(xmlGenericErrorContext,
2523 "Cannot write to %s\n", filename);
2527 switch (node->type) {
2528 case XML_DOCUMENT_NODE:
2529 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2530 xmlGenericError(xmlGenericErrorContext,
2531 "Failed to write to %s\n", filename);
2535 case XML_HTML_DOCUMENT_NODE:
2536 #ifdef LIBXML_HTML_ENABLED
2537 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2538 xmlGenericError(xmlGenericErrorContext,
2539 "Failed to write to %s\n", filename);
2543 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2544 xmlGenericError(xmlGenericErrorContext,
2545 "Failed to write to %s\n", filename);
2548 #endif /* LIBXML_HTML_ENABLED */
2553 f = fopen((char *) filename, "w");
2555 xmlGenericError(xmlGenericErrorContext,
2556 "Failed to write to %s\n", filename);
2559 xmlElemDump(f, ctxt->doc, node);
2568 * @ctxt: the shell context
2569 * @filename: the file name (optional)
2573 * Implements the XML shell function "save"
2574 * Write the current document to the filename, or it's original name
2576 * Returns 0 or -1 in case of error
2579 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2580 xmlNodePtr node ATTRIBUTE_UNUSED,
2581 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2583 if ((ctxt == NULL) || (ctxt->doc == NULL))
2585 if ((filename == NULL) || (filename[0] == 0))
2586 filename = ctxt->filename;
2587 if (filename == NULL)
2590 if (access((char *) filename, W_OK)) {
2591 xmlGenericError(xmlGenericErrorContext,
2592 "Cannot save to %s\n", filename);
2596 switch (ctxt->doc->type) {
2597 case XML_DOCUMENT_NODE:
2598 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2599 xmlGenericError(xmlGenericErrorContext,
2600 "Failed to save to %s\n", filename);
2603 case XML_HTML_DOCUMENT_NODE:
2604 #ifdef LIBXML_HTML_ENABLED
2605 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2606 xmlGenericError(xmlGenericErrorContext,
2607 "Failed to save to %s\n", filename);
2610 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2611 xmlGenericError(xmlGenericErrorContext,
2612 "Failed to save to %s\n", filename);
2614 #endif /* LIBXML_HTML_ENABLED */
2617 xmlGenericError(xmlGenericErrorContext,
2618 "To save to subparts of a document use the 'write' command\n");
2624 #endif /* LIBXML_OUTPUT_ENABLED */
2626 #ifdef LIBXML_VALID_ENABLED
2629 * @ctxt: the shell context
2630 * @dtd: the DTD URI (optional)
2634 * Implements the XML shell function "validate"
2635 * Validate the document, if a DTD path is provided, then the validation
2636 * is done against the given DTD.
2638 * Returns 0 or -1 in case of error
2641 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2642 xmlNodePtr node ATTRIBUTE_UNUSED,
2643 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2648 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2649 vctxt.userData = stderr;
2650 vctxt.error = (xmlValidityErrorFunc) fprintf;
2651 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2653 if ((dtd == NULL) || (dtd[0] == 0)) {
2654 res = xmlValidateDocument(&vctxt, ctxt->doc);
2658 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2659 if (subset != NULL) {
2660 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2667 #endif /* LIBXML_VALID_ENABLED */
2671 * @ctxt: the shell context
2673 * @tree: a node defining a subtree
2676 * Implements the XML shell function "du"
2677 * show the structure of the subtree under node @tree
2678 * If @tree is null, the command works on the current node.
2680 * Returns 0 or -1 in case of error
2683 xmlShellDu(xmlShellCtxtPtr ctxt,
2684 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2685 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2696 while (node != NULL) {
2697 if ((node->type == XML_DOCUMENT_NODE) ||
2698 (node->type == XML_HTML_DOCUMENT_NODE)) {
2699 fprintf(ctxt->output, "/\n");
2700 } else if (node->type == XML_ELEMENT_NODE) {
2701 for (i = 0; i < indent; i++)
2702 fprintf(ctxt->output, " ");
2703 if ((node->ns) && (node->ns->prefix))
2704 fprintf(ctxt->output, "%s:", node->ns->prefix);
2705 fprintf(ctxt->output, "%s\n", node->name);
2710 * Browse the full subtree, deep first
2713 if ((node->type == XML_DOCUMENT_NODE) ||
2714 (node->type == XML_HTML_DOCUMENT_NODE)) {
2715 node = ((xmlDocPtr) node)->children;
2716 } else if ((node->children != NULL)
2717 && (node->type != XML_ENTITY_REF_NODE)) {
2719 node = node->children;
2721 } else if ((node != tree) && (node->next != NULL)) {
2724 } else if (node != tree) {
2725 /* go up to parents->next if needed */
2726 while (node != tree) {
2727 if (node->parent != NULL) {
2728 node = node->parent;
2731 if ((node != tree) && (node->next != NULL)) {
2735 if (node->parent == NULL) {
2744 /* exit condition */
2755 * @ctxt: the shell context
2756 * @buffer: the output buffer
2760 * Implements the XML shell function "pwd"
2761 * Show the full path from the root to the node, if needed building
2762 * thumblers when similar elements exists at a given ancestor level.
2763 * The output is compatible with XPath commands.
2765 * Returns 0 or -1 in case of error
2768 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2769 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2773 if ((node == NULL) || (buffer == NULL))
2776 path = xmlGetNodePath(node);
2781 * This test prevents buffer overflow, because this routine
2782 * is only called by xmlShell, in which the second argument is
2784 * It is a dirty hack before a cleaner solution is found.
2785 * Documentation should mention that the second argument must
2786 * be at least 500 chars long, and could be stripped if too long.
2788 snprintf(buffer, 499, "%s", path);
2797 * @doc: the initial document
2798 * @filename: the output buffer
2799 * @input: the line reading function
2800 * @output: the output FILE*, defaults to stdout if NULL
2802 * Implements the XML shell
2803 * This allow to load, validate, view, modify and save a document
2804 * using a environment similar to a UNIX commandline.
2807 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2810 char prompt[500] = "/ > ";
2811 char *cmdline = NULL, *cur;
2815 xmlShellCtxtPtr ctxt;
2816 xmlXPathObjectPtr list;
2820 if (filename == NULL)
2826 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2831 ctxt->input = input;
2832 ctxt->output = output;
2833 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2834 ctxt->node = (xmlNodePtr) ctxt->doc;
2836 #ifdef LIBXML_XPATH_ENABLED
2837 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2838 if (ctxt->pctxt == NULL) {
2842 #endif /* LIBXML_XPATH_ENABLED */
2844 if (ctxt->node == (xmlNodePtr) ctxt->doc)
2845 snprintf(prompt, sizeof(prompt), "%s > ", "/");
2846 else if ((ctxt->node != NULL) && (ctxt->node->name) &&
2847 (ctxt->node->ns) && (ctxt->node->ns->prefix))
2848 snprintf(prompt, sizeof(prompt), "%s:%s > ",
2849 (ctxt->node->ns->prefix), ctxt->node->name);
2850 else if ((ctxt->node != NULL) && (ctxt->node->name))
2851 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
2853 snprintf(prompt, sizeof(prompt), "? > ");
2854 prompt[sizeof(prompt) - 1] = 0;
2857 * Get a new command line
2859 cmdline = ctxt->input(prompt);
2860 if (cmdline == NULL)
2864 * Parse the command itself
2867 while ((*cur == ' ') || (*cur == '\t'))
2870 while ((*cur != ' ') && (*cur != '\t') &&
2871 (*cur != '\n') && (*cur != '\r')) {
2874 command[i++] = *cur++;
2881 * Parse the argument
2883 while ((*cur == ' ') || (*cur == '\t'))
2886 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2894 * start interpreting the command
2896 if (!strcmp(command, "exit"))
2898 if (!strcmp(command, "quit"))
2900 if (!strcmp(command, "bye"))
2902 if (!strcmp(command, "help")) {
2903 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2904 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2905 fprintf(ctxt->output, "\tbye leave shell\n");
2906 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2907 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2908 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2909 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2910 fprintf(ctxt->output, "\texit leave shell\n");
2911 fprintf(ctxt->output, "\thelp display this help\n");
2912 fprintf(ctxt->output, "\tfree display memory usage\n");
2913 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2914 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
2915 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2916 #ifdef LIBXML_XPATH_ENABLED
2917 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2918 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2919 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2920 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2921 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
2922 #endif /* LIBXML_XPATH_ENABLED */
2923 fprintf(ctxt->output, "\tpwd display current working directory\n");
2924 fprintf(ctxt->output, "\twhereis display absolute path of [path] or current working directory\n");
2925 fprintf(ctxt->output, "\tquit leave shell\n");
2926 #ifdef LIBXML_OUTPUT_ENABLED
2927 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
2928 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2929 #endif /* LIBXML_OUTPUT_ENABLED */
2930 #ifdef LIBXML_VALID_ENABLED
2931 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
2932 #endif /* LIBXML_VALID_ENABLED */
2933 #ifdef LIBXML_SCHEMAS_ENABLED
2934 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2936 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
2937 #ifdef LIBXML_VALID_ENABLED
2938 } else if (!strcmp(command, "validate")) {
2939 xmlShellValidate(ctxt, arg, NULL, NULL);
2940 #endif /* LIBXML_VALID_ENABLED */
2941 } else if (!strcmp(command, "load")) {
2942 xmlShellLoad(ctxt, arg, NULL, NULL);
2943 #ifdef LIBXML_SCHEMAS_ENABLED
2944 } else if (!strcmp(command, "relaxng")) {
2945 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2947 #ifdef LIBXML_OUTPUT_ENABLED
2948 } else if (!strcmp(command, "save")) {
2949 xmlShellSave(ctxt, arg, NULL, NULL);
2950 } else if (!strcmp(command, "write")) {
2952 xmlGenericError(xmlGenericErrorContext,
2953 "Write command requires a filename argument\n");
2955 xmlShellWrite(ctxt, arg, ctxt->node, NULL);
2956 #endif /* LIBXML_OUTPUT_ENABLED */
2957 } else if (!strcmp(command, "grep")) {
2958 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2959 } else if (!strcmp(command, "free")) {
2961 xmlMemShow(ctxt->output, 0);
2965 sscanf(arg, "%d", &len);
2966 xmlMemShow(ctxt->output, len);
2968 } else if (!strcmp(command, "pwd")) {
2971 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2972 fprintf(ctxt->output, "%s\n", dir);
2973 } else if (!strcmp(command, "du")) {
2975 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2977 ctxt->pctxt->node = ctxt->node;
2978 #ifdef LIBXML_XPATH_ENABLED
2979 ctxt->pctxt->node = ctxt->node;
2980 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2983 #endif /* LIBXML_XPATH_ENABLED */
2985 switch (list->type) {
2986 case XPATH_UNDEFINED:
2987 xmlGenericError(xmlGenericErrorContext,
2988 "%s: no such node\n", arg);
2990 case XPATH_NODESET:{
2993 if (list->nodesetval == NULL)
2997 indx < list->nodesetval->nodeNr;
2999 xmlShellDu(ctxt, NULL,
3001 nodeTab[indx], NULL);
3005 xmlGenericError(xmlGenericErrorContext,
3006 "%s is a Boolean\n", arg);
3009 xmlGenericError(xmlGenericErrorContext,
3010 "%s is a number\n", arg);
3013 xmlGenericError(xmlGenericErrorContext,
3014 "%s is a string\n", arg);
3017 xmlGenericError(xmlGenericErrorContext,
3018 "%s is a point\n", arg);
3021 xmlGenericError(xmlGenericErrorContext,
3022 "%s is a range\n", arg);
3024 case XPATH_LOCATIONSET:
3025 xmlGenericError(xmlGenericErrorContext,
3026 "%s is a range\n", arg);
3029 xmlGenericError(xmlGenericErrorContext,
3030 "%s is user-defined\n", arg);
3032 case XPATH_XSLT_TREE:
3033 xmlGenericError(xmlGenericErrorContext,
3034 "%s is an XSLT value tree\n",
3038 #ifdef LIBXML_XPATH_ENABLED
3039 xmlXPathFreeObject(list);
3042 xmlGenericError(xmlGenericErrorContext,
3043 "%s: no such node\n", arg);
3045 ctxt->pctxt->node = NULL;
3047 } else if (!strcmp(command, "base")) {
3048 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
3049 } else if (!strcmp(command, "set")) {
3050 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
3051 #ifdef LIBXML_XPATH_ENABLED
3052 } else if (!strcmp(command, "setns")) {
3054 xmlGenericError(xmlGenericErrorContext,
3055 "setns: prefix=[nsuri] required\n");
3057 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
3059 } else if (!strcmp(command, "setrootns")) {
3062 root = xmlDocGetRootElement(ctxt->doc);
3063 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
3064 } else if (!strcmp(command, "xpath")) {
3066 xmlGenericError(xmlGenericErrorContext,
3067 "xpath: expression required\n");
3069 ctxt->pctxt->node = ctxt->node;
3070 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3071 xmlXPathDebugDumpObject(ctxt->output, list, 0);
3072 xmlXPathFreeObject(list);
3074 #endif /* LIBXML_XPATH_ENABLED */
3075 #ifdef LIBXML_TREE_ENABLED
3076 } else if (!strcmp(command, "setbase")) {
3077 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
3079 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3080 int dir = (!strcmp(command, "dir"));
3084 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3086 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3088 ctxt->pctxt->node = ctxt->node;
3089 #ifdef LIBXML_XPATH_ENABLED
3090 ctxt->pctxt->node = ctxt->node;
3091 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3094 #endif /* LIBXML_XPATH_ENABLED */
3096 switch (list->type) {
3097 case XPATH_UNDEFINED:
3098 xmlGenericError(xmlGenericErrorContext,
3099 "%s: no such node\n", arg);
3101 case XPATH_NODESET:{
3104 if (list->nodesetval == NULL)
3108 indx < list->nodesetval->nodeNr;
3111 xmlShellDir(ctxt, NULL,
3113 nodeTab[indx], NULL);
3115 xmlShellList(ctxt, NULL,
3117 nodeTab[indx], NULL);
3122 xmlGenericError(xmlGenericErrorContext,
3123 "%s is a Boolean\n", arg);
3126 xmlGenericError(xmlGenericErrorContext,
3127 "%s is a number\n", arg);
3130 xmlGenericError(xmlGenericErrorContext,
3131 "%s is a string\n", arg);
3134 xmlGenericError(xmlGenericErrorContext,
3135 "%s is a point\n", arg);
3138 xmlGenericError(xmlGenericErrorContext,
3139 "%s is a range\n", arg);
3141 case XPATH_LOCATIONSET:
3142 xmlGenericError(xmlGenericErrorContext,
3143 "%s is a range\n", arg);
3146 xmlGenericError(xmlGenericErrorContext,
3147 "%s is user-defined\n", arg);
3149 case XPATH_XSLT_TREE:
3150 xmlGenericError(xmlGenericErrorContext,
3151 "%s is an XSLT value tree\n",
3155 #ifdef LIBXML_XPATH_ENABLED
3156 xmlXPathFreeObject(list);
3159 xmlGenericError(xmlGenericErrorContext,
3160 "%s: no such node\n", arg);
3162 ctxt->pctxt->node = NULL;
3164 } else if (!strcmp(command, "whereis")) {
3168 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
3169 fprintf(ctxt->output, "%s\n", dir);
3171 ctxt->pctxt->node = ctxt->node;
3172 #ifdef LIBXML_XPATH_ENABLED
3173 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3176 #endif /* LIBXML_XPATH_ENABLED */
3178 switch (list->type) {
3179 case XPATH_UNDEFINED:
3180 xmlGenericError(xmlGenericErrorContext,
3181 "%s: no such node\n", arg);
3183 case XPATH_NODESET:{
3186 if (list->nodesetval == NULL)
3190 indx < list->nodesetval->nodeNr;
3192 if (!xmlShellPwd(ctxt, dir, list->nodesetval->
3193 nodeTab[indx], NULL))
3194 fprintf(ctxt->output, "%s\n", dir);
3199 xmlGenericError(xmlGenericErrorContext,
3200 "%s is a Boolean\n", arg);
3203 xmlGenericError(xmlGenericErrorContext,
3204 "%s is a number\n", arg);
3207 xmlGenericError(xmlGenericErrorContext,
3208 "%s is a string\n", arg);
3211 xmlGenericError(xmlGenericErrorContext,
3212 "%s is a point\n", arg);
3215 xmlGenericError(xmlGenericErrorContext,
3216 "%s is a range\n", arg);
3218 case XPATH_LOCATIONSET:
3219 xmlGenericError(xmlGenericErrorContext,
3220 "%s is a range\n", arg);
3223 xmlGenericError(xmlGenericErrorContext,
3224 "%s is user-defined\n", arg);
3226 case XPATH_XSLT_TREE:
3227 xmlGenericError(xmlGenericErrorContext,
3228 "%s is an XSLT value tree\n",
3232 #ifdef LIBXML_XPATH_ENABLED
3233 xmlXPathFreeObject(list);
3236 xmlGenericError(xmlGenericErrorContext,
3237 "%s: no such node\n", arg);
3239 ctxt->pctxt->node = NULL;
3241 } else if (!strcmp(command, "cd")) {
3243 ctxt->node = (xmlNodePtr) ctxt->doc;
3245 #ifdef LIBXML_XPATH_ENABLED
3248 ctxt->pctxt->node = ctxt->node;
3250 if ((l >= 2) && (arg[l - 1] == '/'))
3252 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3255 #endif /* LIBXML_XPATH_ENABLED */
3257 switch (list->type) {
3258 case XPATH_UNDEFINED:
3259 xmlGenericError(xmlGenericErrorContext,
3260 "%s: no such node\n", arg);
3263 if (list->nodesetval != NULL) {
3264 if (list->nodesetval->nodeNr == 1) {
3265 ctxt->node = list->nodesetval->nodeTab[0];
3266 if ((ctxt->node != NULL) &&
3267 (ctxt->node->type ==
3268 XML_NAMESPACE_DECL)) {
3269 xmlGenericError(xmlGenericErrorContext,
3270 "cannot cd to namespace\n");
3274 xmlGenericError(xmlGenericErrorContext,
3275 "%s is a %d Node Set\n",
3277 list->nodesetval->nodeNr);
3279 xmlGenericError(xmlGenericErrorContext,
3280 "%s is an empty Node Set\n",
3284 xmlGenericError(xmlGenericErrorContext,
3285 "%s is a Boolean\n", arg);
3288 xmlGenericError(xmlGenericErrorContext,
3289 "%s is a number\n", arg);
3292 xmlGenericError(xmlGenericErrorContext,
3293 "%s is a string\n", arg);
3296 xmlGenericError(xmlGenericErrorContext,
3297 "%s is a point\n", arg);
3300 xmlGenericError(xmlGenericErrorContext,
3301 "%s is a range\n", arg);
3303 case XPATH_LOCATIONSET:
3304 xmlGenericError(xmlGenericErrorContext,
3305 "%s is a range\n", arg);
3308 xmlGenericError(xmlGenericErrorContext,
3309 "%s is user-defined\n", arg);
3311 case XPATH_XSLT_TREE:
3312 xmlGenericError(xmlGenericErrorContext,
3313 "%s is an XSLT value tree\n",
3317 #ifdef LIBXML_XPATH_ENABLED
3318 xmlXPathFreeObject(list);
3321 xmlGenericError(xmlGenericErrorContext,
3322 "%s: no such node\n", arg);
3324 ctxt->pctxt->node = NULL;
3326 #ifdef LIBXML_OUTPUT_ENABLED
3327 } else if (!strcmp(command, "cat")) {
3329 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3331 ctxt->pctxt->node = ctxt->node;
3332 #ifdef LIBXML_XPATH_ENABLED
3333 ctxt->pctxt->node = ctxt->node;
3334 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3337 #endif /* LIBXML_XPATH_ENABLED */
3339 switch (list->type) {
3340 case XPATH_UNDEFINED:
3341 xmlGenericError(xmlGenericErrorContext,
3342 "%s: no such node\n", arg);
3344 case XPATH_NODESET:{
3347 if (list->nodesetval == NULL)
3351 indx < list->nodesetval->nodeNr;
3354 fprintf(ctxt->output, " -------\n");
3355 xmlShellCat(ctxt, NULL,
3357 nodeTab[indx], NULL);
3362 xmlGenericError(xmlGenericErrorContext,
3363 "%s is a Boolean\n", arg);
3366 xmlGenericError(xmlGenericErrorContext,
3367 "%s is a number\n", arg);
3370 xmlGenericError(xmlGenericErrorContext,
3371 "%s is a string\n", arg);
3374 xmlGenericError(xmlGenericErrorContext,
3375 "%s is a point\n", arg);
3378 xmlGenericError(xmlGenericErrorContext,
3379 "%s is a range\n", arg);
3381 case XPATH_LOCATIONSET:
3382 xmlGenericError(xmlGenericErrorContext,
3383 "%s is a range\n", arg);
3386 xmlGenericError(xmlGenericErrorContext,
3387 "%s is user-defined\n", arg);
3389 case XPATH_XSLT_TREE:
3390 xmlGenericError(xmlGenericErrorContext,
3391 "%s is an XSLT value tree\n",
3395 #ifdef LIBXML_XPATH_ENABLED
3396 xmlXPathFreeObject(list);
3399 xmlGenericError(xmlGenericErrorContext,
3400 "%s: no such node\n", arg);
3402 ctxt->pctxt->node = NULL;
3404 #endif /* LIBXML_OUTPUT_ENABLED */
3406 xmlGenericError(xmlGenericErrorContext,
3407 "Unknown command %s\n", command);
3409 free(cmdline); /* not xmlFree here ! */
3412 #ifdef LIBXML_XPATH_ENABLED
3413 xmlXPathFreeContext(ctxt->pctxt);
3414 #endif /* LIBXML_XPATH_ENABLED */
3416 xmlFreeDoc(ctxt->doc);
3418 if (ctxt->filename != NULL)
3419 xmlFree(ctxt->filename);
3421 if (cmdline != NULL)
3422 free(cmdline); /* not xmlFree here ! */
3425 #endif /* LIBXML_XPATH_ENABLED */
3426 #define bottom_debugXML
3427 #include "elfgcchack.h"
3428 #endif /* LIBXML_DEBUG_ENABLED */