Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tester.c : a small tester program for parsing using the SAX API. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel.Veillard@w3.org |
| 7 | */ |
| 8 | |
| 9 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 10 | #include "win32config.h" |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 11 | #else |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 12 | #include "config.h" |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 13 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | #include <stdarg.h> |
| 18 | |
| 19 | #ifdef HAVE_SYS_TYPES_H |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 21 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 22 | #ifdef HAVE_SYS_STAT_H |
| 23 | #include <sys/stat.h> |
| 24 | #endif |
| 25 | #ifdef HAVE_FCNTL_H |
| 26 | #include <fcntl.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_UNISTD_H |
| 29 | #include <unistd.h> |
| 30 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 31 | #ifdef HAVE_STDLIB_H |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 33 | #endif |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 34 | #ifdef HAVE_STRING_H |
| 35 | #include <string.h> |
| 36 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 37 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 38 | |
| 39 | #include "parser.h" |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 40 | #include "parserInternals.h" /* only for xmlNewInputFromFile() */ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 41 | #include "tree.h" |
| 42 | #include "debugXML.h" |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 43 | #include "xmlmemory.h" |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 44 | |
| 45 | static int debug = 0; |
| 46 | static int copy = 0; |
| 47 | static int recovery = 0; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame^] | 48 | static int push = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 49 | |
| 50 | xmlSAXHandler emptySAXHandlerStruct = { |
| 51 | NULL, /* internalSubset */ |
| 52 | NULL, /* isStandalone */ |
| 53 | NULL, /* hasInternalSubset */ |
| 54 | NULL, /* hasExternalSubset */ |
| 55 | NULL, /* resolveEntity */ |
| 56 | NULL, /* getEntity */ |
| 57 | NULL, /* entityDecl */ |
| 58 | NULL, /* notationDecl */ |
| 59 | NULL, /* attributeDecl */ |
| 60 | NULL, /* elementDecl */ |
| 61 | NULL, /* unparsedEntityDecl */ |
| 62 | NULL, /* setDocumentLocator */ |
| 63 | NULL, /* startDocument */ |
| 64 | NULL, /* endDocument */ |
| 65 | NULL, /* startElement */ |
| 66 | NULL, /* endElement */ |
| 67 | NULL, /* reference */ |
| 68 | NULL, /* characters */ |
| 69 | NULL, /* ignorableWhitespace */ |
| 70 | NULL, /* processingInstruction */ |
| 71 | NULL, /* comment */ |
| 72 | NULL, /* xmlParserWarning */ |
| 73 | NULL, /* xmlParserError */ |
| 74 | NULL, /* xmlParserError */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 75 | NULL, /* getParameterEntity */ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 79 | extern xmlSAXHandlerPtr debugSAXHandler; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 80 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 81 | /************************************************************************ |
| 82 | * * |
| 83 | * Debug Handlers * |
| 84 | * * |
| 85 | ************************************************************************/ |
| 86 | |
| 87 | /** |
| 88 | * isStandaloneDebug: |
| 89 | * @ctxt: An XML parser context |
| 90 | * |
| 91 | * Is this document tagged standalone ? |
| 92 | * |
| 93 | * Returns 1 if true |
| 94 | */ |
| 95 | int |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 96 | isStandaloneDebug(void *ctx) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 97 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 98 | fprintf(stdout, "SAX.isStandalone()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 99 | return(0); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * hasInternalSubsetDebug: |
| 104 | * @ctxt: An XML parser context |
| 105 | * |
| 106 | * Does this document has an internal subset |
| 107 | * |
| 108 | * Returns 1 if true |
| 109 | */ |
| 110 | int |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 111 | hasInternalSubsetDebug(void *ctx) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 112 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 113 | fprintf(stdout, "SAX.hasInternalSubset()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 114 | return(0); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * hasExternalSubsetDebug: |
| 119 | * @ctxt: An XML parser context |
| 120 | * |
| 121 | * Does this document has an external subset |
| 122 | * |
| 123 | * Returns 1 if true |
| 124 | */ |
| 125 | int |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 126 | hasExternalSubsetDebug(void *ctx) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 127 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 128 | fprintf(stdout, "SAX.hasExternalSubset()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 129 | return(0); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * hasInternalSubsetDebug: |
| 134 | * @ctxt: An XML parser context |
| 135 | * |
| 136 | * Does this document has an internal subset |
| 137 | */ |
| 138 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 139 | internalSubsetDebug(void *ctx, const xmlChar *name, |
| 140 | const xmlChar *ExternalID, const xmlChar *SystemID) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 141 | { |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 142 | /* xmlDtdPtr externalSubset; */ |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 143 | |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 144 | fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 145 | name, ExternalID, SystemID); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 146 | |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 147 | /*********** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 148 | if ((ExternalID != NULL) || (SystemID != NULL)) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 149 | externalSubset = xmlParseDTD(ExternalID, SystemID); |
| 150 | if (externalSubset != NULL) { |
| 151 | xmlFreeDtd(externalSubset); |
| 152 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 153 | } |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 154 | ***********/ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * resolveEntityDebug: |
| 159 | * @ctxt: An XML parser context |
| 160 | * @publicId: The public ID of the entity |
| 161 | * @systemId: The system ID of the entity |
| 162 | * |
| 163 | * Special entity resolver, better left to the parser, it has |
| 164 | * more context than the application layer. |
| 165 | * The default behaviour is to NOT resolve the entities, in that case |
| 166 | * the ENTITY_REF nodes are built in the structure (and the parameter |
| 167 | * values). |
| 168 | * |
| 169 | * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. |
| 170 | */ |
| 171 | xmlParserInputPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 172 | resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 173 | { |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 174 | /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 175 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 176 | |
| 177 | fprintf(stdout, "SAX.resolveEntity("); |
| 178 | if (publicId != NULL) |
| 179 | fprintf(stdout, "%s", (char *)publicId); |
| 180 | else |
| 181 | fprintf(stdout, " "); |
| 182 | if (systemId != NULL) |
| 183 | fprintf(stdout, ", %s)\n", (char *)systemId); |
| 184 | else |
| 185 | fprintf(stdout, ", )\n"); |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 186 | /********* |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 187 | if (systemId != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 188 | return(xmlNewInputFromFile(ctxt, (char *) systemId)); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 189 | } |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 190 | *********/ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 191 | return(NULL); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * getEntityDebug: |
| 196 | * @ctxt: An XML parser context |
| 197 | * @name: The entity name |
| 198 | * |
| 199 | * Get an entity by name |
| 200 | * |
| 201 | * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. |
| 202 | */ |
| 203 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 204 | getEntityDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 205 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 206 | fprintf(stdout, "SAX.getEntity(%s)\n", name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 207 | return(NULL); |
| 208 | } |
| 209 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 210 | /** |
| 211 | * getParameterEntityDebug: |
| 212 | * @ctxt: An XML parser context |
| 213 | * @name: The entity name |
| 214 | * |
| 215 | * Get a parameter entity by name |
| 216 | * |
| 217 | * Returns the xmlParserInputPtr |
| 218 | */ |
| 219 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 220 | getParameterEntityDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 221 | { |
| 222 | fprintf(stdout, "SAX.getParameterEntity(%s)\n", name); |
| 223 | return(NULL); |
| 224 | } |
| 225 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 226 | |
| 227 | /** |
| 228 | * entityDeclDebug: |
| 229 | * @ctxt: An XML parser context |
| 230 | * @name: the entity name |
| 231 | * @type: the entity type |
| 232 | * @publicId: The public ID of the entity |
| 233 | * @systemId: The system ID of the entity |
| 234 | * @content: the entity value (without processing). |
| 235 | * |
| 236 | * An entity definition has been parsed |
| 237 | */ |
| 238 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 239 | entityDeclDebug(void *ctx, const xmlChar *name, int type, |
| 240 | const xmlChar *publicId, const xmlChar *systemId, xmlChar *content) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 241 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 242 | fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 243 | name, type, publicId, systemId, content); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * attributeDeclDebug: |
| 248 | * @ctxt: An XML parser context |
| 249 | * @name: the attribute name |
| 250 | * @type: the attribute type |
| 251 | * |
| 252 | * An attribute definition has been parsed |
| 253 | */ |
| 254 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 255 | attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name, |
| 256 | int type, int def, const xmlChar *defaultValue, |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 257 | xmlEnumerationPtr tree) |
| 258 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 259 | fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 260 | elem, name, type, def, defaultValue); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * elementDeclDebug: |
| 265 | * @ctxt: An XML parser context |
| 266 | * @name: the element name |
| 267 | * @type: the element type |
| 268 | * @content: the element value (without processing). |
| 269 | * |
| 270 | * An element definition has been parsed |
| 271 | */ |
| 272 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 273 | elementDeclDebug(void *ctx, const xmlChar *name, int type, |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 274 | xmlElementContentPtr content) |
| 275 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 276 | fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 277 | name, type); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * notationDeclDebug: |
| 282 | * @ctxt: An XML parser context |
| 283 | * @name: The name of the notation |
| 284 | * @publicId: The public ID of the entity |
| 285 | * @systemId: The system ID of the entity |
| 286 | * |
| 287 | * What to do when a notation declaration has been parsed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 288 | */ |
| 289 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 290 | notationDeclDebug(void *ctx, const xmlChar *name, |
| 291 | const xmlChar *publicId, const xmlChar *systemId) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 292 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 293 | fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 294 | (char *) name, (char *) publicId, (char *) systemId); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * unparsedEntityDeclDebug: |
| 299 | * @ctxt: An XML parser context |
| 300 | * @name: The name of the entity |
| 301 | * @publicId: The public ID of the entity |
| 302 | * @systemId: The system ID of the entity |
| 303 | * @notationName: the name of the notation |
| 304 | * |
| 305 | * What to do when an unparsed entity declaration is parsed |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 306 | */ |
| 307 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 308 | unparsedEntityDeclDebug(void *ctx, const xmlChar *name, |
| 309 | const xmlChar *publicId, const xmlChar *systemId, |
| 310 | const xmlChar *notationName) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 311 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 312 | fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 313 | (char *) name, (char *) publicId, (char *) systemId, |
| 314 | (char *) notationName); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * setDocumentLocatorDebug: |
| 319 | * @ctxt: An XML parser context |
| 320 | * @loc: A SAX Locator |
| 321 | * |
| 322 | * Receive the document locator at startup, actually xmlDefaultSAXLocator |
| 323 | * Everything is available on the context, so this is useless in our case. |
| 324 | */ |
| 325 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 326 | setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 327 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 328 | fprintf(stdout, "SAX.setDocumentLocator()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * startDocumentDebug: |
| 333 | * @ctxt: An XML parser context |
| 334 | * |
| 335 | * called when the document start being processed. |
| 336 | */ |
| 337 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 338 | startDocumentDebug(void *ctx) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 339 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 340 | fprintf(stdout, "SAX.startDocument()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
| 344 | * endDocumentDebug: |
| 345 | * @ctxt: An XML parser context |
| 346 | * |
| 347 | * called when the document end has been detected. |
| 348 | */ |
| 349 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 350 | endDocumentDebug(void *ctx) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 351 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 352 | fprintf(stdout, "SAX.endDocument()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
| 356 | * startElementDebug: |
| 357 | * @ctxt: An XML parser context |
| 358 | * @name: The element name |
| 359 | * |
| 360 | * called when an opening tag has been processed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 361 | */ |
| 362 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 363 | startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 364 | { |
| 365 | int i; |
| 366 | |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 367 | fprintf(stdout, "SAX.startElement(%s", (char *) name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 368 | if (atts != NULL) { |
| 369 | for (i = 0;(atts[i] != NULL);i++) { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 370 | fprintf(stdout, ", %s='", atts[i++]); |
| 371 | fprintf(stdout, "%s'", atts[i]); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 374 | fprintf(stdout, ")\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * endElementDebug: |
| 379 | * @ctxt: An XML parser context |
| 380 | * @name: The element name |
| 381 | * |
| 382 | * called when the end of an element has been detected. |
| 383 | */ |
| 384 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 385 | endElementDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 386 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 387 | fprintf(stdout, "SAX.endElement(%s)\n", (char *) name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * charactersDebug: |
| 392 | * @ctxt: An XML parser context |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 393 | * @ch: a xmlChar string |
| 394 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 395 | * |
| 396 | * receiving some chars from the parser. |
| 397 | * Question: how much at a time ??? |
| 398 | */ |
| 399 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 400 | charactersDebug(void *ctx, const xmlChar *ch, int len) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 401 | { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 402 | int i; |
| 403 | |
| 404 | fprintf(stdout, "SAX.characters("); |
| 405 | for (i = 0;(i < len) && (i < 30);i++) |
| 406 | fprintf(stdout, "%c", ch[i]); |
| 407 | fprintf(stdout, ", %d)\n", len); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
| 411 | * referenceDebug: |
| 412 | * @ctxt: An XML parser context |
| 413 | * @name: The entity name |
| 414 | * |
| 415 | * called when an entity reference is detected. |
| 416 | */ |
| 417 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 418 | referenceDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 419 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 420 | fprintf(stdout, "SAX.reference(%s)\n", name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
| 424 | * ignorableWhitespaceDebug: |
| 425 | * @ctxt: An XML parser context |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 426 | * @ch: a xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 427 | * @start: the first char in the string |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 428 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 429 | * |
| 430 | * receiving some ignorable whitespaces from the parser. |
| 431 | * Question: how much at a time ??? |
| 432 | */ |
| 433 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 434 | ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 435 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 436 | fprintf(stdout, "SAX.ignorableWhitespace(%.30s, %d)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 437 | (char *) ch, len); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * processingInstructionDebug: |
| 442 | * @ctxt: An XML parser context |
| 443 | * @target: the target name |
| 444 | * @data: the PI data's |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 445 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 446 | * |
| 447 | * A processing instruction has been parsed. |
| 448 | */ |
| 449 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 450 | processingInstructionDebug(void *ctx, const xmlChar *target, |
| 451 | const xmlChar *data) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 452 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 453 | fprintf(stdout, "SAX.processingInstruction(%s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 454 | (char *) target, (char *) data); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * commentDebug: |
| 459 | * @ctxt: An XML parser context |
| 460 | * @value: the comment content |
| 461 | * |
| 462 | * A comment has been parsed. |
| 463 | */ |
| 464 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 465 | commentDebug(void *ctx, const xmlChar *value) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 466 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 467 | fprintf(stdout, "SAX.comment(%s)\n", value); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
| 471 | * warningDebug: |
| 472 | * @ctxt: An XML parser context |
| 473 | * @msg: the message to display/transmit |
| 474 | * @...: extra parameters for the message display |
| 475 | * |
| 476 | * Display and format a warning messages, gives file, line, position and |
| 477 | * extra parameters. |
| 478 | */ |
| 479 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 480 | warningDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 481 | { |
| 482 | va_list args; |
| 483 | |
| 484 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 485 | fprintf(stdout, "SAX.warning: "); |
| 486 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 487 | va_end(args); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * errorDebug: |
| 492 | * @ctxt: An XML parser context |
| 493 | * @msg: the message to display/transmit |
| 494 | * @...: extra parameters for the message display |
| 495 | * |
| 496 | * Display and format a error messages, gives file, line, position and |
| 497 | * extra parameters. |
| 498 | */ |
| 499 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 500 | errorDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 501 | { |
| 502 | va_list args; |
| 503 | |
| 504 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 505 | fprintf(stdout, "SAX.error: "); |
| 506 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 507 | va_end(args); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * fatalErrorDebug: |
| 512 | * @ctxt: An XML parser context |
| 513 | * @msg: the message to display/transmit |
| 514 | * @...: extra parameters for the message display |
| 515 | * |
| 516 | * Display and format a fatalError messages, gives file, line, position and |
| 517 | * extra parameters. |
| 518 | */ |
| 519 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 520 | fatalErrorDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 521 | { |
| 522 | va_list args; |
| 523 | |
| 524 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 525 | fprintf(stdout, "SAX.fatalError: "); |
| 526 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 527 | va_end(args); |
| 528 | } |
| 529 | |
| 530 | xmlSAXHandler debugSAXHandlerStruct = { |
| 531 | internalSubsetDebug, |
| 532 | isStandaloneDebug, |
| 533 | hasInternalSubsetDebug, |
| 534 | hasExternalSubsetDebug, |
| 535 | resolveEntityDebug, |
| 536 | getEntityDebug, |
| 537 | entityDeclDebug, |
| 538 | notationDeclDebug, |
| 539 | attributeDeclDebug, |
| 540 | elementDeclDebug, |
| 541 | unparsedEntityDeclDebug, |
| 542 | setDocumentLocatorDebug, |
| 543 | startDocumentDebug, |
| 544 | endDocumentDebug, |
| 545 | startElementDebug, |
| 546 | endElementDebug, |
| 547 | referenceDebug, |
| 548 | charactersDebug, |
| 549 | ignorableWhitespaceDebug, |
| 550 | processingInstructionDebug, |
| 551 | commentDebug, |
| 552 | warningDebug, |
| 553 | errorDebug, |
| 554 | fatalErrorDebug, |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 555 | getParameterEntityDebug, |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 556 | }; |
| 557 | |
| 558 | xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct; |
| 559 | |
| 560 | /************************************************************************ |
| 561 | * * |
| 562 | * Debug * |
| 563 | * * |
| 564 | ************************************************************************/ |
| 565 | |
| 566 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 567 | int res; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 568 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame^] | 569 | if (push) { |
| 570 | FILE *f; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 571 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame^] | 572 | /* |
| 573 | * Empty callbacks for checking |
| 574 | */ |
| 575 | f = fopen(filename, "r"); |
| 576 | if (f != NULL) { |
| 577 | int res; |
| 578 | char chars[10]; |
| 579 | xmlParserCtxtPtr ctxt; |
| 580 | |
| 581 | res = fread(chars, 1, 4, f); |
| 582 | if (res > 0) { |
| 583 | ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL, |
| 584 | chars, res, filename); |
| 585 | while ((res = fread(chars, 1, 3, f)) > 0) { |
| 586 | xmlParseChunk(ctxt, chars, res, 0); |
| 587 | } |
| 588 | xmlParseChunk(ctxt, chars, 0, 1); |
| 589 | xmlFreeParserCtxt(ctxt); |
| 590 | } |
| 591 | fclose(f); |
| 592 | } else { |
| 593 | fprintf(stderr, "Cannot read file %s\n", filename); |
| 594 | } |
| 595 | /* |
| 596 | * Debug callback |
| 597 | */ |
| 598 | f = fopen(filename, "r"); |
| 599 | if (f != NULL) { |
| 600 | int res; |
| 601 | char chars[10]; |
| 602 | xmlParserCtxtPtr ctxt; |
| 603 | |
| 604 | res = fread(chars, 1, 4, f); |
| 605 | if (res > 0) { |
| 606 | ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL, |
| 607 | chars, res, filename); |
| 608 | while ((res = fread(chars, 1, 3, f)) > 0) { |
| 609 | xmlParseChunk(ctxt, chars, res, 0); |
| 610 | } |
| 611 | res = xmlParseChunk(ctxt, chars, 0, 1); |
| 612 | xmlFreeParserCtxt(ctxt); |
| 613 | if (res != 0) { |
| 614 | fprintf(stdout, |
| 615 | "xmlSAXUserParseFile returned error %d\n", res); |
| 616 | } |
| 617 | } |
| 618 | fclose(f); |
| 619 | } |
| 620 | } else { |
| 621 | /* |
| 622 | * Empty callbacks for checking |
| 623 | */ |
| 624 | res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename); |
| 625 | if (res != 0) { |
| 626 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Debug callback |
| 631 | */ |
| 632 | res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename); |
| 633 | if (res != 0) { |
| 634 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 635 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 639 | |
| 640 | int main(int argc, char **argv) { |
| 641 | int i; |
| 642 | int files = 0; |
| 643 | |
| 644 | for (i = 1; i < argc ; i++) { |
| 645 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 646 | debug++; |
| 647 | else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 648 | copy++; |
| 649 | else if ((!strcmp(argv[i], "-recover")) || |
| 650 | (!strcmp(argv[i], "--recover"))) |
| 651 | recovery++; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame^] | 652 | else if ((!strcmp(argv[i], "-push")) || |
| 653 | (!strcmp(argv[i], "--push"))) |
| 654 | push++; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 655 | } |
| 656 | for (i = 1; i < argc ; i++) { |
| 657 | if (argv[i][0] != '-') { |
| 658 | parseAndPrintFile(argv[i]); |
| 659 | files ++; |
| 660 | } |
| 661 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 662 | xmlCleanupParser(); |
| 663 | xmlMemoryDump(); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 664 | |
| 665 | return(0); |
| 666 | } |