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 | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 39 | #include <libxml/xml-error.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 | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 258 | fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 259 | elem, name, type, def, defaultValue); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * elementDeclDebug: |
| 264 | * @ctxt: An XML parser context |
| 265 | * @name: the element name |
| 266 | * @type: the element type |
| 267 | * @content: the element value (without processing). |
| 268 | * |
| 269 | * An element definition has been parsed |
| 270 | */ |
| 271 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 272 | elementDeclDebug(void *ctx, const xmlChar *name, int type, |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 273 | xmlElementContentPtr content) |
| 274 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 275 | fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 276 | name, type); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * notationDeclDebug: |
| 281 | * @ctxt: An XML parser context |
| 282 | * @name: The name of the notation |
| 283 | * @publicId: The public ID of the entity |
| 284 | * @systemId: The system ID of the entity |
| 285 | * |
| 286 | * What to do when a notation declaration has been parsed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 287 | */ |
| 288 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 289 | notationDeclDebug(void *ctx, const xmlChar *name, |
| 290 | const xmlChar *publicId, const xmlChar *systemId) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 291 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 292 | fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 293 | (char *) name, (char *) publicId, (char *) systemId); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * unparsedEntityDeclDebug: |
| 298 | * @ctxt: An XML parser context |
| 299 | * @name: The name of the entity |
| 300 | * @publicId: The public ID of the entity |
| 301 | * @systemId: The system ID of the entity |
| 302 | * @notationName: the name of the notation |
| 303 | * |
| 304 | * What to do when an unparsed entity declaration is parsed |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 305 | */ |
| 306 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 307 | unparsedEntityDeclDebug(void *ctx, const xmlChar *name, |
| 308 | const xmlChar *publicId, const xmlChar *systemId, |
| 309 | const xmlChar *notationName) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 310 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 311 | fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 312 | (char *) name, (char *) publicId, (char *) systemId, |
| 313 | (char *) notationName); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * setDocumentLocatorDebug: |
| 318 | * @ctxt: An XML parser context |
| 319 | * @loc: A SAX Locator |
| 320 | * |
| 321 | * Receive the document locator at startup, actually xmlDefaultSAXLocator |
| 322 | * Everything is available on the context, so this is useless in our case. |
| 323 | */ |
| 324 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 325 | setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 326 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 327 | fprintf(stdout, "SAX.setDocumentLocator()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * startDocumentDebug: |
| 332 | * @ctxt: An XML parser context |
| 333 | * |
| 334 | * called when the document start being processed. |
| 335 | */ |
| 336 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 337 | startDocumentDebug(void *ctx) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 338 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 339 | fprintf(stdout, "SAX.startDocument()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * endDocumentDebug: |
| 344 | * @ctxt: An XML parser context |
| 345 | * |
| 346 | * called when the document end has been detected. |
| 347 | */ |
| 348 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 349 | endDocumentDebug(void *ctx) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 350 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 351 | fprintf(stdout, "SAX.endDocument()\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * startElementDebug: |
| 356 | * @ctxt: An XML parser context |
| 357 | * @name: The element name |
| 358 | * |
| 359 | * called when an opening tag has been processed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 360 | */ |
| 361 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 362 | startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 363 | { |
| 364 | int i; |
| 365 | |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 366 | fprintf(stdout, "SAX.startElement(%s", (char *) name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 367 | if (atts != NULL) { |
| 368 | for (i = 0;(atts[i] != NULL);i++) { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 369 | fprintf(stdout, ", %s='", atts[i++]); |
Daniel Veillard | 808a3f1 | 2000-08-17 13:50:51 +0000 | [diff] [blame^] | 370 | if (atts[i] != NULL) |
| 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 | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 402 | char output[40]; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 403 | int i; |
| 404 | |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 405 | for (i = 0;(i<len) && (i < 30);i++) |
| 406 | output[i] = ch[i]; |
| 407 | output[i] = 0; |
| 408 | |
| 409 | fprintf(stdout, "SAX.characters(%s, %d)\n", output, len); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * referenceDebug: |
| 414 | * @ctxt: An XML parser context |
| 415 | * @name: The entity name |
| 416 | * |
| 417 | * called when an entity reference is detected. |
| 418 | */ |
| 419 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 420 | referenceDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 421 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 422 | fprintf(stdout, "SAX.reference(%s)\n", name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * ignorableWhitespaceDebug: |
| 427 | * @ctxt: An XML parser context |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 428 | * @ch: a xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 429 | * @start: the first char in the string |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 430 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 431 | * |
| 432 | * receiving some ignorable whitespaces from the parser. |
| 433 | * Question: how much at a time ??? |
| 434 | */ |
| 435 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 436 | ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 437 | { |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 438 | char output[40]; |
| 439 | int i; |
| 440 | |
| 441 | for (i = 0;(i<len) && (i < 30);i++) |
| 442 | output[i] = ch[i]; |
| 443 | output[i] = 0; |
| 444 | fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
| 448 | * processingInstructionDebug: |
| 449 | * @ctxt: An XML parser context |
| 450 | * @target: the target name |
| 451 | * @data: the PI data's |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 452 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 453 | * |
| 454 | * A processing instruction has been parsed. |
| 455 | */ |
| 456 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 457 | processingInstructionDebug(void *ctx, const xmlChar *target, |
| 458 | const xmlChar *data) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 459 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 460 | fprintf(stdout, "SAX.processingInstruction(%s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 461 | (char *) target, (char *) data); |
| 462 | } |
| 463 | |
| 464 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 465 | * cdataBlockDebug: |
| 466 | * @ctx: the user data (XML parser context) |
| 467 | * @value: The pcdata content |
| 468 | * @len: the block length |
| 469 | * |
| 470 | * called when a pcdata block has been parsed |
| 471 | */ |
| 472 | void |
| 473 | cdataBlockDebug(void *ctx, const xmlChar *value, int len) |
| 474 | { |
| 475 | fprintf(stderr, "SAX.pcdata(%.20s, %d)\n", |
| 476 | (char *) value, len); |
| 477 | } |
| 478 | |
| 479 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 480 | * commentDebug: |
| 481 | * @ctxt: An XML parser context |
| 482 | * @value: the comment content |
| 483 | * |
| 484 | * A comment has been parsed. |
| 485 | */ |
| 486 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 487 | commentDebug(void *ctx, const xmlChar *value) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 488 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 489 | fprintf(stdout, "SAX.comment(%s)\n", value); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /** |
| 493 | * warningDebug: |
| 494 | * @ctxt: An XML parser context |
| 495 | * @msg: the message to display/transmit |
| 496 | * @...: extra parameters for the message display |
| 497 | * |
| 498 | * Display and format a warning messages, gives file, line, position and |
| 499 | * extra parameters. |
| 500 | */ |
| 501 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 502 | warningDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 503 | { |
| 504 | va_list args; |
| 505 | |
| 506 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 507 | fprintf(stdout, "SAX.warning: "); |
| 508 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 509 | va_end(args); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * errorDebug: |
| 514 | * @ctxt: An XML parser context |
| 515 | * @msg: the message to display/transmit |
| 516 | * @...: extra parameters for the message display |
| 517 | * |
| 518 | * Display and format a error messages, gives file, line, position and |
| 519 | * extra parameters. |
| 520 | */ |
| 521 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 522 | errorDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 523 | { |
| 524 | va_list args; |
| 525 | |
| 526 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 527 | fprintf(stdout, "SAX.error: "); |
| 528 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 529 | va_end(args); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * fatalErrorDebug: |
| 534 | * @ctxt: An XML parser context |
| 535 | * @msg: the message to display/transmit |
| 536 | * @...: extra parameters for the message display |
| 537 | * |
| 538 | * Display and format a fatalError messages, gives file, line, position and |
| 539 | * extra parameters. |
| 540 | */ |
| 541 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 542 | fatalErrorDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 543 | { |
| 544 | va_list args; |
| 545 | |
| 546 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 547 | fprintf(stdout, "SAX.fatalError: "); |
| 548 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 549 | va_end(args); |
| 550 | } |
| 551 | |
| 552 | xmlSAXHandler debugSAXHandlerStruct = { |
| 553 | internalSubsetDebug, |
| 554 | isStandaloneDebug, |
| 555 | hasInternalSubsetDebug, |
| 556 | hasExternalSubsetDebug, |
| 557 | resolveEntityDebug, |
| 558 | getEntityDebug, |
| 559 | entityDeclDebug, |
| 560 | notationDeclDebug, |
| 561 | attributeDeclDebug, |
| 562 | elementDeclDebug, |
| 563 | unparsedEntityDeclDebug, |
| 564 | setDocumentLocatorDebug, |
| 565 | startDocumentDebug, |
| 566 | endDocumentDebug, |
| 567 | startElementDebug, |
| 568 | endElementDebug, |
| 569 | referenceDebug, |
| 570 | charactersDebug, |
| 571 | ignorableWhitespaceDebug, |
| 572 | processingInstructionDebug, |
| 573 | commentDebug, |
| 574 | warningDebug, |
| 575 | errorDebug, |
| 576 | fatalErrorDebug, |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 577 | getParameterEntityDebug, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 578 | cdataBlockDebug |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 579 | }; |
| 580 | |
| 581 | xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct; |
| 582 | |
| 583 | /************************************************************************ |
| 584 | * * |
| 585 | * Debug * |
| 586 | * * |
| 587 | ************************************************************************/ |
| 588 | |
| 589 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 590 | int res; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 591 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 592 | if (push) { |
| 593 | FILE *f; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 594 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 595 | /* |
| 596 | * Empty callbacks for checking |
| 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(emptySAXHandler, NULL, |
| 607 | chars, res, filename); |
| 608 | while ((res = fread(chars, 1, 3, f)) > 0) { |
| 609 | xmlParseChunk(ctxt, chars, res, 0); |
| 610 | } |
| 611 | xmlParseChunk(ctxt, chars, 0, 1); |
| 612 | xmlFreeParserCtxt(ctxt); |
| 613 | } |
| 614 | fclose(f); |
| 615 | } else { |
| 616 | fprintf(stderr, "Cannot read file %s\n", filename); |
| 617 | } |
| 618 | /* |
| 619 | * Debug callback |
| 620 | */ |
| 621 | f = fopen(filename, "r"); |
| 622 | if (f != NULL) { |
| 623 | int res; |
| 624 | char chars[10]; |
| 625 | xmlParserCtxtPtr ctxt; |
| 626 | |
| 627 | res = fread(chars, 1, 4, f); |
| 628 | if (res > 0) { |
| 629 | ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL, |
| 630 | chars, res, filename); |
| 631 | while ((res = fread(chars, 1, 3, f)) > 0) { |
| 632 | xmlParseChunk(ctxt, chars, res, 0); |
| 633 | } |
| 634 | res = xmlParseChunk(ctxt, chars, 0, 1); |
| 635 | xmlFreeParserCtxt(ctxt); |
| 636 | if (res != 0) { |
| 637 | fprintf(stdout, |
| 638 | "xmlSAXUserParseFile returned error %d\n", res); |
| 639 | } |
| 640 | } |
| 641 | fclose(f); |
| 642 | } |
| 643 | } else { |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 644 | if (!speed) { |
| 645 | /* |
| 646 | * Empty callbacks for checking |
| 647 | */ |
| 648 | res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename); |
| 649 | if (res != 0) { |
| 650 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 651 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 652 | |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 653 | /* |
| 654 | * Debug callback |
| 655 | */ |
| 656 | res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename); |
| 657 | if (res != 0) { |
| 658 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 659 | } |
| 660 | } else { |
| 661 | /* |
| 662 | * test 100x the SAX parse |
| 663 | */ |
| 664 | int i; |
| 665 | |
| 666 | for (i = 0; i<100;i++) |
| 667 | res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename); |
| 668 | if (res != 0) { |
| 669 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 670 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 671 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 675 | |
| 676 | int main(int argc, char **argv) { |
| 677 | int i; |
| 678 | int files = 0; |
| 679 | |
| 680 | for (i = 1; i < argc ; i++) { |
| 681 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 682 | debug++; |
| 683 | else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 684 | copy++; |
| 685 | else if ((!strcmp(argv[i], "-recover")) || |
| 686 | (!strcmp(argv[i], "--recover"))) |
| 687 | recovery++; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 688 | else if ((!strcmp(argv[i], "-push")) || |
| 689 | (!strcmp(argv[i], "--push"))) |
| 690 | push++; |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 691 | else if ((!strcmp(argv[i], "-speed")) || |
| 692 | (!strcmp(argv[i], "--speed"))) |
| 693 | speed++; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 694 | } |
| 695 | for (i = 1; i < argc ; i++) { |
| 696 | if (argv[i][0] != '-') { |
| 697 | parseAndPrintFile(argv[i]); |
| 698 | files ++; |
| 699 | } |
| 700 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 701 | xmlCleanupParser(); |
| 702 | xmlMemoryDump(); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 703 | |
| 704 | return(0); |
| 705 | } |