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