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