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