Annotation of XML/testOasis.c, revision 1.2

1.1       daniel      1: #include <stdio.h>
                      2: #include <string.h>
                      3: #include <stdio.h>
                      4: #include <stdarg.h>
                      5: 
                      6: 
                      7: #include <libxml/xmlmemory.h>
                      8: #include <libxml/parser.h>
                      9: #include <libxml/parserInternals.h>
                     10: #include <libxml/tree.h>
                     11: 
                     12: 
                     13: #define OASIS_TEST "conf/xmlconf.xml"
                     14: 
                     15: 
                     16: /************************************************************************
                     17:  *                                                                     *
                     18:  *                     HTML ouput                                      *
                     19:  *                                                                     *
                     20:  ************************************************************************/
                     21: char buffer[50000];
                     22: 
                     23: void
                     24: xmlHTMLEncodeSend(void) {
                     25:     char *result;
                     26: 
                     27:     result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
                     28:     if (result) {
                     29:        fprintf(stderr, "%s", result);
                     30:        xmlFree(result);
                     31:     }
                     32:     buffer[0] = 0;
                     33: }
                     34: 
                     35: /**
                     36:  * xmlHTMLPrintFileInfo:
                     37:  * @input:  an xmlParserInputPtr input
                     38:  * 
                     39:  * Displays the associated file and line informations for the current input
                     40:  */
                     41: 
                     42: void
                     43: xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
                     44:     fprintf(stderr, "<p>");
                     45:     if (input != NULL) {
                     46:        if (input->filename) {
                     47:            sprintf(&buffer[strlen(buffer)], "%s:%d: ", input->filename,
                     48:                    input->line);
                     49:        } else {
                     50:            sprintf(&buffer[strlen(buffer)], "Entity: line %d: ", input->line);
                     51:        }
                     52:     }
                     53:     xmlHTMLEncodeSend();
                     54: }
                     55: 
                     56: /**
                     57:  * xmlHTMLPrintFileContext:
                     58:  * @input:  an xmlParserInputPtr input
                     59:  * 
                     60:  * Displays current context within the input content for error tracking
                     61:  */
                     62: 
                     63: void
                     64: xmlHTMLPrintFileContext(xmlParserInputPtr input) {
                     65:     const xmlChar *cur, *base;
                     66:     int n;
                     67: 
                     68:     if (input == NULL) return;
                     69:     fprintf(stderr, "<pre>\n");
                     70:     cur = input->cur;
                     71:     base = input->base;
                     72:     while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
                     73:        cur--;
                     74:     }
                     75:     n = 0;
                     76:     while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
                     77:         cur--;
                     78:     if ((*cur == '\n') || (*cur == '\r')) cur++;
                     79:     base = cur;
                     80:     n = 0;
                     81:     while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
                     82:         sprintf(&buffer[strlen(buffer)], "%c", (unsigned char) *cur++);
                     83:        n++;
                     84:     }
                     85:     sprintf(&buffer[strlen(buffer)], "\n");
                     86:     cur = input->cur;
                     87:     while ((*cur == '\n') || (*cur == '\r'))
                     88:        cur--;
                     89:     n = 0;
                     90:     while ((cur != base) && (n++ < 80)) {
                     91:         sprintf(&buffer[strlen(buffer)], " ");
                     92:         base++;
                     93:     }
                     94:     sprintf(&buffer[strlen(buffer)],"^\n");
                     95:     xmlHTMLEncodeSend();
                     96:     fprintf(stderr, "</pre>");
                     97: }
                     98: 
                     99: /**
                    100:  * xmlHTMLError:
                    101:  * @ctx:  an XML parser context
                    102:  * @msg:  the message to display/transmit
                    103:  * @...:  extra parameters for the message display
                    104:  * 
                    105:  * Display and format an error messages, gives file, line, position and
                    106:  * extra parameters.
                    107:  */
                    108: void
                    109: xmlHTMLError(void *ctx, const char *msg, ...)
                    110: {
                    111:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
                    112:     xmlParserInputPtr input;
                    113:     xmlParserInputPtr cur = NULL;
                    114:     va_list args;
                    115: 
                    116:     buffer[0] = 0;
                    117:     input = ctxt->input;
                    118:     if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
                    119:        cur = input;
                    120:         input = ctxt->inputTab[ctxt->inputNr - 2];
                    121:     }
                    122:         
                    123:     xmlHTMLPrintFileInfo(input);
                    124: 
                    125:     fprintf(stderr, "<b>error</b>: ");
                    126:     va_start(args, msg);
                    127:     vsprintf(&buffer[strlen(buffer)], msg, args);
                    128:     va_end(args);
                    129:     xmlHTMLEncodeSend();
                    130:     fprintf(stderr, "</p>\n");
                    131: 
                    132:     xmlHTMLPrintFileContext(input);
                    133:     xmlHTMLEncodeSend();
                    134: }
                    135: 
                    136: /**
                    137:  * xmlHTMLWarning:
                    138:  * @ctx:  an XML parser context
                    139:  * @msg:  the message to display/transmit
                    140:  * @...:  extra parameters for the message display
                    141:  * 
                    142:  * Display and format a warning messages, gives file, line, position and
                    143:  * extra parameters.
                    144:  */
                    145: void
                    146: xmlHTMLWarning(void *ctx, const char *msg, ...)
                    147: {
                    148:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
                    149:     xmlParserInputPtr input;
                    150:     xmlParserInputPtr cur = NULL;
                    151:     va_list args;
                    152: 
                    153:     buffer[0] = 0;
                    154:     input = ctxt->input;
                    155:     if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
                    156:        cur = input;
                    157:         input = ctxt->inputTab[ctxt->inputNr - 2];
                    158:     }
                    159:         
                    160: 
                    161:     xmlHTMLPrintFileInfo(input);
                    162:         
                    163:     fprintf(stderr, "<b>warning</b>: ");
                    164:     va_start(args, msg);
                    165:     vsprintf(&buffer[strlen(buffer)], msg, args);
                    166:     va_end(args);
                    167:     xmlHTMLEncodeSend();
                    168:     fprintf(stderr, "</p>\n");
                    169: 
                    170:     xmlHTMLPrintFileContext(input);
                    171:     xmlHTMLEncodeSend();
                    172: }
                    173: 
                    174: /**
                    175:  * xmlHTMLValidityError:
                    176:  * @ctx:  an XML parser context
                    177:  * @msg:  the message to display/transmit
                    178:  * @...:  extra parameters for the message display
                    179:  * 
                    180:  * Display and format an validity error messages, gives file,
                    181:  * line, position and extra parameters.
                    182:  */
                    183: void
                    184: xmlHTMLValidityError(void *ctx, const char *msg, ...)
                    185: {
                    186:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
                    187:     xmlParserInputPtr input;
                    188:     va_list args;
                    189: 
                    190:     buffer[0] = 0;
                    191:     input = ctxt->input;
                    192:     if ((input->filename == NULL) && (ctxt->inputNr > 1))
                    193:         input = ctxt->inputTab[ctxt->inputNr - 2];
                    194:         
                    195:     xmlHTMLPrintFileInfo(input);
                    196: 
                    197:     fprintf(stderr, "<b>validity error</b>: ");
                    198:     va_start(args, msg);
                    199:     vsprintf(&buffer[strlen(buffer)], msg, args);
                    200:     va_end(args);
                    201:     xmlHTMLEncodeSend();
                    202:     fprintf(stderr, "</p>\n");
                    203: 
                    204:     xmlHTMLPrintFileContext(input);
                    205:     xmlHTMLEncodeSend();
                    206: }
                    207: 
                    208: /**
                    209:  * xmlHTMLValidityWarning:
                    210:  * @ctx:  an XML parser context
                    211:  * @msg:  the message to display/transmit
                    212:  * @...:  extra parameters for the message display
                    213:  * 
                    214:  * Display and format a validity warning messages, gives file, line,
                    215:  * position and extra parameters.
                    216:  */
                    217: void
                    218: xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
                    219: {
                    220:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
                    221:     xmlParserInputPtr input;
                    222:     va_list args;
                    223: 
                    224:     buffer[0] = 0;
                    225:     input = ctxt->input;
                    226:     if ((input->filename == NULL) && (ctxt->inputNr > 1))
                    227:         input = ctxt->inputTab[ctxt->inputNr - 2];
                    228: 
                    229:     xmlHTMLPrintFileInfo(input);
                    230:         
                    231:     fprintf(stderr, "<b>validity warning</b>: ");
                    232:     va_start(args, msg);
                    233:     vsprintf(&buffer[strlen(buffer)], msg, args);
                    234:     va_end(args);
                    235:     xmlHTMLEncodeSend();
                    236:     fprintf(stderr, "</p>\n");
                    237: 
                    238:     xmlHTMLPrintFileContext(input);
                    239:     xmlHTMLEncodeSend();
                    240: }
                    241: 
