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