Annotation of XML/testHTML.c, revision 1.18

1.1       daniel      1: /*
                      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
1.9       daniel     10: #include "win32config.h"
1.1       daniel     11: #else
1.4       daniel     12: #include "config.h"
1.1       daniel     13: #endif
1.3       daniel     14: 
1.12      daniel     15: #include "xmlversion.h"
                     16: #ifdef LIBXML_HTML_ENABLED
                     17: 
1.3       daniel     18: #include <stdio.h>
                     19: #include <string.h>
1.7       daniel     20: #include <stdarg.h>
                     21: 
1.3       daniel     22: 
                     23: #ifdef HAVE_SYS_TYPES_H
1.1       daniel     24: #include <sys/types.h>
1.3       daniel     25: #endif
1.1       daniel     26: #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
1.3       daniel     35: #ifdef HAVE_STDLIB_H
1.1       daniel     36: #include <stdlib.h>
1.3       daniel     37: #endif
1.1       daniel     38: 
1.12      daniel     39: #include <libxml/xmlmemory.h>
                     40: #include <libxml/HTMLparser.h>
                     41: #include <libxml/HTMLtree.h>
                     42: #include <libxml/debugXML.h>
1.1       daniel     43: 
1.12      daniel     44: #ifdef LIBXML_DEBUG_ENABLED
1.1       daniel     45: static int debug = 0;
1.12      daniel     46: #endif
1.1       daniel     47: static int copy = 0;
1.7       daniel     48: static int sax = 0;
                     49: static int repeat = 0;
                     50: static int noout = 0;
1.10      daniel     51: static int push = 0;
1.13      veillard   52: static char *encoding = NULL;
1.1       daniel     53: 
1.7       daniel     54: xmlSAXHandler 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: 
                     82: xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
                     83: extern 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:  */
                     99: int
                    100: isStandaloneDebug(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:  */
                    114: int
                    115: hasInternalSubsetDebug(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:  */
                    129: int
                    130: hasExternalSubsetDebug(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:  */
                    142: void
                    143: internalSubsetDebug(void *ctx, const xmlChar *name,
                    144:               const xmlChar *ExternalID, const xmlChar *SystemID)
                    145: {
1.17      veillard  146:     fprintf(stdout, "SAX.internalSubset(%s,", name);
                    147:     if (ExternalID == NULL)
                    148:        fprintf(stdout, " ,");
                    149:     else
                    150:        fprintf(stdout, " %s,", ExternalID);
                    151:     if (SystemID == NULL)
                    152:        fprintf(stdout, " )\n");
                    153:     else
                    154:        fprintf(stdout, " %s)\n", SystemID);
1.7       daniel    155: }
                    156: 
                    157: /**
                    158:  * resolveEntityDebug:
                    159:  * @ctxt:  An XML parser context
                    160:  * @publicId: The public ID of the entity
                    161:  * @systemId: The system ID of the entity
                    162:  *
                    163:  * Special entity resolver, better left to the parser, it has
                    164:  * more context than the application layer.
                    165:  * The default behaviour is to NOT resolve the entities, in that case
                    166:  * the ENTITY_REF nodes are built in the structure (and the parameter
                    167:  * values).
                    168:  *
                    169:  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
                    170:  */
                    171: xmlParserInputPtr
                    172: resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
                    173: {
                    174:     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
                    175: 
                    176:     
                    177:     fprintf(stdout, "SAX.resolveEntity(");
                    178:     if (publicId != NULL)
                    179:        fprintf(stdout, "%s", (char *)publicId);
                    180:     else
                    181:        fprintf(stdout, " ");
                    182:     if (systemId != NULL)
                    183:        fprintf(stdout, ", %s)\n", (char *)systemId);
                    184:     else
                    185:        fprintf(stdout, ", )\n");
                    186: /*********
                    187:     if (systemId != NULL) {
                    188:         return(xmlNewInputFromFile(ctxt, (char *) systemId));
                    189:     }
                    190:  *********/
                    191:     return(NULL);
                    192: }
                    193: 
                    194: /**
                    195:  * getEntityDebug:
                    196:  * @ctxt:  An XML parser context
                    197:  * @name: The entity name
                    198:  *
                    199:  * Get an entity by name
                    200:  *
                    201:  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
                    202:  */
                    203: xmlEntityPtr
                    204: getEntityDebug(void *ctx, const xmlChar *name)
                    205: {
                    206:     fprintf(stdout, "SAX.getEntity(%s)\n", name);
                    207:     return(NULL);
                    208: }
                    209: 
                    210: /**
                    211:  * getParameterEntityDebug:
                    212:  * @ctxt:  An XML parser context
                    213:  * @name: The entity name
                    214:  *
                    215:  * Get a parameter entity by name
                    216:  *
                    217:  * Returns the xmlParserInputPtr
                    218:  */
                    219: xmlEntityPtr
                    220: getParameterEntityDebug(void *ctx, const xmlChar *name)
                    221: {
                    222:     fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
                    223:     return(NULL);
                    224: }
                    225: 
                    226: 
                    227: /**
                    228:  * entityDeclDebug:
                    229:  * @ctxt:  An XML parser context
                    230:  * @name:  the entity name 
                    231:  * @type:  the entity type 
                    232:  * @publicId: The public ID of the entity
                    233:  * @systemId: The system ID of the entity
                    234:  * @content: the entity value (without processing).
                    235:  *
                    236:  * An entity definition has been parsed
                    237:  */
                    238: void
                    239: entityDeclDebug(void *ctx, const xmlChar *name, int type,
                    240:           const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
                    241: {
                    242:     fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
                    243:             name, type, publicId, systemId, content);
                    244: }
                    245: 
                    246: /**
                    247:  * attributeDeclDebug:
                    248:  * @ctxt:  An XML parser context
                    249:  * @name:  the attribute name 
                    250:  * @type:  the attribute type 
                    251:  *
                    252:  * An attribute definition has been parsed
                    253:  */
                    254: void
                    255: attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
                    256:               int type, int def, const xmlChar *defaultValue,
                    257:              xmlEnumerationPtr tree)
                    258: {
                    259:     fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
                    260:             elem, name, type, def, defaultValue);
                    261: }
                    262: 
                    263: /**
                    264:  * elementDeclDebug:
                    265:  * @ctxt:  An XML parser context
                    266:  * @name:  the element name 
                    267:  * @type:  the element type 
                    268:  * @content: the element value (without processing).
                    269:  *
                    270:  * An element definition has been parsed
                    271:  */
                    272: void
                    273: elementDeclDebug(void *ctx, const xmlChar *name, int type,
                    274:            xmlElementContentPtr content)
                    275: {
                    276:     fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
                    277:             name, type);
                    278: }
                    279: 
                    280: /**
                    281:  * notationDeclDebug:
                    282:  * @ctxt:  An XML parser context
                    283:  * @name: The name of the notation
                    284:  * @publicId: The public ID of the entity
                    285:  * @systemId: The system ID of the entity
                    286:  *
                    287:  * What to do when a notation declaration has been parsed.
                    288:  */
                    289: void
                    290: notationDeclDebug(void *ctx, const xmlChar *name,
                    291:             const xmlChar *publicId, const xmlChar *systemId)
                    292: {
                    293:     fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
                    294:             (char *) name, (char *) publicId, (char *) systemId);
                    295: }
                    296: 
                    297: /**
                    298:  * unparsedEntityDeclDebug:
                    299:  * @ctxt:  An XML parser context
                    300:  * @name: The name of the entity
                    301:  * @publicId: The public ID of the entity
                    302:  * @systemId: The system ID of the entity
                    303:  * @notationName: the name of the notation
                    304:  *
                    305:  * What to do when an unparsed entity declaration is parsed
                    306:  */
                    307: void
                    308: unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
                    309:                   const xmlChar *publicId, const xmlChar *systemId,
                    310:                   const xmlChar *notationName)
                    311: {
                    312:     fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
                    313:             (char *) name, (char *) publicId, (char *) systemId,
                    314:            (char *) notationName);
                    315: }
                    316: 
                    317: /**
                    318:  * setDocumentLocatorDebug:
                    319:  * @ctxt:  An XML parser context
                    320:  * @loc: A SAX Locator
                    321:  *
                    322:  * Receive the document locator at startup, actually xmlDefaultSAXLocator
                    323:  * Everything is available on the context, so this is useless in our case.
                    324:  */
                    325: void
                    326: setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
                    327: {
                    328:     fprintf(stdout, "SAX.setDocumentLocator()\n");
                    329: }
                    330: 
                    331: /**
                    332:  * startDocumentDebug:
                    333:  * @ctxt:  An XML parser context
                    334:  *
                    335:  * called when the document start being processed.
                    336:  */
                    337: void
                    338: startDocumentDebug(void *ctx)
                    339: {
                    340:     fprintf(stdout, "SAX.startDocument()\n");
                    341: }
                    342: 
                    343: /**
                    344:  * endDocumentDebug:
                    345:  * @ctxt:  An XML parser context
                    346:  *
                    347:  * called when the document end has been detected.
                    348:  */
                    349: void
                    350: endDocumentDebug(void *ctx)
                    351: {
                    352:     fprintf(stdout, "SAX.endDocument()\n");
                    353: }
                    354: 
                    355: /**
                    356:  * startElementDebug:
                    357:  * @ctxt:  An XML parser context
                    358:  * @name:  The element name
                    359:  *
                    360:  * called when an opening tag has been processed.
                    361:  */
                    362: void
                    363: startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
                    364: {
                    365:     int i;
                    366: 
                    367:     fprintf(stdout, "SAX.startElement(%s", (char *) name);
                    368:     if (atts != NULL) {
                    369:         for (i = 0;(atts[i] != NULL);i++) {
1.17      veillard  370:            fprintf(stdout, ", %s", atts[i++]);
                    371:            if (atts[i] != NULL)
                    372:                fprintf(stdout, "='%s'", atts[i]);
1.7       daniel    373:        }
                    374:     }
                    375:     fprintf(stdout, ")\n");
                    376: }
                    377: 
                    378: /**
                    379:  * endElementDebug:
                    380:  * @ctxt:  An XML parser context
                    381:  * @name:  The element name
                    382:  *
                    383:  * called when the end of an element has been detected.
                    384:  */
                    385: void
                    386: endElementDebug(void *ctx, const xmlChar *name)
                    387: {
                    388:     fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
                    389: }
                    390: 
                    391: /**
                    392:  * charactersDebug:
                    393:  * @ctxt:  An XML parser context
                    394:  * @ch:  a xmlChar string
                    395:  * @len: the number of xmlChar
                    396:  *
                    397:  * receiving some chars from the parser.
                    398:  * Question: how much at a time ???
                    399:  */
                    400: void
                    401: charactersDebug(void *ctx, const xmlChar *ch, int len)
                    402: {
1.15      veillard  403:     char output[40];
1.7       daniel    404:     int i;
                    405: 
1.15      veillard  406:     for (i = 0;(i<len) && (i < 30);i++)
                    407:        output[i] = ch[i];
                    408:     output[i] = 0;
                    409: 
                    410:     fprintf(stdout, "SAX.characters(%s, %d)\n", output, len);
1.7       daniel    411: }
                    412: 
                    413: /**
                    414:  * referenceDebug:
                    415:  * @ctxt:  An XML parser context
                    416:  * @name:  The entity name
                    417:  *
                    418:  * called when an entity reference is detected. 
                    419:  */
                    420: void
                    421: referenceDebug(void *ctx, const xmlChar *name)
                    422: {
                    423:     fprintf(stdout, "SAX.reference(%s)\n", name);
                    424: }
                    425: 
                    426: /**
                    427:  * ignorableWhitespaceDebug:
                    428:  * @ctxt:  An XML parser context
                    429:  * @ch:  a xmlChar string
                    430:  * @start: the first char in the string
                    431:  * @len: the number of xmlChar
                    432:  *
                    433:  * receiving some ignorable whitespaces from the parser.
                    434:  * Question: how much at a time ???
                    435:  */
                    436: void
                    437: ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
                    438: {
1.15      veillard  439:     char output[40];
                    440:     int i;
                    441: 
                    442:     for (i = 0;(i<len) && (i < 30);i++)
                    443:        output[i] = ch[i];
                    444:     output[i] = 0;
                    445: 
                    446:     fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len);
