blob: 69901b7c117c9af34dd4f5f3affd09270b2e2fd1 [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
18void xmlDebugDumpString(FILE *output, const CHAR *str) {
19 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:
155 fprintf(output, "Error, DOCUMENT found here\n");
156 break;
157 case XML_DOCUMENT_TYPE_NODE:
158 fprintf(output, "DOCUMENT_TYPE\n");
159 break;
160 case XML_DOCUMENT_FRAG_NODE:
161 fprintf(output, "DOCUMENT_FRAG\n");
162 break;
163 case XML_NOTATION_NODE:
164 fprintf(output, "NOTATION\n");
165 break;
166 default:
167 fprintf(output, "NODE_%d\n", node->type);
168 }
169 if (node->doc == NULL) {
170 fprintf(output, shift);
171 fprintf(output, "doc == NULL !!!\n");
172 }
173 if (node->nsDef != NULL)
174 xmlDebugDumpNamespaceList(output, node->nsDef, depth + 1);
175 if (node->properties != NULL)
176 xmlDebugDumpAttrList(output, node->properties, depth + 1);
177 if (node->type != XML_ENTITY_REF_NODE) {
178 if (node->content != NULL) {
179 fprintf(output, shift);
180 fprintf(output, "content=");
181 xmlDebugDumpString(output, node->content);
182 fprintf(output, "\n");
183 }
184 } else {
185 xmlEntityPtr ent;
186 ent = xmlGetDocEntity(node->doc, node->name);
187 if (ent != NULL)
188 xmlDebugDumpEntity(output, ent, depth + 1);
189 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000190}
191
192void xmlDebugDumpNode(FILE *output, xmlNodePtr node, int depth) {
193 xmlDebugDumpOneNode(output, node, depth);
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000194 if (node->childs != NULL)
195 xmlDebugDumpNodeList(output, node->childs, depth + 1);
196}
197
198void xmlDebugDumpNodeList(FILE *output, xmlNodePtr node, int depth) {
199 while (node != NULL) {
200 xmlDebugDumpNode(output, node, depth);
201 node = node->next;
202 }
203}
204
205
206void xmlDebugDumpDocument(FILE *output, xmlDocPtr doc) {
207 if (output == NULL) output = stdout;
208 if (doc == NULL) {
209 fprintf(output, "DOCUMENT == NULL !\n");
210 return;
211 }
212
213 switch (doc->type) {
214 case XML_ELEMENT_NODE:
215 fprintf(output, "Error, ELEMENT found here ");
216 break;
217 case XML_ATTRIBUTE_NODE:
218 fprintf(output, "Error, ATTRIBUTE found here\n");
219 break;
220 case XML_TEXT_NODE:
221 fprintf(output, "Error, TEXT\n");
222 break;
223 case XML_CDATA_SECTION_NODE:
224 fprintf(output, "Error, CDATA_SECTION\n");
225 break;
226 case XML_ENTITY_REF_NODE:
227 fprintf(output, "Error, ENTITY_REF\n");
228 break;
229 case XML_ENTITY_NODE:
230 fprintf(output, "Error, ENTITY\n");
231 break;
232 case XML_PI_NODE:
233 fprintf(output, "Error, PI\n");
234 break;
235 case XML_COMMENT_NODE:
236 fprintf(output, "Error, COMMENT\n");
237 break;
238 case XML_DOCUMENT_NODE:
239 fprintf(output, "DOCUMENT\n");
240 break;
241 case XML_DOCUMENT_TYPE_NODE:
242 fprintf(output, "Error, DOCUMENT_TYPE\n");
243 break;
244 case XML_DOCUMENT_FRAG_NODE:
245 fprintf(output, "Error, DOCUMENT_FRAG\n");
246 break;
247 case XML_NOTATION_NODE:
248 fprintf(output, "Error, NOTATION\n");
249 break;
250 default:
251 fprintf(output, "NODE_%d\n", doc->type);
252 }
253 if (doc->name != NULL) {
254 fprintf(output, "name=");
Daniel Veillardb96e6431999-08-29 21:02:19 +0000255 xmlDebugDumpString(output, BAD_CAST doc->name);
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000256 fprintf(output, "\n");
257 }
258 if (doc->version != NULL) {
259 fprintf(output, "version=");
260 xmlDebugDumpString(output, doc->version);
261 fprintf(output, "\n");
262 }
263 if (doc->encoding != NULL) {
264 fprintf(output, "encoding=");
265 xmlDebugDumpString(output, doc->encoding);
266 fprintf(output, "\n");
267 }
268 if (doc->standalone)
269 fprintf(output, "standalone=true\n");
270 if (doc->oldNs != NULL)
271 xmlDebugDumpNamespaceList(output, doc->oldNs, 0);
272 if (doc->root != NULL)
273 xmlDebugDumpNodeList(output, doc->root, 1);
274}