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