import java.net.URL;
import javax.xml.validation.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import java.io.*;
import javax.xml.transform.stream.StreamSource;

public class ValidateCellML
{
  public static void
  main (String args[]) 
  {
    if (args.length < 1)
    {
      System.err.println("Usage: ValidateCellML URLofCellMLDocument*");
      System.exit(-1);
    }

    SchemaFactory xsdSchemaFactory =
        SchemaFactory.newInstance
        (
         "http://www.w3.org/2001/XMLSchema"
        );
    Schema xsdSchema = null;
    try
    {
      xsdSchema = xsdSchemaFactory.newSchema
          (new StreamSource("http://www.cellml.org/tools/cellml_1_1_schema/" +
                            "cellml_1_1.xsd"));
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.err.println("Cannot load the CellML XSD schema.");
      System.exit(-1);
    }
    
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setSchema(xsdSchema);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = null;
    try
    {
      db = dbf.newDocumentBuilder();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.exit(-1);
    }

    int i;
    for (i = 0; i < args.length; i++)
    {
      try
      {
        System.out.println("Validating " + args[i]);
        db.parse(args[i]);
      }
      catch (IOException ioe)
      {
        System.out.println("I/O error reading " + args[i]);
      }
      catch (SAXException se)
      {
        System.out.println("Error validating " + args[i] + ": " +
                           se.getMessage());
      }
    }
  }
}
