src/xml_validate.c
int
valMain(int argc, char **argv)
{
    int start;
    static valOptions ops;
    static ErrorInfo errorInfo;
    int invalidFound = 0;
    int options = XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR;

    if (argc <= 2) valUsage(argc, argv, EXIT_BAD_ARGS);
    valInitOptions(&ops);
    start = valParseOptions(&ops, argc, argv);
    if (ops.nonet) options |= XML_PARSE_NONET;

    errorInfo.verbose = ops.err;
    xmlSetStructuredErrorFunc(&errorInfo, reportError);
    xmlLineNumbersDefault(1);

    if (ops.dtd)
    {
        /* xmlReader doesn't work with external dtd, have to use SAX
         * interface */
        int i;

        /* we have to exit() from the error reporting function to implement
           --stop */
        errorInfo.stop = ops.stop;

        for (i=start; i<argc; i++)
        {
            xmlDocPtr doc;
            int failed;

            failed = 0;
            doc = NULL;

            errorInfo.filename = argv[i];
            doc = xmlReadFile(argv[i], NULL, options);
            if (doc)
            {
                /* TODO: precompile DTD once */                
                failed = valAgainstDtd(&ops, ops.dtd, doc, argv[i]);
                xmlFreeDoc(doc);
            }
            else
            {
                failed = 1; /* Malformed XML or could not open file */
                if ((ops.listGood < 0) && !ops.show_val_res)
                {
                    fprintf(stdout, "%s\n", argv[i]);
                }
            }
            if (failed) invalidFound = 1;

            if (ops.show_val_res)
            {
                if (!failed)
                    fprintf(stdout, "%s - valid\n", argv[i]);
                else
                    fprintf(stdout, "%s - invalid\n", argv[i]);
            }
        }
    }
    else if (ops.schema || ops.relaxng || ops.embed || ops.wellFormed)
    {
        int i;
        xmlTextReaderPtr reader = NULL;

#ifdef LIBXML_SCHEMAS_ENABLED
        xmlSchemaPtr schema = NULL;
        xmlSchemaParserCtxtPtr schemaParserCtxt = NULL;
        xmlSchemaValidCtxtPtr schemaCtxt = NULL;

        xmlRelaxNGPtr relaxng = NULL;
        xmlRelaxNGParserCtxtPtr relaxngParserCtxt = NULL;
        /* there is no xmlTextReaderRelaxNGValidateCtxt() !?  */

        /* TODO: Do not print debug stuff */
        if (ops.schema)
        {
            schemaParserCtxt = xmlSchemaNewParserCtxt(ops.schema);
            if (!schemaParserCtxt)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }
            errorInfo.filename = ops.schema;
            schema = xmlSchemaParse(schemaParserCtxt);
            if (!schema)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }

            xmlSchemaFreeParserCtxt(schemaParserCtxt);
            schemaCtxt = xmlSchemaNewValidCtxt(schema);
            if (!schemaCtxt)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }

        }
        else if (ops.relaxng)
        {
            relaxngParserCtxt = xmlRelaxNGNewParserCtxt(ops.relaxng);
            if (!relaxngParserCtxt)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }

            errorInfo.filename = ops.relaxng;
            relaxng = xmlRelaxNGParse(relaxngParserCtxt);
            if (!relaxng)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }

        }
#endif  /* LIBXML_SCHEMAS_ENABLED */

        for (i=start; i<argc; i++)
        {
            int failed = 0;
            if (ops.embed) options |= XML_PARSE_DTDVALID;

            if (!reader)
            {
                reader = xmlReaderForFile(argv[i], NULL, options);
            }
            else
            {
                failed = xmlReaderNewFile(reader, argv[i], NULL, options);
            }

            errorInfo.xmlReader = reader;
            errorInfo.filename = argv[i];

            /* It makes no sense to continue if we are not reporting errors
             * anyway. Note this doesn't apply to the --dtd case because the we
             * can't stop there without aborting the whole program (and
             * therefore we wouldn't be able to check multiple files).
             */
            if (!ops.err)
                ops.stop = STOP;

            if (reader && !failed)
            {
#ifdef LIBXML_SCHEMAS_ENABLED
                if (schemaCtxt)
                {
                    failed = xmlTextReaderSchemaValidateCtxt(reader,
                        schemaCtxt, 0);
                }
                else if (relaxng)
                {
                    failed = xmlTextReaderRelaxNGSetSchema(reader,
                        relaxng);
                }
#endif  /* LIBXML_SCHEMAS_ENABLED */

                if (failed == 0)
                {
                    int more_nodes;
                    int validating = (schema || relaxng || ops.embed);
                    do
                    {
                        more_nodes = xmlTextReaderRead(reader);
                        failed =
                            (more_nodes == -1)? 1 :
                            (!validating)? 0 :
                            xmlTextReaderIsValid(reader) != 1;
                    } while (more_nodes == 1 && (!failed || !ops.stop));
                }
            }
            else
            {
                if (ops.err)
                    fprintf(stderr, "couldn't read file '%s'\n", errorInfo.filename);
                failed = 1; /* could not open file */
            }
            if (failed) invalidFound = 1;

            if (!ops.show_val_res)
            {
                if ((ops.listGood > 0) && !failed)
                    fprintf(stdout, "%s\n", argv[i]);
                if ((ops.listGood < 0) && failed)
                    fprintf(stdout, "%s\n", argv[i]);
            }
            else
            {
                if (!failed)
                    fprintf(stdout, "%s - valid\n", argv[i]);
                else
                    fprintf(stdout, "%s - invalid\n", argv[i]);
            }
        }
        errorInfo.xmlReader = NULL;
        xmlFreeTextReader(reader);

