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
17 #include <libxml/xmlmemory.h>
18 #include <libxml/tree.h>
19 #include <libxml/parser.h>
20 #include <libxml/parserInternals.h>
21 #include <libxml/valid.h>
22 #include <libxml/debugXML.h>
23 #include <libxml/HTMLtree.h>
24 #include <libxml/HTMLparser.h>
25 #include <libxml/xmlerror.h>
26 #include <libxml/globals.h>
27 #include <libxml/xpathInternals.h>
28 #include <libxml/uri.h>
29 #ifdef LIBXML_SCHEMAS_ENABLED
30 #include <libxml/relaxng.h>
33 #include "private/error.h"
35 #define DUMP_TEXT_TYPE 1
37 typedef struct _xmlDebugCtxt xmlDebugCtxt
;
38 typedef xmlDebugCtxt
*xmlDebugCtxtPtr
;
39 struct _xmlDebugCtxt
{
40 FILE *output
; /* the output file */
41 char shift
[101]; /* used for indenting */
42 int depth
; /* current depth */
43 xmlDocPtr doc
; /* current document */
44 xmlNodePtr node
; /* current node */
45 xmlDictPtr dict
; /* the doc dictionary */
46 int check
; /* do just checkings */
47 int errors
; /* number of errors found */
48 int nodict
; /* if the document has no dictionary */
49 int options
; /* options */
52 static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt
, xmlNodePtr node
);
55 xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt
)
62 ctxt
->output
= stdout
;
68 for (i
= 0; i
< 100; i
++)
74 xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED
)
76 /* remove the ATTRIBUTE_UNUSED when this is added */
82 * @ns: the namespace node
84 * Check that a given namespace is in scope on a node.
86 * Returns 1 if in scope, -1 in case of argument error,
87 * -2 if the namespace is not in scope, and -3 if not on
91 xmlNsCheckScope(xmlNodePtr node
, xmlNsPtr ns
)
95 if ((node
== NULL
) || (ns
== NULL
))
98 if ((node
->type
!= XML_ELEMENT_NODE
) &&
99 (node
->type
!= XML_ATTRIBUTE_NODE
) &&
100 (node
->type
!= XML_DOCUMENT_NODE
) &&
101 (node
->type
!= XML_TEXT_NODE
) &&
102 (node
->type
!= XML_HTML_DOCUMENT_NODE
) &&
103 (node
->type
!= XML_XINCLUDE_START
))
106 while ((node
!= NULL
) &&
107 ((node
->type
== XML_ELEMENT_NODE
) ||
108 (node
->type
== XML_ATTRIBUTE_NODE
) ||
109 (node
->type
== XML_TEXT_NODE
) ||
110 (node
->type
== XML_XINCLUDE_START
))) {
111 if ((node
->type
== XML_ELEMENT_NODE
) ||
112 (node
->type
== XML_XINCLUDE_START
)) {
114 while (cur
!= NULL
) {
117 if (xmlStrEqual(cur
->prefix
, ns
->prefix
))
124 /* the xml namespace may be declared on the document node */
125 if ((node
!= NULL
) &&
126 ((node
->type
== XML_DOCUMENT_NODE
) ||
127 (node
->type
== XML_HTML_DOCUMENT_NODE
))) {
128 xmlNsPtr oldNs
= ((xmlDocPtr
) node
)->oldNs
;
136 xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt
)
140 if ((ctxt
->output
!= NULL
) && (ctxt
->depth
> 0)) {
141 if (ctxt
->depth
< 50)
142 fprintf(ctxt
->output
, "%s", &ctxt
->shift
[100 - 2 * ctxt
->depth
]);
144 fprintf(ctxt
->output
, "%s", ctxt
->shift
);
150 * @ctxt: a debug context
151 * @error: the error code
153 * Handle a debug error.
156 xmlDebugErr(xmlDebugCtxtPtr ctxt
, int error
, const char *msg
)
159 __xmlRaiseError(NULL
, NULL
, NULL
,
160 NULL
, ctxt
->node
, XML_FROM_CHECK
,
161 error
, XML_ERR_ERROR
, NULL
, 0,
162 NULL
, NULL
, NULL
, 0, 0,
165 static void LIBXML_ATTR_FORMAT(3,0)
166 xmlDebugErr2(xmlDebugCtxtPtr ctxt
, int error
, const char *msg
, int extra
)
169 __xmlRaiseError(NULL
, NULL
, NULL
,
170 NULL
, ctxt
->node
, XML_FROM_CHECK
,
171 error
, XML_ERR_ERROR
, NULL
, 0,
172 NULL
, NULL
, NULL
, 0, 0,
175 static void LIBXML_ATTR_FORMAT(3,0)
176 xmlDebugErr3(xmlDebugCtxtPtr ctxt
, int error
, const char *msg
, const char *extra
)
179 __xmlRaiseError(NULL
, NULL
, NULL
,
180 NULL
, ctxt
->node
, XML_FROM_CHECK
,
181 error
, XML_ERR_ERROR
, NULL
, 0,
182 NULL
, NULL
, NULL
, 0, 0,
187 * xmlCtxtNsCheckScope:
188 * @ctxt: the debugging context
190 * @ns: the namespace node
192 * Report if a given namespace is is not in scope.
195 xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt
, xmlNodePtr node
, xmlNsPtr ns
)
199 ret
= xmlNsCheckScope(node
, ns
);
201 if (ns
->prefix
== NULL
)
202 xmlDebugErr(ctxt
, XML_CHECK_NS_SCOPE
,
203 "Reference to default namespace not in scope\n");
205 xmlDebugErr3(ctxt
, XML_CHECK_NS_SCOPE
,
206 "Reference to namespace '%s' not in scope\n",
207 (char *) ns
->prefix
);
210 if (ns
->prefix
== NULL
)
211 xmlDebugErr(ctxt
, XML_CHECK_NS_ANCESTOR
,
212 "Reference to default namespace not on ancestor\n");
214 xmlDebugErr3(ctxt
, XML_CHECK_NS_ANCESTOR
,
215 "Reference to namespace '%s' not on ancestor\n",
216 (char *) ns
->prefix
);
221 * xmlCtxtCheckString:
222 * @ctxt: the debug context
225 * Do debugging on the string, currently it just checks the UTF-8 content
228 xmlCtxtCheckString(xmlDebugCtxtPtr ctxt
, const xmlChar
* str
)
230 if (str
== NULL
) return;
232 if (!xmlCheckUTF8(str
)) {
233 xmlDebugErr3(ctxt
, XML_CHECK_NOT_UTF8
,
234 "String is not UTF-8 %s", (const char *) str
);
241 * @ctxt: the debug context
244 * Do debugging on the name, for example the dictionary status and
245 * conformance to the Name production.
248 xmlCtxtCheckName(xmlDebugCtxtPtr ctxt
, const xmlChar
* name
)
252 xmlDebugErr(ctxt
, XML_CHECK_NO_NAME
, "Name is NULL");
255 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
256 if (xmlValidateName(name
, 0)) {
257 xmlDebugErr3(ctxt
, XML_CHECK_NOT_NCNAME
,
258 "Name is not an NCName '%s'", (const char *) name
);
261 if ((ctxt
->dict
!= NULL
) &&
262 (!xmlDictOwns(ctxt
->dict
, name
)) &&
263 ((ctxt
->doc
== NULL
) ||
264 ((ctxt
->doc
->parseFlags
& (XML_PARSE_SAX1
| XML_PARSE_NODICT
)) == 0))) {
265 xmlDebugErr3(ctxt
, XML_CHECK_OUTSIDE_DICT
,
266 "Name is not from the document dictionary '%s'",
267 (const char *) name
);
273 xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt
, xmlNodePtr node
) {
279 if (node
->parent
== NULL
)
280 xmlDebugErr(ctxt
, XML_CHECK_NO_PARENT
,
281 "Node has no parent\n");
282 if (node
->doc
== NULL
) {
283 xmlDebugErr(ctxt
, XML_CHECK_NO_DOC
,
284 "Node has no doc\n");
288 if ((dict
== NULL
) && (ctxt
->nodict
== 0)) {
290 /* deactivated right now as it raises too many errors */
291 if (doc
->type
== XML_DOCUMENT_NODE
)
292 xmlDebugErr(ctxt
, XML_CHECK_NO_DICT
,
293 "Document has no dictionary\n");
297 if (ctxt
->doc
== NULL
)
300 if (ctxt
->dict
== NULL
) {
304 if ((node
->parent
!= NULL
) && (node
->doc
!= node
->parent
->doc
) &&
305 (!xmlStrEqual(node
->name
, BAD_CAST
"pseudoroot")))
306 xmlDebugErr(ctxt
, XML_CHECK_WRONG_DOC
,
307 "Node doc differs from parent's one\n");
308 if (node
->prev
== NULL
) {
309 if (node
->type
== XML_ATTRIBUTE_NODE
) {
310 if ((node
->parent
!= NULL
) &&
311 (node
!= (xmlNodePtr
) node
->parent
->properties
))
312 xmlDebugErr(ctxt
, XML_CHECK_NO_PREV
,
313 "Attr has no prev and not first of attr list\n");
315 } else if ((node
->parent
!= NULL
) && (node
->parent
->children
!= node
))
316 xmlDebugErr(ctxt
, XML_CHECK_NO_PREV
,
317 "Node has no prev and not first of parent list\n");
319 if (node
->prev
->next
!= node
)
320 xmlDebugErr(ctxt
, XML_CHECK_WRONG_PREV
,
321 "Node prev->next : back link wrong\n");
323 if (node
->next
== NULL
) {
324 if ((node
->parent
!= NULL
) && (node
->type
!= XML_ATTRIBUTE_NODE
) &&
325 (node
->parent
->last
!= node
) &&
326 (node
->parent
->type
== XML_ELEMENT_NODE
))
327 xmlDebugErr(ctxt
, XML_CHECK_NO_NEXT
,
328 "Node has no next and not last of parent list\n");
330 if (node
->next
->prev
!= node
)
331 xmlDebugErr(ctxt
, XML_CHECK_WRONG_NEXT
,
332 "Node next->prev : forward link wrong\n");
333 if (node
->next
->parent
!= node
->parent
)
334 xmlDebugErr(ctxt
, XML_CHECK_WRONG_PARENT
,
335 "Node next->prev : forward link wrong\n");
337 if (node
->type
== XML_ELEMENT_NODE
) {
342 xmlCtxtNsCheckScope(ctxt
, node
, ns
);
345 if (node
->ns
!= NULL
)
346 xmlCtxtNsCheckScope(ctxt
, node
, node
->ns
);
347 } else if (node
->type
== XML_ATTRIBUTE_NODE
) {
348 if (node
->ns
!= NULL
)
349 xmlCtxtNsCheckScope(ctxt
, node
, node
->ns
);
352 if ((node
->type
!= XML_ELEMENT_NODE
) &&
353 (node
->type
!= XML_ATTRIBUTE_NODE
) &&
354 (node
->type
!= XML_ELEMENT_DECL
) &&
355 (node
->type
!= XML_ATTRIBUTE_DECL
) &&
356 (node
->type
!= XML_DTD_NODE
) &&
357 (node
->type
!= XML_HTML_DOCUMENT_NODE
) &&
358 (node
->type
!= XML_DOCUMENT_NODE
)) {
359 if (node
->content
!= NULL
)
360 xmlCtxtCheckString(ctxt
, (const xmlChar
*) node
->content
);
362 switch (node
->type
) {
363 case XML_ELEMENT_NODE
:
364 case XML_ATTRIBUTE_NODE
:
365 xmlCtxtCheckName(ctxt
, node
->name
);
368 if ((node
->name
== xmlStringText
) ||
369 (node
->name
== xmlStringTextNoenc
))
371 /* some case of entity substitution can lead to this */
372 if ((ctxt
->dict
!= NULL
) &&
373 (node
->name
== xmlDictLookup(ctxt
->dict
, BAD_CAST
"nbktext",
377 xmlDebugErr3(ctxt
, XML_CHECK_WRONG_NAME
,
378 "Text node has wrong name '%s'",
379 (const char *) node
->name
);
381 case XML_COMMENT_NODE
:
382 if (node
->name
== xmlStringComment
)
384 xmlDebugErr3(ctxt
, XML_CHECK_WRONG_NAME
,
385 "Comment node has wrong name '%s'",
386 (const char *) node
->name
);
389 xmlCtxtCheckName(ctxt
, node
->name
);
391 case XML_CDATA_SECTION_NODE
:
392 if (node
->name
== NULL
)
394 xmlDebugErr3(ctxt
, XML_CHECK_NAME_NOT_NULL
,
395 "CData section has non NULL name '%s'",
396 (const char *) node
->name
);
398 case XML_ENTITY_REF_NODE
:
399 case XML_ENTITY_NODE
:
400 case XML_DOCUMENT_TYPE_NODE
:
401 case XML_DOCUMENT_FRAG_NODE
:
402 case XML_NOTATION_NODE
:
404 case XML_ELEMENT_DECL
:
405 case XML_ATTRIBUTE_DECL
:
406 case XML_ENTITY_DECL
:
407 case XML_NAMESPACE_DECL
:
408 case XML_XINCLUDE_START
:
409 case XML_XINCLUDE_END
:
410 case XML_DOCUMENT_NODE
:
411 case XML_HTML_DOCUMENT_NODE
:
417 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt
, const xmlChar
* str
)
424 /* TODO: check UTF8 content of the string */
426 fprintf(ctxt
->output
, "(NULL)");
429 for (i
= 0; i
< 40; i
++)
432 else if (IS_BLANK_CH(str
[i
]))
433 fputc(' ', ctxt
->output
);
434 else if (str
[i
] >= 0x80)
435 fprintf(ctxt
->output
, "#%X", str
[i
]);
437 fputc(str
[i
], ctxt
->output
);
438 fprintf(ctxt
->output
, "...");
442 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt
, xmlDtdPtr dtd
)
444 xmlCtxtDumpSpaces(ctxt
);
448 fprintf(ctxt
->output
, "DTD node is NULL\n");
452 if (dtd
->type
!= XML_DTD_NODE
) {
453 xmlDebugErr(ctxt
, XML_CHECK_NOT_DTD
,
454 "Node is not a DTD");
458 if (dtd
->name
!= NULL
)
459 fprintf(ctxt
->output
, "DTD(%s)", (char *) dtd
->name
);
461 fprintf(ctxt
->output
, "DTD");
462 if (dtd
->ExternalID
!= NULL
)
463 fprintf(ctxt
->output
, ", PUBLIC %s", (char *) dtd
->ExternalID
);
464 if (dtd
->SystemID
!= NULL
)
465 fprintf(ctxt
->output
, ", SYSTEM %s", (char *) dtd
->SystemID
);
466 fprintf(ctxt
->output
, "\n");
469 * Do a bit of checking
471 xmlCtxtGenericNodeCheck(ctxt
, (xmlNodePtr
) dtd
);
475 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt
, xmlAttributePtr attr
)
477 xmlCtxtDumpSpaces(ctxt
);
481 fprintf(ctxt
->output
, "Attribute declaration is NULL\n");
484 if (attr
->type
!= XML_ATTRIBUTE_DECL
) {
485 xmlDebugErr(ctxt
, XML_CHECK_NOT_ATTR_DECL
,
486 "Node is not an attribute declaration");
489 if (attr
->name
!= NULL
) {
491 fprintf(ctxt
->output
, "ATTRDECL(%s)", (char *) attr
->name
);
493 xmlDebugErr(ctxt
, XML_CHECK_NO_NAME
,
494 "Node attribute declaration has no name");
495 if (attr
->elem
!= NULL
) {
497 fprintf(ctxt
->output
, " for %s", (char *) attr
->elem
);
499 xmlDebugErr(ctxt
, XML_CHECK_NO_ELEM
,
500 "Node attribute declaration has no element name");
502 switch (attr
->atype
) {
503 case XML_ATTRIBUTE_CDATA
:
504 fprintf(ctxt
->output
, " CDATA");
506 case XML_ATTRIBUTE_ID
:
507 fprintf(ctxt
->output
, " ID");
509 case XML_ATTRIBUTE_IDREF
:
510 fprintf(ctxt
->output
, " IDREF");
512 case XML_ATTRIBUTE_IDREFS
:
513 fprintf(ctxt
->output
, " IDREFS");
515 case XML_ATTRIBUTE_ENTITY
:
516 fprintf(ctxt
->output
, " ENTITY");
518 case XML_ATTRIBUTE_ENTITIES
:
519 fprintf(ctxt
->output
, " ENTITIES");
521 case XML_ATTRIBUTE_NMTOKEN
:
522 fprintf(ctxt
->output
, " NMTOKEN");
524 case XML_ATTRIBUTE_NMTOKENS
:
525 fprintf(ctxt
->output
, " NMTOKENS");
527 case XML_ATTRIBUTE_ENUMERATION
:
528 fprintf(ctxt
->output
, " ENUMERATION");
530 case XML_ATTRIBUTE_NOTATION
:
531 fprintf(ctxt
->output
, " NOTATION ");
534 if (attr
->tree
!= NULL
) {
536 xmlEnumerationPtr cur
= attr
->tree
;
538 for (indx
= 0; indx
< 5; indx
++) {
540 fprintf(ctxt
->output
, "|%s", (char *) cur
->name
);
542 fprintf(ctxt
->output
, " (%s", (char *) cur
->name
);
548 fprintf(ctxt
->output
, ")");
550 fprintf(ctxt
->output
, "...)");
553 case XML_ATTRIBUTE_NONE
:
555 case XML_ATTRIBUTE_REQUIRED
:
556 fprintf(ctxt
->output
, " REQUIRED");
558 case XML_ATTRIBUTE_IMPLIED
:
559 fprintf(ctxt
->output
, " IMPLIED");
561 case XML_ATTRIBUTE_FIXED
:
562 fprintf(ctxt
->output
, " FIXED");
565 if (attr
->defaultValue
!= NULL
) {
566 fprintf(ctxt
->output
, "\"");
567 xmlCtxtDumpString(ctxt
, attr
->defaultValue
);
568 fprintf(ctxt
->output
, "\"");
570 fprintf(ctxt
->output
, "\n");
574 * Do a bit of checking
576 xmlCtxtGenericNodeCheck(ctxt
, (xmlNodePtr
) attr
);
580 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt
, xmlElementPtr elem
)
582 xmlCtxtDumpSpaces(ctxt
);
586 fprintf(ctxt
->output
, "Element declaration is NULL\n");
589 if (elem
->type
!= XML_ELEMENT_DECL
) {
590 xmlDebugErr(ctxt
, XML_CHECK_NOT_ELEM_DECL
,
591 "Node is not an element declaration");
594 if (elem
->name
!= NULL
) {
596 fprintf(ctxt
->output
, "ELEMDECL(");
597 xmlCtxtDumpString(ctxt
, elem
->name
);
598 fprintf(ctxt
->output
, ")");
601 xmlDebugErr(ctxt
, XML_CHECK_NO_NAME
,
602 "Element declaration has no name");
604 switch (elem
->etype
) {
605 case XML_ELEMENT_TYPE_UNDEFINED
:
606 fprintf(ctxt
->output
, ", UNDEFINED");
608 case XML_ELEMENT_TYPE_EMPTY
:
609 fprintf(ctxt
->output
, ", EMPTY");
611 case XML_ELEMENT_TYPE_ANY
:
612 fprintf(ctxt
->output
, ", ANY");
614 case XML_ELEMENT_TYPE_MIXED
:
615 fprintf(ctxt
->output
, ", MIXED ");
617 case XML_ELEMENT_TYPE_ELEMENT
:
618 fprintf(ctxt
->output
, ", MIXED ");
621 if ((elem
->type
!= XML_ELEMENT_NODE
) && (elem
->content
!= NULL
)) {
625 xmlSnprintfElementContent(buf
, 5000, elem
->content
, 1);
627 fprintf(ctxt
->output
, "%s", buf
);
629 fprintf(ctxt
->output
, "\n");
633 * Do a bit of checking
635 xmlCtxtGenericNodeCheck(ctxt
, (xmlNodePtr
) elem
);
639 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt
, xmlEntityPtr ent
)
641 xmlCtxtDumpSpaces(ctxt
);
645 fprintf(ctxt
->output
, "Entity declaration is NULL\n");
648 if (ent
->type
!= XML_ENTITY_DECL
) {
649 xmlDebugErr(ctxt
, XML_CHECK_NOT_ENTITY_DECL
,
650 "Node is not an entity declaration");
653 if (ent
->name
!= NULL
) {
655 fprintf(ctxt
->output
, "ENTITYDECL(");
656 xmlCtxtDumpString(ctxt
, ent
->name
);
657 fprintf(ctxt
->output
, ")");
660 xmlDebugErr(ctxt
, XML_CHECK_NO_NAME
,
661 "Entity declaration has no name");
663 switch (ent
->etype
) {
664 case XML_INTERNAL_GENERAL_ENTITY
:
665 fprintf(ctxt
->output
, ", internal\n");
667 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
668 fprintf(ctxt
->output
, ", external parsed\n");
670 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
671 fprintf(ctxt
->output
, ", unparsed\n");
673 case XML_INTERNAL_PARAMETER_ENTITY
:
674 fprintf(ctxt
->output
, ", parameter\n");
676 case XML_EXTERNAL_PARAMETER_ENTITY
:
677 fprintf(ctxt
->output
, ", external parameter\n");
679 case XML_INTERNAL_PREDEFINED_ENTITY
:
680 fprintf(ctxt
->output
, ", predefined\n");
683 if (ent
->ExternalID
) {
684 xmlCtxtDumpSpaces(ctxt
);
685 fprintf(ctxt
->output
, " ExternalID=%s\n",
686 (char *) ent
->ExternalID
);
689 xmlCtxtDumpSpaces(ctxt
);
690 fprintf(ctxt
->output
, " SystemID=%s\n",
691 (char *) ent
->SystemID
);
693 if (ent
->URI
!= NULL
) {
694 xmlCtxtDumpSpaces(ctxt
);
695 fprintf(ctxt
->output
, " URI=%s\n", (char *) ent
->URI
);
698 xmlCtxtDumpSpaces(ctxt
);
699 fprintf(ctxt
->output
, " content=");
700 xmlCtxtDumpString(ctxt
, ent
->content
);
701 fprintf(ctxt
->output
, "\n");
706 * Do a bit of checking
708 xmlCtxtGenericNodeCheck(ctxt
, (xmlNodePtr
) ent
);
712 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt
, xmlNsPtr ns
)
714 xmlCtxtDumpSpaces(ctxt
);
718 fprintf(ctxt
->output
, "namespace node is NULL\n");
721 if (ns
->type
!= XML_NAMESPACE_DECL
) {
722 xmlDebugErr(ctxt
, XML_CHECK_NOT_NS_DECL
,
723 "Node is not a namespace declaration");
726 if (ns
->href
== NULL
) {
727 if (ns
->prefix
!= NULL
)
728 xmlDebugErr3(ctxt
, XML_CHECK_NO_HREF
,
729 "Incomplete namespace %s href=NULL\n",
730 (char *) ns
->prefix
);
732 xmlDebugErr(ctxt
, XML_CHECK_NO_HREF
,
733 "Incomplete default namespace href=NULL\n");
736 if (ns
->prefix
!= NULL
)
737 fprintf(ctxt
->output
, "namespace %s href=",
738 (char *) ns
->prefix
);
740 fprintf(ctxt
->output
, "default namespace href=");
742 xmlCtxtDumpString(ctxt
, ns
->href
);
743 fprintf(ctxt
->output
, "\n");
749 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt
, xmlNsPtr ns
)
752 xmlCtxtDumpNamespace(ctxt
, ns
);
758 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt
, xmlEntityPtr ent
)
760 xmlCtxtDumpSpaces(ctxt
);
764 fprintf(ctxt
->output
, "Entity is NULL\n");
768 switch (ent
->etype
) {
769 case XML_INTERNAL_GENERAL_ENTITY
:
770 fprintf(ctxt
->output
, "INTERNAL_GENERAL_ENTITY ");
772 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
773 fprintf(ctxt
->output
, "EXTERNAL_GENERAL_PARSED_ENTITY ");
775 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
776 fprintf(ctxt
->output
, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
778 case XML_INTERNAL_PARAMETER_ENTITY
:
779 fprintf(ctxt
->output
, "INTERNAL_PARAMETER_ENTITY ");
781 case XML_EXTERNAL_PARAMETER_ENTITY
:
782 fprintf(ctxt
->output
, "EXTERNAL_PARAMETER_ENTITY ");
785 fprintf(ctxt
->output
, "ENTITY_%d ! ", (int) ent
->etype
);
787 fprintf(ctxt
->output
, "%s\n", ent
->name
);
788 if (ent
->ExternalID
) {
789 xmlCtxtDumpSpaces(ctxt
);
790 fprintf(ctxt
->output
, "ExternalID=%s\n",
791 (char *) ent
->ExternalID
);
794 xmlCtxtDumpSpaces(ctxt
);
795 fprintf(ctxt
->output
, "SystemID=%s\n", (char *) ent
->SystemID
);
798 xmlCtxtDumpSpaces(ctxt
);
799 fprintf(ctxt
->output
, "URI=%s\n", (char *) ent
->URI
);
802 xmlCtxtDumpSpaces(ctxt
);
803 fprintf(ctxt
->output
, "content=");
804 xmlCtxtDumpString(ctxt
, ent
->content
);
805 fprintf(ctxt
->output
, "\n");
812 * @output: the FILE * for the output
813 * @attr: the attribute
814 * @depth: the indentation level.
816 * Dumps debug information for the attribute
819 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt
, xmlAttrPtr attr
)
821 xmlCtxtDumpSpaces(ctxt
);
825 fprintf(ctxt
->output
, "Attr is NULL");
829 fprintf(ctxt
->output
, "ATTRIBUTE ");
830 xmlCtxtDumpString(ctxt
, attr
->name
);
831 fprintf(ctxt
->output
, "\n");
832 if (attr
->children
!= NULL
) {
834 xmlCtxtDumpNodeList(ctxt
, attr
->children
);
838 if (attr
->name
== NULL
)
839 xmlDebugErr(ctxt
, XML_CHECK_NO_NAME
,
840 "Attribute has no name");
843 * Do a bit of checking
845 xmlCtxtGenericNodeCheck(ctxt
, (xmlNodePtr
) attr
);
849 * xmlCtxtDumpAttrList:
850 * @output: the FILE * for the output
851 * @attr: the attribute list
852 * @depth: the indentation level.
854 * Dumps debug information for the attribute list
857 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt
, xmlAttrPtr attr
)
859 while (attr
!= NULL
) {
860 xmlCtxtDumpAttr(ctxt
, attr
);
866 * xmlCtxtDumpOneNode:
867 * @output: the FILE * for the output
869 * @depth: the indentation level.
871 * Dumps debug information for the element node, it is not recursive
874 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt
, xmlNodePtr node
)
878 xmlCtxtDumpSpaces(ctxt
);
879 fprintf(ctxt
->output
, "node is NULL\n");
885 switch (node
->type
) {
886 case XML_ELEMENT_NODE
:
888 xmlCtxtDumpSpaces(ctxt
);
889 fprintf(ctxt
->output
, "ELEMENT ");
890 if ((node
->ns
!= NULL
) && (node
->ns
->prefix
!= NULL
)) {
891 xmlCtxtDumpString(ctxt
, node
->ns
->prefix
);
892 fprintf(ctxt
->output
, ":");
894 xmlCtxtDumpString(ctxt
, node
->name
);
895 fprintf(ctxt
->output
, "\n");
898 case XML_ATTRIBUTE_NODE
:
900 xmlCtxtDumpSpaces(ctxt
);
901 fprintf(ctxt
->output
, "Error, ATTRIBUTE found here\n");
902 xmlCtxtGenericNodeCheck(ctxt
, node
);
906 xmlCtxtDumpSpaces(ctxt
);
907 if (node
->name
== (const xmlChar
*) xmlStringTextNoenc
)
908 fprintf(ctxt
->output
, "TEXT no enc");
910 fprintf(ctxt
->output
, "TEXT");
911 if (ctxt
->options
& DUMP_TEXT_TYPE
) {
912 if (node
->content
== (xmlChar
*) &(node
->properties
))
913 fprintf(ctxt
->output
, " compact\n");
914 else if (xmlDictOwns(ctxt
->dict
, node
->content
) == 1)
915 fprintf(ctxt
->output
, " interned\n");
917 fprintf(ctxt
->output
, "\n");
919 fprintf(ctxt
->output
, "\n");
922 case XML_CDATA_SECTION_NODE
:
924 xmlCtxtDumpSpaces(ctxt
);
925 fprintf(ctxt
->output
, "CDATA_SECTION\n");
928 case XML_ENTITY_REF_NODE
:
930 xmlCtxtDumpSpaces(ctxt
);
931 fprintf(ctxt
->output
, "ENTITY_REF(%s)\n",
932 (char *) node
->name
);
935 case XML_ENTITY_NODE
:
937 xmlCtxtDumpSpaces(ctxt
);
938 fprintf(ctxt
->output
, "ENTITY\n");
943 xmlCtxtDumpSpaces(ctxt
);
944 fprintf(ctxt
->output
, "PI %s\n", (char *) node
->name
);
947 case XML_COMMENT_NODE
:
949 xmlCtxtDumpSpaces(ctxt
);
950 fprintf(ctxt
->output
, "COMMENT\n");
953 case XML_DOCUMENT_NODE
:
954 case XML_HTML_DOCUMENT_NODE
:
956 xmlCtxtDumpSpaces(ctxt
);
958 fprintf(ctxt
->output
, "Error, DOCUMENT found here\n");
959 xmlCtxtGenericNodeCheck(ctxt
, node
);
961 case XML_DOCUMENT_TYPE_NODE
:
963 xmlCtxtDumpSpaces(ctxt
);
964 fprintf(ctxt
->output
, "DOCUMENT_TYPE\n");
967 case XML_DOCUMENT_FRAG_NODE
:
969 xmlCtxtDumpSpaces(ctxt
);
970 fprintf(ctxt
->output
, "DOCUMENT_FRAG\n");
973 case XML_NOTATION_NODE
:
975 xmlCtxtDumpSpaces(ctxt
);
976 fprintf(ctxt
->output
, "NOTATION\n");
980 xmlCtxtDumpDtdNode(ctxt
, (xmlDtdPtr
) node
);
982 case XML_ELEMENT_DECL
:
983 xmlCtxtDumpElemDecl(ctxt
, (xmlElementPtr
) node
);
985 case XML_ATTRIBUTE_DECL
:
986 xmlCtxtDumpAttrDecl(ctxt
, (xmlAttributePtr
) node
);
988 case XML_ENTITY_DECL
:
989 xmlCtxtDumpEntityDecl(ctxt
, (xmlEntityPtr
) node
);
991 case XML_NAMESPACE_DECL
:
992 xmlCtxtDumpNamespace(ctxt
, (xmlNsPtr
) node
);
994 case XML_XINCLUDE_START
:
996 xmlCtxtDumpSpaces(ctxt
);
997 fprintf(ctxt
->output
, "INCLUDE START\n");
1000 case XML_XINCLUDE_END
:
1002 xmlCtxtDumpSpaces(ctxt
);
1003 fprintf(ctxt
->output
, "INCLUDE END\n");
1008 xmlCtxtDumpSpaces(ctxt
);
1009 xmlDebugErr2(ctxt
, XML_CHECK_UNKNOWN_NODE
,
1010 "Unknown node type %d\n", node
->type
);
1013 if (node
->doc
== NULL
) {
1015 xmlCtxtDumpSpaces(ctxt
);
1017 fprintf(ctxt
->output
, "PBM: doc == NULL !!!\n");
1020 if ((node
->type
== XML_ELEMENT_NODE
) && (node
->nsDef
!= NULL
))
1021 xmlCtxtDumpNamespaceList(ctxt
, node
->nsDef
);
1022 if ((node
->type
== XML_ELEMENT_NODE
) && (node
->properties
!= NULL
))
1023 xmlCtxtDumpAttrList(ctxt
, node
->properties
);
1024 if (node
->type
!= XML_ENTITY_REF_NODE
) {
1025 if ((node
->type
!= XML_ELEMENT_NODE
) && (node
->content
!= NULL
)) {
1027 xmlCtxtDumpSpaces(ctxt
);
1028 fprintf(ctxt
->output
, "content=");
1029 xmlCtxtDumpString(ctxt
, node
->content
);
1030 fprintf(ctxt
->output
, "\n");
1036 ent
= xmlGetDocEntity(node
->doc
, node
->name
);
1038 xmlCtxtDumpEntity(ctxt
, ent
);
1043 * Do a bit of checking
1045 xmlCtxtGenericNodeCheck(ctxt
, node
);
1050 * @output: the FILE * for the output
1052 * @depth: the indentation level.
1054 * Dumps debug information for the element node, it is recursive
1057 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt
, xmlNodePtr node
)
1061 xmlCtxtDumpSpaces(ctxt
);
1062 fprintf(ctxt
->output
, "node is NULL\n");
1066 xmlCtxtDumpOneNode(ctxt
, node
);
1067 if ((node
->type
!= XML_NAMESPACE_DECL
) &&
1068 (node
->children
!= NULL
) && (node
->type
!= XML_ENTITY_REF_NODE
)) {
1070 xmlCtxtDumpNodeList(ctxt
, node
->children
);
1076 * xmlCtxtDumpNodeList:
1077 * @output: the FILE * for the output
1078 * @node: the node list
1079 * @depth: the indentation level.
1081 * Dumps debug information for the list of element node, it is recursive
1084 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt
, xmlNodePtr node
)
1086 while (node
!= NULL
) {
1087 xmlCtxtDumpNode(ctxt
, node
);
1093 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt
, xmlDocPtr doc
)
1097 fprintf(ctxt
->output
, "DOCUMENT == NULL !\n");
1100 ctxt
->node
= (xmlNodePtr
) doc
;
1102 switch (doc
->type
) {
1103 case XML_ELEMENT_NODE
:
1104 xmlDebugErr(ctxt
, XML_CHECK_FOUND_ELEMENT
,
1105 "Misplaced ELEMENT node\n");
1107 case XML_ATTRIBUTE_NODE
:
1108 xmlDebugErr(ctxt
, XML_CHECK_FOUND_ATTRIBUTE
,
1109 "Misplaced ATTRIBUTE node\n");
1112 xmlDebugErr(ctxt
, XML_CHECK_FOUND_TEXT
,
1113 "Misplaced TEXT node\n");
1115 case XML_CDATA_SECTION_NODE
:
1116 xmlDebugErr(ctxt
, XML_CHECK_FOUND_CDATA
,
1117 "Misplaced CDATA node\n");
1119 case XML_ENTITY_REF_NODE
:
1120 xmlDebugErr(ctxt
, XML_CHECK_FOUND_ENTITYREF
,
1121 "Misplaced ENTITYREF node\n");
1123 case XML_ENTITY_NODE
:
1124 xmlDebugErr(ctxt
, XML_CHECK_FOUND_ENTITY
,
1125 "Misplaced ENTITY node\n");
1128 xmlDebugErr(ctxt
, XML_CHECK_FOUND_PI
,
1129 "Misplaced PI node\n");
1131 case XML_COMMENT_NODE
:
1132 xmlDebugErr(ctxt
, XML_CHECK_FOUND_COMMENT
,
1133 "Misplaced COMMENT node\n");
1135 case XML_DOCUMENT_NODE
:
1137 fprintf(ctxt
->output
, "DOCUMENT\n");
1139 case XML_HTML_DOCUMENT_NODE
:
1141 fprintf(ctxt
->output
, "HTML DOCUMENT\n");
1143 case XML_DOCUMENT_TYPE_NODE
:
1144 xmlDebugErr(ctxt
, XML_CHECK_FOUND_DOCTYPE
,
1145 "Misplaced DOCTYPE node\n");
1147 case XML_DOCUMENT_FRAG_NODE
:
1148 xmlDebugErr(ctxt
, XML_CHECK_FOUND_FRAGMENT
,
1149 "Misplaced FRAGMENT node\n");
1151 case XML_NOTATION_NODE
:
1152 xmlDebugErr(ctxt
, XML_CHECK_FOUND_NOTATION
,
1153 "Misplaced NOTATION node\n");
1156 xmlDebugErr2(ctxt
, XML_CHECK_UNKNOWN_NODE
,
1157 "Unknown node type %d\n", doc
->type
);
1162 * xmlCtxtDumpDocumentHead:
1163 * @output: the FILE * for the output
1164 * @doc: the document
1166 * Dumps debug information concerning the document, not recursive
1169 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt
, xmlDocPtr doc
)
1171 if (doc
== NULL
) return;
1172 xmlCtxtDumpDocHead(ctxt
, doc
);
1174 if (doc
->name
!= NULL
) {
1175 fprintf(ctxt
->output
, "name=");
1176 xmlCtxtDumpString(ctxt
, BAD_CAST doc
->name
);
1177 fprintf(ctxt
->output
, "\n");
1179 if (doc
->version
!= NULL
) {
1180 fprintf(ctxt
->output
, "version=");
1181 xmlCtxtDumpString(ctxt
, doc
->version
);
1182 fprintf(ctxt
->output
, "\n");
1184 if (doc
->encoding
!= NULL
) {
1185 fprintf(ctxt
->output
, "encoding=");
1186 xmlCtxtDumpString(ctxt
, doc
->encoding
);
1187 fprintf(ctxt
->output
, "\n");
1189 if (doc
->URL
!= NULL
) {
1190 fprintf(ctxt
->output
, "URL=");
1191 xmlCtxtDumpString(ctxt
, doc
->URL
);
1192 fprintf(ctxt
->output
, "\n");
1194 if (doc
->standalone
)
1195 fprintf(ctxt
->output
, "standalone=true\n");
1197 if (doc
->oldNs
!= NULL
)
1198 xmlCtxtDumpNamespaceList(ctxt
, doc
->oldNs
);
1202 * xmlCtxtDumpDocument:
1203 * @output: the FILE * for the output
1204 * @doc: the document
1206 * Dumps debug information for the document, it's recursive
1209 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt
, xmlDocPtr doc
)
1213 fprintf(ctxt
->output
, "DOCUMENT == NULL !\n");
1216 xmlCtxtDumpDocumentHead(ctxt
, doc
);
1217 if (((doc
->type
== XML_DOCUMENT_NODE
) ||
1218 (doc
->type
== XML_HTML_DOCUMENT_NODE
))
1219 && (doc
->children
!= NULL
)) {
1221 xmlCtxtDumpNodeList(ctxt
, doc
->children
);
1227 xmlCtxtDumpEntityCallback(void *payload
, void *data
,
1228 const xmlChar
*name ATTRIBUTE_UNUSED
)
1230 xmlEntityPtr cur
= (xmlEntityPtr
) payload
;
1231 xmlDebugCtxtPtr ctxt
= (xmlDebugCtxtPtr
) data
;
1234 fprintf(ctxt
->output
, "Entity is NULL");
1238 fprintf(ctxt
->output
, "%s : ", (char *) cur
->name
);
1239 switch (cur
->etype
) {
1240 case XML_INTERNAL_GENERAL_ENTITY
:
1241 fprintf(ctxt
->output
, "INTERNAL GENERAL, ");
1243 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
1244 fprintf(ctxt
->output
, "EXTERNAL PARSED, ");
1246 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
1247 fprintf(ctxt
->output
, "EXTERNAL UNPARSED, ");
1249 case XML_INTERNAL_PARAMETER_ENTITY
:
1250 fprintf(ctxt
->output
, "INTERNAL PARAMETER, ");
1252 case XML_EXTERNAL_PARAMETER_ENTITY
:
1253 fprintf(ctxt
->output
, "EXTERNAL PARAMETER, ");
1256 xmlDebugErr2(ctxt
, XML_CHECK_ENTITY_TYPE
,
1257 "Unknown entity type %d\n", cur
->etype
);
1259 if (cur
->ExternalID
!= NULL
)
1260 fprintf(ctxt
->output
, "ID \"%s\"", (char *) cur
->ExternalID
);
1261 if (cur
->SystemID
!= NULL
)
1262 fprintf(ctxt
->output
, "SYSTEM \"%s\"", (char *) cur
->SystemID
);
1263 if (cur
->orig
!= NULL
)
1264 fprintf(ctxt
->output
, "\n orig \"%s\"", (char *) cur
->orig
);
1265 if ((cur
->type
!= XML_ELEMENT_NODE
) && (cur
->content
!= NULL
))
1266 fprintf(ctxt
->output
, "\n content \"%s\"",
1267 (char *) cur
->content
);
1268 fprintf(ctxt
->output
, "\n");
1273 * xmlCtxtDumpEntities:
1274 * @output: the FILE * for the output
1275 * @doc: the document
1277 * Dumps debug information for all the entities in use by the document
1280 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt
, xmlDocPtr doc
)
1282 if (doc
== NULL
) return;
1283 xmlCtxtDumpDocHead(ctxt
, doc
);
1284 if ((doc
->intSubset
!= NULL
) && (doc
->intSubset
->entities
!= NULL
)) {
1285 xmlEntitiesTablePtr table
= (xmlEntitiesTablePtr
)
1286 doc
->intSubset
->entities
;
1289 fprintf(ctxt
->output
, "Entities in internal subset\n");
1290 xmlHashScan(table
, xmlCtxtDumpEntityCallback
, ctxt
);
1292 fprintf(ctxt
->output
, "No entities in internal subset\n");
1293 if ((doc
->extSubset
!= NULL
) && (doc
->extSubset
->entities
!= NULL
)) {
1294 xmlEntitiesTablePtr table
= (xmlEntitiesTablePtr
)
1295 doc
->extSubset
->entities
;
1298 fprintf(ctxt
->output
, "Entities in external subset\n");
1299 xmlHashScan(table
, xmlCtxtDumpEntityCallback
, ctxt
);
1300 } else if (!ctxt
->check
)
1301 fprintf(ctxt
->output
, "No entities in external subset\n");
1306 * @output: the FILE * for the output
1309 * Dumps debug information for the DTD
1312 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt
, xmlDtdPtr dtd
)
1316 fprintf(ctxt
->output
, "DTD is NULL\n");
1319 xmlCtxtDumpDtdNode(ctxt
, dtd
);
1320 if (dtd
->children
== NULL
)
1321 fprintf(ctxt
->output
, " DTD is empty\n");
1324 xmlCtxtDumpNodeList(ctxt
, dtd
->children
);
1329 /************************************************************************
1331 * Public entry points for dump *
1333 ************************************************************************/
1336 * xmlDebugDumpString:
1337 * @output: the FILE * for the output
1340 * Dumps information about the string, shorten it if necessary
1343 xmlDebugDumpString(FILE * output
, const xmlChar
* str
)
1350 fprintf(output
, "(NULL)");
1353 for (i
= 0; i
< 40; i
++)
1356 else if (IS_BLANK_CH(str
[i
]))
1358 else if (str
[i
] >= 0x80)
1359 fprintf(output
, "#%X", str
[i
]);
1361 fputc(str
[i
], output
);
1362 fprintf(output
, "...");
1367 * @output: the FILE * for the output
1368 * @attr: the attribute
1369 * @depth: the indentation level.
1371 * Dumps debug information for the attribute
1374 xmlDebugDumpAttr(FILE *output
, xmlAttrPtr attr
, int depth
) {
1377 if (output
== NULL
) return;
1378 xmlCtxtDumpInitCtxt(&ctxt
);
1379 ctxt
.output
= output
;
1381 xmlCtxtDumpAttr(&ctxt
, attr
);
1382 xmlCtxtDumpCleanCtxt(&ctxt
);
1387 * xmlDebugDumpEntities:
1388 * @output: the FILE * for the output
1389 * @doc: the document
1391 * Dumps debug information for all the entities in use by the document
1394 xmlDebugDumpEntities(FILE * output
, xmlDocPtr doc
)
1398 if (output
== NULL
) return;
1399 xmlCtxtDumpInitCtxt(&ctxt
);
1400 ctxt
.output
= output
;
1401 xmlCtxtDumpEntities(&ctxt
, doc
);
1402 xmlCtxtDumpCleanCtxt(&ctxt
);
1406 * xmlDebugDumpAttrList:
1407 * @output: the FILE * for the output
1408 * @attr: the attribute list
1409 * @depth: the indentation level.
1411 * Dumps debug information for the attribute list
1414 xmlDebugDumpAttrList(FILE * output
, xmlAttrPtr attr
, int depth
)
1418 if (output
== NULL
) return;
1419 xmlCtxtDumpInitCtxt(&ctxt
);
1420 ctxt
.output
= output
;
1422 xmlCtxtDumpAttrList(&ctxt
, attr
);
1423 xmlCtxtDumpCleanCtxt(&ctxt
);
1427 * xmlDebugDumpOneNode:
1428 * @output: the FILE * for the output
1430 * @depth: the indentation level.
1432 * Dumps debug information for the element node, it is not recursive
1435 xmlDebugDumpOneNode(FILE * output
, xmlNodePtr node
, int depth
)
1439 if (output
== NULL
) return;
1440 xmlCtxtDumpInitCtxt(&ctxt
);
1441 ctxt
.output
= output
;
1443 xmlCtxtDumpOneNode(&ctxt
, node
);
1444 xmlCtxtDumpCleanCtxt(&ctxt
);
1449 * @output: the FILE * for the output
1451 * @depth: the indentation level.
1453 * Dumps debug information for the element node, it is recursive
1456 xmlDebugDumpNode(FILE * output
, xmlNodePtr node
, int depth
)
1462 xmlCtxtDumpInitCtxt(&ctxt
);
1463 ctxt
.output
= output
;
1465 xmlCtxtDumpNode(&ctxt
, node
);
1466 xmlCtxtDumpCleanCtxt(&ctxt
);
1470 * xmlDebugDumpNodeList:
1471 * @output: the FILE * for the output
1472 * @node: the node list
1473 * @depth: the indentation level.
1475 * Dumps debug information for the list of element node, it is recursive
1478 xmlDebugDumpNodeList(FILE * output
, xmlNodePtr node
, int depth
)
1484 xmlCtxtDumpInitCtxt(&ctxt
);
1485 ctxt
.output
= output
;
1487 xmlCtxtDumpNodeList(&ctxt
, node
);
1488 xmlCtxtDumpCleanCtxt(&ctxt
);
1492 * xmlDebugDumpDocumentHead:
1493 * @output: the FILE * for the output
1494 * @doc: the document
1496 * Dumps debug information concerning the document, not recursive
1499 xmlDebugDumpDocumentHead(FILE * output
, xmlDocPtr doc
)
1505 xmlCtxtDumpInitCtxt(&ctxt
);
1506 ctxt
.options
|= DUMP_TEXT_TYPE
;
1507 ctxt
.output
= output
;
1508 xmlCtxtDumpDocumentHead(&ctxt
, doc
);
1509 xmlCtxtDumpCleanCtxt(&ctxt
);
1513 * xmlDebugDumpDocument:
1514 * @output: the FILE * for the output
1515 * @doc: the document
1517 * Dumps debug information for the document, it's recursive
1520 xmlDebugDumpDocument(FILE * output
, xmlDocPtr doc
)
1526 xmlCtxtDumpInitCtxt(&ctxt
);
1527 ctxt
.options
|= DUMP_TEXT_TYPE
;
1528 ctxt
.output
= output
;
1529 xmlCtxtDumpDocument(&ctxt
, doc
);
1530 xmlCtxtDumpCleanCtxt(&ctxt
);
1535 * @output: the FILE * for the output
1538 * Dumps debug information for the DTD
1541 xmlDebugDumpDTD(FILE * output
, xmlDtdPtr dtd
)
1547 xmlCtxtDumpInitCtxt(&ctxt
);
1548 ctxt
.options
|= DUMP_TEXT_TYPE
;
1549 ctxt
.output
= output
;
1550 xmlCtxtDumpDTD(&ctxt
, dtd
);
1551 xmlCtxtDumpCleanCtxt(&ctxt
);
1554 /************************************************************************
1556 * Public entry points for checkings *
1558 ************************************************************************/
1561 * xmlDebugCheckDocument:
1562 * @output: the FILE * for the output
1563 * @doc: the document
1565 * Check the document for potential content problems, and output
1566 * the errors to @output
1568 * Returns the number of errors found
1571 xmlDebugCheckDocument(FILE * output
, xmlDocPtr doc
)
1577 xmlCtxtDumpInitCtxt(&ctxt
);
1578 ctxt
.output
= output
;
1580 xmlCtxtDumpDocument(&ctxt
, doc
);
1581 xmlCtxtDumpCleanCtxt(&ctxt
);
1582 return(ctxt
.errors
);
1585 /************************************************************************
1587 * Helpers for Shell *
1589 ************************************************************************/
1593 * @node: the node to count
1595 * Count the children of @node.
1597 * Returns the number of children of @node.
1600 xmlLsCountNode(xmlNodePtr node
) {
1602 xmlNodePtr list
= NULL
;
1607 switch (node
->type
) {
1608 case XML_ELEMENT_NODE
:
1609 list
= node
->children
;
1611 case XML_DOCUMENT_NODE
:
1612 case XML_HTML_DOCUMENT_NODE
:
1613 list
= ((xmlDocPtr
) node
)->children
;
1615 case XML_ATTRIBUTE_NODE
:
1616 list
= ((xmlAttrPtr
) node
)->children
;
1619 case XML_CDATA_SECTION_NODE
:
1621 case XML_COMMENT_NODE
:
1622 if (node
->content
!= NULL
) {
1623 ret
= xmlStrlen(node
->content
);
1626 case XML_ENTITY_REF_NODE
:
1627 case XML_DOCUMENT_TYPE_NODE
:
1628 case XML_ENTITY_NODE
:
1629 case XML_DOCUMENT_FRAG_NODE
:
1630 case XML_NOTATION_NODE
:
1632 case XML_ELEMENT_DECL
:
1633 case XML_ATTRIBUTE_DECL
:
1634 case XML_ENTITY_DECL
:
1635 case XML_NAMESPACE_DECL
:
1636 case XML_XINCLUDE_START
:
1637 case XML_XINCLUDE_END
:
1641 for (;list
!= NULL
;ret
++)
1648 * @output: the FILE * for the output
1649 * @node: the node to dump
1651 * Dump to @output the type and name of @node.
1654 xmlLsOneNode(FILE *output
, xmlNodePtr node
) {
1655 if (output
== NULL
) return;
1657 fprintf(output
, "NULL\n");
1660 switch (node
->type
) {
1661 case XML_ELEMENT_NODE
:
1662 fprintf(output
, "-");
1664 case XML_ATTRIBUTE_NODE
:
1665 fprintf(output
, "a");
1668 fprintf(output
, "t");
1670 case XML_CDATA_SECTION_NODE
:
1671 fprintf(output
, "C");
1673 case XML_ENTITY_REF_NODE
:
1674 fprintf(output
, "e");
1676 case XML_ENTITY_NODE
:
1677 fprintf(output
, "E");
1680 fprintf(output
, "p");
1682 case XML_COMMENT_NODE
:
1683 fprintf(output
, "c");
1685 case XML_DOCUMENT_NODE
:
1686 fprintf(output
, "d");
1688 case XML_HTML_DOCUMENT_NODE
:
1689 fprintf(output
, "h");
1691 case XML_DOCUMENT_TYPE_NODE
:
1692 fprintf(output
, "T");
1694 case XML_DOCUMENT_FRAG_NODE
:
1695 fprintf(output
, "F");
1697 case XML_NOTATION_NODE
:
1698 fprintf(output
, "N");
1700 case XML_NAMESPACE_DECL
:
1701 fprintf(output
, "n");
1704 fprintf(output
, "?");
1706 if (node
->type
!= XML_NAMESPACE_DECL
) {
1707 if (node
->properties
!= NULL
)
1708 fprintf(output
, "a");
1710 fprintf(output
, "-");
1711 if (node
->nsDef
!= NULL
)
1712 fprintf(output
, "n");
1714 fprintf(output
, "-");
1717 fprintf(output
, " %8d ", xmlLsCountNode(node
));
1719 switch (node
->type
) {
1720 case XML_ELEMENT_NODE
:
1721 if (node
->name
!= NULL
) {
1722 if ((node
->ns
!= NULL
) && (node
->ns
->prefix
!= NULL
))
1723 fprintf(output
, "%s:", node
->ns
->prefix
);
1724 fprintf(output
, "%s", (const char *) node
->name
);
1727 case XML_ATTRIBUTE_NODE
:
1728 if (node
->name
!= NULL
)
1729 fprintf(output
, "%s", (const char *) node
->name
);
1732 if (node
->content
!= NULL
) {
1733 xmlDebugDumpString(output
, node
->content
);
1736 case XML_CDATA_SECTION_NODE
:
1738 case XML_ENTITY_REF_NODE
:
1739 if (node
->name
!= NULL
)
1740 fprintf(output
, "%s", (const char *) node
->name
);
1742 case XML_ENTITY_NODE
:
1743 if (node
->name
!= NULL
)
1744 fprintf(output
, "%s", (const char *) node
->name
);
1747 if (node
->name
!= NULL
)
1748 fprintf(output
, "%s", (const char *) node
->name
);
1750 case XML_COMMENT_NODE
:
1752 case XML_DOCUMENT_NODE
:
1754 case XML_HTML_DOCUMENT_NODE
:
1756 case XML_DOCUMENT_TYPE_NODE
:
1758 case XML_DOCUMENT_FRAG_NODE
:
1760 case XML_NOTATION_NODE
:
1762 case XML_NAMESPACE_DECL
: {
1763 xmlNsPtr ns
= (xmlNsPtr
) node
;
1765 if (ns
->prefix
== NULL
)
1766 fprintf(output
, "default -> %s", (char *)ns
->href
);
1768 fprintf(output
, "%s -> %s", (char *)ns
->prefix
,
1773 if (node
->name
!= NULL
)
1774 fprintf(output
, "%s", (const char *) node
->name
);
1776 fprintf(output
, "\n");
1781 * @boolval: a bool to turn into text
1783 * Convenient way to turn bool into text
1785 * Returns a pointer to either "True" or "False"
1788 xmlBoolToText(int boolval
)
1796 #ifdef LIBXML_XPATH_ENABLED
1797 /****************************************************************
1799 * The XML shell related functions *
1801 ****************************************************************/
1806 * TODO: Improvement/cleanups for the XML shell
1807 * - allow to shell out an editor on a subpart
1808 * - cleanup function registrations (with help) and calling
1809 * - provide registration routines
1813 * xmlShellPrintXPathError:
1814 * @errorType: valid xpath error id
1815 * @arg: the argument that cause xpath to fail
1817 * Print the xpath error to libxml default error channel
1820 xmlShellPrintXPathError(int errorType
, const char *arg
)
1822 const char *default_arg
= "Result";
1827 switch (errorType
) {
1828 case XPATH_UNDEFINED
:
1829 xmlGenericError(xmlGenericErrorContext
,
1830 "%s: no such node\n", arg
);
1834 xmlGenericError(xmlGenericErrorContext
,
1835 "%s is a Boolean\n", arg
);
1838 xmlGenericError(xmlGenericErrorContext
,
1839 "%s is a number\n", arg
);
1842 xmlGenericError(xmlGenericErrorContext
,
1843 "%s is a string\n", arg
);
1845 #ifdef LIBXML_XPTR_LOCS_ENABLED
1847 xmlGenericError(xmlGenericErrorContext
,
1848 "%s is a point\n", arg
);
1851 xmlGenericError(xmlGenericErrorContext
,
1852 "%s is a range\n", arg
);
1854 case XPATH_LOCATIONSET
:
1855 xmlGenericError(xmlGenericErrorContext
,
1856 "%s is a range\n", arg
);
1858 #endif /* LIBXML_XPTR_LOCS_ENABLED */
1860 xmlGenericError(xmlGenericErrorContext
,
1861 "%s is user-defined\n", arg
);
1863 case XPATH_XSLT_TREE
:
1864 xmlGenericError(xmlGenericErrorContext
,
1865 "%s is an XSLT value tree\n", arg
);
1869 xmlGenericError(xmlGenericErrorContext
,
1870 "Try casting the result string function (xpath builtin)\n",
1876 #ifdef LIBXML_OUTPUT_ENABLED
1878 * xmlShellPrintNodeCtxt:
1879 * @ctxt : a non-null shell context
1880 * @node : a non-null node to print to the output FILE
1882 * Print node to the output FILE
1885 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt
,xmlNodePtr node
)
1896 if (node
->type
== XML_DOCUMENT_NODE
)
1897 xmlDocDump(fp
, (xmlDocPtr
) node
);
1898 else if (node
->type
== XML_ATTRIBUTE_NODE
)
1899 xmlDebugDumpAttrList(fp
, (xmlAttrPtr
) node
, 0);
1901 xmlElemDump(fp
, node
->doc
, node
);
1907 * xmlShellPrintNode:
1908 * @node : a non-null node to print to the output FILE
1910 * Print node to the output FILE
1913 xmlShellPrintNode(xmlNodePtr node
)
1915 xmlShellPrintNodeCtxt(NULL
, node
);
1917 #endif /* LIBXML_OUTPUT_ENABLED */
1920 * xmlShellPrintXPathResultCtxt:
1921 * @ctxt: a valid shell context
1922 * @list: a valid result generated by an xpath evaluation
1924 * Prints result to the output FILE
1927 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt
,xmlXPathObjectPtr list
)
1933 switch (list
->type
) {
1934 case XPATH_NODESET
:{
1935 #ifdef LIBXML_OUTPUT_ENABLED
1938 if (list
->nodesetval
) {
1939 for (indx
= 0; indx
< list
->nodesetval
->nodeNr
;
1941 xmlShellPrintNodeCtxt(ctxt
,
1942 list
->nodesetval
->nodeTab
[indx
]);
1945 xmlGenericError(xmlGenericErrorContext
,
1946 "Empty node set\n");
1950 xmlGenericError(xmlGenericErrorContext
,
1952 #endif /* LIBXML_OUTPUT_ENABLED */
1955 xmlGenericError(xmlGenericErrorContext
,
1956 "Is a Boolean:%s\n",
1957 xmlBoolToText(list
->boolval
));
1960 xmlGenericError(xmlGenericErrorContext
,
1961 "Is a number:%0g\n", list
->floatval
);
1964 xmlGenericError(xmlGenericErrorContext
,
1965 "Is a string:%s\n", list
->stringval
);
1969 xmlShellPrintXPathError(list
->type
, NULL
);
1975 * xmlShellPrintXPathResult:
1976 * @list: a valid result generated by an xpath evaluation
1978 * Prints result to the output FILE
1981 xmlShellPrintXPathResult(xmlXPathObjectPtr list
)
1983 xmlShellPrintXPathResultCtxt(NULL
, list
);
1988 * @ctxt: the shell context
1993 * Implements the XML shell function "ls"
1994 * Does an Unix like listing of the given node (like a directory)
1999 xmlShellList(xmlShellCtxtPtr ctxt
,
2000 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2001 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2007 fprintf(ctxt
->output
, "NULL\n");
2010 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2011 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2012 cur
= ((xmlDocPtr
) node
)->children
;
2013 } else if (node
->type
== XML_NAMESPACE_DECL
) {
2014 xmlLsOneNode(ctxt
->output
, node
);
2016 } else if (node
->children
!= NULL
) {
2017 cur
= node
->children
;
2019 xmlLsOneNode(ctxt
->output
, node
);
2022 while (cur
!= NULL
) {
2023 xmlLsOneNode(ctxt
->output
, cur
);
2031 * @ctxt: the shell context
2036 * Implements the XML shell function "base"
2037 * dumps the current XML base of the node
2042 xmlShellBase(xmlShellCtxtPtr ctxt
,
2043 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2044 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2050 fprintf(ctxt
->output
, "NULL\n");
2054 base
= xmlNodeGetBase(node
->doc
, node
);
2057 fprintf(ctxt
->output
, " No base found !!!\n");
2059 fprintf(ctxt
->output
, "%s\n", base
);
2065 #ifdef LIBXML_TREE_ENABLED
2068 * @ctxt: the shell context
2069 * @arg: the new base
2073 * Implements the XML shell function "setbase"
2074 * change the current XML base of the node
2079 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2080 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2081 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2083 xmlNodeSetBase(node
, (xmlChar
*) arg
);
2088 #ifdef LIBXML_XPATH_ENABLED
2090 * xmlShellRegisterNamespace:
2091 * @ctxt: the shell context
2092 * @arg: a string in prefix=nsuri format
2096 * Implements the XML shell function "setns"
2097 * register/unregister a prefix=namespace pair
2098 * on the XPath context
2100 * Returns 0 on success and a negative value otherwise.
2103 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt
, char *arg
,
2104 xmlNodePtr node ATTRIBUTE_UNUSED
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2111 nsListDup
= xmlStrdup((xmlChar
*) arg
);
2113 while(next
!= NULL
) {
2115 /*while((*next) == ' ') next++;*/
2116 if((*next
) == '\0') break;
2120 next
= (xmlChar
*)xmlStrchr(next
, '=');
2122 fprintf(ctxt
->output
, "setns: prefix=[nsuri] required\n");
2130 next
= (xmlChar
*)xmlStrchr(next
, ' ');
2135 /* do register namespace */
2136 if(xmlXPathRegisterNs(ctxt
->pctxt
, prefix
, href
) != 0) {
2137 fprintf(ctxt
->output
,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix
, href
);
2147 * xmlShellRegisterRootNamespaces:
2148 * @ctxt: the shell context
2150 * @node: the root element
2153 * Implements the XML shell function "setrootns"
2154 * which registers all namespaces declarations found on the root element.
2156 * Returns 0 on success and a negative value otherwise.
2159 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt
, char *arg ATTRIBUTE_UNUSED
,
2160 xmlNodePtr root
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2164 if ((root
== NULL
) || (root
->type
!= XML_ELEMENT_NODE
) ||
2165 (root
->nsDef
== NULL
) || (ctxt
== NULL
) || (ctxt
->pctxt
== NULL
))
2168 while (ns
!= NULL
) {
2169 if (ns
->prefix
== NULL
)
2170 xmlXPathRegisterNs(ctxt
->pctxt
, BAD_CAST
"defaultns", ns
->href
);
2172 xmlXPathRegisterNs(ctxt
->pctxt
, ns
->prefix
, ns
->href
);
2181 * @ctxt: the shell context
2182 * @arg: the string or regular expression to find
2186 * Implements the XML shell function "grep"
2187 * dumps information about the node (namespace, attributes, content).
2192 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2193 char *arg
, xmlNodePtr node
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2201 #ifdef LIBXML_REGEXP_ENABLED
2202 if ((xmlStrchr((xmlChar
*) arg
, '?')) ||
2203 (xmlStrchr((xmlChar
*) arg
, '*')) ||
2204 (xmlStrchr((xmlChar
*) arg
, '.')) ||
2205 (xmlStrchr((xmlChar
*) arg
, '['))) {
2208 while (node
!= NULL
) {
2209 if (node
->type
== XML_COMMENT_NODE
) {
2210 if (xmlStrstr(node
->content
, (xmlChar
*) arg
)) {
2212 fprintf(ctxt
->output
, "%s : ", xmlGetNodePath(node
));
2213 xmlShellList(ctxt
, NULL
, node
, NULL
);
2215 } else if (node
->type
== XML_TEXT_NODE
) {
2216 if (xmlStrstr(node
->content
, (xmlChar
*) arg
)) {
2218 fprintf(ctxt
->output
, "%s : ", xmlGetNodePath(node
->parent
));
2219 xmlShellList(ctxt
, NULL
, node
->parent
, NULL
);
2224 * Browse the full subtree, deep first
2227 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2228 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2229 node
= ((xmlDocPtr
) node
)->children
;
2230 } else if ((node
->children
!= NULL
)
2231 && (node
->type
!= XML_ENTITY_REF_NODE
)) {
2233 node
= node
->children
;
2234 } else if (node
->next
!= NULL
) {
2238 /* go up to parents->next if needed */
2239 while (node
!= NULL
) {
2240 if (node
->parent
!= NULL
) {
2241 node
= node
->parent
;
2243 if (node
->next
!= NULL
) {
2247 if (node
->parent
== NULL
) {
2259 * @ctxt: the shell context
2264 * Implements the XML shell function "dir"
2265 * dumps information about the node (namespace, attributes, content).
2270 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2271 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr node
,
2272 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2277 fprintf(ctxt
->output
, "NULL\n");
2280 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2281 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2282 xmlDebugDumpDocumentHead(ctxt
->output
, (xmlDocPtr
) node
);
2283 } else if (node
->type
== XML_ATTRIBUTE_NODE
) {
2284 xmlDebugDumpAttr(ctxt
->output
, (xmlAttrPtr
) node
, 0);
2286 xmlDebugDumpOneNode(ctxt
->output
, node
, 0);
2292 * xmlShellSetContent:
2293 * @ctxt: the shell context
2294 * @value: the content as a string
2298 * Implements the XML shell function "dir"
2299 * dumps information about the node (namespace, attributes, content).
2304 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
,
2305 char *value
, xmlNodePtr node
,
2306 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2309 xmlParserErrors ret
;
2314 fprintf(ctxt
->output
, "NULL\n");
2317 if (value
== NULL
) {
2318 fprintf(ctxt
->output
, "NULL\n");
2322 ret
= xmlParseInNodeContext(node
, value
, strlen(value
), 0, &results
);
2323 if (ret
== XML_ERR_OK
) {
2324 if (node
->children
!= NULL
) {
2325 xmlFreeNodeList(node
->children
);
2326 node
->children
= NULL
;
2329 xmlAddChildList(node
, results
);
2331 fprintf(ctxt
->output
, "failed to parse content\n");
2336 #ifdef LIBXML_SCHEMAS_ENABLED
2338 * xmlShellRNGValidate:
2339 * @ctxt: the shell context
2340 * @schemas: the path to the Relax-NG schemas
2344 * Implements the XML shell function "relaxng"
2345 * validating the instance against a Relax-NG schemas
2350 xmlShellRNGValidate(xmlShellCtxtPtr sctxt
, char *schemas
,
2351 xmlNodePtr node ATTRIBUTE_UNUSED
,
2352 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2354 xmlRelaxNGPtr relaxngschemas
;
2355 xmlRelaxNGParserCtxtPtr ctxt
;
2356 xmlRelaxNGValidCtxtPtr vctxt
;
2359 ctxt
= xmlRelaxNGNewParserCtxt(schemas
);
2360 xmlRelaxNGSetParserErrors(ctxt
, xmlGenericError
, xmlGenericError
, NULL
);
2361 relaxngschemas
= xmlRelaxNGParse(ctxt
);
2362 xmlRelaxNGFreeParserCtxt(ctxt
);
2363 if (relaxngschemas
== NULL
) {
2364 xmlGenericError(xmlGenericErrorContext
,
2365 "Relax-NG schema %s failed to compile\n", schemas
);
2368 vctxt
= xmlRelaxNGNewValidCtxt(relaxngschemas
);
2369 xmlRelaxNGSetValidErrors(vctxt
, xmlGenericError
, xmlGenericError
, NULL
);
2370 ret
= xmlRelaxNGValidateDoc(vctxt
, sctxt
->doc
);
2372 fprintf(stderr
, "%s validates\n", sctxt
->filename
);
2373 } else if (ret
> 0) {
2374 fprintf(stderr
, "%s fails to validate\n", sctxt
->filename
);
2376 fprintf(stderr
, "%s validation generated an internal error\n",
2379 xmlRelaxNGFreeValidCtxt(vctxt
);
2380 if (relaxngschemas
!= NULL
)
2381 xmlRelaxNGFree(relaxngschemas
);
2386 #ifdef LIBXML_OUTPUT_ENABLED
2389 * @ctxt: the shell context
2394 * Implements the XML shell function "cat"
2395 * dumps the serialization node content (XML or HTML).
2400 xmlShellCat(xmlShellCtxtPtr ctxt
, char *arg ATTRIBUTE_UNUSED
,
2401 xmlNodePtr node
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2406 fprintf(ctxt
->output
, "NULL\n");
2409 if (ctxt
->doc
->type
== XML_HTML_DOCUMENT_NODE
) {
2410 #ifdef LIBXML_HTML_ENABLED
2411 if (node
->type
== XML_HTML_DOCUMENT_NODE
)
2412 htmlDocDump(ctxt
->output
, (htmlDocPtr
) node
);
2414 htmlNodeDumpFile(ctxt
->output
, ctxt
->doc
, node
);
2416 if (node
->type
== XML_DOCUMENT_NODE
)
2417 xmlDocDump(ctxt
->output
, (xmlDocPtr
) node
);
2419 xmlElemDump(ctxt
->output
, ctxt
->doc
, node
);
2420 #endif /* LIBXML_HTML_ENABLED */
2422 if (node
->type
== XML_DOCUMENT_NODE
)
2423 xmlDocDump(ctxt
->output
, (xmlDocPtr
) node
);
2425 xmlElemDump(ctxt
->output
, ctxt
->doc
, node
);
2427 fprintf(ctxt
->output
, "\n");
2430 #endif /* LIBXML_OUTPUT_ENABLED */
2434 * @ctxt: the shell context
2435 * @filename: the file name
2439 * Implements the XML shell function "load"
2440 * loads a new document specified by the filename
2442 * Returns 0 or -1 if loading failed
2445 xmlShellLoad(xmlShellCtxtPtr ctxt
, char *filename
,
2446 xmlNodePtr node ATTRIBUTE_UNUSED
,
2447 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2452 if ((ctxt
== NULL
) || (filename
== NULL
)) return(-1);
2453 if (ctxt
->doc
!= NULL
)
2454 html
= (ctxt
->doc
->type
== XML_HTML_DOCUMENT_NODE
);
2457 #ifdef LIBXML_HTML_ENABLED
2458 doc
= htmlParseFile(filename
, NULL
);
2460 fprintf(ctxt
->output
, "HTML support not compiled in\n");
2462 #endif /* LIBXML_HTML_ENABLED */
2464 doc
= xmlReadFile(filename
,NULL
,0);
2467 if (ctxt
->loaded
== 1) {
2468 xmlFreeDoc(ctxt
->doc
);
2471 #ifdef LIBXML_XPATH_ENABLED
2472 xmlXPathFreeContext(ctxt
->pctxt
);
2473 #endif /* LIBXML_XPATH_ENABLED */
2474 xmlFree(ctxt
->filename
);
2476 ctxt
->node
= (xmlNodePtr
) doc
;
2477 #ifdef LIBXML_XPATH_ENABLED
2478 ctxt
->pctxt
= xmlXPathNewContext(doc
);
2479 #endif /* LIBXML_XPATH_ENABLED */
2480 ctxt
->filename
= (char *) xmlCanonicPath((xmlChar
*) filename
);
2486 #ifdef LIBXML_OUTPUT_ENABLED
2489 * @ctxt: the shell context
2490 * @filename: the file name
2491 * @node: a node in the tree
2494 * Implements the XML shell function "write"
2495 * Write the current node to the filename, it saves the serialization
2496 * of the subtree under the @node specified
2498 * Returns 0 or -1 in case of error
2501 xmlShellWrite(xmlShellCtxtPtr ctxt
, char *filename
, xmlNodePtr node
,
2502 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2506 if ((filename
== NULL
) || (filename
[0] == 0)) {
2510 if (access((char *) filename
, W_OK
)) {
2511 xmlGenericError(xmlGenericErrorContext
,
2512 "Cannot write to %s\n", filename
);
2516 switch (node
->type
) {
2517 case XML_DOCUMENT_NODE
:
2518 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < -1) {
2519 xmlGenericError(xmlGenericErrorContext
,
2520 "Failed to write to %s\n", filename
);
2524 case XML_HTML_DOCUMENT_NODE
:
2525 #ifdef LIBXML_HTML_ENABLED
2526 if (htmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2527 xmlGenericError(xmlGenericErrorContext
,
2528 "Failed to write to %s\n", filename
);
2532 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < -1) {
2533 xmlGenericError(xmlGenericErrorContext
,
2534 "Failed to write to %s\n", filename
);
2537 #endif /* LIBXML_HTML_ENABLED */
2542 f
= fopen((char *) filename
, "w");
2544 xmlGenericError(xmlGenericErrorContext
,
2545 "Failed to write to %s\n", filename
);
2548 xmlElemDump(f
, ctxt
->doc
, node
);
2557 * @ctxt: the shell context
2558 * @filename: the file name (optional)
2562 * Implements the XML shell function "save"
2563 * Write the current document to the filename, or it's original name
2565 * Returns 0 or -1 in case of error
2568 xmlShellSave(xmlShellCtxtPtr ctxt
, char *filename
,
2569 xmlNodePtr node ATTRIBUTE_UNUSED
,
2570 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2572 if ((ctxt
== NULL
) || (ctxt
->doc
== NULL
))
2574 if ((filename
== NULL
) || (filename
[0] == 0))
2575 filename
= ctxt
->filename
;
2576 if (filename
== NULL
)
2579 if (access((char *) filename
, W_OK
)) {
2580 xmlGenericError(xmlGenericErrorContext
,
2581 "Cannot save to %s\n", filename
);
2585 switch (ctxt
->doc
->type
) {
2586 case XML_DOCUMENT_NODE
:
2587 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2588 xmlGenericError(xmlGenericErrorContext
,
2589 "Failed to save to %s\n", filename
);
2592 case XML_HTML_DOCUMENT_NODE
:
2593 #ifdef LIBXML_HTML_ENABLED
2594 if (htmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2595 xmlGenericError(xmlGenericErrorContext
,
2596 "Failed to save to %s\n", filename
);
2599 if (xmlSaveFile((char *) filename
, ctxt
->doc
) < 0) {
2600 xmlGenericError(xmlGenericErrorContext
,
2601 "Failed to save to %s\n", filename
);
2603 #endif /* LIBXML_HTML_ENABLED */
2606 xmlGenericError(xmlGenericErrorContext
,
2607 "To save to subparts of a document use the 'write' command\n");
2613 #endif /* LIBXML_OUTPUT_ENABLED */
2615 #ifdef LIBXML_VALID_ENABLED
2618 * @ctxt: the shell context
2619 * @dtd: the DTD URI (optional)
2623 * Implements the XML shell function "validate"
2624 * Validate the document, if a DTD path is provided, then the validation
2625 * is done against the given DTD.
2627 * Returns 0 or -1 in case of error
2630 xmlShellValidate(xmlShellCtxtPtr ctxt
, char *dtd
,
2631 xmlNodePtr node ATTRIBUTE_UNUSED
,
2632 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2637 if ((ctxt
== NULL
) || (ctxt
->doc
== NULL
)) return(-1);
2638 memset(&vctxt
, 0, sizeof(vctxt
));
2639 vctxt
.error
= xmlGenericError
;
2640 vctxt
.warning
= xmlGenericError
;
2642 if ((dtd
== NULL
) || (dtd
[0] == 0)) {
2643 res
= xmlValidateDocument(&vctxt
, ctxt
->doc
);
2647 subset
= xmlParseDTD(NULL
, (xmlChar
*) dtd
);
2648 if (subset
!= NULL
) {
2649 res
= xmlValidateDtd(&vctxt
, ctxt
->doc
, subset
);
2656 #endif /* LIBXML_VALID_ENABLED */
2660 * @ctxt: the shell context
2662 * @tree: a node defining a subtree
2665 * Implements the XML shell function "du"
2666 * show the structure of the subtree under node @tree
2667 * If @tree is null, the command works on the current node.
2669 * Returns 0 or -1 in case of error
2672 xmlShellDu(xmlShellCtxtPtr ctxt
,
2673 char *arg ATTRIBUTE_UNUSED
, xmlNodePtr tree
,
2674 xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2685 while (node
!= NULL
) {
2686 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2687 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2688 fprintf(ctxt
->output
, "/\n");
2689 } else if (node
->type
== XML_ELEMENT_NODE
) {
2690 for (i
= 0; i
< indent
; i
++)
2691 fprintf(ctxt
->output
, " ");
2692 if ((node
->ns
) && (node
->ns
->prefix
))
2693 fprintf(ctxt
->output
, "%s:", node
->ns
->prefix
);
2694 fprintf(ctxt
->output
, "%s\n", node
->name
);
2699 * Browse the full subtree, deep first
2702 if ((node
->type
== XML_DOCUMENT_NODE
) ||
2703 (node
->type
== XML_HTML_DOCUMENT_NODE
)) {
2704 node
= ((xmlDocPtr
) node
)->children
;
2705 } else if ((node
->children
!= NULL
)
2706 && (node
->type
!= XML_ENTITY_REF_NODE
)) {
2708 node
= node
->children
;
2710 } else if ((node
!= tree
) && (node
->next
!= NULL
)) {
2713 } else if (node
!= tree
) {
2714 /* go up to parents->next if needed */
2715 while (node
!= tree
) {
2716 if (node
->parent
!= NULL
) {
2717 node
= node
->parent
;
2720 if ((node
!= tree
) && (node
->next
!= NULL
)) {
2724 if (node
->parent
== NULL
) {
2733 /* exit condition */
2744 * @ctxt: the shell context
2745 * @buffer: the output buffer
2749 * Implements the XML shell function "pwd"
2750 * Show the full path from the root to the node, if needed building
2751 * thumblers when similar elements exists at a given ancestor level.
2752 * The output is compatible with XPath commands.
2754 * Returns 0 or -1 in case of error
2757 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED
, char *buffer
,
2758 xmlNodePtr node
, xmlNodePtr node2 ATTRIBUTE_UNUSED
)
2762 if ((node
== NULL
) || (buffer
== NULL
))
2765 path
= xmlGetNodePath(node
);
2770 * This test prevents buffer overflow, because this routine
2771 * is only called by xmlShell, in which the second argument is
2773 * It is a dirty hack before a cleaner solution is found.
2774 * Documentation should mention that the second argument must
2775 * be at least 500 chars long, and could be stripped if too long.
2777 snprintf(buffer
, 499, "%s", path
);
2786 * @doc: the initial document
2787 * @filename: the output buffer
2788 * @input: the line reading function
2789 * @output: the output FILE*, defaults to stdout if NULL
2791 * Implements the XML shell
2792 * This allow to load, validate, view, modify and save a document
2793 * using a environment similar to a UNIX commandline.
2796 xmlShell(xmlDocPtr doc
, char *filename
, xmlShellReadlineFunc input
,
2799 char prompt
[500] = "/ > ";
2800 char *cmdline
= NULL
, *cur
;
2804 xmlShellCtxtPtr ctxt
;
2805 xmlXPathObjectPtr list
;
2809 if (filename
== NULL
)
2815 ctxt
= (xmlShellCtxtPtr
) xmlMalloc(sizeof(xmlShellCtxt
));
2820 ctxt
->input
= input
;
2821 ctxt
->output
= output
;
2822 ctxt
->filename
= (char *) xmlStrdup((xmlChar
*) filename
);
2823 ctxt
->node
= (xmlNodePtr
) ctxt
->doc
;
2825 #ifdef LIBXML_XPATH_ENABLED
2826 ctxt
->pctxt
= xmlXPathNewContext(ctxt
->doc
);
2827 if (ctxt
->pctxt
== NULL
) {
2831 #endif /* LIBXML_XPATH_ENABLED */
2833 if (ctxt
->node
== (xmlNodePtr
) ctxt
->doc
)
2834 snprintf(prompt
, sizeof(prompt
), "%s > ", "/");
2835 else if ((ctxt
->node
!= NULL
) && (ctxt
->node
->name
) &&
2836 (ctxt
->node
->ns
) && (ctxt
->node
->ns
->prefix
))
2837 snprintf(prompt
, sizeof(prompt
), "%s:%s > ",
2838 (ctxt
->node
->ns
->prefix
), ctxt
->node
->name
);
2839 else if ((ctxt
->node
!= NULL
) && (ctxt
->node
->name
))
2840 snprintf(prompt
, sizeof(prompt
), "%s > ", ctxt
->node
->name
);
2842 snprintf(prompt
, sizeof(prompt
), "? > ");
2843 prompt
[sizeof(prompt
) - 1] = 0;
2846 * Get a new command line
2848 cmdline
= ctxt
->input(prompt
);
2849 if (cmdline
== NULL
)
2853 * Parse the command itself
2856 while ((*cur
== ' ') || (*cur
== '\t'))
2859 while ((*cur
!= ' ') && (*cur
!= '\t') &&
2860 (*cur
!= '\n') && (*cur
!= '\r')) {
2863 command
[i
++] = *cur
++;
2870 * Parse the argument
2872 while ((*cur
== ' ') || (*cur
== '\t'))
2875 while ((*cur
!= '\n') && (*cur
!= '\r') && (*cur
!= 0)) {
2883 * start interpreting the command
2885 if (!strcmp(command
, "exit"))
2887 if (!strcmp(command
, "quit"))
2889 if (!strcmp(command
, "bye"))
2891 if (!strcmp(command
, "help")) {
2892 fprintf(ctxt
->output
, "\tbase display XML base of the node\n");
2893 fprintf(ctxt
->output
, "\tsetbase URI change the XML base of the node\n");
2894 fprintf(ctxt
->output
, "\tbye leave shell\n");
2895 fprintf(ctxt
->output
, "\tcat [node] display node or current node\n");
2896 fprintf(ctxt
->output
, "\tcd [path] change directory to path or to root\n");
2897 fprintf(ctxt
->output
, "\tdir [path] dumps information about the node (namespace, attributes, content)\n");
2898 fprintf(ctxt
->output
, "\tdu [path] show the structure of the subtree under path or the current node\n");
2899 fprintf(ctxt
->output
, "\texit leave shell\n");
2900 fprintf(ctxt
->output
, "\thelp display this help\n");
2901 fprintf(ctxt
->output
, "\tfree display memory usage\n");
2902 fprintf(ctxt
->output
, "\tload [name] load a new document with name\n");
2903 fprintf(ctxt
->output
, "\tls [path] list contents of path or the current directory\n");
2904 fprintf(ctxt
->output
, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2905 #ifdef LIBXML_XPATH_ENABLED
2906 fprintf(ctxt
->output
, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2907 fprintf(ctxt
->output
, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2908 fprintf(ctxt
->output
, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2909 fprintf(ctxt
->output
, "\tsetrootns register all namespace found on the root element\n");
2910 fprintf(ctxt
->output
, "\t the default namespace if any uses 'defaultns' prefix\n");
2911 #endif /* LIBXML_XPATH_ENABLED */
2912 fprintf(ctxt
->output
, "\tpwd display current working directory\n");
2913 fprintf(ctxt
->output
, "\twhereis display absolute path of [path] or current working directory\n");
2914 fprintf(ctxt
->output
, "\tquit leave shell\n");
2915 #ifdef LIBXML_OUTPUT_ENABLED
2916 fprintf(ctxt
->output
, "\tsave [name] save this document to name or the original name\n");
2917 fprintf(ctxt
->output
, "\twrite [name] write the current node to the filename\n");
2918 #endif /* LIBXML_OUTPUT_ENABLED */
2919 #ifdef LIBXML_VALID_ENABLED
2920 fprintf(ctxt
->output
, "\tvalidate check the document for errors\n");
2921 #endif /* LIBXML_VALID_ENABLED */
2922 #ifdef LIBXML_SCHEMAS_ENABLED
2923 fprintf(ctxt
->output
, "\trelaxng rng validate the document against the Relax-NG schemas\n");
2925 fprintf(ctxt
->output
, "\tgrep string search for a string in the subtree\n");
2926 #ifdef LIBXML_VALID_ENABLED
2927 } else if (!strcmp(command
, "validate")) {
2928 xmlShellValidate(ctxt
, arg
, NULL
, NULL
);
2929 #endif /* LIBXML_VALID_ENABLED */
2930 } else if (!strcmp(command
, "load")) {
2931 xmlShellLoad(ctxt
, arg
, NULL
, NULL
);
2932 #ifdef LIBXML_SCHEMAS_ENABLED
2933 } else if (!strcmp(command
, "relaxng")) {
2934 xmlShellRNGValidate(ctxt
, arg
, NULL
, NULL
);
2936 #ifdef LIBXML_OUTPUT_ENABLED
2937 } else if (!strcmp(command
, "save")) {
2938 xmlShellSave(ctxt
, arg
, NULL
, NULL
);
2939 } else if (!strcmp(command
, "write")) {
2941 xmlGenericError(xmlGenericErrorContext
,
2942 "Write command requires a filename argument\n");
2944 xmlShellWrite(ctxt
, arg
, ctxt
->node
, NULL
);
2945 #endif /* LIBXML_OUTPUT_ENABLED */
2946 } else if (!strcmp(command
, "grep")) {
2947 xmlShellGrep(ctxt
, arg
, ctxt
->node
, NULL
);
2948 } else if (!strcmp(command
, "free")) {
2950 xmlMemShow(ctxt
->output
, 0);
2954 sscanf(arg
, "%d", &len
);
2955 xmlMemShow(ctxt
->output
, len
);
2957 } else if (!strcmp(command
, "pwd")) {
2960 if (!xmlShellPwd(ctxt
, dir
, ctxt
->node
, NULL
))
2961 fprintf(ctxt
->output
, "%s\n", dir
);
2962 } else if (!strcmp(command
, "du")) {
2964 xmlShellDu(ctxt
, NULL
, ctxt
->node
, NULL
);
2966 ctxt
->pctxt
->node
= ctxt
->node
;
2967 #ifdef LIBXML_XPATH_ENABLED
2968 ctxt
->pctxt
->node
= ctxt
->node
;
2969 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
2972 #endif /* LIBXML_XPATH_ENABLED */
2974 switch (list
->type
) {
2975 case XPATH_UNDEFINED
:
2976 xmlGenericError(xmlGenericErrorContext
,
2977 "%s: no such node\n", arg
);
2979 case XPATH_NODESET
:{
2982 if (list
->nodesetval
== NULL
)
2986 indx
< list
->nodesetval
->nodeNr
;
2988 xmlShellDu(ctxt
, NULL
,
2990 nodeTab
[indx
], NULL
);
2994 xmlGenericError(xmlGenericErrorContext
,
2995 "%s is a Boolean\n", arg
);
2998 xmlGenericError(xmlGenericErrorContext
,
2999 "%s is a number\n", arg
);
3002 xmlGenericError(xmlGenericErrorContext
,
3003 "%s is a string\n", arg
);
3005 #ifdef LIBXML_XPTR_LOCS_ENABLED
3007 xmlGenericError(xmlGenericErrorContext
,
3008 "%s is a point\n", arg
);
3011 xmlGenericError(xmlGenericErrorContext
,
3012 "%s is a range\n", arg
);
3014 case XPATH_LOCATIONSET
:
3015 xmlGenericError(xmlGenericErrorContext
,
3016 "%s is a range\n", arg
);
3018 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3020 xmlGenericError(xmlGenericErrorContext
,
3021 "%s is user-defined\n", arg
);
3023 case XPATH_XSLT_TREE
:
3024 xmlGenericError(xmlGenericErrorContext
,
3025 "%s is an XSLT value tree\n",
3029 #ifdef LIBXML_XPATH_ENABLED
3030 xmlXPathFreeObject(list
);
3033 xmlGenericError(xmlGenericErrorContext
,
3034 "%s: no such node\n", arg
);
3036 ctxt
->pctxt
->node
= NULL
;
3038 } else if (!strcmp(command
, "base")) {
3039 xmlShellBase(ctxt
, NULL
, ctxt
->node
, NULL
);
3040 } else if (!strcmp(command
, "set")) {
3041 xmlShellSetContent(ctxt
, arg
, ctxt
->node
, NULL
);
3042 #ifdef LIBXML_XPATH_ENABLED
3043 } else if (!strcmp(command
, "setns")) {
3045 xmlGenericError(xmlGenericErrorContext
,
3046 "setns: prefix=[nsuri] required\n");
3048 xmlShellRegisterNamespace(ctxt
, arg
, NULL
, NULL
);
3050 } else if (!strcmp(command
, "setrootns")) {
3053 root
= xmlDocGetRootElement(ctxt
->doc
);
3054 xmlShellRegisterRootNamespaces(ctxt
, NULL
, root
, NULL
);
3055 } else if (!strcmp(command
, "xpath")) {
3057 xmlGenericError(xmlGenericErrorContext
,
3058 "xpath: expression required\n");
3060 ctxt
->pctxt
->node
= ctxt
->node
;
3061 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3062 xmlXPathDebugDumpObject(ctxt
->output
, list
, 0);
3063 xmlXPathFreeObject(list
);
3065 #endif /* LIBXML_XPATH_ENABLED */
3066 #ifdef LIBXML_TREE_ENABLED
3067 } else if (!strcmp(command
, "setbase")) {
3068 xmlShellSetBase(ctxt
, arg
, ctxt
->node
, NULL
);
3070 } else if ((!strcmp(command
, "ls")) || (!strcmp(command
, "dir"))) {
3071 int dir
= (!strcmp(command
, "dir"));
3075 xmlShellDir(ctxt
, NULL
, ctxt
->node
, NULL
);
3077 xmlShellList(ctxt
, NULL
, ctxt
->node
, NULL
);
3079 ctxt
->pctxt
->node
= ctxt
->node
;
3080 #ifdef LIBXML_XPATH_ENABLED
3081 ctxt
->pctxt
->node
= ctxt
->node
;
3082 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3085 #endif /* LIBXML_XPATH_ENABLED */
3087 switch (list
->type
) {
3088 case XPATH_UNDEFINED
:
3089 xmlGenericError(xmlGenericErrorContext
,
3090 "%s: no such node\n", arg
);
3092 case XPATH_NODESET
:{
3095 if (list
->nodesetval
== NULL
)
3099 indx
< list
->nodesetval
->nodeNr
;
3102 xmlShellDir(ctxt
, NULL
,
3104 nodeTab
[indx
], NULL
);
3106 xmlShellList(ctxt
, NULL
,
3108 nodeTab
[indx
], NULL
);
3113 xmlGenericError(xmlGenericErrorContext
,
3114 "%s is a Boolean\n", arg
);
3117 xmlGenericError(xmlGenericErrorContext
,
3118 "%s is a number\n", arg
);
3121 xmlGenericError(xmlGenericErrorContext
,
3122 "%s is a string\n", arg
);
3124 #ifdef LIBXML_XPTR_LOCS_ENABLED
3126 xmlGenericError(xmlGenericErrorContext
,
3127 "%s is a point\n", arg
);
3130 xmlGenericError(xmlGenericErrorContext
,
3131 "%s is a range\n", arg
);
3133 case XPATH_LOCATIONSET
:
3134 xmlGenericError(xmlGenericErrorContext
,
3135 "%s is a range\n", arg
);
3137 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3139 xmlGenericError(xmlGenericErrorContext
,
3140 "%s is user-defined\n", arg
);
3142 case XPATH_XSLT_TREE
:
3143 xmlGenericError(xmlGenericErrorContext
,
3144 "%s is an XSLT value tree\n",
3148 #ifdef LIBXML_XPATH_ENABLED
3149 xmlXPathFreeObject(list
);
3152 xmlGenericError(xmlGenericErrorContext
,
3153 "%s: no such node\n", arg
);
3155 ctxt
->pctxt
->node
= NULL
;
3157 } else if (!strcmp(command
, "whereis")) {
3161 if (!xmlShellPwd(ctxt
, dir
, ctxt
->node
, NULL
))
3162 fprintf(ctxt
->output
, "%s\n", dir
);
3164 ctxt
->pctxt
->node
= ctxt
->node
;
3165 #ifdef LIBXML_XPATH_ENABLED
3166 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3169 #endif /* LIBXML_XPATH_ENABLED */
3171 switch (list
->type
) {
3172 case XPATH_UNDEFINED
:
3173 xmlGenericError(xmlGenericErrorContext
,
3174 "%s: no such node\n", arg
);
3176 case XPATH_NODESET
:{
3179 if (list
->nodesetval
== NULL
)
3183 indx
< list
->nodesetval
->nodeNr
;
3185 if (!xmlShellPwd(ctxt
, dir
, list
->nodesetval
->
3186 nodeTab
[indx
], NULL
))
3187 fprintf(ctxt
->output
, "%s\n", dir
);
3192 xmlGenericError(xmlGenericErrorContext
,
3193 "%s is a Boolean\n", arg
);
3196 xmlGenericError(xmlGenericErrorContext
,
3197 "%s is a number\n", arg
);
3200 xmlGenericError(xmlGenericErrorContext
,
3201 "%s is a string\n", arg
);
3203 #ifdef LIBXML_XPTR_LOCS_ENABLED
3205 xmlGenericError(xmlGenericErrorContext
,
3206 "%s is a point\n", arg
);
3209 xmlGenericError(xmlGenericErrorContext
,
3210 "%s is a range\n", arg
);
3212 case XPATH_LOCATIONSET
:
3213 xmlGenericError(xmlGenericErrorContext
,
3214 "%s is a range\n", arg
);
3216 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3218 xmlGenericError(xmlGenericErrorContext
,
3219 "%s is user-defined\n", arg
);
3221 case XPATH_XSLT_TREE
:
3222 xmlGenericError(xmlGenericErrorContext
,
3223 "%s is an XSLT value tree\n",
3227 #ifdef LIBXML_XPATH_ENABLED
3228 xmlXPathFreeObject(list
);
3231 xmlGenericError(xmlGenericErrorContext
,
3232 "%s: no such node\n", arg
);
3234 ctxt
->pctxt
->node
= NULL
;
3236 } else if (!strcmp(command
, "cd")) {
3238 ctxt
->node
= (xmlNodePtr
) ctxt
->doc
;
3240 #ifdef LIBXML_XPATH_ENABLED
3243 ctxt
->pctxt
->node
= ctxt
->node
;
3245 if ((l
>= 2) && (arg
[l
- 1] == '/'))
3247 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3250 #endif /* LIBXML_XPATH_ENABLED */
3252 switch (list
->type
) {
3253 case XPATH_UNDEFINED
:
3254 xmlGenericError(xmlGenericErrorContext
,
3255 "%s: no such node\n", arg
);
3258 if (list
->nodesetval
!= NULL
) {
3259 if (list
->nodesetval
->nodeNr
== 1) {
3260 ctxt
->node
= list
->nodesetval
->nodeTab
[0];
3261 if ((ctxt
->node
!= NULL
) &&
3262 (ctxt
->node
->type
==
3263 XML_NAMESPACE_DECL
)) {
3264 xmlGenericError(xmlGenericErrorContext
,
3265 "cannot cd to namespace\n");
3269 xmlGenericError(xmlGenericErrorContext
,
3270 "%s is a %d Node Set\n",
3272 list
->nodesetval
->nodeNr
);
3274 xmlGenericError(xmlGenericErrorContext
,
3275 "%s is an empty Node Set\n",
3279 xmlGenericError(xmlGenericErrorContext
,
3280 "%s is a Boolean\n", arg
);
3283 xmlGenericError(xmlGenericErrorContext
,
3284 "%s is a number\n", arg
);
3287 xmlGenericError(xmlGenericErrorContext
,
3288 "%s is a string\n", arg
);
3290 #ifdef LIBXML_XPTR_LOCS_ENABLED
3292 xmlGenericError(xmlGenericErrorContext
,
3293 "%s is a point\n", arg
);
3296 xmlGenericError(xmlGenericErrorContext
,
3297 "%s is a range\n", arg
);
3299 case XPATH_LOCATIONSET
:
3300 xmlGenericError(xmlGenericErrorContext
,
3301 "%s is a range\n", arg
);
3303 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3305 xmlGenericError(xmlGenericErrorContext
,
3306 "%s is user-defined\n", arg
);
3308 case XPATH_XSLT_TREE
:
3309 xmlGenericError(xmlGenericErrorContext
,
3310 "%s is an XSLT value tree\n",
3314 #ifdef LIBXML_XPATH_ENABLED
3315 xmlXPathFreeObject(list
);
3318 xmlGenericError(xmlGenericErrorContext
,
3319 "%s: no such node\n", arg
);
3321 ctxt
->pctxt
->node
= NULL
;
3323 #ifdef LIBXML_OUTPUT_ENABLED
3324 } else if (!strcmp(command
, "cat")) {
3326 xmlShellCat(ctxt
, NULL
, ctxt
->node
, NULL
);
3328 ctxt
->pctxt
->node
= ctxt
->node
;
3329 #ifdef LIBXML_XPATH_ENABLED
3330 ctxt
->pctxt
->node
= ctxt
->node
;
3331 list
= xmlXPathEval((xmlChar
*) arg
, ctxt
->pctxt
);
3334 #endif /* LIBXML_XPATH_ENABLED */
3336 switch (list
->type
) {
3337 case XPATH_UNDEFINED
:
3338 xmlGenericError(xmlGenericErrorContext
,
3339 "%s: no such node\n", arg
);
3341 case XPATH_NODESET
:{
3344 if (list
->nodesetval
== NULL
)
3348 indx
< list
->nodesetval
->nodeNr
;
3351 fprintf(ctxt
->output
, " -------\n");
3352 xmlShellCat(ctxt
, NULL
,
3354 nodeTab
[indx
], NULL
);
3359 xmlGenericError(xmlGenericErrorContext
,
3360 "%s is a Boolean\n", arg
);
3363 xmlGenericError(xmlGenericErrorContext
,
3364 "%s is a number\n", arg
);
3367 xmlGenericError(xmlGenericErrorContext
,
3368 "%s is a string\n", arg
);
3370 #ifdef LIBXML_XPTR_LOCS_ENABLED
3372 xmlGenericError(xmlGenericErrorContext
,
3373 "%s is a point\n", arg
);
3376 xmlGenericError(xmlGenericErrorContext
,
3377 "%s is a range\n", arg
);
3379 case XPATH_LOCATIONSET
:
3380 xmlGenericError(xmlGenericErrorContext
,
3381 "%s is a range\n", arg
);
3383 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3385 xmlGenericError(xmlGenericErrorContext
,
3386 "%s is user-defined\n", arg
);
3388 case XPATH_XSLT_TREE
:
3389 xmlGenericError(xmlGenericErrorContext
,
3390 "%s is an XSLT value tree\n",
3394 #ifdef LIBXML_XPATH_ENABLED
3395 xmlXPathFreeObject(list
);
3398 xmlGenericError(xmlGenericErrorContext
,
3399 "%s: no such node\n", arg
);
3401 ctxt
->pctxt
->node
= NULL
;
3403 #endif /* LIBXML_OUTPUT_ENABLED */
3405 xmlGenericError(xmlGenericErrorContext
,
3406 "Unknown command %s\n", command
);
3408 free(cmdline
); /* not xmlFree here ! */
3411 #ifdef LIBXML_XPATH_ENABLED
3412 xmlXPathFreeContext(ctxt
->pctxt
);
3413 #endif /* LIBXML_XPATH_ENABLED */
3415 xmlFreeDoc(ctxt
->doc
);
3417 if (ctxt
->filename
!= NULL
)
3418 xmlFree(ctxt
->filename
);
3420 if (cmdline
!= NULL
)
3421 free(cmdline
); /* not xmlFree here ! */
3424 #endif /* LIBXML_XPATH_ENABLED */
3426 #endif /* LIBXML_DEBUG_ENABLED */