blob: 721f0e519e81f12e1263061b3247dfb83c92c03b [file] [log] [blame]
Daniel Veillardbe70ff71999-07-05 16:50:46 +00001/*
2 * testHTML.c : a small tester program for HTML input.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel.Veillard@w3.org
7 */
8
9#ifdef WIN32
Daniel Veillard3c558c31999-12-22 11:30:41 +000010#include "win32config.h"
Daniel Veillardbe70ff71999-07-05 16:50:46 +000011#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000012#include "config.h"
Daniel Veillardbe70ff71999-07-05 16:50:46 +000013#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000014
Daniel Veillard361d8452000-04-03 19:48:13 +000015#include "xmlversion.h"
16#ifdef LIBXML_HTML_ENABLED
17
Daniel Veillard7f7d1111999-09-22 09:46:25 +000018#include <stdio.h>
19#include <string.h>
Daniel Veillard7c1206f1999-10-14 09:10:25 +000020#include <stdarg.h>
21
Daniel Veillard7f7d1111999-09-22 09:46:25 +000022
23#ifdef HAVE_SYS_TYPES_H
Daniel Veillardbe70ff71999-07-05 16:50:46 +000024#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000025#endif
Daniel Veillardbe70ff71999-07-05 16:50:46 +000026#ifdef HAVE_SYS_STAT_H
27#include <sys/stat.h>
28#endif
29#ifdef HAVE_FCNTL_H
30#include <fcntl.h>
31#endif
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000035#ifdef HAVE_STDLIB_H
Daniel Veillardbe70ff71999-07-05 16:50:46 +000036#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000037#endif
Daniel Veillardbe70ff71999-07-05 16:50:46 +000038
Daniel Veillard361d8452000-04-03 19:48:13 +000039#include <libxml/xmlmemory.h>
40#include <libxml/HTMLparser.h>
41#include <libxml/HTMLtree.h>
42#include <libxml/debugXML.h>
Daniel Veillardbe70ff71999-07-05 16:50:46 +000043
Daniel Veillard361d8452000-04-03 19:48:13 +000044#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardbe70ff71999-07-05 16:50:46 +000045static int debug = 0;
Daniel Veillard361d8452000-04-03 19:48:13 +000046#endif
Daniel Veillardbe70ff71999-07-05 16:50:46 +000047static int copy = 0;
Daniel Veillard7c1206f1999-10-14 09:10:25 +000048static int sax = 0;
49static int repeat = 0;
50static int noout = 0;
Daniel Veillard5e5c6231999-12-29 12:49:06 +000051static int push = 0;
Daniel Veillard32bc74e2000-07-14 14:49:25 +000052static char *encoding = NULL;
Daniel Veillardbe70ff71999-07-05 16:50:46 +000053
Daniel Veillard7c1206f1999-10-14 09:10:25 +000054xmlSAXHandler emptySAXHandlerStruct = {
55 NULL, /* internalSubset */
56 NULL, /* isStandalone */
57 NULL, /* hasInternalSubset */
58 NULL, /* hasExternalSubset */
59 NULL, /* resolveEntity */
60 NULL, /* getEntity */
61 NULL, /* entityDecl */
62 NULL, /* notationDecl */
63 NULL, /* attributeDecl */
64 NULL, /* elementDecl */
65 NULL, /* unparsedEntityDecl */
66 NULL, /* setDocumentLocator */
67 NULL, /* startDocument */
68 NULL, /* endDocument */
69 NULL, /* startElement */
70 NULL, /* endElement */
71 NULL, /* reference */
72 NULL, /* characters */
73 NULL, /* ignorableWhitespace */
74 NULL, /* processingInstruction */
75 NULL, /* comment */
76 NULL, /* xmlParserWarning */
77 NULL, /* xmlParserError */
78 NULL, /* xmlParserError */
79 NULL, /* getParameterEntity */
80};
81
82xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
83extern xmlSAXHandlerPtr debugSAXHandler;
84
85/************************************************************************
86 * *
87 * Debug Handlers *
88 * *
89 ************************************************************************/
90
91/**
92 * isStandaloneDebug:
93 * @ctxt: An XML parser context
94 *
95 * Is this document tagged standalone ?
96 *
97 * Returns 1 if true
98 */
99int
100isStandaloneDebug(void *ctx)
101{
102 fprintf(stdout, "SAX.isStandalone()\n");
103 return(0);
104}
105
106/**
107 * hasInternalSubsetDebug:
108 * @ctxt: An XML parser context
109 *
110 * Does this document has an internal subset
111 *
112 * Returns 1 if true
113 */
114int
115hasInternalSubsetDebug(void *ctx)
116{
117 fprintf(stdout, "SAX.hasInternalSubset()\n");
118 return(0);
119}
120
121/**
122 * hasExternalSubsetDebug:
123 * @ctxt: An XML parser context
124 *
125 * Does this document has an external subset
126 *
127 * Returns 1 if true
128 */
129int
130hasExternalSubsetDebug(void *ctx)
131{
132 fprintf(stdout, "SAX.hasExternalSubset()\n");
133 return(0);
134}
135
136/**
137 * hasInternalSubsetDebug:
138 * @ctxt: An XML parser context
139 *
140 * Does this document has an internal subset
141 */
142void
143internalSubsetDebug(void *ctx, const xmlChar *name,
144 const xmlChar *ExternalID, const xmlChar *SystemID)
145{
146 /* xmlDtdPtr externalSubset; */
147
148 fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
Daniel Veillard1255ab72000-08-14 15:13:33 +0000149 name, (ExternalID == NULL) ? "(null)" : ExternalID,
150 (SystemID == NULL) ? "(null)" : SystemID);
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000151
152/***********
153 if ((ExternalID != NULL) || (SystemID != NULL)) {
154 externalSubset = xmlParseDTD(ExternalID, SystemID);
155 if (externalSubset != NULL) {
156 xmlFreeDtd(externalSubset);
157 }
158 }
159 ***********/
160}
161
162/**
163 * resolveEntityDebug:
164 * @ctxt: An XML parser context
165 * @publicId: The public ID of the entity
166 * @systemId: The system ID of the entity
167 *
168 * Special entity resolver, better left to the parser, it has
169 * more context than the application layer.
170 * The default behaviour is to NOT resolve the entities, in that case
171 * the ENTITY_REF nodes are built in the structure (and the parameter
172 * values).
173 *
174 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
175 */
176xmlParserInputPtr
177resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
178{
179 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
180
181
182 fprintf(stdout, "SAX.resolveEntity(");
183 if (publicId != NULL)
184 fprintf(stdout, "%s", (char *)publicId);
185 else
186 fprintf(stdout, " ");
187 if (systemId != NULL)
188 fprintf(stdout, ", %s)\n", (char *)systemId);
189 else
190 fprintf(stdout, ", )\n");
191/*********
192 if (systemId != NULL) {
193 return(xmlNewInputFromFile(ctxt, (char *) systemId));
194 }
195 *********/
196 return(NULL);
197}
198
199/**
200 * getEntityDebug:
201 * @ctxt: An XML parser context
202 * @name: The entity name
203 *
204 * Get an entity by name
205 *
206 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
207 */
208xmlEntityPtr
209getEntityDebug(void *ctx, const xmlChar *name)
210{
211 fprintf(stdout, "SAX.getEntity(%s)\n", name);
212 return(NULL);
213}
214
215/**
216 * getParameterEntityDebug:
217 * @ctxt: An XML parser context
218 * @name: The entity name
219 *
220 * Get a parameter entity by name
221 *
222 * Returns the xmlParserInputPtr
223 */
224xmlEntityPtr
225getParameterEntityDebug(void *ctx, const xmlChar *name)
226{
227 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
228 return(NULL);
229}
230
231
232/**
233 * entityDeclDebug:
234 * @ctxt: An XML parser context
235 * @name: the entity name
236 * @type: the entity type
237 * @publicId: The public ID of the entity
238 * @systemId: The system ID of the entity
239 * @content: the entity value (without processing).
240 *
241 * An entity definition has been parsed
242 */
243void
244entityDeclDebug(void *ctx, const xmlChar *name, int type,
245 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
246{
247 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
248 name, type, publicId, systemId, content);
249}
250
251/**
252 * attributeDeclDebug:
253 * @ctxt: An XML parser context
254 * @name: the attribute name
255 * @type: the attribute type
256 *
257 * An attribute definition has been parsed
258 */
259void
260attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
261 int type, int def, const xmlChar *defaultValue,
262 xmlEnumerationPtr tree)
263{
264 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
265 elem, name, type, def, defaultValue);
266}
267
268/**
269 * elementDeclDebug:
270 * @ctxt: An XML parser context
271 * @name: the element name
272 * @type: the element type
273 * @content: the element value (without processing).
274 *
275 * An element definition has been parsed
276 */
277void
278elementDeclDebug(void *ctx, const xmlChar *name, int type,
279 xmlElementContentPtr content)
280{
281 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
282 name, type);
283}
284
285/**
286 * notationDeclDebug:
287 * @ctxt: An XML parser context
288 * @name: The name of the notation
289 * @publicId: The public ID of the entity
290 * @systemId: The system ID of the entity
291 *
292 * What to do when a notation declaration has been parsed.
293 */
294void
295notationDeclDebug(void *ctx, const xmlChar *name,
296 const xmlChar *publicId, const xmlChar *systemId)
297{
298 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
299 (char *) name, (char *) publicId, (char *) systemId);
300}
301
302/**
303 * unparsedEntityDeclDebug:
304 * @ctxt: An XML parser context
305 * @name: The name of the entity
306 * @publicId: The public ID of the entity
307 * @systemId: The system ID of the entity
308 * @notationName: the name of the notation
309 *
310 * What to do when an unparsed entity declaration is parsed
311 */
312void
313unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
314 const xmlChar *publicId, const xmlChar *systemId,
315 const xmlChar *notationName)
316{
317 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
318 (char *) name, (char *) publicId, (char *) systemId,
319 (char *) notationName);
320}
321
322/**
323 * setDocumentLocatorDebug:
324 * @ctxt: An XML parser context
325 * @loc: A SAX Locator
326 *
327 * Receive the document locator at startup, actually xmlDefaultSAXLocator
328 * Everything is available on the context, so this is useless in our case.
329 */
330void
331setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
332{
333 fprintf(stdout, "SAX.setDocumentLocator()\n");
334}
335
336/**
337 * startDocumentDebug:
338 * @ctxt: An XML parser context
339 *
340 * called when the document start being processed.
341 */
342void
343startDocumentDebug(void *ctx)
344{
345 fprintf(stdout, "SAX.startDocument()\n");
346}
347
348/**
349 * endDocumentDebug:
350 * @ctxt: An XML parser context
351 *
352 * called when the document end has been detected.
353 */
354void
355endDocumentDebug(void *ctx)
356{
357 fprintf(stdout, "SAX.endDocument()\n");
358}
359
360/**
361 * startElementDebug:
362 * @ctxt: An XML parser context
363 * @name: The element name
364 *
365 * called when an opening tag has been processed.
366 */
367void
368startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
369{
370 int i;
371
372 fprintf(stdout, "SAX.startElement(%s", (char *) name);
373 if (atts != NULL) {
374 for (i = 0;(atts[i] != NULL);i++) {
375 fprintf(stdout, ", %s='", atts[i++]);
Daniel Veillard1255ab72000-08-14 15:13:33 +0000376 fprintf(stdout, "%s'", (atts[i] == NULL) ? "(null)" : atts[i]);
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000377 }
378 }
379 fprintf(stdout, ")\n");
380}
381
382/**
383 * endElementDebug:
384 * @ctxt: An XML parser context
385 * @name: The element name
386 *
387 * called when the end of an element has been detected.
388 */
389void
390endElementDebug(void *ctx, const xmlChar *name)
391{
392 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
393}
394
395/**
396 * charactersDebug:
397 * @ctxt: An XML parser context
398 * @ch: a xmlChar string
399 * @len: the number of xmlChar
400 *
401 * receiving some chars from the parser.
402 * Question: how much at a time ???
403 */
404void
405charactersDebug(void *ctx, const xmlChar *ch, int len)
406{
Daniel Veillard87b95392000-08-12 21:12:04 +0000407 char output[40];
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000408 int i;
409
Daniel Veillard87b95392000-08-12 21:12:04 +0000410 for (i = 0;(i<len) && (i < 30);i++)
411 output[i] = ch[i];
412 output[i] = 0;
413
414 fprintf(stdout, "SAX.characters(%s, %d)\n", output, len);
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000415}
416
417/**
418 * referenceDebug:
419 * @ctxt: An XML parser context
420 * @name: The entity name
421 *
422 * called when an entity reference is detected.
423 */
424void
425referenceDebug(void *ctx, const xmlChar *name)
426{
427 fprintf(stdout, "SAX.reference(%s)\n", name);
428}
429
430/**
431 * ignorableWhitespaceDebug:
432 * @ctxt: An XML parser context
433 * @ch: a xmlChar string
434 * @start: the first char in the string
435 * @len: the number of xmlChar
436 *
437 * receiving some ignorable whitespaces from the parser.
438 * Question: how much at a time ???
439 */
440void
441ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
442{
Daniel Veillard87b95392000-08-12 21:12:04 +0000443 char output[40];
444 int i;
445
446 for (i = 0;(i<len) && (i < 30);i++)
447 output[i] = ch[i];
448 output[i] = 0;
449
450 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len);
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000451}
452
453/**
454 * processingInstructionDebug:
455 * @ctxt: An XML parser context
456 * @target: the target name
457 * @data: the PI data's
458 * @len: the number of xmlChar
459 *
460 * A processing instruction has been parsed.
461 */
462void
463processingInstructionDebug(void *ctx, const xmlChar *target,
464 const xmlChar *data)
465{
466 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
467 (char *) target, (char *) data);
468}
469
470/**
471 * commentDebug:
472 * @ctxt: An XML parser context
473 * @value: the comment content
474 *
475 * A comment has been parsed.
476 */
477void
478commentDebug(void *ctx, const xmlChar *value)
479{
480 fprintf(stdout, "SAX.comment(%s)\n", value);
481}
482
483/**
484 * warningDebug:
485 * @ctxt: An XML parser context
486 * @msg: the message to display/transmit
487 * @...: extra parameters for the message display
488 *
489 * Display and format a warning messages, gives file, line, position and
490 * extra parameters.
491 */
492void
493warningDebug(void *ctx, const char *msg, ...)
494{
495 va_list args;
496
497 va_start(args, msg);
498 fprintf(stdout, "SAX.warning: ");
499 vfprintf(stdout, msg, args);
500 va_end(args);
501}
502
503/**
504 * errorDebug:
505 * @ctxt: An XML parser context
506 * @msg: the message to display/transmit
507 * @...: extra parameters for the message display
508 *
509 * Display and format a error messages, gives file, line, position and
510 * extra parameters.
511 */
512void
513errorDebug(void *ctx, const char *msg, ...)
514{
515 va_list args;
516
517 va_start(args, msg);
518 fprintf(stdout, "SAX.error: ");
519 vfprintf(stdout, msg, args);
520 va_end(args);
521}
522
523/**
524 * fatalErrorDebug:
525 * @ctxt: An XML parser context
526 * @msg: the message to display/transmit
527 * @...: extra parameters for the message display
528 *
529 * Display and format a fatalError messages, gives file, line, position and
530 * extra parameters.
531 */
532void
533fatalErrorDebug(void *ctx, const char *msg, ...)
534{
535 va_list args;
536
537 va_start(args, msg);
538 fprintf(stdout, "SAX.fatalError: ");
539 vfprintf(stdout, msg, args);
540 va_end(args);
541}
542
543xmlSAXHandler debugSAXHandlerStruct = {
544 internalSubsetDebug,
545 isStandaloneDebug,
546 hasInternalSubsetDebug,
547 hasExternalSubsetDebug,
548 resolveEntityDebug,
549 getEntityDebug,
550 entityDeclDebug,
551 notationDeclDebug,
552 attributeDeclDebug,
553 elementDeclDebug,
554 unparsedEntityDeclDebug,
555 setDocumentLocatorDebug,
556 startDocumentDebug,
557 endDocumentDebug,
558 startElementDebug,
559 endElementDebug,
560 referenceDebug,
561 charactersDebug,
562 ignorableWhitespaceDebug,
563 processingInstructionDebug,
564 commentDebug,
565 warningDebug,
566 errorDebug,
567 fatalErrorDebug,
568 getParameterEntityDebug,
569};
570
571xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000572/************************************************************************
573 * *
574 * Debug *
575 * *
576 ************************************************************************/
577
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000578void parseSAXFile(char *filename) {
579 htmlDocPtr doc;
580 /*
581 * Empty callbacks for checking
582 */
Daniel Veillard87b95392000-08-12 21:12:04 +0000583 if (push) {
584 FILE *f;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000585
Daniel Veillard87b95392000-08-12 21:12:04 +0000586 f = fopen(filename, "r");
587 if (f != NULL) {
588 int res, size = 3;
589 char chars[4096];
590 htmlParserCtxtPtr ctxt;
591
592 /* if (repeat) */
593 size = 4096;
594 res = fread(chars, 1, 4, f);
595 if (res > 0) {
596 ctxt = htmlCreatePushParserCtxt(emptySAXHandler, NULL,
597 chars, res, filename, 0);
598 while ((res = fread(chars, 1, size, f)) > 0) {
599 htmlParseChunk(ctxt, chars, res, 0);
600 }
601 htmlParseChunk(ctxt, chars, 0, 1);
602 doc = ctxt->myDoc;
603 htmlFreeParserCtxt(ctxt);
604 }
605 if (doc != NULL) {
606 fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
607 xmlFreeDoc(doc);
608 }
609 fclose(f);
610 }
611 if (!noout) {
612 f = fopen(filename, "r");
613 if (f != NULL) {
614 int res, size = 3;
615 char chars[4096];
616 htmlParserCtxtPtr ctxt;
617
618 /* if (repeat) */
619 size = 4096;
620 res = fread(chars, 1, 4, f);
621 if (res > 0) {
622 ctxt = htmlCreatePushParserCtxt(debugSAXHandler, NULL,
623 chars, res, filename, 0);
624 while ((res = fread(chars, 1, size, f)) > 0) {
625 htmlParseChunk(ctxt, chars, res, 0);
626 }
627 htmlParseChunk(ctxt, chars, 0, 1);
628 doc = ctxt->myDoc;
629 htmlFreeParserCtxt(ctxt);
630 }
631 if (doc != NULL) {
632 fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
633 xmlFreeDoc(doc);
634 }
635 fclose(f);
636 }
637 }
638 } else {
639 doc = htmlSAXParseFile(filename, NULL, emptySAXHandler, NULL);
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000640 if (doc != NULL) {
641 fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
642 xmlFreeDoc(doc);
643 }
Daniel Veillard87b95392000-08-12 21:12:04 +0000644
645 if (!noout) {
646 /*
647 * Debug callback
648 */
649 doc = htmlSAXParseFile(filename, NULL, debugSAXHandler, NULL);
650 if (doc != NULL) {
651 fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
652 xmlFreeDoc(doc);
653 }
654 }
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000655 }
656}
657
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000658void parseAndPrintFile(char *filename) {
Daniel Veillard2eac5032000-01-09 21:08:56 +0000659 htmlDocPtr doc = NULL, tmp;
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000660
661 /*
662 * build an HTML tree from a string;
663 */
Daniel Veillard5e5c6231999-12-29 12:49:06 +0000664 if (push) {
665 FILE *f;
666
667 f = fopen(filename, "r");
668 if (f != NULL) {
669 int res, size = 3;
Daniel Veillard87b95392000-08-12 21:12:04 +0000670 char chars[4096];
Daniel Veillard5e5c6231999-12-29 12:49:06 +0000671 htmlParserCtxtPtr ctxt;
672
Daniel Veillard87b95392000-08-12 21:12:04 +0000673 /* if (repeat) */
674 size = 4096;
Daniel Veillard5e5c6231999-12-29 12:49:06 +0000675 res = fread(chars, 1, 4, f);
676 if (res > 0) {
677 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
678 chars, res, filename, 0);
679 while ((res = fread(chars, 1, size, f)) > 0) {
680 htmlParseChunk(ctxt, chars, res, 0);
681 }
682 htmlParseChunk(ctxt, chars, 0, 1);
683 doc = ctxt->myDoc;
684 htmlFreeParserCtxt(ctxt);
685 }
Daniel Veillard87b95392000-08-12 21:12:04 +0000686 fclose(f);
Daniel Veillard5e5c6231999-12-29 12:49:06 +0000687 }
688 } else {
689 doc = htmlParseFile(filename, NULL);
690 }
691 if (doc == NULL) {
692 fprintf(stderr, "Could not parse %s\n", filename);
693 }
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000694
695 /*
696 * test intermediate copy if needed.
697 */
698 if (copy) {
699 tmp = doc;
700 doc = xmlCopyDoc(doc, 1);
701 xmlFreeDoc(tmp);
702 }
703
704 /*
705 * print it.
706 */
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000707 if (!noout) {
Daniel Veillard361d8452000-04-03 19:48:13 +0000708#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000709 if (!debug) {
710 if (encoding)
711 htmlSaveFileEnc("-", doc, encoding);
712 else
713 htmlDocDump(stdout, doc);
714 } else
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000715 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard361d8452000-04-03 19:48:13 +0000716#else
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000717 if (encoding)
718 htmlSaveFileEnc("-", doc, encoding);
719 else
720 htmlDocDump(stdout, doc);
Daniel Veillard361d8452000-04-03 19:48:13 +0000721#endif
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000722 }
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000723
724 /*
725 * free it.
726 */
727 xmlFreeDoc(doc);
728}
729
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000730int main(int argc, char **argv) {
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000731 int i, count;
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000732 int files = 0;
733
734 for (i = 1; i < argc ; i++) {
Daniel Veillard361d8452000-04-03 19:48:13 +0000735#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000736 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
737 debug++;
Daniel Veillard361d8452000-04-03 19:48:13 +0000738 else
739#endif
740 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000741 copy++;
Daniel Veillard5e5c6231999-12-29 12:49:06 +0000742 else if ((!strcmp(argv[i], "-push")) || (!strcmp(argv[i], "--push")))
743 push++;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000744 else if ((!strcmp(argv[i], "-sax")) || (!strcmp(argv[i], "--sax")))
745 sax++;
746 else if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout")))
747 noout++;
748 else if ((!strcmp(argv[i], "-repeat")) ||
749 (!strcmp(argv[i], "--repeat")))
750 repeat++;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000751 else if ((!strcmp(argv[i], "-encode")) ||
752 (!strcmp(argv[i], "--encode"))) {
753 i++;
754 encoding = argv[i];
755 }
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000756 }
757 for (i = 1; i < argc ; i++) {
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000758 if ((!strcmp(argv[i], "-encode")) ||
759 (!strcmp(argv[i], "--encode"))) {
760 i++;
761 continue;
762 }
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000763 if (argv[i][0] != '-') {
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000764 if (repeat) {
765 for (count = 0;count < 100 * repeat;count++) {
766 if (sax)
767 parseSAXFile(argv[i]);
768 else
769 parseAndPrintFile(argv[i]);
770 }
771 } else {
772 if (sax)
773 parseSAXFile(argv[i]);
774 else
775 parseAndPrintFile(argv[i]);
776 }
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000777 files ++;
778 }
779 }
780 if (files == 0) {
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000781 printf("Usage : %s [--debug] [--copy] [--copy] HTMLfiles ...\n",
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000782 argv[0]);
783 printf("\tParse the HTML files and output the result of the parsing\n");
Daniel Veillard361d8452000-04-03 19:48:13 +0000784#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000785 printf("\t--debug : dump a debug tree of the in-memory document\n");
Daniel Veillard361d8452000-04-03 19:48:13 +0000786#endif
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000787 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000788 printf("\t--sax : debug the sequence of SAX callbacks\n");
Daniel Veillard5e5c6231999-12-29 12:49:06 +0000789 printf("\t--repeat : parse the file 100 times, for timing\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000790 printf("\t--noout : do not print the result\n");
Daniel Veillard5e5c6231999-12-29 12:49:06 +0000791 printf("\t--push : use the push mode parser\n");
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000792 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000793 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000794 xmlCleanupParser();
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000795 xmlMemoryDump();
Daniel Veillardbe70ff71999-07-05 16:50:46 +0000796
797 return(0);
798}
Daniel Veillard361d8452000-04-03 19:48:13 +0000799#else /* !LIBXML_HTML_ENABLED */
800#include <stdio.h>
801int main(int argc, char **argv) {
802 printf("%s : HTML support not compiled in\n", argv[0]);
803 return(0);
804}
805#endif