Annotation of XML/debugXML.c, revision 1.8

1.1       daniel      1: /*
                      2:  * debugXML.c : This is a set of routines used for debugging the tree
                      3:  *              produced by the XML parser.
                      4:  *
1.2       daniel      5:  * See Copyright for the status of this software.
                      6:  *
1.1       daniel      7:  * 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: 
1.6       daniel     18: void xmlDebugDumpString(FILE *output, const xmlChar *str) {
1.1       daniel     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: 
                     27: void 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: 
                     43: void xmlDebugDumpNamespaceList(FILE *output, xmlNsPtr ns, int depth) {
                     44:     while (ns != NULL) {
                     45:         xmlDebugDumpNamespace(output, ns, depth);
                     46:        ns = ns->next;
                     47:     }
                     48: }
                     49: 
                     50: void 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: 
                     95: void 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: 
                    109: void xmlDebugDumpAttrList(FILE *output, xmlAttrPtr attr, int depth) {
                    110:     while (attr != NULL) {
                    111:         xmlDebugDumpAttr(output, attr, depth);
                    112:        attr = attr->next;
                    113:     }
                    114: }
                    115: 
1.3       daniel    116: void xmlDebugDumpOneNode(FILE *output, xmlNodePtr node, int depth) {
1.1       daniel    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:
1.4       daniel    149:            fprintf(output, "PI %s\n", node->name);
1.1       daniel    150:            break;
                    151:        case XML_COMMENT_NODE:
                    152:            fprintf(output, "COMMENT\n");
                    153:            break;
                    154:        case XML_DOCUMENT_NODE:
1.7       daniel    155:        case XML_HTML_DOCUMENT_NODE:
1.1       daniel    156:            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=");
1.8     ! daniel    182: #ifndef XML_USE_BUFFER_CONTENT     
1.1       daniel    183:            xmlDebugDumpString(output, node->content);
1.8     ! daniel    184: #else
        !           185:            xmlDebugDumpString(output, xmlBufferContent(node->content));
        !           186: #endif
1.1       daniel    187:            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:     }
1.3       daniel    195: }
                    196: 
                    197: void xmlDebugDumpNode(FILE *output, xmlNodePtr node, int depth) {
                    198:     xmlDebugDumpOneNode(output, node, depth);
1.1       daniel    199:     if (node->childs != NULL)
                    200:        xmlDebugDumpNodeList(output, node->childs, depth + 1);
                    201: }
                    202: 
                    203: void xmlDebugDumpNodeList(FILE *output, xmlNodePtr node, int depth) {
                    204:     while (node != NULL) {
                    205:         xmlDebugDumpNode(output, node, depth);
                    206:        node = node->next;
                    207:     }
                    208: }
                    209: 
                    210: 
                    211: void 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");
1.7       daniel    245:            break;
                    246:        case XML_HTML_DOCUMENT_NODE:
                    247:            fprintf(output, "HTML DOCUMENT\n");
1.1       daniel    248:            break;
                    249:        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=");
1.5       daniel    263:         xmlDebugDumpString(output, BAD_CAST doc->name);
1.1       daniel    264:        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: }

Webmaster