1.2     ! daniel    242: /************************************************************************
        !           243:  *                                                                     *
        !           244:  *                     OASIS testsuite interface                       *
        !           245:  *                                                                     *
        !           246:  ************************************************************************/
1.1       daniel    247: xmlDocPtr readOasisSuite(const char *filename) {
                    248:     int ret;
                    249:     xmlParserCtxtPtr ctxt;
                    250:     xmlSAXHandler silent, *old;
                    251: 
                    252:     xmlDocPtr doc;
                    253: 
                    254:     /*
                    255:      * strip blanks from the test result tree and read in the external
                    256:      * entities.
                    257:      */
                    258:     xmlKeepBlanksDefault(0);
                    259:     xmlSubstituteEntitiesDefault(1);
                    260: 
                    261:     /*
                    262:      * Do a silent parsing
                    263:      */
                    264:     ctxt = xmlCreateFileParserCtxt(filename);
                    265:     memcpy(&silent, ctxt->sax, sizeof(silent));
                    266:     old = ctxt->sax;
                    267:     silent.error = NULL;
                    268:     silent.warning = NULL;
                    269:     silent.fatalError = NULL;
                    270: 
                    271:     xmlParseDocument(ctxt);
                    272: 
                    273:     ret = ctxt->wellFormed;
                    274:     doc = ctxt->myDoc;
                    275:     ctxt->sax = old;
                    276:     xmlFreeParserCtxt(ctxt);
                    277:     if (!ret) {
                    278:         xmlFreeDoc(doc);
                    279:        doc = NULL;
                    280:     }
                    281: 
                    282:     /*
                    283:      * switch back to decent defaults.
                    284:      */
                    285:     xmlKeepBlanksDefault(1);
                    286:     xmlSubstituteEntitiesDefault(0);
                    287:     return(doc);
                    288: }
                    289: 
                    290: int analyzeTest(xmlNodePtr test) {
                    291:     xmlChar *type;
                    292:     xmlChar *sections;
                    293:     xmlChar *id;
                    294:     xmlChar *URI;
                    295: 
                    296:     id = xmlGetProp(test, (xmlChar *) "ID");
                    297:     if (id == NULL) {
                    298:         fprintf(stderr, "Test without ID\n");
                    299:        return(0);
                    300:     }
                    301:     type = xmlGetProp(test, (xmlChar *) "TYPE");
                    302:     if (type == NULL) {
                    303:         fprintf(stderr, "Test %s without TYPE\n", id);
                    304:        xmlFree(id);
                    305:        return(0);
                    306:     }
                    307:     URI = xmlGetProp(test, (xmlChar *) "URI");
                    308:     if (URI == NULL) {
                    309:         fprintf(stderr, "Test %s without URI\n", id);
                    310:        xmlFree(id);
                    311:        xmlFree(type);
                    312:        return(0);
                    313:     }
                    314:     sections = xmlGetProp(test, (xmlChar *) "SECTIONS");
                    315:     printf("Test: %s, type %s, URI %s", id, type, URI);
                    316:     if (sections != NULL) {
                    317:         printf(", sections %s\n", sections);
                    318:        xmlFree(sections);
                    319:     } else 
                    320:         printf("\n");
                    321:     xmlFree(id);
                    322:     xmlFree(type);
                    323:     xmlFree(URI);
                    324:     return(1);
                    325: }
                    326: 
                    327: int analyzeCases(xmlNodePtr cases) {
                    328:     xmlChar *profile;
                    329:     int nbtests = 0;
                    330:     xmlNodePtr child;
                    331: 
                    332:     profile = xmlGetProp(cases, (xmlChar *) "PROFILE");
                    333: 
                    334:     if (profile) {
                    335:        printf("\n  Suite: %s\n\n", (char *) profile);
                    336:     } else {
                    337:        printf("\n  Suite: unknown tests\n\n");
                    338:     }
                    339: 
                    340:     child = cases->children;
                    341:     while (child != NULL) {
                    342:         if ((child->type == XML_ELEMENT_NODE) &&
                    343:             (!strcmp(child->name, "TEST"))) {
                    344:            nbtests += analyzeTest(child);
                    345:        }
                    346:        child = child->next;
                    347:     }
                    348: 
                    349:     if (profile) {
                    350:        printf("\n  Suite: %s : %d tests\n\n", (char *) profile, nbtests);
                    351:        xmlFree(profile);
                    352:     } else {
                    353:        printf("\n  Suite: %d tests\n\n", nbtests);
                    354:     }
                    355: 
                    356:     return(nbtests);
                    357: }
                    358: 
                    359: int analyzeSuite(xmlNodePtr suite) {
                    360:     xmlChar *profile;
                    361:     int nbtests = 0;
                    362:     xmlNodePtr child;
                    363: 
                    364:     if ((suite == NULL) || strcmp(suite->name, "TESTSUITE")) {
                    365:         fprintf(stderr, "No TESTSUITE root\n");
                    366:        return(-1);
                    367:     }
                    368:     profile = xmlGetProp(suite, (xmlChar *) "PROFILE");
                    369: 
                    370:     if (profile) {
                    371:        printf("\n     Testing: %s\n\n", (char *) profile);
                    372:     } else {
                    373:        printf("\n     Testing: unknown test suite\n\n");
                    374:     }
                    375: 
                    376:     child = suite->children;
                    377:     while (child != NULL) {
                    378:         if ((child->type == XML_ELEMENT_NODE) &&
                    379:             (!strcmp(child->name, "TESTCASES"))) {
                    380:            nbtests += analyzeCases(child);
                    381:        }
                    382:        child = child->next;
                    383:     }
                    384: 
                    385:     if (profile) {
                    386:        printf("\n     Testing: %s : %d tests\n\n", (char *) profile, nbtests);
                    387:        xmlFree(profile);
                    388:     } else {
                    389:        printf("\n     Testing: test suite : %d tests\n\n", nbtests);
                    390:     }
                    391: 
                    392:     return(nbtests);
                    393: }
                    394: 
                    395: int main(int argc, char ** argv) {
                    396:     xmlDocPtr tree;
                    397: 
                    398:     tree = readOasisSuite(OASIS_TEST);
                    399:     
                    400:     if (tree == NULL) {
                    401:         fprintf(stderr, "failed to load %s\n", OASIS_TEST);
                    402:         return(-1);
                    403:     }
                    404:     analyzeSuite(xmlDocGetRootElement(tree));
                    405:     return(0);
                    406: }

Webmaster