1.7       daniel    447: }
                    448: 
                    449: /**
                    450:  * processingInstructionDebug:
                    451:  * @ctxt:  An XML parser context
                    452:  * @target:  the target name
                    453:  * @data: the PI data's
                    454:  * @len: the number of xmlChar
                    455:  *
                    456:  * A processing instruction has been parsed.
                    457:  */
                    458: void
                    459: processingInstructionDebug(void *ctx, const xmlChar *target,
                    460:                       const xmlChar *data)
                    461: {
                    462:     fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
                    463:             (char *) target, (char *) data);
                    464: }
                    465: 
                    466: /**
                    467:  * commentDebug:
                    468:  * @ctxt:  An XML parser context
                    469:  * @value:  the comment content
                    470:  *
                    471:  * A comment has been parsed.
                    472:  */
                    473: void
                    474: commentDebug(void *ctx, const xmlChar *value)
                    475: {
                    476:     fprintf(stdout, "SAX.comment(%s)\n", value);
                    477: }
                    478: 
                    479: /**
                    480:  * warningDebug:
                    481:  * @ctxt:  An XML parser context
                    482:  * @msg:  the message to display/transmit
                    483:  * @...:  extra parameters for the message display
                    484:  *
                    485:  * Display and format a warning messages, gives file, line, position and
                    486:  * extra parameters.
                    487:  */
                    488: void
                    489: warningDebug(void *ctx, const char *msg, ...)
                    490: {
                    491:     va_list args;
                    492: 
                    493:     va_start(args, msg);
                    494:     fprintf(stdout, "SAX.warning: ");
                    495:     vfprintf(stdout, msg, args);
                    496:     va_end(args);
                    497: }
                    498: 
                    499: /**
                    500:  * errorDebug:
                    501:  * @ctxt:  An XML parser context
                    502:  * @msg:  the message to display/transmit
                    503:  * @...:  extra parameters for the message display
                    504:  *
                    505:  * Display and format a error messages, gives file, line, position and
                    506:  * extra parameters.
                    507:  */
                    508: void
                    509: errorDebug(void *ctx, const char *msg, ...)
                    510: {
                    511:     va_list args;
                    512: 
                    513:     va_start(args, msg);
                    514:     fprintf(stdout, "SAX.error: ");
                    515:     vfprintf(stdout, msg, args);
                    516:     va_end(args);
                    517: }
                    518: 
                    519: /**
                    520:  * fatalErrorDebug:
                    521:  * @ctxt:  An XML parser context
                    522:  * @msg:  the message to display/transmit
                    523:  * @...:  extra parameters for the message display
                    524:  *
                    525:  * Display and format a fatalError messages, gives file, line, position and
                    526:  * extra parameters.
                    527:  */
                    528: void
                    529: fatalErrorDebug(void *ctx, const char *msg, ...)
                    530: {
                    531:     va_list args;
                    532: 
                    533:     va_start(args, msg);
                    534:     fprintf(stdout, "SAX.fatalError: ");
                    535:     vfprintf(stdout, msg, args);
                    536:     va_end(args);
                    537: }
                    538: 
                    539: xmlSAXHandler debugSAXHandlerStruct = {
                    540:     internalSubsetDebug,
                    541:     isStandaloneDebug,
                    542:     hasInternalSubsetDebug,
                    543:     hasExternalSubsetDebug,
                    544:     resolveEntityDebug,
                    545:     getEntityDebug,
                    546:     entityDeclDebug,
                    547:     notationDeclDebug,
                    548:     attributeDeclDebug,
                    549:     elementDeclDebug,
                    550:     unparsedEntityDeclDebug,
                    551:     setDocumentLocatorDebug,
                    552:     startDocumentDebug,
                    553:     endDocumentDebug,
                    554:     startElementDebug,
                    555:     endElementDebug,
                    556:     referenceDebug,
                    557:     charactersDebug,
                    558:     ignorableWhitespaceDebug,
                    559:     processingInstructionDebug,
                    560:     commentDebug,
                    561:     warningDebug,
                    562:     errorDebug,
                    563:     fatalErrorDebug,
                    564:     getParameterEntityDebug,
                    565: };
                    566: 
                    567: xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1.1       daniel    568: /************************************************************************
                    569:  *                                                                     *
                    570:  *                             Debug                                   *
                    571:  *                                                                     *
                    572:  ************************************************************************/
                    573: 
