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