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