#ifdef LIBXML_SCHEMAS_ENABLED
    schemaCleanup:
        xmlSchemaFreeValidCtxt(schemaCtxt);
        xmlRelaxNGFree(relaxng);
        xmlSchemaFree(schema);
        xmlRelaxNGCleanupTypes();
        xmlSchemaCleanupTypes();
#endif  /* LIBXML_SCHEMAS_ENABLED */
    }

    xmlCleanupParser();
    return invalidFound;
}
/**
 *  This is the main function for 'validate' option
 */
This is the main function for 'validate' option
This is the main function for ' validate ' option
int
valMain(int argc, char **argv)
{
    int start;
    static valOptions ops;
    static ErrorInfo errorInfo;
    int invalidFound = 0;
    int options = XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR;
    if (argc <= 2) valUsage(argc, argv, EXIT_BAD_ARGS);
    valInitOptions(&ops);
    start = valParseOptions(&ops, argc, argv);
    if (ops.nonet) options |= XML_PARSE_NONET;
    errorInfo.verbose = ops.err;
    xmlSetStructuredErrorFunc(&errorInfo, reportError);
    xmlLineNumbersDefault(1);
    if (ops.dtd)
    {
        int i;
        errorInfo.stop = ops.stop;
        for (i=start; i<argc; i++)
        {
            xmlDocPtr doc;
            int failed;
            failed = 0;
            doc = NULL;
            errorInfo.filename = argv[i];
            doc = xmlReadFile(argv[i], NULL, options);
            if (doc)
            {
                failed = valAgainstDtd(&ops, ops.dtd, doc, argv[i]);
                xmlFreeDoc(doc);
            }
            else
            {
                failed = 1; 
                if ((ops.listGood < 0) && !ops.show_val_res)
                {
                    fprintf(stdout, "%s\n", argv[i]);
                }
            }
            if (failed) invalidFound = 1;
            if (ops.show_val_res)
            {
                if (!failed)
                    fprintf(stdout, "%s - valid\n", argv[i]);
                else
                    fprintf(stdout, "%s - invalid\n", argv[i]);
            }
        }
    }
    else if (ops.schema || ops.relaxng || ops.embed || ops.wellFormed)
    {
        int i;
        xmlTextReaderPtr reader = NULL;
#ifdef LIBXML_SCHEMAS_ENABLED
        xmlSchemaPtr schema = NULL;
        xmlSchemaParserCtxtPtr schemaParserCtxt = NULL;
        xmlSchemaValidCtxtPtr schemaCtxt = NULL;
        xmlRelaxNGPtr relaxng = NULL;
        xmlRelaxNGParserCtxtPtr relaxngParserCtxt = NULL;
        if (ops.schema)
        {
            schemaParserCtxt = xmlSchemaNewParserCtxt(ops.schema);
            if (!schemaParserCtxt)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }
            errorInfo.filename = ops.schema;
            schema = xmlSchemaParse(schemaParserCtxt);
            if (!schema)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }
            xmlSchemaFreeParserCtxt(schemaParserCtxt);
            schemaCtxt = xmlSchemaNewValidCtxt(schema);
            if (!schemaCtxt)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }
        }
        else if (ops.relaxng)
        {
            relaxngParserCtxt = xmlRelaxNGNewParserCtxt(ops.relaxng);
            if (!relaxngParserCtxt)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }
            errorInfo.filename = ops.relaxng;
            relaxng = xmlRelaxNGParse(relaxngParserCtxt);
            if (!relaxng)
            {
                invalidFound = 2;
                goto schemaCleanup;
            }
        }
