blob: 5abec63ee43d2e44eec6f70ef7df045883567072 [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
116void xmlDebugDumpNode(FILE *output, xmlNodePtr node, int depth) {
117 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:
149 fprintf(output, "PI\n");
150 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 }
190 if (node->childs != NULL)
191 xmlDebugDumpNodeList(output, node->childs, depth + 1);
192}
193
194void xmlDebugDumpNodeList(FILE *output, xmlNodePtr node, int depth) {
195 while (node != NULL) {
196 xmlDebugDumpNode(output, node, depth);
197 node = node->next;
198 }
199}
200
201
202void xmlDebugDumpDocument(FILE *output, xmlDocPtr doc) {
203 if (output == NULL) output = stdout;
204 if (doc == NULL) {
205 fprintf(output, "DOCUMENT == NULL !\n");
206 return;
207 }
208
209 switch (doc->type) {
210 case XML_ELEMENT_NODE:
211 fprintf(output, "Error, ELEMENT found here ");
212 break;
213 case XML_ATTRIBUTE_NODE:
214 fprintf(output, "Error, ATTRIBUTE found here\n");
215 break;
216 case XML_TEXT_NODE:
217 fprintf(output, "Error, TEXT\n");
218 break;
219 case XML_CDATA_SECTION_NODE:
220 fprintf(output, "Error, CDATA_SECTION\n");
221 break;
222 case XML_ENTITY_REF_NODE:
223 fprintf(output, "Error, ENTITY_REF\n");
224 break;
225 case XML_ENTITY_NODE:
226 fprintf(output, "Error, ENTITY\n");
227 break;
228 case XML_PI_NODE:
229 fprintf(output, "Error, PI\n");
230 break;
231 case XML_COMMENT_NODE:
232 fprintf(output, "Error, COMMENT\n");
233 break;
234 case XML_DOCUMENT_NODE:
235 fprintf(output, "DOCUMENT\n");
236 break;
237 case XML_DOCUMENT_TYPE_NODE:
238 fprintf(output, "Error, DOCUMENT_TYPE\n");
239 break;
240 case XML_DOCUMENT_FRAG_NODE:
241 fprintf(output, "Error, DOCUMENT_FRAG\n");
242 break;
243 case XML_NOTATION_NODE:
244 fprintf(output, "Error, NOTATION\n");
245 break;
246 default:
247 fprintf(output, "NODE_%d\n", doc->type);
248 }
249 if (doc->name != NULL) {
250 fprintf(output, "name=");
251 xmlDebugDumpString(output, doc->name);
252 fprintf(output, "\n");
253 }
254 if (doc->version != NULL) {
255 fprintf(output, "version=");
256 xmlDebugDumpString(output, doc->version);
257 fprintf(output, "\n");
258 }
259 if (doc->encoding != NULL) {
260 fprintf(output, "encoding=");
261 xmlDebugDumpString(output, doc->encoding);
262 fprintf(output, "\n");
263 }
264 if (doc->standalone)
265 fprintf(output, "standalone=true\n");
266 if (doc->oldNs != NULL)
267 xmlDebugDumpNamespaceList(output, doc->oldNs, 0);
268 if (doc->root != NULL)
269 xmlDebugDumpNodeList(output, doc->root, 1);
270}