Beispiel #1
0
int
test (const char* base) {
    xmlDocPtr docPtr = NULL;
    char filename[100];
    xmlSchemaPtr wxschemas = NULL;

    Handle2Path = xmlHashCreate(0);

    /* Read the schema. */
    {
        /* There is no visitibility into parserCtxt. */
	xmlSchemaParserCtxtPtr parserCtxt;

        /* parserCtxt->ctxtType is xmlSchemaTypePtr */

        snprintf(filename, 100, "%s.xsd", base);
        printf("\n\n\n----------------------------------------------------------------\n\n\n");
        printf("\n----> Reading schema %s...\n", filename);

	parserCtxt = xmlSchemaNewParserCtxt(filename);
	xmlSchemaSetParserErrors(parserCtxt,
		(xmlSchemaValidityErrorFunc) fprintf,
		(xmlSchemaValidityWarningFunc) fprintf,
		stdout);
	xmlSchemaSetParserAnnotation(parserCtxt, schema_annotation_callback, NULL);
	wxschemas = xmlSchemaParse(parserCtxt);
	if (wxschemas == NULL)
        {
            printf("***** schema parsing failed!\n");
	}
	xmlSchemaFreeParserCtxt(parserCtxt);
        printf("\n<---- Schema read!\n\n");
    }

    /* Read the XML. */
    {
        snprintf(filename, 100, "%s.xml", base);
        if ((docPtr = xmlReadFile(filename, NULL, 0)) == NULL)
        {
            printf("failed to parse \"%s\".\n", filename);
            return -1;
        }
    }

    if (!SKIP) {
        /* There is no visibility into schemaCtxt. */
	xmlSchemaValidCtxtPtr schemaCtxt;
	int ret;

        printf("\n----------------------------------------------------------------\n");
        printf("\n----> Validating document %s...\n", filename);

        /* This sets up the schemaCtxt, including a pointer to wxschemas. */
	schemaCtxt = xmlSchemaNewValidCtxt(wxschemas);
	xmlSchemaSetValidErrors(schemaCtxt,
		(xmlSchemaValidityErrorFunc) fprintf,
		(xmlSchemaValidityWarningFunc) fprintf,
		stdout);
	ret = xmlSchemaValidateDoc(schemaCtxt, docPtr);	/* read me! */
	if (ret == 0)
        {
	    /* printf("%s validates\n", filename); */
	}
        else if (ret > 0)
        {
	    printf("%s fails to validate\n", filename);
	}
        else
        {
	    printf("%s validation generated an internal error\n",
		   filename);
	}
	xmlSchemaFreeValidCtxt(schemaCtxt);
        printf("\n<---- Document validated!\n");

    }

    /* Generate a doc and validate it. */
    {
	xmlDocPtr newDoc = xmlNewDoc(BAD_CAST "1.0");
	{
	    xmlSchemaValidCtxtPtr schemaCtxt;
	    int ret;

	    schemaCtxt = xmlSchemaNewValidCtxt(wxschemas);
	    xmlSchemaSetValidErrors(schemaCtxt,
				    (xmlSchemaValidityErrorFunc) fprintf,
				    (xmlSchemaValidityWarningFunc) fprintf,
				    stdout);
	    xmlSchemaSetGeneratorCallback(schemaCtxt, BAD_CAST "people", NULL, &generation_callback, newDoc);
	    ret = xmlSchemaValidateDoc(schemaCtxt, newDoc);
	    if (ret == 0) {
/* 		xmlDocSetRootElement(newDoc, vctxt->node); */
		dump_doc(newDoc, NULL);
	    } else if (ret > 0)
		printf("%s fails to validate\n", filename);
	    else
		printf("%s validation generated an internal error\n",
		       filename);
	    xmlSchemaFreeValidCtxt(schemaCtxt);
	    printf("\n<---- Schema read!\n\n");
	}

	{
	    xmlSchemaValidCtxtPtr schemaCtxt;
	    int ret;

	    schemaCtxt = xmlSchemaNewValidCtxt(wxschemas);
	    xmlSchemaSetValidErrors(schemaCtxt,
				    (xmlSchemaValidityErrorFunc) fprintf,
				    (xmlSchemaValidityWarningFunc) fprintf,
				    stdout);
	    ret = xmlSchemaValidateDoc(schemaCtxt, newDoc);
	    if (ret == 0)
		;
	    else if (ret > 0)
		printf("%s fails to validate\n", filename);
	    else
		printf("%s validation generated an internal error\n",
		       filename);
	    xmlSchemaFreeValidCtxt(schemaCtxt);
	    printf("\n<---- Schema read!\n\n");
	}

	xmlFreeDoc(newDoc);
    }

#if 0
    /* why can't I just start with doc->children? */
    tree_trunk = xmlDocGetRootElement(docPtr);
#endif

#if 0
    tree_trunk = docPtr->children;

    printf("\n\n\n----------------------------------------------------------------\n\n\n");
    printf("\nWalking doc tree...\n");
    walk_doc_tree(tree_trunk, 0);
    printf("\n");
#endif
    printf("\n\n\n----------------------------------------------------------------\n\n\n");
    printf("\nWalking schema tree...\n");
    walk_schema_tree(wxschemas);
    printf("\n");

    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/
    /* This will tell me, for example, how to decode sequences. */
    printf("\n\n\n----------------------------------------------------------------\n\n\n");
    xmlSchemaDump(stdout, wxschemas);
    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/
    /*****************************************************************/

    xmlFreeDoc(docPtr);

    xmlCleanupParser();

    xmlHashFree(Handle2Path, NULL);

    return 0;
}
Beispiel #2
0
int main(int argc, char **argv) {

    int i;

    int files = 0;

    xmlSchemaPtr schema = NULL;

    for (i = 1; i < argc ; i++) {

#ifdef LIBXML_DEBUG_ENABLED

	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))

	    debug++;

	else