1.7       daniel    574: void parseSAXFile(char *filename) {
1.18    ! veillard  575:     htmlDocPtr doc = NULL;
        !           576: 
1.7       daniel    577:     /*
                    578:      * Empty callbacks for checking
                    579:      */
1.15      veillard  580:     if (push) {
                    581:        FILE *f;
                    582: 
                    583:        f = fopen(filename, "r");
                    584:        if (f != NULL) {
                    585:            int res, size = 3;
                    586:            char chars[4096];
                    587:            htmlParserCtxtPtr ctxt;
1.7       daniel    588: 
1.15      veillard  589:            /* if (repeat) */
                    590:                size = 4096;
                    591:            res = fread(chars, 1, 4, f);
                    592:            if (res > 0) {
                    593:                ctxt = htmlCreatePushParserCtxt(emptySAXHandler, NULL,
                    594:                            chars, res, filename, 0);
                    595:                while ((res = fread(chars, 1, size, f)) > 0) {
                    596:                    htmlParseChunk(ctxt, chars, res, 0);
                    597:                }
                    598:                htmlParseChunk(ctxt, chars, 0, 1);
                    599:                doc = ctxt->myDoc;
                    600:                htmlFreeParserCtxt(ctxt);
                    601:            }
                    602:            if (doc != NULL) {
                    603:                fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    604:                xmlFreeDoc(doc);
                    605:            }
                    606:            fclose(f);
                    607:        }
                    608:        if (!noout) {
                    609:            f = fopen(filename, "r");
                    610:            if (f != NULL) {
                    611:                int res, size = 3;
                    612:                char chars[4096];
                    613:                htmlParserCtxtPtr ctxt;
                    614: 
                    615:                /* if (repeat) */
                    616:                    size = 4096;
                    617:                res = fread(chars, 1, 4, f);
                    618:                if (res > 0) {
                    619:                    ctxt = htmlCreatePushParserCtxt(debugSAXHandler, NULL,
                    620:                                chars, res, filename, 0);
                    621:                    while ((res = fread(chars, 1, size, f)) > 0) {
                    622:                        htmlParseChunk(ctxt, chars, res, 0);
                    623:                    }
                    624:                    htmlParseChunk(ctxt, chars, 0, 1);
                    625:                    doc = ctxt->myDoc;
                    626:                    htmlFreeParserCtxt(ctxt);
                    627:                }
                    628:                if (doc != NULL) {
                    629:                    fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    630:                    xmlFreeDoc(doc);
                    631:                }
                    632:                fclose(f);
                    633:            }
                    634:        }
                    635:     } else {   
                    636:        doc = htmlSAXParseFile(filename, NULL, emptySAXHandler, NULL);
1.7       daniel    637:        if (doc != NULL) {
                    638:            fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    639:            xmlFreeDoc(doc);
                    640:        }
1.15      veillard  641: 
                    642:        if (!noout) {
                    643:            /*
                    644:             * Debug callback
                    645:             */
                    646:            doc = htmlSAXParseFile(filename, NULL, debugSAXHandler, NULL);
                    647:            if (doc != NULL) {
                    648:                fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    649:                xmlFreeDoc(doc);
                    650:            }
                    651:        }
1.7       daniel    652:     }
                    653: }
                    654: 
