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(void *payload
, void *data
,
1233 const xmlChar
*name ATTRIBUTE_UNUSED
)
1235 xmlEntityPtr cur
= (xmlEntityPtr
) payload
;
1236 xmlDebugCtxtPtr ctxt
= (xmlDebugCtxtPtr
) data
;
1239 fprintf(ctxt
->output
, "Entity is NULL");
1243 fprintf(ctxt
->output
, "%s : ", (char *) cur
->name
);
1244 switch (cur
->etype
) {
1245 case XML_INTERNAL_GENERAL_ENTITY
:
1246 fprintf(ctxt
->output
, "INTERNAL GENERAL, ");
1248 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
1249 fprintf(ctxt
->output
, "EXTERNAL PARSED, ");
1251 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
1252 fprintf(ctxt
->output
, "EXTERNAL UNPARSED, ");
1254 case XML_INTERNAL_PARAMETER_ENTITY
:
1255 fprintf(ctxt
->output
, "INTERNAL PARAMETER, ");
1257 case XML_EXTERNAL_PARAMETER_ENTITY
:
1258 fprintf(ctxt
->output
, "EXTERNAL PARAMETER, ");
1261 xmlDebugErr2(ctxt
, XML_CHECK_ENTITY_TYPE
,
1262 "Unknown entity type %d\n", cur
->etype
);
1264 if (cur
->ExternalID
!= NULL
)
1265 fprintf(ctxt
->output
, "ID \"%s\"", (char *) cur
->ExternalID
);
1266 if (cur
->SystemID
!= NULL
)
1267 fprintf(ctxt
->output
, "SYSTEM \"%s\"", (char *) cur
->SystemID
);
1268 if (cur
->orig
!= NULL
)
1269 fprintf(ctxt
->output
, "\n orig \"%s\"", (char *) cur
->orig
);
1270 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
))
1271 fprintf(ctxt
->output
, "\n content \"%s\"",
1272 (char *) cur
->content
);
1273 fprintf(ctxt
->output
, "\n");
1278 * xmlCtxtDumpEntities:
1279 * @output: the FILE * for the output
1280 * @doc: the document
1282 * Dumps debug information for all the entities in use by the document
1285 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt
, xmlDocPtr doc
)
1287 if (doc
== NULL
) return;
1288 xmlCtxtDumpDocHead(ctxt
, doc
);
1289 if ((doc
->intSubset
!= NULL
) && (doc
->intSubset
->entities
!= NULL
)) {
1290 xmlEntitiesTablePtr table
= (xmlEntitiesTablePtr
)
1291 doc
->intSubset
->entities
;
1294 fprintf(ctxt
->output
, "Entities in internal subset\n");
1295 xmlHashScan(table
, xmlCtxtDumpEntityCallback
, ctxt
);
1297 fprintf(ctxt
->output
, "No entities in internal subset\n");
1298 if ((doc
->extSubset
!= NULL
) && (doc
->extSubset
->entities
!= NULL
)) {
1299 xmlEntitiesTablePtr table
= (xmlEntitiesTablePtr
)
1300 doc
->extSubset
->entities
;
1303 fprintf(ctxt
->output
, "Entities in external subset\n");
1304 xmlHashScan(table
, xmlCtxtDumpEntityCallback
, ctxt
);
1305 } else if (!ctxt
->check
)
1306 fprintf(ctxt
->output
, "No entities in external subset\n");
1311 * @output: the FILE * for the output
1314 * Dumps debug information for the DTD
1317 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt
, xmlDtdPtr dtd
)
1321 fprintf(ctxt
->output
, "DTD is NULL\n");
1324 xmlCtxtDumpDtdNode(ctxt
, dtd
);
1325 if (dtd
->children
== NULL
)
1326 fprintf(ctxt
->output
, " DTD is empty\n");
1329 xmlCtxtDumpNodeList(ctxt
, dtd
->children
);
1334 /************************************************************************
1336 * Public entry points for dump *
1338 ************************************************************************/
1341 * xmlDebugDumpString:
1342 * @output: the FILE * for the output
1345 * Dumps informations about the string, shorten it if necessary
1348 xmlDebugDumpString(FILE * output
, const xmlChar
* str
)
1355 fprintf(output
, "(NULL)");
1358 for (i
= 0; i
< 40; i
++)
1361 else if (IS_BLANK_CH(str
[i
]))
1363 else if (str
[i
] >= 0x80)
1364 fprintf(output
, "#%X", str
[i
]);
1366 fputc(str
[i
], output
);
1367 fprintf(output
, "...");
1372 * @output: the FILE * for the output
1373 * @attr: the attribute
1374 * @depth: the indentation level.
1376 * Dumps debug information for the attribute
1379 xmlDebugDumpAttr(FILE *output
, xmlAttrPtr attr
, int depth
) {
1382 if (output
== NULL
) return;
1383 xmlCtxtDumpInitCtxt(&ctxt
);
1384 ctxt
.output
= output
;
1386 xmlCtxtDumpAttr(&ctxt
, attr
);
1387 xmlCtxtDumpCleanCtxt(&ctxt
);
1392 * xmlDebugDumpEntities:
1393 * @output: the FILE * for the output
1394 * @doc: the document
1396 * Dumps debug information for all the entities in use by the document
1399 xmlDebugDumpEntities(FILE * output
, xmlDocPtr doc
)
1403 if (output
== NULL
) return;
1404 xmlCtxtDumpInitCtxt(&ctxt
);
1405 ctxt
.output
= output
;
1406 xmlCtxtDumpEntities(&ctxt
, doc
);
1407 xmlCtxtDumpCleanCtxt(&ctxt
);
1411 * xmlDebugDumpAttrList:
1412 * @output: the FILE * for the output
1413 * @attr: the attribute list
1414 * @depth: the indentation level.
1416 * Dumps debug information for the attribute list
1419 xmlDebugDumpAttrList(FILE * output
, xmlAttrPtr attr
, int depth
)
1423 if (output
== NULL
) return;
1424 xmlCtxtDumpInitCtxt(&ctxt
);
1425 ctxt
.output
= output
;
1427 xmlCtxtDumpAttrList(&ctxt
, attr
);
1428 xmlCtxtDumpCleanCtxt(&ctxt
);
1432 * xmlDebugDumpOneNode:
1433 * @output: the FILE * for the output
1435 * @depth: the indentation level.
1437 * Dumps debug information for the element node, it is not recursive
1440 xmlDebugDumpOneNode(FILE * output
, xmlNodePtr node
, int depth
)
1444 if (output
== NULL
) return;
1445 xmlCtxtDumpInitCtxt(&ctxt
);
1446 ctxt
.output
= output
;
1448 xmlCtxtDumpOneNode(&ctxt
, node
);
1449 xmlCtxtDumpCleanCtxt(&ctxt
);
1454 * @output: the FILE * for the output
1456 * @depth: the indentation level.
1458 * Dumps debug information for the element node, it is recursive
1461 xmlDebugDumpNode(FILE * output
, xmlNodePtr node
, int depth
)
1467 xmlCtxtDumpInitCtxt(&ctxt
);
1468 ctxt
.output
= output
;
1470 xmlCtxtDumpNode(&ctxt
, node
);
1471 xmlCtxtDumpCleanCtxt(&ctxt
);
1475 * xmlDebugDumpNodeList:
1476 * @output: the FILE * for the output
1477 * @node: the node list
1478 * @depth: the indentation level.
1480 * Dumps debug information for the list of element node, it is recursive
1483 xmlDebugDumpNodeList(FILE * output
, xmlNodePtr node
, int depth
)
1489 xmlCtxtDumpInitCtxt(&ctxt
);
1490 ctxt
.output
= output
;
1492 xmlCtxtDumpNodeList(&ctxt
, node
);
1493 xmlCtxtDumpCleanCtxt(&ctxt
);
1497 * xmlDebugDumpDocumentHead:
1498 * @output: the FILE * for the output
1499 * @doc: the document
1501 * Dumps debug information cncerning the document, not recursive
1504 xmlDebugDumpDocumentHead(FILE * output
, xmlDocPtr doc
)
1510 xmlCtxtDumpInitCtxt(&ctxt
);
1511 ctxt
.options
|= DUMP_TEXT_TYPE
;
1512 ctxt
.output
= output
;
1513 xmlCtxtDumpDocumentHead(&ctxt
, doc
);
1514 xmlCtxtDumpCleanCtxt(&ctxt
);
1518 * xmlDebugDumpDocument:
1519 * @output: the FILE * for the output
1520 * @doc: the document
1522 * Dumps debug information for the document, it's recursive
1525 xmlDebugDumpDocument(FILE * output
, xmlDocPtr doc
)
1531 xmlCtxtDumpInitCtxt(&ctxt
);
1532 ctxt
.options
|= DUMP_TEXT_TYPE
;
1533 ctxt
.output
= output
;
1534 xmlCtxtDumpDocument(&ctxt
, doc
);
1535 xmlCtxtDumpCleanCtxt(&ctxt
);
1540 * @output: the FILE * for the output
1543 * Dumps debug information for the DTD
1546 xmlDebugDumpDTD(FILE * output
, xmlDtdPtr dtd
)
1552 xmlCtxtDumpInitCtxt(&ctxt
);
1553 ctxt
.options
|= DUMP_TEXT_TYPE
;
1554 ctxt
.output
= output
;
1555 xmlCtxtDumpDTD(&ctxt
, dtd
);
1556 xmlCtxtDumpCleanCtxt(&ctxt
);
1559 /************************************************************************
1561 * Public entry points for checkings *
1563 ************************************************************************/
1566 * xmlDebugCheckDocument:
1567 * @output: the FILE * for the output
1568 * @doc: the document
1570 * Check the document for potential content problems, and output
1571 * the errors to @output
1573 * Returns the number of errors found
1576 xmlDebugCheckDocument(FILE * output
, xmlDocPtr doc
)
1582 xmlCtxtDumpInitCtxt(&ctxt
);
1583 ctxt
.output
= output
;
1585 xmlCtxtDumpDocument(&ctxt
, doc
);
1586 xmlCtxtDumpCleanCtxt(&ctxt
);
1587 return(ctxt
.errors
);
1590 /************************************************************************
1592 * Helpers for Shell *
1594 ************************************************************************/
1598 * @node: the node to count
1600 * Count the children of @node.
1602 * Returns the number of children of @node.
1605 xmlLsCountNode(xmlNodePtr node
) {
1607 xmlNodePtr list
= NULL
;
1612 switch (node
->type
) {
1613 case XML_ELEMENT_NODE
:
1614 list
= node
->children
;
1616 case XML_DOCUMENT_NODE
:
1617 case XML_HTML_DOCUMENT_NODE
:
1618 #ifdef LIBXML_DOCB_ENABLED
1619 case XML_DOCB_DOCUMENT_NODE
:
1621 list
= ((xmlDocPtr
) node
)->children
;
1623 case XML_ATTRIBUTE_NODE
:
1624 list
= ((xmlAttrPtr
) node
)->children
;
1627 case XML_CDATA_SECTION_NODE
:
1629 case XML_COMMENT_NODE
:
1630 if (node
->content
!= NULL
) {
1631 ret
= xmlStrlen(node
->content
);
1634 case XML_ENTITY_REF_NODE
:
1635 case XML_DOCUMENT_TYPE_NODE
:
1636 case XML_ENTITY_NODE
:
1637 case XML_DOCUMENT_FRAG_NODE
:
1638 case XML_NOTATION_NODE
:
1640 case XML_ELEMENT_DECL
:
1641 case XML_ATTRIBUTE_DECL
:
1642 case XML_ENTITY_DECL
:
1643 case XML_NAMESPACE_DECL
:
1644 case XML_XINCLUDE_START
:
1645 case XML_XINCLUDE_END
:
1649 for (;list
!= NULL
;ret
++)
1656 * @output: the FILE * for the output
1657 * @node: the node to dump
1659 * Dump to @output the type and name of @node.
1662 xmlLsOneNode(FILE *output
, xmlNodePtr node
) {
1663 if (output
== NULL
) return;
1665 fprintf(output
, "NULL\n");
1668 switch (node
->type
) {
1669 case XML_ELEMENT_NODE
:
1670 fprintf(output
, "-");
1672 case XML_ATTRIBUTE_NODE
:
1673 fprintf(output
, "a");
1676 fprintf(output
, "t");
1678 case XML_CDATA_SECTION_NODE
:
1679 fprintf(output
, "C");
1681 case XML_ENTITY_REF_NODE
:
1682 fprintf(output
, "e");
1684 case XML_ENTITY_NODE
:
1685 fprintf(output
, "E");
1688 fprintf(output
, "p");
1690 case XML_COMMENT_NODE
:
1691 fprintf(output
, "c");
1693 case XML_DOCUMENT_NODE
:
1694 fprintf(output
, "d");
1696 case XML_HTML_DOCUMENT_NODE
:
1697 fprintf(output
, "h");
1699 case XML_DOCUMENT_TYPE_NODE
:
1700 fprintf(output
, "T");
1702 case XML_DOCUMENT_FRAG_NODE
:
1703 fprintf(output
, "F");
1705 case XML_NOTATION_NODE
:
1706 fprintf(output
, "N");
1708 case XML_NAMESPACE_DECL
:
1709 fprintf(output
, "n");
1712 fprintf(output
, "?");
1714 if (node
->type
!= XML_NAMESPACE_DECL
) {
1715 if (node
->properties
!= NULL
)
1716 fprintf(output
, "a");
1718 fprintf(output
, "-");
1719 if (node
->nsDef
!= NULL
)
1720 fprintf(output
, "n");
1722 fprintf(output
, "-");
1725 fprintf(output
, " %8d ", xmlLsCountNode(node
));
1727 switch (node
->type
) {
1728 case XML_ELEMENT_NODE
:
1729 if (node
->name
!= NULL
) {
1730 if ((node
->ns
!= NULL
) && (node
->ns
->prefix
!= NULL
))
1731 fprintf(output
, "%s:", node
->ns
->prefix
);
1732 fprintf(output
, "%s", (const char *) node
->name
);
1735 case XML_ATTRIBUTE_NODE
:
1736 if (node
->name
!= NULL
)
1737 fprintf(output
, "%s", (const char *) node
->name
);
1740 if (node
->content
!= NULL
) {
1741 xmlDebugDumpString(output
, node
->content
);
1744 case XML_CDATA_SECTION_NODE
:
1746 case XML_ENTITY_REF_NODE
:
1747 if (node
->name
!= NULL
)
1748 fprintf(output
, "%s", (const char *) node
->name
);
1750 case XML_ENTITY_NODE
:
1751 if (node
->name
!= NULL
)
1752 fprintf(output
, "%s", (const char *) node
->name
);
1755 if (node
->name
!= NULL
)
1756 fprintf(output
, "%s", (const char *) node
->name
);
1758 case XML_COMMENT_NODE
:
1760 case XML_DOCUMENT_NODE
:
1762 case XML_HTML_DOCUMENT_NODE
:
1764 case XML_DOCUMENT_TYPE_NODE
:
1766 case XML_DOCUMENT_FRAG_NODE
:
1768 case XML_NOTATION_NODE
:
1770 case XML_NAMESPACE_DECL
: {
1771 xmlNsPtr ns
= (xmlNsPtr
) node
;
1773 if (ns
->prefix
== NULL
)
1774 fprintf(output
, "default -> %s", (char *)ns
->href
);
1776 fprintf(output
, "%s -> %s", (char *)ns
->prefix
,
1781 if (node
->name
!= NULL
)
1782 fprintf(output
, "%s", (const char *) node
->name
);
1784 fprintf(output
, "\n");
1789 * @boolval: a bool to turn into text
1791 * Convenient way to turn bool into text
1793 * Returns a pointer to either "True" or "False"
1796 xmlBoolToText(int boolval
)
1804 #ifdef LIBXML_XPATH_ENABLED
1805 /****************************************************************
1807 * The XML shell related functions *
1809 ****************************************************************/
1814 * TODO: Improvement/cleanups for the XML shell
1815 * - allow to shell out an editor on a subpart
1816 * - cleanup function registrations (with help) and calling
1817 * - provide registration routines
1821 * xmlShellPrintXPathError:
1822 * @errorType: valid xpath error id
1823 * @arg: the argument that cause xpath to fail
1825 * Print the xpath error to libxml default error channel
1828 xmlShellPrintXPathError(int errorType
, const char *arg
)
1830 const char *default_arg
= "Result";
1835 switch (errorType
) {
1836 case XPATH_UNDEFINED
:
1837 xmlGenericError(xmlGenericErrorContext
,
1838 "%s: no such node\n", arg
);
1842 xmlGenericError(xmlGenericErrorContext
,
1843 "%s is a Boolean\n", arg
);
1846 xmlGenericError(xmlGenericErrorContext
,
1847 "%s is a number\n", arg
);
1850 xmlGenericError(xmlGenericErrorContext
,
1851 "%s is a string\n", arg
);
1854 xmlGenericError(xmlGenericErrorContext
,
1855 "%s is a point\n", arg
);
1858 xmlGenericError(xmlGenericErrorContext
,
1859 "%s is a range\n", arg
);
1861 case XPATH_LOCATIONSET
:
1862 xmlGenericError(xmlGenericErrorContext
,
1863 "%s is a range\n", arg
);
1866 xmlGenericError(xmlGenericErrorContext
,
1867 "%s is user-defined\n", arg
);
1869 case XPATH_XSLT_TREE
:
1870 xmlGenericError(xmlGenericErrorContext
,
1871 "%s is an XSLT value tree\n", arg
);
1875 xmlGenericError(xmlGenericErrorContext
,
1876 "Try casting the result string function (xpath builtin)\n",
1882 #ifdef LIBXML_OUTPUT_ENABLED
1884 * xmlShellPrintNodeCtxt:
1885 * @ctxt : a non-null shell context
1886 * @node : a non-null node to print to the output FILE
1888 * Print node to the output FILE
1891 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt
,xmlNodePtr node
)
1902 if (node
->type
== XML_DOCUMENT_NODE
)
1903 xmlDocDump(fp
, (xmlDocPtr
) node
);
1904 else if (node
->type
== XML_ATTRIBUTE_NODE
)
1905 xmlDebugDumpAttrList(fp
, (xmlAttrPtr
) node
, 0);
1907 xmlElemDump(fp
, node
->doc
, node
);
1913 * xmlShellPrintNode:
1914 * @node : a non-null node to print to the output FILE
1916 * Print node to the output FILE
1919 xmlShellPrintNode(xmlNodePtr node
)
1921 xmlShellPrintNodeCtxt(NULL
, node
);
1923 #endif /* LIBXML_OUTPUT_ENABLED */
1926 * xmlShellPrintXPathResultCtxt:
1927 * @ctxt: a valid shell context
1928 * @list: a valid result generated by an xpath evaluation
1930 * Prints result to the output FILE
1933 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt
,xmlXPathObjectPtr list
)
1939 switch (list
->type
) {
1940 case XPATH_NODESET
:{
1941 #ifdef LIBXML_OUTPUT_ENABLED
1944 if (list
->nodesetval
) {
1945 for (indx
= 0; indx
< list
->nodesetval
->nodeNr
;
1947 xmlShellPrintNodeCtxt(ctxt
,
1948 list
->nodesetval
->nodeTab
[indx
]);
1951 xmlGenericError(xmlGenericErrorContext
,
1952 "Empty node set\n");
1956 xmlGenericError(xmlGenericErrorContext
,
1958 #endif /* LIBXML_OUTPUT_ENABLED */
1961 xmlGenericError(xmlGenericErrorContext
,
1962 "Is a Boolean:%s\n",
1963 xmlBoolToText(list
->boolval
));
1966 xmlGenericError(xmlGenericErrorContext
,
1967 "Is a number:%0g\n", list
->floatval
);
1970 xmlGenericError(xmlGenericErrorContext
,
1971 "Is a string:%s\n", list
->stringval
);
1975 xmlShellPrintXPathError(list
->type
, NULL
);
1981 * xmlShellPrintXPathResult:
1982 * @list: a valid result generated by an xpath evaluation
1984 * Prints result to the output FILE
1987 xmlShellPrintXPathResult(xmlXPathObjectPtr list
)
1989 xmlShellPrintXPathResultCtxt(NULL
, list
);
1994 * @ctxt: the shell context
1999 * Implements the XML shell function "ls"
2000 * Does an Unix like listing of the given node (like a directory)
2005 xmlShellList(xmlShellCtxtPtr ctxt
,
2006 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2007 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2013 fprintf(ctxt
->output
, "NULL\n");
2016 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2017 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2018 cur
= ((xmlDocPtr
) node
)->children
;
2019 } else if (node
->type
== XML_NAMESPACE_DECL
) {
2020 xmlLsOneNode(ctxt
->output
, node
);
2022 } else if (node
->children
!= NULL
) {
2023 cur
= node
->children
;
2025 xmlLsOneNode(ctxt
->output
, node
);
2028 while (cur
!= NULL
) {
2029 xmlLsOneNode(ctxt
->output
, cur
);
2037 * @ctxt: the shell context
2042 * Implements the XML shell function "base"
2043 * dumps the current XML base of the node
2048 xmlShellBase(xmlShellCtxtPtr ctxt
,
2049 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2050 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2056 fprintf(ctxt
->output
, "NULL\n");
2060 base
= xmlNodeGetBase(node
->doc
, node
);
2063 fprintf(ctxt
->output
, " No base found !!!\n");
2065 fprintf(ctxt
->output
, "%s\n", base
);
2071 #ifdef LIBXML_TREE_ENABLED
2074 * @ctxt: the shell context
2075 * @arg: the new base
2079 * Implements the XML shell function "setbase"
2080 * change the current XML base of the node
2085 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2086 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2087 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2089 xmlNodeSetBase(node
, (xmlChar
*) arg
);
2094 #ifdef LIBXML_XPATH_ENABLED
2096 * xmlShellRegisterNamespace:
2097 * @ctxt: the shell context
2098 * @arg: a string in prefix=nsuri format
2102 * Implements the XML shell function "setns"
2103 * register/unregister a prefix=namespace pair
2104 * on the XPath context
2106 * Returns 0 on success and a negative value otherwise.
2109 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt
, char *arg
,
2110 xmlNodePtr node ATTRIBUTE_UNUSED
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2117 nsListDup
= xmlStrdup((xmlChar
*) arg
);
2119 while(next
!= NULL
) {
2121 /*while((*next) == ' ') next++;*/
2122 if((*next
) == '\0') break;
2126 next
= (xmlChar
*)xmlStrchr(next
, '=');
2128 fprintf(ctxt
->output
, "setns: prefix=[nsuri] required\n");
2136 next
= (xmlChar
*)xmlStrchr(next
, ' ');
2141 /* do register namespace */
2142 if(xmlXPathRegisterNs(ctxt
->pctxt
, prefix
, href
) != 0) {
2143 fprintf(ctxt
->output
,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix
, href
);
2153 * xmlShellRegisterRootNamespaces:
2154 * @ctxt: the shell context
2156 * @node: the root element
2159 * Implements the XML shell function "setrootns"
2160 * which registers all namespaces declarations found on the root element.
2162 * Returns 0 on success and a negative value otherwise.
2165 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt
, char *arg ATTRIBUTE_UNUSED
,
2166 xmlNodePtr root
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2170 if ((root
== NULL
) || (root
->type
!= XML_ELEMENT_NODE
) ||
2171 (root
->nsDef
== NULL
) || (ctxt
== NULL
) || (ctxt
->pctxt
== NULL
))
2174 while (ns
!= NULL
) {
2175 if (ns
->prefix
== NULL
)
2176 xmlXPathRegisterNs(ctxt
->pctxt
, BAD_CAST
"defaultns", ns
->href
);
2178 xmlXPathRegisterNs(ctxt
->pctxt
, ns
->prefix
, ns
->href
);
2187 * @ctxt: the shell context
2188 * @arg: the string or regular expression to find
2192 * Implements the XML shell function "grep"
2193 * dumps informations about the node (namespace, attributes, content).
2198 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2199 char *arg
, xmlNodePtr node
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2207 #ifdef LIBXML_REGEXP_ENABLED
2208 if ((xmlStrchr((xmlChar
*) arg
, '?')) ||
2209 (xmlStrchr((xmlChar
*) arg
, '*')) ||
2210 (xmlStrchr((xmlChar
*) arg
, '.')) ||
2211 (xmlStrchr((xmlChar
*) arg
, '['))) {
2214 while (node
!= NULL
) {
2215 if (node
->type
== XML_COMMENT_NODE
) {
2216 if (xmlStrstr(node
->content
, (xmlChar
*) arg
)) {
2218 fprintf(ctxt
->output
, "%s : ", xmlGetNodePath(node
));
2219 xmlShellList(ctxt
, NULL
, node
, NULL
);
2221 } else if (node
->type
== XML_TEXT_NODE
) {
2222 if (xmlStrstr(node
->content
, (xmlChar
*) arg
)) {
2224 fprintf(ctxt
->output
, "%s : ", xmlGetNodePath(node
->parent
));
2225 xmlShellList(ctxt
, NULL
, node
->parent
, NULL
);
2230 * Browse the full subtree, deep first
2233 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2234 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2235 node
= ((xmlDocPtr
) node
)->children
;
2236 } else if ((node
->children
!= NULL
)
2237 && (node
->type
!= XML_ENTITY_REF_NODE
)) {
2239 node
= node
->children
;
2240 } else if (node
->next
!= NULL
) {
2244 /* go up to parents->next if needed */
2245 while (node
!= NULL
) {
2246 if (node
->parent
!= NULL
) {
2247 node
= node
->parent
;
2249 if (node
->next
!= NULL
) {
2253 if (node
->parent
== NULL
) {
2265 * @ctxt: the shell context
2270 * Implements the XML shell function "dir"
2271 * dumps informations about the node (namespace, attributes, content).
2276 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2277 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2278 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2283 fprintf(ctxt
->output
, "NULL\n");
2286 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2287 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2288 xmlDebugDumpDocumentHead(ctxt
->output
, (xmlDocPtr
) node
);
2289 } else if (node
->type
== XML_ATTRIBUTE_NODE
) {
2290 xmlDebugDumpAttr(ctxt
->output
, (xmlAttrPtr
) node
, 0);
2292 xmlDebugDumpOneNode(ctxt
->output
, node
, 0);
2298 * xmlShellSetContent:
2299 * @ctxt: the shell context
2300 * @value: the content as a string
2304 * Implements the XML shell function "dir"
2305 * dumps informations about the node (namespace, attributes, content).
2310 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2311 char *value
, xmlNodePtr node
,
2312 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2315 xmlParserErrors ret
;
2320 fprintf(ctxt
->output
, "NULL\n");
2323 if (value
== NULL
) {
2324 fprintf(ctxt
->output
, "NULL\n");
2328 ret
= xmlParseInNodeContext(node
, value
, strlen(value
), 0, &results
);
2329 if (ret
== XML_ERR_OK
) {
2330 if (node
->children
!= NULL
) {
2331 xmlFreeNodeList(node
->children
);
2332 node
->children
= NULL
;
2335 xmlAddChildList(node
, results
);
2337 fprintf(ctxt
->output
, "failed to parse content\n");
2342 #ifdef LIBXML_SCHEMAS_ENABLED
2344 * xmlShellRNGValidate:
2345 * @ctxt: the shell context
2346 * @schemas: the path to the Relax-NG schemas
2350 * Implements the XML shell function "relaxng"
2351 * validating the instance against a Relax-NG schemas
2356 xmlShellRNGValidate(xmlShellCtxtPtr sctxt
, char *schemas
,
2357 xmlNodePtr node ATTRIBUTE_UNUSED
,
2358 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2360 xmlRelaxNGPtr relaxngschemas
;
2361 xmlRelaxNGParserCtxtPtr ctxt
;
2362 xmlRelaxNGValidCtxtPtr vctxt
;
2365 ctxt
= xmlRelaxNGNewParserCtxt(schemas
);
2366 xmlRelaxNGSetParserErrors(ctxt
,
2367 (xmlRelaxNGValidityErrorFunc
) fprintf
,
2368 (xmlRelaxNGValidityWarningFunc
) fprintf
,
2370 relaxngschemas
= xmlRelaxNGParse(ctxt
);
2371 xmlRelaxNGFreeParserCtxt(ctxt
);
2372 if (relaxngschemas
== NULL
) {
2373 xmlGenericError(xmlGenericErrorContext
,
2374 "Relax-NG schema %s failed to compile\n", schemas
);
2377 vctxt
= xmlRelaxNGNewValidCtxt(relaxngschemas
);
2378 xmlRelaxNGSetValidErrors(vctxt
,
2379 (xmlRelaxNGValidityErrorFunc
) fprintf
,
2380 (xmlRelaxNGValidityWarningFunc
) fprintf
,
2382 ret
= xmlRelaxNGValidateDoc(vctxt
, sctxt
->doc
);
2384 fprintf(stderr
, "%s validates\n", sctxt
->filename
);
2385 } else if (ret
> 0) {
2386 fprintf(stderr
, "%s fails to validate\n", sctxt
->filename
);
2388 fprintf(stderr
, "%s validation generated an internal error\n",
2391 xmlRelaxNGFreeValidCtxt(vctxt
);
2392 if (relaxngschemas
!= NULL
)
2393 xmlRelaxNGFree(relaxngschemas
);
2398 #ifdef LIBXML_OUTPUT_ENABLED
2401 * @ctxt: the shell context
2406 * Implements the XML shell function "cat"
2407 * dumps the serialization node content (XML or HTML).
2412 xmlShellCat(xmlShellCtxtPtr ctxt
, char *arg ATTRIBUTE_UNUSED
,
2413 xmlNodePtr node
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2418 fprintf(ctxt
->output
, "NULL\n");
2421 if (ctxt
->doc
->type
== XML_HTML_DOCUMENT_NODE
) {
2422 #ifdef LIBXML_HTML_ENABLED
2423 if (node
->type
== XML_HTML_DOCUMENT_NODE
)
2424 htmlDocDump(ctxt
->output
, (htmlDocPtr
) node
);
2426 htmlNodeDumpFile(ctxt
->output
, ctxt
->doc
, node
);
2428 if (node
->type
== XML_DOCUMENT_NODE
)
2429 xmlDocDump(ctxt
->output
, (xmlDocPtr
) node
);
2431 xmlElemDump(ctxt
->output
, ctxt
->doc
, node
);
2432 #endif /* LIBXML_HTML_ENABLED */
2434 if (node
->type
== XML_DOCUMENT_NODE
)
2435 xmlDocDump(ctxt
->output
, (xmlDocPtr
) node
);
2437 xmlElemDump(ctxt
->output
, ctxt
->doc
, node
);
2439 fprintf(ctxt
->output
, "\n");
2442 #endif /* LIBXML_OUTPUT_ENABLED */
2446 * @ctxt: the shell context
2447 * @filename: the file name
2451 * Implements the XML shell function "load"
2452 * loads a new document specified by the filename
2454 * Returns 0 or -1 if loading failed
2457 xmlShellLoad(xmlShellCtxtPtr ctxt
, char *filename
,
2458 xmlNodePtr node ATTRIBUTE_UNUSED
,
2459 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2464 if ((ctxt
== NULL
) || (filename
== NULL
)) return(-1);
2465 if (ctxt
->doc
!= NULL
)
2466 html
= (ctxt
->doc
->type
== XML_HTML_DOCUMENT_NODE
);
2469 #ifdef LIBXML_HTML_ENABLED
2470 doc
= htmlParseFile(filename
, NULL
);
2472 fprintf(ctxt
->output
, "HTML support not compiled in\n");
2474 #endif /* LIBXML_HTML_ENABLED */
2476 doc
= xmlReadFile(filename
,NULL
,0);
2479 if (ctxt
->loaded
== 1) {
2480 xmlFreeDoc(ctxt
->doc
);
2483 #ifdef LIBXML_XPATH_ENABLED
2484 xmlXPathFreeContext(ctxt
->pctxt
);
2485 #endif /* LIBXML_XPATH_ENABLED */
2486 xmlFree(ctxt
->filename
);
2488 ctxt
->node
= (xmlNodePtr
) doc
;
2489 #ifdef LIBXML_XPATH_ENABLED
2490 ctxt
->pctxt
= xmlXPathNewContext(doc
);
2491 #endif /* LIBXML_XPATH_ENABLED */
2492 ctxt
->filename
= (char *) xmlCanonicPath((xmlChar
*) filename
);
2498 #ifdef LIBXML_OUTPUT_ENABLED
2501 * @ctxt: the shell context
2502 * @filename: the file name
2503 * @node: a node in the tree
2506 * Implements the XML shell function "write"
2507 * Write the current node to the filename, it saves the serialization
2508 * of the subtree under the @node specified
2510 * Returns 0 or -1 in case of error
2513 xmlShellWrite(xmlShellCtxtPtr ctxt
, char *filename
, xmlNodePtr node
,
2514 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2518 if ((filename
== NULL
) || (filename
[0] == 0)) {
2522 if (access((char *) filename
, W_OK
)) {
2523 xmlGenericError(xmlGenericErrorContext
,
2524 "Cannot write to %s\n", filename
);
2528 switch (node
->type
) {
2529 case XML_DOCUMENT_NODE
:
2530 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < -1) {
2531 xmlGenericError(xmlGenericErrorContext
,
2532 "Failed to write to %s\n", filename
);
2536 case XML_HTML_DOCUMENT_NODE
:
2537 #ifdef LIBXML_HTML_ENABLED
2538 if (htmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2539 xmlGenericError(xmlGenericErrorContext
,
2540 "Failed to write to %s\n", filename
);
2544 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < -1) {
2545 xmlGenericError(xmlGenericErrorContext
,
2546 "Failed to write to %s\n", filename
);
2549 #endif /* LIBXML_HTML_ENABLED */
2554 f
= fopen((char *) filename
, "w");
2556 xmlGenericError(xmlGenericErrorContext
,
2557 "Failed to write to %s\n", filename
);
2560 xmlElemDump(f
, ctxt
->doc
, node
);
2569 * @ctxt: the shell context
2570 * @filename: the file name (optional)
2574 * Implements the XML shell function "save"
2575 * Write the current document to the filename, or it's original name
2577 * Returns 0 or -1 in case of error
2580 xmlShellSave(xmlShellCtxtPtr ctxt
, char *filename
,
2581 xmlNodePtr node ATTRIBUTE_UNUSED
,
2582 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2584 if ((ctxt
== NULL
) || (ctxt
->doc
== NULL
))
2586 if ((filename
== NULL
) || (filename
[0] == 0))
2587 filename
= ctxt
->filename
;
2588 if (filename
== NULL
)
2591 if (access((char *) filename
, W_OK
)) {
2592 xmlGenericError(xmlGenericErrorContext
,
2593 "Cannot save to %s\n", filename
);
2597 switch (ctxt
->doc
->type
) {
2598 case XML_DOCUMENT_NODE
:
2599 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2600 xmlGenericError(xmlGenericErrorContext
,
2601 "Failed to save to %s\n", filename
);
2604 case XML_HTML_DOCUMENT_NODE
:
2605 #ifdef LIBXML_HTML_ENABLED
2606 if (htmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2607 xmlGenericError(xmlGenericErrorContext
,
2608 "Failed to save to %s\n", filename
);
2611 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2612 xmlGenericError(xmlGenericErrorContext
,
2613 "Failed to save to %s\n", filename
);
2615 #endif /* LIBXML_HTML_ENABLED */
2618 xmlGenericError(xmlGenericErrorContext
,
2619 "To save to subparts of a document use the 'write' command\n");
2625 #endif /* LIBXML_OUTPUT_ENABLED */
2627 #ifdef LIBXML_VALID_ENABLED
2630 * @ctxt: the shell context
2631 * @dtd: the DTD URI (optional)
2635 * Implements the XML shell function "validate"
2636 * Validate the document, if a DTD path is provided, then the validation
2637 * is done against the given DTD.
2639 * Returns 0 or -1 in case of error
2642 xmlShellValidate(xmlShellCtxtPtr ctxt
, char *dtd
,
2643 xmlNodePtr node ATTRIBUTE_UNUSED
,
2644 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2649 if ((ctxt
== NULL
) || (ctxt
->doc
== NULL
)) return(-1);
2650 vctxt
.userData
= stderr
;
2651 vctxt
.error
= (xmlValidityErrorFunc
) fprintf
;
2652 vctxt
.warning
= (xmlValidityWarningFunc
) fprintf
;
2654 if ((dtd
== NULL
) || (dtd
[0] == 0)) {
2655 res
= xmlValidateDocument(&vctxt
, ctxt
->doc
);
2659 subset
= xmlParseDTD(NULL
, (xmlChar
*) dtd
);
2660 if (subset
!= NULL
) {
2661 res
= xmlValidateDtd(&vctxt
, ctxt
->doc
, subset
);
2668 #endif /* LIBXML_VALID_ENABLED */
2672 * @ctxt: the shell context
2674 * @tree: a node defining a subtree
2677 * Implements the XML shell function "du"
2678 * show the structure of the subtree under node @tree
2679 * If @tree is null, the command works on the current node.
2681 * Returns 0 or -1 in case of error
2684 xmlShellDu(xmlShellCtxtPtr ctxt
,
2685 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr tree
,
2686 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2697 while (node
!= NULL
) {
2698 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2699 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2700 fprintf(ctxt
->output
, "/\n");
2701 } else if (node
->type
== XML_ELEMENT_NODE
) {
2702 for (i
= 0; i
< indent
; i
++)
2703 fprintf(ctxt
->output
, " ");
2704 if ((node
->ns
) && (node
->ns
->prefix
))
2705 fprintf(ctxt
->output
, "%s:", node
->ns
->prefix
);
2706 fprintf(ctxt
->output
, "%s\n", node
->name
);
2711 * Browse the full subtree, deep first
2714 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2715 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2716 node
= ((xmlDocPtr
) node
)->children
;
2717 } else if ((node
->children
!= NULL
)
2718 && (node
->type
!= XML_ENTITY_REF_NODE
)) {
2720 node
= node
->children
;
2722 } else if ((node
!= tree
) && (node
->next
!= NULL
)) {
2725 } else if (node
!= tree
) {
2726 /* go up to parents->next if needed */
2727 while (node
!= tree
) {
2728 if (node
->parent
!= NULL
) {
2729 node
= node
->parent
;
2732 if ((node
!= tree
) && (node
->next
!= NULL
)) {
2736 if (node
->parent
== NULL
) {
2745 /* exit condition */
2756 * @ctxt: the shell context
2757 * @buffer: the output buffer
2761 * Implements the XML shell function "pwd"
2762 * Show the full path from the root to the node, if needed building
2763 * thumblers when similar elements exists at a given ancestor level.
2764 * The output is compatible with XPath commands.
2766 * Returns 0 or -1 in case of error
2769 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
, char *buffer
,
2770 xmlNodePtr node
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2774 if ((node
== NULL
) || (buffer
== NULL
))
2777 path
= xmlGetNodePath(node
);
2782 * This test prevents buffer overflow, because this routine
2783 * is only called by xmlShell, in which the second argument is
2785 * It is a dirty hack before a cleaner solution is found.
2786 * Documentation should mention that the second argument must
2787 * be at least 500 chars long, and could be stripped if too long.
2789 snprintf(buffer
, 499, "%s", path
);
2798 * @doc: the initial document
2799 * @filename: the output buffer
2800 * @input: the line reading function
2801 * @output: the output FILE*, defaults to stdout if NULL
2803 * Implements the XML shell
2804 * This allow to load, validate, view, modify and save a document
2805 * using a environment similar to a UNIX commandline.
2808 xmlShell(xmlDocPtr doc
, char *filename
, xmlShellReadlineFunc input
,
2811 char prompt
[500] = "/ > ";
2812 char *cmdline
= NULL
, *cur
;
2816 xmlShellCtxtPtr ctxt
;
2817 xmlXPathObjectPtr list
;
2821 if (filename
== NULL
)
2827 ctxt
= (xmlShellCtxtPtr
) xmlMalloc(sizeof(xmlShellCtxt
));
2832 ctxt
->input
= input
;
2833 ctxt
->output
= output
;
2834 ctxt
->filename
= (char *) xmlStrdup((xmlChar
*) filename
);
2835 ctxt
->node
= (xmlNodePtr
) ctxt
->doc
;
2837 #ifdef LIBXML_XPATH_ENABLED
2838 ctxt
->pctxt
= xmlXPathNewContext(ctxt
->doc
);
2839 if (ctxt
->pctxt
== NULL
) {
2843 #endif /* LIBXML_XPATH_ENABLED */
2845 if (ctxt
->node
== (xmlNodePtr
) ctxt
->doc
)
2846 snprintf(prompt
, sizeof(prompt
), "%s > ", "/");
2847 else if ((ctxt
->node
!= NULL
) && (ctxt
->node
->name
) &&
2848 (ctxt
->node
->ns
) && (ctxt
->node
->ns
->prefix
))
2849 snprintf(prompt
, sizeof(prompt
), "%s:%s > ",
2850 (ctxt
->node
->ns
->prefix
), ctxt
->node
->name
);
2851 else if ((ctxt
->node
!= NULL
) && (ctxt
->node
->name
))
2852 snprintf(prompt
, sizeof(prompt
), "%s > ", ctxt
->node
->name
);
2854 snprintf(prompt
, sizeof(prompt
), "? > ");
2855 prompt
[sizeof(prompt
) - 1] = 0;
2858 * Get a new command line
2860 cmdline
= ctxt
->input(prompt
);
2861 if (cmdline
== NULL
)
2865 * Parse the command itself
2868 while ((*cur
== ' ') || (*cur
== '\t'))
2871 while ((*cur
!= ' ') && (*cur
!= '\t') &&
2872 (*cur
!= '\n') && (*cur
!= '\r')) {
2875 command
[i
++] = *cur
++;
2882 * Parse the argument
2884 while ((*cur
== ' ') || (*cur
== '\t'))
2887 while ((*cur
!= '\n') && (*cur
!= '\r') && (*cur
!= 0)) {
2895 * start interpreting the command
2897 if (!strcmp(command
, "exit"))
2899 if (!strcmp(command
, "quit"))
2901 if (!strcmp(command
, "bye"))
2903 if (!strcmp(command
, "help")) {
2904 fprintf(ctxt
->output
, "\tbase display XML base of the node\n");
2905 fprintf(ctxt
->output
, "\tsetbase URI change the XML base of the node\n");
2906 fprintf(ctxt
->output
, "\tbye leave shell\n");
2907 fprintf(ctxt
->output
, "\tcat [node] display node or current node\n");
2908 fprintf(ctxt
->output
, "\tcd [path] change directory to path or to root\n");
2909 fprintf(ctxt
->output
, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2910 fprintf(ctxt
->output
, "\tdu [path] show the structure of the subtree under path or the current node\n");
2911 fprintf(ctxt
->output
, "\texit leave shell\n");
2912 fprintf(ctxt
->output
, "\thelp display this help\n");
2913 fprintf(ctxt
->output
, "\tfree display memory usage\n");
2914 fprintf(ctxt
->output
, "\tload [name] load a new document with name\n");
2915 fprintf(ctxt
->output
, "\tls [path] list contents of path or the current directory\n");
2916 fprintf(ctxt
->output
, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2917 #ifdef LIBXML_XPATH_ENABLED
2918 fprintf(ctxt
->output
, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2919 fprintf(ctxt
->output
, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2920 fprintf(ctxt
->output
, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2921 fprintf(ctxt
->output
, "\tsetrootns register all namespace found on the root element\n");
2922 fprintf(ctxt
->output
, "\t the default namespace if any uses 'defaultns' prefix\n");
2923 #endif /* LIBXML_XPATH_ENABLED */
2924 fprintf(ctxt
->output
, "\tpwd display current working directory\n");
2925 fprintf(ctxt
->output
, "\twhereis display absolute path of [path] or current working directory\n");
2926 fprintf(ctxt
->output
, "\tquit leave shell\n");
2927 #ifdef LIBXML_OUTPUT_ENABLED
2928 fprintf(ctxt
->output
, "\tsave [name] save this document to name or the original name\n");
2929 fprintf(ctxt
->output
, "\twrite [name] write the current node to the filename\n");
2930 #endif /* LIBXML_OUTPUT_ENABLED */
2931 #ifdef LIBXML_VALID_ENABLED
2932 fprintf(ctxt
->output
, "\tvalidate check the document for errors\n");
2933 #endif /* LIBXML_VALID_ENABLED */
2934 #ifdef LIBXML_SCHEMAS_ENABLED
2935 fprintf(ctxt
->output
, "\trelaxng rng validate the document against the Relax-NG schemas\n");
2937 fprintf(ctxt
->output
, "\tgrep string search for a string in the subtree\n");
2938 #ifdef LIBXML_VALID_ENABLED
2939 } else if (!strcmp(command
, "validate")) {
2940 xmlShellValidate(ctxt
, arg
, NULL
, NULL
);
2941 #endif /* LIBXML_VALID_ENABLED */
2942 } else if (!strcmp(command
, "load")) {
2943 xmlShellLoad(ctxt
, arg
, NULL
, NULL
);
2944 #ifdef LIBXML_SCHEMAS_ENABLED
2945 } else if (!strcmp(command
, "relaxng")) {
2946 xmlShellRNGValidate(ctxt
, arg
, NULL
, NULL
);
2948 #ifdef LIBXML_OUTPUT_ENABLED
2949 } else if (!strcmp(command
, "save")) {
2950 xmlShellSave(ctxt
, arg
, NULL
, NULL
);
2951 } else if (!strcmp(command
, "write")) {
2953 xmlGenericError(xmlGenericErrorContext
,
2954 "Write command requires a filename argument\n");
2956 xmlShellWrite(ctxt
, arg
, ctxt
->node
, NULL
);
2957 #endif /* LIBXML_OUTPUT_ENABLED */
2958 } else if (!strcmp(command
, "grep")) {
2959 xmlShellGrep(ctxt
, arg
, ctxt
->node
, NULL
);
2960 } else if (!strcmp(command
, "free")) {
2962 xmlMemShow(ctxt
->output
, 0);
2966 sscanf(arg
, "%d", &len
);
2967 xmlMemShow(ctxt
->output
, len
);
2969 } else if (!strcmp(command
, "pwd")) {
2972 if (!xmlShellPwd(ctxt
, dir
, ctxt
->node
, NULL
))
2973 fprintf(ctxt
->output
, "%s\n", dir
);
2974 } else if (!strcmp(command
, "du")) {
2976 xmlShellDu(ctxt
, NULL
, ctxt
->node
, NULL
);
2978 ctxt
->pctxt
->node
= ctxt
->node
;
2979 #ifdef LIBXML_XPATH_ENABLED
2980 ctxt
->pctxt
->node
= ctxt
->node
;
2981 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
2984 #endif /* LIBXML_XPATH_ENABLED */
2986 switch (list
->type
) {
2987 case XPATH_UNDEFINED
:
2988 xmlGenericError(xmlGenericErrorContext
,
2989 "%s: no such node\n", arg
);
2991 case XPATH_NODESET
:{
2994 if (list
->nodesetval
== NULL
)
2998 indx
< list
->nodesetval
->nodeNr
;
3000 xmlShellDu(ctxt
, NULL
,
3002 nodeTab
[indx
], NULL
);
3006 xmlGenericError(xmlGenericErrorContext
,
3007 "%s is a Boolean\n", arg
);
3010 xmlGenericError(xmlGenericErrorContext
,
3011 "%s is a number\n", arg
);
3014 xmlGenericError(xmlGenericErrorContext
,
3015 "%s is a string\n", arg
);
3018 xmlGenericError(xmlGenericErrorContext
,
3019 "%s is a point\n", arg
);
3022 xmlGenericError(xmlGenericErrorContext
,
3023 "%s is a range\n", arg
);
3025 case XPATH_LOCATIONSET
:
3026 xmlGenericError(xmlGenericErrorContext
,
3027 "%s is a range\n", arg
);
3030 xmlGenericError(xmlGenericErrorContext
,
3031 "%s is user-defined\n", arg
);
3033 case XPATH_XSLT_TREE
:
3034 xmlGenericError(xmlGenericErrorContext
,
3035 "%s is an XSLT value tree\n",
3039 #ifdef LIBXML_XPATH_ENABLED
3040 xmlXPathFreeObject(list
);
3043 xmlGenericError(xmlGenericErrorContext
,
3044 "%s: no such node\n", arg
);
3046 ctxt
->pctxt
->node
= NULL
;
3048 } else if (!strcmp(command
, "base")) {
3049 xmlShellBase(ctxt
, NULL
, ctxt
->node
, NULL
);
3050 } else if (!strcmp(command
, "set")) {
3051 xmlShellSetContent(ctxt
, arg
, ctxt
->node
, NULL
);
3052 #ifdef LIBXML_XPATH_ENABLED
3053 } else if (!strcmp(command
, "setns")) {
3055 xmlGenericError(xmlGenericErrorContext
,
3056 "setns: prefix=[nsuri] required\n");
3058 xmlShellRegisterNamespace(ctxt
, arg
, NULL
, NULL
);
3060 } else if (!strcmp(command
, "setrootns")) {
3063 root
= xmlDocGetRootElement(ctxt
->doc
);
3064 xmlShellRegisterRootNamespaces(ctxt
, NULL
, root
, NULL
);
3065 } else if (!strcmp(command
, "xpath")) {
3067 xmlGenericError(xmlGenericErrorContext
,
3068 "xpath: expression required\n");
3070 ctxt
->pctxt
->node
= ctxt
->node
;
3071 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3072 xmlXPathDebugDumpObject(ctxt
->output
, list
, 0);
3073 xmlXPathFreeObject(list
);
3075 #endif /* LIBXML_XPATH_ENABLED */
3076 #ifdef LIBXML_TREE_ENABLED
3077 } else if (!strcmp(command
, "setbase")) {
3078 xmlShellSetBase(ctxt
, arg
, ctxt
->node
, NULL
);
3080 } else if ((!strcmp(command
, "ls")) || (!strcmp(command
, "dir"))) {
3081 int dir
= (!strcmp(command
, "dir"));
3085 xmlShellDir(ctxt
, NULL
, ctxt
->node
, NULL
);
3087 xmlShellList(ctxt
, NULL
, ctxt
->node
, NULL
);
3089 ctxt
->pctxt
->node
= ctxt
->node
;
3090 #ifdef LIBXML_XPATH_ENABLED
3091 ctxt
->pctxt
->node
= ctxt
->node
;
3092 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3095 #endif /* LIBXML_XPATH_ENABLED */
3097 switch (list
->type
) {
3098 case XPATH_UNDEFINED
:
3099 xmlGenericError(xmlGenericErrorContext
,
3100 "%s: no such node\n", arg
);
3102 case XPATH_NODESET
:{
3105 if (list
->nodesetval
== NULL
)
3109 indx
< list
->nodesetval
->nodeNr
;
3112 xmlShellDir(ctxt
, NULL
,
3114 nodeTab
[indx
], NULL
);
3116 xmlShellList(ctxt
, NULL
,
3118 nodeTab
[indx
], NULL
);
3123 xmlGenericError(xmlGenericErrorContext
,
3124 "%s is a Boolean\n", arg
);
3127 xmlGenericError(xmlGenericErrorContext
,
3128 "%s is a number\n", arg
);
3131 xmlGenericError(xmlGenericErrorContext
,
3132 "%s is a string\n", arg
);
3135 xmlGenericError(xmlGenericErrorContext
,
3136 "%s is a point\n", arg
);
3139 xmlGenericError(xmlGenericErrorContext
,
3140 "%s is a range\n", arg
);
3142 case XPATH_LOCATIONSET
:
3143 xmlGenericError(xmlGenericErrorContext
,
3144 "%s is a range\n", arg
);
3147 xmlGenericError(xmlGenericErrorContext
,
3148 "%s is user-defined\n", arg
);
3150 case XPATH_XSLT_TREE
:
3151 xmlGenericError(xmlGenericErrorContext
,
3152 "%s is an XSLT value tree\n",
3156 #ifdef LIBXML_XPATH_ENABLED
3157 xmlXPathFreeObject(list
);
3160 xmlGenericError(xmlGenericErrorContext
,
3161 "%s: no such node\n", arg
);
3163 ctxt
->pctxt
->node
= NULL
;
3165 } else if (!strcmp(command
, "whereis")) {
3169 if (!xmlShellPwd(ctxt
, dir
, ctxt
->node
, NULL
))
3170 fprintf(ctxt
->output
, "%s\n", dir
);
3172 ctxt
->pctxt
->node
= ctxt
->node
;
3173 #ifdef LIBXML_XPATH_ENABLED
3174 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3177 #endif /* LIBXML_XPATH_ENABLED */
3179 switch (list
->type
) {
3180 case XPATH_UNDEFINED
:
3181 xmlGenericError(xmlGenericErrorContext
,
3182 "%s: no such node\n", arg
);
3184 case XPATH_NODESET
:{
3187 if (list
->nodesetval
== NULL
)
3191 indx
< list
->nodesetval
->nodeNr
;
3193 if (!xmlShellPwd(ctxt
, dir
, list
->nodesetval
->
3194 nodeTab
[indx
], NULL
))
3195 fprintf(ctxt
->output
, "%s\n", dir
);
3200 xmlGenericError(xmlGenericErrorContext
,
3201 "%s is a Boolean\n", arg
);
3204 xmlGenericError(xmlGenericErrorContext
,
3205 "%s is a number\n", arg
);
3208 xmlGenericError(xmlGenericErrorContext
,
3209 "%s is a string\n", arg
);
3212 xmlGenericError(xmlGenericErrorContext
,
3213 "%s is a point\n", arg
);
3216 xmlGenericError(xmlGenericErrorContext
,
3217 "%s is a range\n", arg
);
3219 case XPATH_LOCATIONSET
:
3220 xmlGenericError(xmlGenericErrorContext
,
3221 "%s is a range\n", arg
);
3224 xmlGenericError(xmlGenericErrorContext
,
3225 "%s is user-defined\n", arg
);
3227 case XPATH_XSLT_TREE
:
3228 xmlGenericError(xmlGenericErrorContext
,
3229 "%s is an XSLT value tree\n",
3233 #ifdef LIBXML_XPATH_ENABLED
3234 xmlXPathFreeObject(list
);
3237 xmlGenericError(xmlGenericErrorContext
,
3238 "%s: no such node\n", arg
);
3240 ctxt
->pctxt
->node
= NULL
;
3242 } else if (!strcmp(command
, "cd")) {
3244 ctxt
->node
= (xmlNodePtr
) ctxt
->doc
;
3246 #ifdef LIBXML_XPATH_ENABLED
3249 ctxt
->pctxt
->node
= ctxt
->node
;
3251 if ((l
>= 2) && (arg
[l
- 1] == '/'))
3253 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3256 #endif /* LIBXML_XPATH_ENABLED */
3258 switch (list
->type
) {
3259 case XPATH_UNDEFINED
:
3260 xmlGenericError(xmlGenericErrorContext
,
3261 "%s: no such node\n", arg
);
3264 if (list
->nodesetval
!= NULL
) {
3265 if (list
->nodesetval
->nodeNr
== 1) {
3266 ctxt
->node
= list
->nodesetval
->nodeTab
[0];
3267 if ((ctxt
->node
!= NULL
) &&
3268 (ctxt
->node
->type
==
3269 XML_NAMESPACE_DECL
)) {
3270 xmlGenericError(xmlGenericErrorContext
,
3271 "cannot cd to namespace\n");
3275 xmlGenericError(xmlGenericErrorContext
,
3276 "%s is a %d Node Set\n",
3278 list
->nodesetval
->nodeNr
);
3280 xmlGenericError(xmlGenericErrorContext
,
3281 "%s is an empty Node Set\n",
3285 xmlGenericError(xmlGenericErrorContext
,
3286 "%s is a Boolean\n", arg
);
3289 xmlGenericError(xmlGenericErrorContext
,
3290 "%s is a number\n", arg
);
3293 xmlGenericError(xmlGenericErrorContext
,
3294 "%s is a string\n", arg
);
3297 xmlGenericError(xmlGenericErrorContext
,
3298 "%s is a point\n", arg
);
3301 xmlGenericError(xmlGenericErrorContext
,
3302 "%s is a range\n", arg
);
3304 case XPATH_LOCATIONSET
:
3305 xmlGenericError(xmlGenericErrorContext
,
3306 "%s is a range\n", arg
);
3309 xmlGenericError(xmlGenericErrorContext
,
3310 "%s is user-defined\n", arg
);
3312 case XPATH_XSLT_TREE
:
3313 xmlGenericError(xmlGenericErrorContext
,
3314 "%s is an XSLT value tree\n",
3318 #ifdef LIBXML_XPATH_ENABLED
3319 xmlXPathFreeObject(list
);
3322 xmlGenericError(xmlGenericErrorContext
,
3323 "%s: no such node\n", arg
);
3325 ctxt
->pctxt
->node
= NULL
;
3327 #ifdef LIBXML_OUTPUT_ENABLED
3328 } else if (!strcmp(command
, "cat")) {
3330 xmlShellCat(ctxt
, NULL
, ctxt
->node
, NULL
);
3332 ctxt
->pctxt
->node
= ctxt
->node
;
3333 #ifdef LIBXML_XPATH_ENABLED
3334 ctxt
->pctxt
->node
= ctxt
->node
;
3335 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3338 #endif /* LIBXML_XPATH_ENABLED */
3340 switch (list
->type
) {
3341 case XPATH_UNDEFINED
:
3342 xmlGenericError(xmlGenericErrorContext
,
3343 "%s: no such node\n", arg
);
3345 case XPATH_NODESET
:{
3348 if (list
->nodesetval
== NULL
)
3352 indx
< list
->nodesetval
->nodeNr
;
3355 fprintf(ctxt
->output
, " -------\n");
3356 xmlShellCat(ctxt
, NULL
,
3358 nodeTab
[indx
], NULL
);
3363 xmlGenericError(xmlGenericErrorContext
,
3364 "%s is a Boolean\n", arg
);
3367 xmlGenericError(xmlGenericErrorContext
,
3368 "%s is a number\n", arg
);
3371 xmlGenericError(xmlGenericErrorContext
,
3372 "%s is a string\n", arg
);
3375 xmlGenericError(xmlGenericErrorContext
,
3376 "%s is a point\n", arg
);
3379 xmlGenericError(xmlGenericErrorContext
,
3380 "%s is a range\n", arg
);
3382 case XPATH_LOCATIONSET
:
3383 xmlGenericError(xmlGenericErrorContext
,
3384 "%s is a range\n", arg
);
3387 xmlGenericError(xmlGenericErrorContext
,
3388 "%s is user-defined\n", arg
);
3390 case XPATH_XSLT_TREE
:
3391 xmlGenericError(xmlGenericErrorContext
,
3392 "%s is an XSLT value tree\n",
3396 #ifdef LIBXML_XPATH_ENABLED
3397 xmlXPathFreeObject(list
);
3400 xmlGenericError(xmlGenericErrorContext
,
3401 "%s: no such node\n", arg
);
3403 ctxt
->pctxt
->node
= NULL
;
3405 #endif /* LIBXML_OUTPUT_ENABLED */
3407 xmlGenericError(xmlGenericErrorContext
,
3408 "Unknown command %s\n", command
);
3410 free(cmdline
); /* not xmlFree here ! */
3413 #ifdef LIBXML_XPATH_ENABLED
3414 xmlXPathFreeContext(ctxt
->pctxt
);
3415 #endif /* LIBXML_XPATH_ENABLED */
3417 xmlFreeDoc(ctxt
->doc
);
3419 if (ctxt
->filename
!= NULL
)
3420 xmlFree(ctxt
->filename
);
3422 if (cmdline
!= NULL
)
3423 free(cmdline
); /* not xmlFree here ! */
3426 #endif /* LIBXML_XPATH_ENABLED */
3427 #define bottom_debugXML
3428 #include "elfgcchack.h"
3429 #endif /* LIBXML_DEBUG_ENABLED */