package com.progress.stylus;

import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.SAXException;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.LinkedList;

public class XercesValidator
{
	static final String JAXP_SCHEMA_SOURCE =
        "http://java.sun.com/xml/jaxp/properties/schemaSource";

	public static void main(String[] args)
	{
		System.out.println("Xerces Java 2 Validation Starting.");
			//System.out.println("args.length=" + args.length);
			//for(int i=0; i<args.length; i++) 
			//	System.out.println(args[i]);

		String fileNameToValidate = null;
        String[] schemaNames = null;
        
        if (args.length==1) {
               // no external schema list
            fileNameToValidate = args[0];
        }
        else
        if (args.length == 3 && args[0].equalsIgnoreCase("-schemaURLFile")) {
            // external schema list
            fileNameToValidate = args[2];
            schemaNames = getSchemaNames(args[1]);
        }
        else 
        {
            System.out.println("Usage: com.progress.stylus.XercesValidator [-schemaURLFile <file>] <xmlfile>");
            return;
        }

		try
		{
			XMLReader p = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

			// Set the error handler since we only care about validation errors.
            SAXErrorHandler errHandler=new SAXErrorHandler();
			p.setErrorHandler(errHandler);

			// Set the validation features
			p.setFeature("http://xml.org/sax/features/validation", true);
			p.setFeature("http://apache.org/xml/features/validation/schema", true );
			p.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true );
			p.setFeature("http://apache.org/xml/features/xinclude", true);

			// add the grammar pool
			if (schemaNames != null)
				p.setProperty(JAXP_SCHEMA_SOURCE, schemaNames);

			// Call parse, errors are reported through SAXErrorHandler
			p.parse(fileNameToValidate);

            if(errHandler.nErrors>0)
    	        System.out.println("Xerces Java 2 Validation completed with errors.");
            else
		        System.out.println("Xerces Java 2 Validation Complete.");
		}
		catch(SAXException se)
		{
			System.out.println("There was an error during the parsing of " + fileNameToValidate);
			if (se.getMessage() != null)
				System.out.println("Reason = " + se.getMessage());
    		System.out.println("Xerces Java 2 Validation completed with errors.");
		}
		catch(Exception e)
		{
			// All errors (including fatal) should already be reported by now.
			System.out.println("There was an error during validation of " + fileNameToValidate);
			if (e.getMessage() != null)
				System.out.println("Reason = " + e.getMessage());
    		System.out.println("Xerces Java 2 Validation completed with errors.");
		}
	}
    
    private static String[] getSchemaNames(String listFileName) {
        BufferedReader in = null;
        String[] schemaNames = null;

        try {
            in = new BufferedReader(new FileReader(listFileName));
            String inbuf;
            LinkedList schemaList = new LinkedList();
            while((inbuf=in.readLine()) != null) {
                System.out.println("Loading schema " + inbuf);
                schemaList.addLast(inbuf);
            }
               
            if (schemaList.size() > 0)
                schemaNames = (String[])schemaList.toArray(new String[0]);
        }
        catch(IOException e) {
            System.out.println("Unable to validate.");
            System.out.println(e.toString());
            System.exit(2);
        }
        return schemaNames;
    }
}