#endif  
        for (i=start; i<argc; i++)
        {
            int failed = 0;
            if (ops.embed) options |= XML_PARSE_DTDVALID;
            if (!reader)
            {
                reader = xmlReaderForFile(argv[i], NULL, options);
            }
            else
            {
                failed = xmlReaderNewFile(reader, argv[i], NULL, options);
            }
            errorInfo.xmlReader = reader;
            errorInfo.filename = argv[i];
            if (!ops.err)
                ops.stop = STOP;
            if (reader && !failed)
            {
#ifdef LIBXML_SCHEMAS_ENABLED
                if (schemaCtxt)
                {
                    failed = xmlTextReaderSchemaValidateCtxt(reader,
                        schemaCtxt, 0);
                }
                else if (relaxng)
                {
                    failed = xmlTextReaderRelaxNGSetSchema(reader,
                        relaxng);
                }
#endif  
                if (failed == 0)
                {
                    int more_nodes;
                    int validating = (schema || relaxng || ops.embed);
                    do
                    {
                        more_nodes = xmlTextReaderRead(reader);
                        failed =
                            (more_nodes == -1)? 1 :
                            (!validating)? 0 :
                            xmlTextReaderIsValid(reader) != 1;
                    } while (more_nodes == 1 && (!failed || !ops.stop));
                }
            }
            else
            {
                if (ops.err)
                    fprintf(stderr, "couldn't read file '%s'\n", errorInfo.filename);
                failed = 1; 
            }
            if (failed) invalidFound = 1;
            if (!ops.show_val_res)
            {
                if ((ops.listGood > 0) && !failed)
                    fprintf(stdout, "%s\n", argv[i]);
                if ((ops.listGood < 0) && failed)
                    fprintf(stdout, "%s\n", argv[i]);
            }
            else
            {
                if (!failed)
                    fprintf(stdout, "%s - valid\n", argv[i]);
                else
                    fprintf(stdout, "%s - invalid\n", argv[i]);
            }
        }
        errorInfo.xmlReader = NULL;
        xmlFreeTextReader(reader);
#ifdef LIBXML_SCHEMAS_ENABLED
    schemaCleanup:
        xmlSchemaFreeValidCtxt(schemaCtxt);
        xmlRelaxNGFree(relaxng);
        xmlSchemaFree(schema);
        xmlRelaxNGCleanupTypes();
        xmlSchemaCleanupTypes();
