/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlconfiguration;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author blackpanther
 */
public class Main {

    private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    
    private static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    private static       SAXParser        saxParser;
    
    private static final SchemaFactory    sf = SchemaFactory.newInstance(W3C_XML_SCHEMA);

    static {
        saxParserFactory.setNamespaceAware(true);

        /* Validation against a XSD*/
        //saxParserFactory.setValidating(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            /* database properties */
            Properties props = new Properties();
            /* SAX handler */
            XMLConfigHandler handler = new XMLConfigHandler(props);
            
            /* Creating a schema from xsd file*/
            Schema schema = schema = sf.newSchema(new File("xml/configNS.xsd"));
            
            //saxParserFactory.setSchema(schema);
            
            saxParser = saxParserFactory.newSAXParser();

            saxParser.parse("xml/config.xml", handler);
            
            if( handler.isValid() ) {
                System.out.println("Document is valid");
                props.list(System.err);             
            } else
                System.out.println("Document is not valid");
            
            
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
