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