blob: 650fc596d53b4d3430e6aaced80a3870bf279fdc [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=");
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000182#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000183 xmlDebugDumpString(output, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000184#else
185 xmlDebugDumpString(output, xmlBufferContent(node->content));
186#endif
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000187 fprintf(output, "\n");
188 }
189 } else {
190 xmlEntityPtr ent;
191 ent = xmlGetDocEntity(node->doc, node->name);
192 if (ent != NULL)
193 xmlDebugDumpEntity(output, ent, depth + 1);
194 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000195}
196
197void xmlDebugDumpNode(FILE *output, xmlNodePtr node, int depth) {
198 xmlDebugDumpOneNode(output, node, depth);
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000199 if (node->childs != NULL)
200 xmlDebugDumpNodeList(output, node->childs, depth + 1);
201}
202
203void xmlDebugDumpNodeList(FILE *output, xmlNodePtr node, int depth) {
204 while (node != NULL) {
205 xmlDebugDumpNode(output, node, depth);
206 node = node->next;
207 }
208}
209
210
211void xmlDebugDumpDocument(FILE *output, xmlDocPtr doc) {
212 if (output == NULL) output = stdout;
213 if (doc == NULL) {
214 fprintf(output, "DOCUMENT == NULL !\n");
215 return;
216 }
217
218 switch (doc->type) {
219 case XML_ELEMENT_NODE:
220 fprintf(output, "Error, ELEMENT found here ");
221 break;
222 case XML_ATTRIBUTE_NODE:
223 fprintf(output, "Error, ATTRIBUTE found here\n");
224 break;
225 case XML_TEXT_NODE:
226 fprintf(output, "Error, TEXT\n");
227 break;
228 case XML_CDATA_SECTION_NODE:
229 fprintf(output, "Error, CDATA_SECTION\n");
230 break;
231 case XML_ENTITY_REF_NODE:
232 fprintf(output, "Error, ENTITY_REF\n");
233 break;
234 case XML_ENTITY_NODE:
235 fprintf(output, "Error, ENTITY\n");
236 break;
237 case XML_PI_NODE:
238 fprintf(output, "Error, PI\n");
239 break;
240 case XML_COMMENT_NODE:
241 fprintf(output, "Error, COMMENT\n");
242 break;
243 case XML_DOCUMENT_NODE:
244 fprintf(output, "DOCUMENT\n");
245 break;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000246 case XML_HTML_DOCUMENT_NODE:
247 fprintf(output, "HTML DOCUMENT\n");
248 break;
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000249 case XML_DOCUMENT_TYPE_NODE:
250 fprintf(output, "Error, DOCUMENT_TYPE\n");
251 break;
252 case XML_DOCUMENT_FRAG_NODE:
253 fprintf(output, "Error, DOCUMENT_FRAG\n");
254 break;
255 case XML_NOTATION_NODE:
256 fprintf(output, "Error, NOTATION\n");
257 break;
258 default:
259 fprintf(output, "NODE_%d\n", doc->type);
260 }
261 if (doc->name != NULL) {
262 fprintf(output, "name=");
Daniel Veillardb96e6431999-08-29 21:02:19 +0000263 xmlDebugDumpString(output, BAD_CAST doc->name);
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000264 fprintf(output, "\n");
265 }
266 if (doc->version != NULL) {
267 fprintf(output, "version=");
268 xmlDebugDumpString(output, doc->version);
269 fprintf(output, "\n");
270 }
271 if (doc->encoding != NULL) {
272 fprintf(output, "encoding=");
273 xmlDebugDumpString(output, doc->encoding);
274 fprintf(output, "\n");
275 }
276 if (doc->standalone)
277 fprintf(output, "standalone=true\n");
278 if (doc->oldNs != NULL)
279 xmlDebugDumpNamespaceList(output, doc->oldNs, 0);
280 if (doc->root != NULL)
281 xmlDebugDumpNodeList(output, doc->root, 1);
282}
Daniel Veillard10a2c651999-12-12 13:03:50 +0000283
284void xmlDebugDumpEntities(FILE *output, xmlDocPtr doc) {
285 int i;
286 xmlEntityPtr cur;
287
288 if (output == NULL) output = stdout;
289 if (doc == NULL) {
290 fprintf(output, "DOCUMENT == NULL !\n");
291 return;
292 }
293
294 switch (doc->type) {
295 case XML_ELEMENT_NODE:
296 fprintf(output, "Error, ELEMENT found here ");
297 break;
298 case XML_ATTRIBUTE_NODE:
299 fprintf(output, "Error, ATTRIBUTE found here\n");
300 break;
301 case XML_TEXT_NODE:
302 fprintf(output, "Error, TEXT\n");
303 break;
304 case XML_CDATA_SECTION_NODE:
305 fprintf(output, "Error, CDATA_SECTION\n");
306 break;
307 case XML_ENTITY_REF_NODE:
308 fprintf(output, "Error, ENTITY_REF\n");
309 break;
310 case XML_ENTITY_NODE:
311 fprintf(output, "Error, ENTITY\n");
312 break;
313 case XML_PI_NODE:
314 fprintf(output, "Error, PI\n");
315 break;
316 case XML_COMMENT_NODE:
317 fprintf(output, "Error, COMMENT\n");
318 break;
319 case XML_DOCUMENT_NODE:
320 fprintf(output, "DOCUMENT\n");
321 break;
322 case XML_HTML_DOCUMENT_NODE:
323 fprintf(output, "HTML DOCUMENT\n");
324 break;
325 case XML_DOCUMENT_TYPE_NODE:
326 fprintf(output, "Error, DOCUMENT_TYPE\n");
327 break;
328 case XML_DOCUMENT_FRAG_NODE:
329 fprintf(output, "Error, DOCUMENT_FRAG\n");
330 break;
331 case XML_NOTATION_NODE:
332 fprintf(output, "Error, NOTATION\n");
333 break;
334 default:
335 fprintf(output, "NODE_%d\n", doc->type);
336 }
337 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
338 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
339 doc->intSubset->entities;
340 fprintf(output, "Entities in internal subset\n");
341 for (i = 0;i < table->nb_entities;i++) {
342 cur = &table->table[i];
343 fprintf(output, "%d : %s : ", i, cur->name);
344 switch (cur->type) {
345 case XML_INTERNAL_GENERAL_ENTITY:
346 fprintf(output, "INTERNAL GENERAL");
347 break;
348 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
349 fprintf(output, "EXTERNAL PARSED");
350 break;
351 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
352 fprintf(output, "EXTERNAL UNPARSED");
353 break;
354 case XML_INTERNAL_PARAMETER_ENTITY:
355 fprintf(output, "INTERNAL PARAMETER");
356 break;
357 case XML_EXTERNAL_PARAMETER_ENTITY:
358 fprintf(output, "EXTERNAL PARAMETER");
359 break;
360 default:
361 fprintf(output, "UNKNOWN TYPE %d",
362 cur->type);
363 }
364 if (cur->ExternalID != NULL)
365 fprintf(output, "ID \"%s\"", cur->ExternalID);
366 if (cur->SystemID != NULL)
367 fprintf(output, "SYSTEM \"%s\"", cur->SystemID);
368 if (cur->orig != NULL)
369 fprintf(output, "\n orig \"%s\"", cur->orig);
370 if (cur->content != NULL)
371 fprintf(output, "\n content \"%s\"", cur->content);
372 fprintf(output, "\n");
373 }
374 } else
375 fprintf(output, "No entities in internal subset\n");
376 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
377 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
378 doc->extSubset->entities;
379 fprintf(output, "Entities in external subset\n");
380 for (i = 0;i < table->nb_entities;i++) {
381 cur = &table->table[i];
382 fprintf(output, "%d : %s : ", i, cur->name);
383 switch (cur->type) {
384 case XML_INTERNAL_GENERAL_ENTITY:
385 fprintf(output, "INTERNAL GENERAL");
386 break;
387 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
388 fprintf(output, "EXTERNAL PARSED");
389 break;
390 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
391 fprintf(output, "EXTERNAL UNPARSED");
392 break;
393 case XML_INTERNAL_PARAMETER_ENTITY:
394 fprintf(output, "INTERNAL PARAMETER");
395 break;
396 case XML_EXTERNAL_PARAMETER_ENTITY:
397 fprintf(output, "EXTERNAL PARAMETER");
398 break;
399 default:
400 fprintf(output, "UNKNOWN TYPE %d",
401 cur->type);
402 }
403 if (cur->ExternalID != NULL)
404 fprintf(output, "ID \"%s\"", cur->ExternalID);
405 if (cur->SystemID != NULL)
406 fprintf(output, "SYSTEM \"%s\"", cur->SystemID);
407 if (cur->orig != NULL)
408 fprintf(output, "\n orig \"%s\"", cur->orig);
409 if (cur->content != NULL)
410 fprintf(output, "\n content \"%s\"", cur->content);
411 fprintf(output, "\n");
412 }
413 } else
414 fprintf(output, "No entities in external subset\n");
415}