package gov.nih.nci.cbiit.cmts.transform.validation;

/**
 * This class validate an XML against using XQuery
 * 
 * @author Eugene Wang
 * @author LAST UPDATE $Author: wangeug $
 * @since CMTS v1.0
 * @version $Revision: 1.0 $
 * @date $Date: 2010-10-14 19:01:17 $
 * 
 */
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;

import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.validation.Validator;
import java.io.*;
public class XsdSchemaSaxValidator {
	
  public static void main(String[] a) {
    if (a.length<2) {
      System.out.println("Usage:");
      System.out.println("java XsdSchemaValidator schema_file_name "
        + "xml_file_name");
    } else {
      String schemaName = a[0];
      String xmlName = a[1];
      Schema schema = loadSchema(schemaName, null);
      validateXmlFile(schema, xmlName, null);
    }
  }
  
  public static void validateXmlSource(Schema schema,SAXSource saxSource, ErrorHandler errorHandler)
  {
	    try {
	        // creating a Validator instance
	        Validator validator = schema.newValidator();
	        System.out.println();
	        System.out.println("Validator Class: "
	          + validator.getClass().getName());
	        validator.setErrorHandler(errorHandler);     

	        // validating the SAX source against the schema
	        validator.validate(saxSource);
	        System.out.println();
	        System.out.println("Validation passed.");

	      } catch (Exception e) {
	        // catching all validation exceptions
	        System.out.println();
	        System.out.println(e.toString());
	      }  
  }
  
  public static void validateXmlFile(Schema schema, String xmlName, ErrorHandler errorHandler) {
	  System.out.println("XsdSchemaSaxValidator.validateXml()..xml:"+xmlName);
      // preparing the XML file as a SAX source
      try {
		SAXSource source = new SAXSource(
		    new InputSource(new java.io.FileInputStream(xmlName)));
		validateXmlSource(schema, source, errorHandler);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  
  public static void validateXmlData(Schema schema, String xmlData, ErrorHandler errorHandler) {
	  System.out.println("XsdSchemaSaxValidator.validateXml()..xml:"+xmlData);
      // preparing the XML file as a SAX source
   	  StringReader stReader=new StringReader(xmlData);
   	  InputSource inSrc =new InputSource(stReader);
   	  SAXSource source = new SAXSource(inSrc);
   	  validateXmlSource(schema, source, errorHandler);
 
   }
 
  public static Schema loadSchema(String name, ErrorHandler errorHandler) {
	  Schema schema = null;
      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      SchemaFactory factory = SchemaFactory.newInstance(language);
      factory.setErrorHandler(errorHandler);
      try {
		schema = factory.newSchema(new File(name));
	} catch (SAXException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    return schema;
  }
}


@@ -278,16 +278,109 @@
  %0D%0A */%0D%0A
+import java.io.File;%0D%0Aimport java.io.FileNotFoundException;%0D%0Aimport java.io.StringReader;%0D%0A%0D%0A
 import j
@@ -415,16 +415,16 @@
 ctory;%0D%0A
-
 import j
@@ -676,27 +676,8 @@
 r;%0D%0A
-import java.io.*;%0D%0A
 publ
@@ -714,404 +714,11 @@
  %7B%0D%0A
+
 %09%0D%0A
-  public static void main(String%5B%5D a) %7B%0D%0A    if (a.length%3C2) %7B%0D%0A      System.out.println(%22Usage:%22);%0D%0A      System.out.println(%22java XsdSchemaValidator schema_file_name %22%0D%0A        + %22xml_file_name%22);%0D%0A    %7D else %7B%0D%0A      String schemaName = a%5B0%5D;%0D%0A      String xmlName = a%5B1%5D;%0D%0A      Schema schema = loadSchema(schemaName, null);%0D%0A      validateXmlFile(schema, xmlName, null);%0D%0A    %7D%0D%0A  %7D%0D%0A  %0D%0A
   pu

remove "main" method