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