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 | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 145 | /* xmlDtdPtr externalSubset; */ |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 146 | |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 147 | fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 148 | name, ExternalID, SystemID); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 149 | |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 150 | /*********** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 151 | if ((ExternalID != NULL) || (SystemID != NULL)) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 152 | externalSubset = xmlParseDTD(ExternalID, SystemID); |
| 153 | if (externalSubset != NULL) { |
| 154 | xmlFreeDtd(externalSubset); |
| 155 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 156 | } |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 157 | ***********/ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * resolveEntityDebug: |
| 162 | * @ctxt: An XML parser context |
| 163 | * @publicId: The public ID of the entity |
| 164 | * @systemId: The system ID of the entity |
| 165 | * |
| 166 | * Special entity resolver, better left to the parser, it has |
| 167 | * more context than the application layer. |
| 168 | * The default behaviour is to NOT resolve the entities, in that case |
| 169 | * the ENTITY_REF nodes are built in the structure (and the parameter |
| 170 | * values). |
| 171 | * |
| 172 | * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. |
| 173 | */ |
| 174 | xmlParserInputPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 175 | resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 176 | { |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 177 | /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 178 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 179 | |
| 180 | fprintf(stdout, "SAX.resolveEntity("); |
| 181 | if (publicId != NULL) |
| 182 | fprintf(stdout, "%s", (char *)publicId); |
| 183 | else |
| 184 | fprintf(stdout, " "); |
| 185 | if (systemId != NULL) |
| 186 | fprintf(stdout, ", %s)\n", (char *)systemId); |
| 187 | else |
| 188 | fprintf(stdout, ", )\n"); |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 189 | /********* |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 190 | if (systemId != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 191 | return(xmlNewInputFromFile(ctxt, (char *) systemId)); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 192 | } |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 193 | *********/ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 194 | return(NULL); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * getEntityDebug: |
| 199 | * @ctxt: An XML parser context |
| 200 | * @name: The entity name |
| 201 | * |
| 202 | * Get an entity by name |
| 203 | * |
| 204 | * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. |
| 205 | */ |
| 206 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 207 | getEntityDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 208 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 209 | fprintf(stdout, "SAX.getEntity(%s)\n", name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 210 | return(NULL); |
| 211 | } |
| 212 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 213 | /** |
| 214 | * getParameterEntityDebug: |
| 215 | * @ctxt: An XML parser context |
| 216 | * @name: The entity name |
| 217 | * |
| 218 | * Get a parameter entity by name |
| 219 | * |
| 220 | * Returns the xmlParserInputPtr |
| 221 | */ |
| 222 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 223 | getParameterEntityDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 224 | { |
| 225 | fprintf(stdout, "SAX.getParameterEntity(%s)\n", name); |
| 226 | return(NULL); |
| 227 | } |
| 228 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * entityDeclDebug: |
| 232 | * @ctxt: An XML parser context |
| 233 | * @name: the entity name |
| 234 | * @type: the entity type |
| 235 | * @publicId: The public ID of the entity |
| 236 | * @systemId: The system ID of the entity |
| 237 | * @content: the entity value (without processing). |
| 238 | * |
| 239 | * An entity definition has been parsed |
| 240 | */ |
| 241 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 242 | entityDeclDebug(void *ctx, const xmlChar *name, int type, |
| 243 | const xmlChar *publicId, const xmlChar *systemId, xmlChar *content) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 244 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 245 | fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 246 | name, type, publicId, systemId, content); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * attributeDeclDebug: |
| 251 | * @ctxt: An XML parser context |
| 252 | * @name: the attribute name |
| 253 | * @type: the attribute type |
| 254 | * |
| 255 | * An attribute definition has been parsed |
| 256 | */ |
| 257 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 258 | attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name, |
| 259 | int type, int def, const xmlChar *defaultValue, |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 260 | xmlEnumerationPtr tree) |
| 261 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 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++]); |
| 374 | fprintf(stdout, "%s'", atts[i]); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 377 | fprintf(stdout, ")\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
| 381 | * endElementDebug: |
| 382 | * @ctxt: An XML parser context |
| 383 | * @name: The element name |
| 384 | * |
| 385 | * called when the end of an element has been detected. |
| 386 | */ |
| 387 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 388 | endElementDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 389 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 390 | fprintf(stdout, "SAX.endElement(%s)\n", (char *) name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * charactersDebug: |
| 395 | * @ctxt: An XML parser context |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 396 | * @ch: a xmlChar string |
| 397 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 398 | * |
| 399 | * receiving some chars from the parser. |
| 400 | * Question: how much at a time ??? |
| 401 | */ |
| 402 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 403 | charactersDebug(void *ctx, const xmlChar *ch, int len) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 404 | { |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 405 | char output[40]; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 406 | int i; |
| 407 | |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 408 | for (i = 0;(i<len) && (i < 30);i++) |
| 409 | output[i] = ch[i]; |
| 410 | output[i] = 0; |
| 411 | |
| 412 | fprintf(stdout, "SAX.characters(%s, %d)\n", output, len); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
| 416 | * referenceDebug: |
| 417 | * @ctxt: An XML parser context |
| 418 | * @name: The entity name |
| 419 | * |
| 420 | * called when an entity reference is detected. |
| 421 | */ |
| 422 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 423 | referenceDebug(void *ctx, const xmlChar *name) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 424 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 425 | fprintf(stdout, "SAX.reference(%s)\n", name); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
| 429 | * ignorableWhitespaceDebug: |
| 430 | * @ctxt: An XML parser context |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 431 | * @ch: a xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 432 | * @start: the first char in the string |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 433 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 434 | * |
| 435 | * receiving some ignorable whitespaces from the parser. |
| 436 | * Question: how much at a time ??? |
| 437 | */ |
| 438 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 439 | ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 440 | { |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 441 | char output[40]; |
| 442 | int i; |
| 443 | |
| 444 | for (i = 0;(i<len) && (i < 30);i++) |
| 445 | output[i] = ch[i]; |
| 446 | output[i] = 0; |
| 447 | fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
| 451 | * processingInstructionDebug: |
| 452 | * @ctxt: An XML parser context |
| 453 | * @target: the target name |
| 454 | * @data: the PI data's |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 455 | * @len: the number of xmlChar |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 456 | * |
| 457 | * A processing instruction has been parsed. |
| 458 | */ |
| 459 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 460 | processingInstructionDebug(void *ctx, const xmlChar *target, |
| 461 | const xmlChar *data) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 462 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 463 | fprintf(stdout, "SAX.processingInstruction(%s, %s)\n", |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 464 | (char *) target, (char *) data); |
| 465 | } |
| 466 | |
| 467 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 468 | * cdataBlockDebug: |
| 469 | * @ctx: the user data (XML parser context) |
| 470 | * @value: The pcdata content |
| 471 | * @len: the block length |
| 472 | * |
| 473 | * called when a pcdata block has been parsed |
| 474 | */ |
| 475 | void |
| 476 | cdataBlockDebug(void *ctx, const xmlChar *value, int len) |
| 477 | { |
| 478 | fprintf(stderr, "SAX.pcdata(%.20s, %d)\n", |
| 479 | (char *) value, len); |
| 480 | } |
| 481 | |
| 482 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 483 | * commentDebug: |
| 484 | * @ctxt: An XML parser context |
| 485 | * @value: the comment content |
| 486 | * |
| 487 | * A comment has been parsed. |
| 488 | */ |
| 489 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 490 | commentDebug(void *ctx, const xmlChar *value) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 491 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 492 | fprintf(stdout, "SAX.comment(%s)\n", value); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
| 496 | * warningDebug: |
| 497 | * @ctxt: An XML parser context |
| 498 | * @msg: the message to display/transmit |
| 499 | * @...: extra parameters for the message display |
| 500 | * |
| 501 | * Display and format a warning messages, gives file, line, position and |
| 502 | * extra parameters. |
| 503 | */ |
| 504 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 505 | warningDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 506 | { |
| 507 | va_list args; |
| 508 | |
| 509 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 510 | fprintf(stdout, "SAX.warning: "); |
| 511 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 512 | va_end(args); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * errorDebug: |
| 517 | * @ctxt: An XML parser context |
| 518 | * @msg: the message to display/transmit |
| 519 | * @...: extra parameters for the message display |
| 520 | * |
| 521 | * Display and format a error messages, gives file, line, position and |
| 522 | * extra parameters. |
| 523 | */ |
| 524 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 525 | errorDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 526 | { |
| 527 | va_list args; |
| 528 | |
| 529 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 530 | fprintf(stdout, "SAX.error: "); |
| 531 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 532 | va_end(args); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * fatalErrorDebug: |
| 537 | * @ctxt: An XML parser context |
| 538 | * @msg: the message to display/transmit |
| 539 | * @...: extra parameters for the message display |
| 540 | * |
| 541 | * Display and format a fatalError messages, gives file, line, position and |
| 542 | * extra parameters. |
| 543 | */ |
| 544 | void |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 545 | fatalErrorDebug(void *ctx, const char *msg, ...) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 546 | { |
| 547 | va_list args; |
| 548 | |
| 549 | va_start(args, msg); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 550 | fprintf(stdout, "SAX.fatalError: "); |
| 551 | vfprintf(stdout, msg, args); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 552 | va_end(args); |
| 553 | } |
| 554 | |
| 555 | xmlSAXHandler debugSAXHandlerStruct = { |
| 556 | internalSubsetDebug, |
| 557 | isStandaloneDebug, |
| 558 | hasInternalSubsetDebug, |
| 559 | hasExternalSubsetDebug, |
| 560 | resolveEntityDebug, |
| 561 | getEntityDebug, |
| 562 | entityDeclDebug, |
| 563 | notationDeclDebug, |
| 564 | attributeDeclDebug, |
| 565 | elementDeclDebug, |
| 566 | unparsedEntityDeclDebug, |
| 567 | setDocumentLocatorDebug, |
| 568 | startDocumentDebug, |
| 569 | endDocumentDebug, |
| 570 | startElementDebug, |
| 571 | endElementDebug, |
| 572 | referenceDebug, |
| 573 | charactersDebug, |
| 574 | ignorableWhitespaceDebug, |
| 575 | processingInstructionDebug, |
| 576 | commentDebug, |
| 577 | warningDebug, |
| 578 | errorDebug, |
| 579 | fatalErrorDebug, |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 580 | getParameterEntityDebug, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 581 | cdataBlockDebug |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 582 | }; |
| 583 | |
| 584 | xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct; |
| 585 | |
| 586 | /************************************************************************ |
| 587 | * * |
| 588 | * Debug * |
| 589 | * * |
| 590 | ************************************************************************/ |
| 591 | |
| 592 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | 7a66ee6 | 1999-09-26 11:31:02 +0000 | [diff] [blame] | 593 | int res; |
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 | if (push) { |
| 596 | FILE *f; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 597 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 598 | /* |
| 599 | * Empty callbacks for checking |
| 600 | */ |
| 601 | f = fopen(filename, "r"); |
| 602 | if (f != NULL) { |
| 603 | int res; |
| 604 | char chars[10]; |
| 605 | xmlParserCtxtPtr ctxt; |
| 606 | |
| 607 | res = fread(chars, 1, 4, f); |
| 608 | if (res > 0) { |
| 609 | ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL, |
| 610 | chars, res, filename); |
| 611 | while ((res = fread(chars, 1, 3, f)) > 0) { |
| 612 | xmlParseChunk(ctxt, chars, res, 0); |
| 613 | } |
| 614 | xmlParseChunk(ctxt, chars, 0, 1); |
| 615 | xmlFreeParserCtxt(ctxt); |
| 616 | } |
| 617 | fclose(f); |
| 618 | } else { |
| 619 | fprintf(stderr, "Cannot read file %s\n", filename); |
| 620 | } |
| 621 | /* |
| 622 | * Debug callback |
| 623 | */ |
| 624 | f = fopen(filename, "r"); |
| 625 | if (f != NULL) { |
| 626 | int res; |
| 627 | char chars[10]; |
| 628 | xmlParserCtxtPtr ctxt; |
| 629 | |
| 630 | res = fread(chars, 1, 4, f); |
| 631 | if (res > 0) { |
| 632 | ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL, |
| 633 | chars, res, filename); |
| 634 | while ((res = fread(chars, 1, 3, f)) > 0) { |
| 635 | xmlParseChunk(ctxt, chars, res, 0); |
| 636 | } |
| 637 | res = xmlParseChunk(ctxt, chars, 0, 1); |
| 638 | xmlFreeParserCtxt(ctxt); |
| 639 | if (res != 0) { |
| 640 | fprintf(stdout, |
| 641 | "xmlSAXUserParseFile returned error %d\n", res); |
| 642 | } |
| 643 | } |
| 644 | fclose(f); |
| 645 | } |
| 646 | } else { |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 647 | if (!speed) { |
| 648 | /* |
| 649 | * Empty callbacks for checking |
| 650 | */ |
| 651 | res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename); |
| 652 | if (res != 0) { |
| 653 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 654 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 655 | |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 656 | /* |
| 657 | * Debug callback |
| 658 | */ |
| 659 | res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename); |
| 660 | if (res != 0) { |
| 661 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 662 | } |
| 663 | } else { |
| 664 | /* |
| 665 | * test 100x the SAX parse |
| 666 | */ |
| 667 | int i; |
| 668 | |
| 669 | for (i = 0; i<100;i++) |
| 670 | res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename); |
| 671 | if (res != 0) { |
| 672 | fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res); |
| 673 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 674 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 678 | |
| 679 | int main(int argc, char **argv) { |
| 680 | int i; |
| 681 | int files = 0; |
| 682 | |
| 683 | for (i = 1; i < argc ; i++) { |
| 684 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 685 | debug++; |
| 686 | else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 687 | copy++; |
| 688 | else if ((!strcmp(argv[i], "-recover")) || |
| 689 | (!strcmp(argv[i], "--recover"))) |
| 690 | recovery++; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 691 | else if ((!strcmp(argv[i], "-push")) || |
| 692 | (!strcmp(argv[i], "--push"))) |
| 693 | push++; |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 694 | else if ((!strcmp(argv[i], "-speed")) || |
| 695 | (!strcmp(argv[i], "--speed"))) |
| 696 | speed++; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 697 | } |
| 698 | for (i = 1; i < argc ; i++) { |
| 699 | if (argv[i][0] != '-') { |
| 700 | parseAndPrintFile(argv[i]); |
| 701 | files ++; |
| 702 | } |
| 703 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 704 | xmlCleanupParser(); |
| 705 | xmlMemoryDump(); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 706 | |
| 707 | return(0); |
| 708 | } |