#endif  
    }
    xmlCleanupParser();
    return invalidFound;
}
int valMain ( int argc , char * * argv ) { int start ; static valOptions ops ; static ErrorInfo errorInfo ; int invalidFound = 0 ; int options = XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR ; if ( argc <= 2 ) valUsage ( argc , argv , EXIT_BAD_ARGS ) ; valInitOptions ( & ops ) ; start = valParseOptions ( & ops , argc , argv ) ; if ( ops . nonet ) options |= XML_PARSE_NONET ; errorInfo . verbose = ops . err ; xmlSetStructuredErrorFunc ( & errorInfo , reportError ) ; xmlLineNumbersDefault ( 1 ) ; if ( ops . dtd ) { int i ; errorInfo . stop = ops . stop ; for ( i = start ; i < argc ; i ++ ) { xmlDocPtr doc ; int failed ; failed = 0 ; doc = NULL ; errorInfo . filename = argv [ i ] ; doc = xmlReadFile ( argv [ i ] , NULL , options ) ; if ( doc ) { failed = valAgainstDtd ( & ops , ops . dtd , doc , argv [ i ] ) ; xmlFreeDoc ( doc ) ; } else { failed = 1 ; if ( ( ops . listGood < 0 ) && ! ops . show_val_res ) { fprintf ( stdout , " \n " , argv [ i ] ) ; } } if ( failed ) invalidFound = 1 ; if ( ops . show_val_res ) { if ( ! failed ) fprintf ( stdout , " \n " , argv [ i ] ) ; else fprintf ( stdout , " \n " , argv [ i ] ) ; } } } else if ( ops . schema || ops . relaxng || ops . embed || ops . wellFormed ) { int i ; xmlTextReaderPtr reader = NULL ; #ifdef LIBXML_SCHEMAS_ENABLED xmlSchemaPtr schema = NULL ; xmlSchemaParserCtxtPtr schemaParserCtxt = NULL ; xmlSchemaValidCtxtPtr schemaCtxt = NULL ; xmlRelaxNGPtr relaxng = NULL ; xmlRelaxNGParserCtxtPtr relaxngParserCtxt = NULL ; if ( ops . schema ) { schemaParserCtxt = xmlSchemaNewParserCtxt ( ops . schema ) ; if ( ! schemaParserCtxt ) { invalidFound = 2 ; goto schemaCleanup ; } errorInfo . filename = ops . schema ; schema = xmlSchemaParse ( schemaParserCtxt ) ; if ( ! schema ) { invalidFound = 2 ; goto schemaCleanup ; } xmlSchemaFreeParserCtxt ( schemaParserCtxt ) ; schemaCtxt = xmlSchemaNewValidCtxt ( schema ) ; if ( ! schemaCtxt ) { invalidFound = 2 ; goto schemaCleanup ; } } else if ( ops . relaxng ) { relaxngParserCtxt = xmlRelaxNGNewParserCtxt ( ops . relaxng ) ; if ( ! relaxngParserCtxt ) { invalidFound = 2 ; goto schemaCleanup ; } errorInfo . filename = ops . relaxng ; relaxng = xmlRelaxNGParse ( relaxngParserCtxt ) ; if ( ! relaxng ) { invalidFound = 2 ; goto schemaCleanup ; } } #endif for ( i = start ; i < argc ; i ++ ) { int failed = 0 ; if ( ops . embed ) options |= XML_PARSE_DTDVALID ; if ( ! reader ) { reader = xmlReaderForFile ( argv [ i ] , NULL , options ) ; } else { failed = xmlReaderNewFile ( reader , argv [ i ] , NULL , options ) ; } errorInfo . xmlReader = reader ; errorInfo . filename = argv [ i ] ; if ( ! ops . err ) ops . stop = STOP ; if ( reader && ! failed ) { #ifdef LIBXML_SCHEMAS_ENABLED if ( schemaCtxt ) { failed = xmlTextReaderSchemaValidateCtxt ( reader , schemaCtxt , 0 ) ; } else if ( relaxng ) { failed = xmlTextReaderRelaxNGSetSchema ( reader , relaxng ) ; } #endif if ( failed == 0 ) { int more_nodes ; int validating = ( schema || relaxng || ops . embed ) ; do { more_nodes = xmlTextReaderRead ( reader ) ; failed = ( more_nodes == -1 ) ? 1 : ( ! validating ) ? 0 : xmlTextReaderIsValid ( reader ) != 1 ; } while ( more_nodes == 1 && ( ! failed || ! ops . stop ) ) ; } } else { if ( ops . err ) fprintf ( stderr , " \n " , errorInfo . filename ) ; failed = 1 ; } if ( failed ) invalidFound = 1 ; if ( ! ops . show_val_res ) { if ( ( ops . listGood > 0 ) && ! failed ) fprintf ( stdout , " \n " , argv [ i ] ) ; if ( ( ops . listGood < 0 ) && failed ) fprintf ( stdout , " \n " , argv [ i ] ) ; } else { if ( ! failed ) fprintf ( stdout , " \n " , argv [ i ] ) ; else fprintf ( stdout , " \n " , argv [ i ] ) ; } } errorInfo . xmlReader = NULL ; xmlFreeTextReader ( reader ) ; #ifdef LIBXML_SCHEMAS_ENABLED schemaCleanup : xmlSchemaFreeValidCtxt ( schemaCtxt ) ; xmlRelaxNGFree ( relaxng ) ; xmlSchemaFree ( schema ) ; xmlRelaxNGCleanupTypes ( ) ; xmlSchemaCleanupTypes ( ) ; #endif } xmlCleanupParser ( ) ; return invalidFound ; }
This is the main function for 'validate' option
This is the main function for ' validate ' option
/* xmlReader doesn't work with external dtd, have to use SAX
         * interface */ /* we have to exit() from the error reporting function to implement
           --stop */ /* TODO: precompile DTD once */ /* Malformed XML or could not open file */ /* there is no xmlTextReaderRelaxNGValidateCtxt() !?  */ /* TODO: Do not print debug stuff */ /* LIBXML_SCHEMAS_ENABLED */ /* It makes no sense to continue if we are not reporting errors
             * anyway. Note this doesn't apply to the --dtd case because the we
             * can't stop there without aborting the whole program (and
             * therefore we wouldn't be able to check multiple files).
             */ /* LIBXML_SCHEMAS_ENABLED */ /* could not open file */ /* LIBXML_SCHEMAS_ENABLED */