/*
 * Problem:
 *
 * I'm wondering if schemaLocation in an XML document is configurable 
 * i.e. not hardcoded in the XML instance. For example, I want to be able 
 * to do xml validation using a schema file located on our intranet, then 
 * send the xml document to another organization via HTTP. The other 
 * organization's system won't be able to access our schma file. So I 
 * would like this organization to use a copy of the schema but located 
 * in their intranet to validate the XML. (Lei Chen)
 *
 * Solution:
 *   - Use http://apache.org/xml/properties/schema/external-schemaLocation
 *     property to specify xsd file! (Maik Weber)
 *
 * Resources:
 * [1] Xerces 1.4.1 docs on properties
 *     C:\Program Files\xerces-1_4_1\docs\html\properties.html
 *
 */

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

import java.io.*;

/**
 * Parts of the code is from the sax.SAX2Count example.
 *
 * @author Gavin Bong (gavinbong@visto.com)
 */
class Test extends DefaultHandler{

  public void startDocument(){
    System.out.println("Reading document...");
  }

  public void endDocument(){
    System.out.println("Finished with document");
  }

  /** Warning. */
  public void warning(SAXParseException ex) {
      System.err.println("[Warning] "+
                         ex.getMessage());
  }

  /** Error. */
  public void error(SAXParseException ex) {
      System.err.println("[Error] "+
                         ex.getMessage());
  }

  /** Fatal error. */
  public void fatalError(SAXParseException ex) throws SAXException {
      System.err.println("[Fatal Error] "+
                         ex.getMessage());
  }

  public static void main(String[] args){
    SAXParser parser;
    SAXParserFactory factory = SAXParserFactory.newInstance();
    Test t = new Test();

    try{
      factory.setNamespaceAware(true);
      factory.setValidating(true);
      parser = factory.newSAXParser();

// change this to read value from the command line!!
      parser.setProperty( "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", 
                          "file:///C:/_projects/jaxp/hide/personal.xsd" ); 
      parser.parse("personal.xml", t);      
    }catch(SAXException se){
      System.err.println( se.getMessage() );
    }catch(IOException ioe){
      System.err.println( ioe.getMessage() );
    }catch(ParserConfigurationException pce){
      System.err.println( pce.getMessage() );
    }
  } 
}