1.1       daniel    655: void parseAndPrintFile(char *filename) {
1.11      daniel    656:     htmlDocPtr doc = NULL, tmp;
1.1       daniel    657: 
                    658:     /*
                    659:      * build an HTML tree from a string;
                    660:      */
1.10      daniel    661:     if (push) {
                    662:        FILE *f;
                    663: 
                    664:        f = fopen(filename, "r");
                    665:        if (f != NULL) {
                    666:            int res, size = 3;
1.14      veillard  667:            char chars[4096];
1.10      daniel    668:            htmlParserCtxtPtr ctxt;
                    669: 
1.14      veillard  670:            /* if (repeat) */
                    671:                size = 4096;
1.10      daniel    672:            res = fread(chars, 1, 4, f);
                    673:            if (res > 0) {
                    674:                ctxt = htmlCreatePushParserCtxt(NULL, NULL,
                    675:                            chars, res, filename, 0);
                    676:                while ((res = fread(chars, 1, size, f)) > 0) {
                    677:                    htmlParseChunk(ctxt, chars, res, 0);
                    678:                }
                    679:                htmlParseChunk(ctxt, chars, 0, 1);
                    680:                doc = ctxt->myDoc;
                    681:                htmlFreeParserCtxt(ctxt);
                    682:            }
1.15      veillard  683:            fclose(f);
1.10      daniel    684:        }
                    685:     } else {   
                    686:        doc = htmlParseFile(filename, NULL);
                    687:     }
                    688:     if (doc == NULL) {
                    689:         fprintf(stderr, "Could not parse %s\n", filename);
                    690:     }
1.1       daniel    691: 
                    692:     /*
                    693:      * test intermediate copy if needed.
                    694:      */
                    695:     if (copy) {
                    696:         tmp = doc;
                    697:        doc = xmlCopyDoc(doc, 1);
                    698:        xmlFreeDoc(tmp);
                    699:     }
                    700: 
                    701:     /*
                    702:      * print it.
                    703:      */
1.7       daniel    704:     if (!noout) { 
1.12      daniel    705: #ifdef LIBXML_DEBUG_ENABLED
1.13      veillard  706:        if (!debug) {
                    707:            if (encoding)
                    708:                htmlSaveFileEnc("-", doc, encoding);
                    709:            else
                    710:                htmlDocDump(stdout, doc);
                    711:        } else
1.7       daniel    712:            xmlDebugDumpDocument(stdout, doc);
1.12      daniel    713: #else
1.13      veillard  714:        if (encoding)
                    715:            htmlSaveFileEnc("-", doc, encoding);
                    716:        else
                    717:            htmlDocDump(stdout, doc);
1.12      daniel    718: #endif
1.7       daniel    719:     }  
1.1       daniel    720: 
                    721:     /*
                    722:      * free it.
                    723:      */
                    724:     xmlFreeDoc(doc);
                    725: }
                    726: 
                    727: int main(int argc, char **argv) {
1.7       daniel    728:     int i, count;
1.1       daniel    729:     int files = 0;
                    730: 
                    731:     for (i = 1; i < argc ; i++) {
1.12      daniel    732: #ifdef LIBXML_DEBUG_ENABLED
1.1       daniel    733:        if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
                    734:            debug++;
1.12      daniel    735:        else
                    736: #endif
                    737:            if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1.1       daniel    738:            copy++;
1.10      daniel    739:        else if ((!strcmp(argv[i], "-push")) || (!strcmp(argv[i], "--push")))
                    740:            push++;
1.7       daniel    741:        else if ((!strcmp(argv[i], "-sax")) || (!strcmp(argv[i], "--sax")))
                    742:            sax++;
                    743:        else if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout")))
                    744:            noout++;
                    745:        else if ((!strcmp(argv[i], "-repeat")) ||
                    746:                 (!strcmp(argv[i], "--repeat")))
                    747:            repeat++;
1.13      veillard  748:        else if ((!strcmp(argv[i], "-encode")) ||
                    749:                 (!strcmp(argv[i], "--encode"))) {
                    750:            i++;
                    751:            encoding = argv[i];
                    752:         }
1.1       daniel    753:     }
                    754:     for (i = 1; i < argc ; i++) {
1.13      veillard  755:        if ((!strcmp(argv[i], "-encode")) ||
                    756:                 (!strcmp(argv[i], "--encode"))) {
                    757:            i++;
                    758:            continue;
                    759:         }
1.1       daniel    760:        if (argv[i][0] != '-') {
1.7       daniel    761:            if (repeat) {
                    762:                for (count = 0;count < 100 * repeat;count++) {
                    763:                    if (sax)
                    764:                        parseSAXFile(argv[i]);
                    765:                    else   
                    766:                        parseAndPrintFile(argv[i]);
                    767:                }    
                    768:            } else {
                    769:                if (sax)
                    770:                    parseSAXFile(argv[i]);
                    771:                else   
                    772:                    parseAndPrintFile(argv[i]);
                    773:            }
1.1       daniel    774:            files ++;
                    775:        }
                    776:     }
                    777:     if (files == 0) {
1.7       daniel    778:        printf("Usage : %s [--debug] [--copy] [--copy] HTMLfiles ...\n",
1.1       daniel    779:               argv[0]);
                    780:        printf("\tParse the HTML files and output the result of the parsing\n");
1.12      daniel    781: #ifdef LIBXML_DEBUG_ENABLED
1.1       daniel    782:        printf("\t--debug : dump a debug tree of the in-memory document\n");
1.12      daniel    783: #endif
1.1       daniel    784:        printf("\t--copy : used to test the internal copy implementation\n");
1.7       daniel    785:        printf("\t--sax : debug the sequence of SAX callbacks\n");
1.10      daniel    786:        printf("\t--repeat : parse the file 100 times, for timing\n");
1.7       daniel    787:        printf("\t--noout : do not print the result\n");
1.10      daniel    788:        printf("\t--push : use the push mode parser\n");
1.13      veillard  789:        printf("\t--encode encoding : output in the given encoding\n");
1.1       daniel    790:     }
1.8       daniel    791:     xmlCleanupParser();
1.6       daniel    792:     xmlMemoryDump();
1.1       daniel    793: 
                    794:     return(0);
                    795: }
1.12      daniel    796: #else /* !LIBXML_HTML_ENABLED */
                    797: #include <stdio.h>
                    798: int main(int argc, char **argv) {
                    799:     printf("%s : HTML support not compiled in\n", argv[0]);
                    800:     return(0);
                    801: }
                    802: #endif

Webmaster