#endif

#ifdef HAVE_SYS_MMAN_H

	if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) {

	    memory++;

        } else

#endif

	if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {

	    noout++;

        }

    }

    /*xmlLineNumbersDefault(1);*/

    for (i = 1; i < argc ; i++) {

	if (argv[i][0] != '-') {

	    if (schema == NULL) {

		xmlSchemaParserCtxtPtr ctxt;



#ifdef HAVE_SYS_MMAN_H

		if (memory) {

		    int fd;

		    struct stat info;

		    const char *base;

		    if (stat(argv[i], &info) < 0) 

			break;

		    if ((fd = open(argv[i], O_RDONLY)) < 0)

			break;

		    base = mmap(NULL, info.st_size, PROT_READ,

			        MAP_SHARED, fd, 0) ;

		    if (base == (void *) MAP_FAILED)

			break;



		    ctxt = xmlSchemaNewMemParserCtxt((char *)base,info.st_size);



		    xmlSchemaSetParserErrors(ctxt,

			    (xmlSchemaValidityErrorFunc) fprintf,

			    (xmlSchemaValidityWarningFunc) fprintf,

			    stderr);

		    schema = xmlSchemaParse(ctxt);

		    xmlSchemaFreeParserCtxt(ctxt);

		    munmap((char *) base, info.st_size);

		} else

#endif

		{	

		    /*printf("\n**** CALLING :: xmlSchemaNewParserCtxt argv[i] = %s \n ", argv[i]);*/



		    printf("\n**** CALLING :: xmlSchemaNewParserCtxt \n ");



		    ctxt = xmlSchemaNewParserCtxt(argv[i]);

		    //ctxt = xmlSchemaNewParserCtxt("/root/Code/libxml2-2.6.15/ixml/basic/memory_test.xsd");



		    printf("\n**** CALLING :: xmlSchemaParse \n ");



		    xmlSchemaSetParserErrors(ctxt,

			    (xmlSchemaValidityErrorFunc) fprintf,

			    (xmlSchemaValidityWarningFunc) fprintf,

			    stderr);

		    printf("\n**** CALLING :: xmlSchemaParse \n ");

		    schema = xmlSchemaParse(ctxt);

		    xmlSchemaFreeParserCtxt(ctxt);

		}

#if 0		

#ifdef LIBXML_OUTPUT_ENABLED

#ifdef LIBXML_DEBUG_ENABLED

		if (debug)

		    xmlSchemaDump(stdout, schema);

#endif

#endif /* LIBXML_OUTPUT_ENABLED */

#endif /*#if 0*/		

		if (schema == NULL) 

		    goto failed_schemas;

	    } else {



		IXML_Document *doc;



       	printf("\n ***** CALLING :: xmlReadFile:> %s ", argv[i]);



		//doc = xmlReadFile(argv[i],NULL,0);

		//doc = ixmlLoadDocument("/root/Code/libxml2-2.6.15/ixml/basic/memory_test.xml");

		doc = ixmlLoadDocument(argv[i]);

#if 0		

	printf("nodeName = %s\n",doc->n.firstChild->nodeName);

	printf("firstAttr->nodeName = %s\n",doc->n.firstChild->firstChild->firstAttr->nodeName);

	printf("firstAttr->nodeValue = %s\n",doc->n.firstChild->firstChild->firstAttr->nodeValue);

	printf("firstAttr->nextSibling->nodeName = %s\n",doc->n.firstChild->firstChild->firstAttr->nextSibling->nodeName);

	printf("firstAttr->nextSibling->nodeValue = %s\n",doc->n.firstChild->firstChild->firstAttr->nextSibling->nodeValue);

#endif

		if (doc == NULL) {

		    fprintf(stderr, "Could not parse %s\n", argv[i]);

		} else {

		    xmlSchemaValidCtxtPtr ctxt;

		    int ret;

	        	printf("\n ***** CALLING :: xmlSchemaNewValidCtxt *****\n ");



		    ctxt = xmlSchemaNewValidCtxt(schema);



	        	printf("\n ***** CALLING :: xmlSchemaSetValidErrors *****\n ");



		    xmlSchemaSetValidErrors(ctxt,

			    (xmlSchemaValidityErrorFunc) fprintf,

			    (xmlSchemaValidityWarningFunc) fprintf,

			    stderr);

		    ret = xmlSchemaValidateDoc(ctxt, doc);

			

#if 0			

			printf("nodeName = %s\n",doc->n.firstChild->nodeName);

			printf("firstAttr->nodeName = %s\n",doc->n.firstChild->firstChild->firstAttr->nodeName);

			printf("firstAttr->nodeValue = %s\n",doc->n.firstChild->firstChild->firstAttr->nodeValue);

			printf("firstAttr->nextSibling->nodeName = %s\n",doc->n.firstChild->firstChild->firstAttr->nextSibling->nodeName);

			printf("firstAttr->nextSibling->nodeValue = %s\n",doc->n.firstChild->firstChild->firstAttr->nextSibling->nodeValue);

#endif			

		    if (ret == 0) {

			printf("%s validates\n", argv[i]);

		    } else if (ret > 0) {

			printf("%s fails to validate\n", argv[i]);

		    } else {

			printf("%s validation generated an internal error\n",

			       argv[i]);

		    }

		    xmlSchemaFreeValidCtxt(ctxt);



		    ixmlDocument_free(doc);

		}

	    }

	    files ++;

	}

    }

    if (schema != NULL)

	xmlSchemaFree(schema);

    if (files == 0) {

	printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",

	       argv[0]);

	printf("\tParse the HTML files and output the result of the parsing\n");

#ifdef LIBXML_DEBUG_ENABLED

	printf("\t--debug : dump a debug tree of the in-memory document\n");

#endif

	printf("\t--noout : do not print the result\n");

#ifdef HAVE_SYS_MMAN_H

	printf("\t--memory : test the schemas in memory parsing\n");

#endif

    }

failed_schemas:

	/*printf("\n**** failed_schemas\n");*/

    xmlSchemaCleanupTypes();

    /*xmlCleanupParser();

	xmlResetLastError();

    xmlMemoryDump();*/



    return(0);

}