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