blob: 0c77b653eb1f08ddc39df5aaf78e551d3d8339fe [file] [log] [blame]
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001/*
2 * debugXML.c : This is a set of routines used for debugging the tree
3 * produced by the XML parser.
4 *
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00005 * See Copyright for the status of this software.
6 *
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00007 * Daniel Veillard <Daniel.Veillard@w3.org>
8 */
9
10#include <stdio.h>
11#include "tree.h"
12#include "parser.h"
13#include "debugXML.h"
14
15#define IS_BLANK(c) \
16 (((c) == '\n') || ((c) == '\r') || ((c) == '\t') || ((c) == ' '))
17
Daniel Veillarddd6b3671999-09-23 22:19:22 +000018void xmlDebugDumpString(FILE *output, const xmlChar *str) {
Daniel Veillardbaf4cd51998-10-27 22:56:57 +000019 int i;
20 for (i = 0;i < 40;i++)
21 if (str[i] == 0) return;
22 else if (IS_BLANK(str[i])) fputc(' ', output);
23 else fputc(str[i], output);
24 fprintf(output, "...");
25}
26
27void xmlDebugDumpNamespace(FILE *output, xmlNsPtr ns, int depth) {
28 int i;
29 char shift[100];
30
31 for (i = 0;((i < depth) && (i < 25));i++)
32 shift[2 * i] = shift[2 * i + 1] = ' ';
33 shift[2 * i] = shift[2 * i + 1] = 0;
34
35 fprintf(output, shift);
36 if (ns->type == XML_GLOBAL_NAMESPACE)
37 fprintf(output, "old ");
38 fprintf(output, "namespace %s href=", ns->prefix);
39 xmlDebugDumpString(output, ns->href);
40 fprintf(output, "\n");
41}
42
43void xmlDebugDumpNamespaceList(FILE *output, xmlNsPtr ns, int depth) {
44 while (ns != NULL) {
45 xmlDebugDumpNamespace(output, ns, depth);
46 ns = ns->next;
47 }
48}
49
50void xmlDebugDumpEntity(FILE *output, xmlEntityPtr ent, int depth) {
51 int i;
52 char shift[100];
53
54 for (i = 0;((i < depth) && (i < 25));i++)
55 shift[2 * i] = shift[2 * i + 1] = ' ';
56 shift[2 * i] = shift[2 * i + 1] = 0;
57
58 fprintf(output, shift);
59 switch (ent->type) {
60 case XML_INTERNAL_GENERAL_ENTITY:
61 fprintf(output, "INTERNAL_GENERAL_ENTITY ");
62 break;
63 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
64 fprintf(output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
65 break;
66 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
67 fprintf(output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
68 break;
69 case XML_INTERNAL_PARAMETER_ENTITY:
70 fprintf(output, "INTERNAL_PARAMETER_ENTITY ");
71 break;
72 case XML_EXTERNAL_PARAMETER_ENTITY:
73 fprintf(output, "EXTERNAL_PARAMETER_ENTITY ");
74 break;
75 default:
76 fprintf(output, "ENTITY_%d ! ", ent->type);
77 }
78 fprintf(output, "%s\n", ent->name);
79 if (ent->ExternalID) {
80 fprintf(output, shift);
81 fprintf(output, "ExternalID=%s\n", ent->ExternalID);
82 }
83 if (ent->SystemID) {
84 fprintf(output, shift);
85 fprintf(output, "SystemID=%s\n", ent->SystemID);
86 }
87 if (ent->content) {
88 fprintf(output, shift);
89 fprintf(output, "content=");
90 xmlDebugDumpString(output, ent->content);
91 fprintf(output, "\n");
92 }
93}
94
95void xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
96 int i;
97 char shift[100];
98
99 for (i = 0;((i < depth) && (i < 25));i++)
100 shift[2 * i] = shift[2 * i + 1] = ' ';
101 shift[2 * i] = shift[2 * i + 1] = 0;
102
103 fprintf(output, shift);
104 fprintf(output, "ATTRIBUTE %s\n", attr->name);
105 if (attr->val != NULL)
106 xmlDebugDumpNodeList(output, attr->val, depth + 1);
107}
108
109void xmlDebugDumpAttrList(FILE *output, xmlAttrPtr attr, int depth) {
110 while (attr != NULL) {
111 xmlDebugDumpAttr(output, attr, depth);
112 attr = attr->next;
113 }
114}
115
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000116void xmlDebugDumpOneNode(FILE *output, xmlNodePtr node, int depth) {
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000117 int i;
118 char shift[100];
119
120 for (i = 0;((i < depth) && (i < 25));i++)
121 shift[2 * i] = shift[2 * i + 1] = ' ';
122 shift[2 * i] = shift[2 * i + 1] = 0;
123
124 fprintf(output, shift);
125 switch (node->type) {
126 case XML_ELEMENT_NODE:
127 fprintf(output, "ELEMENT ");
128 if (node->ns != NULL)
129 fprintf(output, "%s:%s\n", node->ns->prefix, node->name);
130 else
131 fprintf(output, "%s\n", node->name);
132 break;
133 case XML_ATTRIBUTE_NODE:
134 fprintf(output, "Error, ATTRIBUTE found here\n");
135 break;
136 case XML_TEXT_NODE:
137 fprintf(output, "TEXT\n");
138 break;
139 case XML_CDATA_SECTION_NODE:
140 fprintf(output, "CDATA_SECTION\n");
141 break;
142 case XML_ENTITY_REF_NODE:
143 fprintf(output, "ENTITY_REF\n");
144 break;
145 case XML_ENTITY_NODE:
146 fprintf(output, "ENTITY\n");
147 break;
148 case XML_PI_NODE:
Daniel Veillardb96e6431999-08-29 21:02:19 +0000149 fprintf(output, "PI %s\n", node->name);
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000150 break;
151 case XML_COMMENT_NODE:
152 fprintf(output, "COMMENT\n");
153 break;
154 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000155 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000156 fprintf(output, "Error, DOCUMENT found here\n");
157 break;
158 case XML_DOCUMENT_TYPE_NODE:
159 fprintf(output, "DOCUMENT_TYPE\n");
160 break;
161 case XML_DOCUMENT_FRAG_NODE:
162 fprintf(output, "DOCUMENT_FRAG\n");
163 break;
164 case XML_NOTATION_NODE:
165 fprintf(output, "NOTATION\n");
166 break;
167 default:
168 fprintf(output, "NODE_%d\n", node->type);
169 }
170 if (node->doc == NULL) {
171 fprintf(output, shift);
172 fprintf(output, "doc == NULL !!!\n");
173 }
174 if (node->nsDef != NULL)
175 xmlDebugDumpNamespaceList(output, node->nsDef, depth + 1);
176 if (node->properties != NULL)
177 xmlDebugDumpAttrList(output, node->properties, depth + 1);
178 if (node->type != XML_ENTITY_REF_NODE) {
179 if (node->content != NULL) {
180 fprintf(output, shift);
181 fprintf(output, "content=");
182 xmlDebugDumpString(output, node->content);
183 fprintf(output, "\n");
184 }
185 } else {
186 xmlEntityPtr ent;
187 ent = xmlGetDocEntity(node->doc, node->name);
188 if (ent != NULL)
189 xmlDebugDumpEntity(output, ent, depth + 1);
190 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000191}
192
193void xmlDebugDumpNode(FILE *output, xmlNodePtr node, int depth) {
194 xmlDebugDumpOneNode(output, node, depth);
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000195 if (node->childs != NULL)
196 xmlDebugDumpNodeList(output, node->childs, depth + 1);
197}
198
199void xmlDebugDumpNodeList(FILE *output, xmlNodePtr node, int depth) {
200 while (node != NULL) {
201 xmlDebugDumpNode(output, node, depth);
202 node = node->next;
203 }
204}
205
206
207void xmlDebugDumpDocument(FILE *output, xmlDocPtr doc) {
208 if (output == NULL) output = stdout;
209 if (doc == NULL) {
210 fprintf(output, "DOCUMENT == NULL !\n");
211 return;
212 }
213
214 switch (doc->type) {
215 case XML_ELEMENT_NODE:
216 fprintf(output, "Error, ELEMENT found here ");
217 break;
218 case XML_ATTRIBUTE_NODE:
219 fprintf(output, "Error, ATTRIBUTE found here\n");
220 break;
221 case XML_TEXT_NODE:
222 fprintf(output, "Error, TEXT\n");
223 break;
224 case XML_CDATA_SECTION_NODE:
225 fprintf(output, "Error, CDATA_SECTION\n");
226 break;
227 case XML_ENTITY_REF_NODE:
228 fprintf(output, "Error, ENTITY_REF\n");
229 break;
230 case XML_ENTITY_NODE:
231 fprintf(output, "Error, ENTITY\n");
232 break;
233 case XML_PI_NODE:
234 fprintf(output, "Error, PI\n");
235 break;
236 case XML_COMMENT_NODE:
237 fprintf(output, "Error, COMMENT\n");
238 break;
239 case XML_DOCUMENT_NODE:
240 fprintf(output, "DOCUMENT\n");
241 break;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000242 case XML_HTML_DOCUMENT_NODE:
243 fprintf(output, "HTML DOCUMENT\n");
244 break;
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000245 case XML_DOCUMENT_TYPE_NODE:
246 fprintf(output, "Error, DOCUMENT_TYPE\n");
247 break;
248 case XML_DOCUMENT_FRAG_NODE:
249 fprintf(output, "Error, DOCUMENT_FRAG\n");
250 break;
251 case XML_NOTATION_NODE:
252 fprintf(output, "Error, NOTATION\n");
253 break;
254 default:
255 fprintf(output, "NODE_%d\n", doc->type);
256 }
257 if (doc->name != NULL) {
258 fprintf(output, "name=");
Daniel Veillardb96e6431999-08-29 21:02:19 +0000259 xmlDebugDumpString(output, BAD_CAST doc->name);
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000260 fprintf(output, "\n");
261 }
262 if (doc->version != NULL) {
263 fprintf(output, "version=");
264 xmlDebugDumpString(output, doc->version);
265 fprintf(output, "\n");
266 }
267 if (doc->encoding != NULL) {
268 fprintf(output, "encoding=");
269 xmlDebugDumpString(output, doc->encoding);
270 fprintf(output, "\n");
271 }
272 if (doc->standalone)
273 fprintf(output, "standalone=true\n");
274 if (doc->oldNs != NULL)
275 xmlDebugDumpNamespaceList(output, doc->oldNs, 0);
276 if (doc->root != NULL)
277 xmlDebugDumpNodeList(output, doc->root, 1);
278}