/*
   Tamot --- Task Modelling Editor
   Copyright (C) 2000 Commonwealth Scientific and Industrial Research Organisation
   (CSIRO), Australia. All rights reserved. 

   The Software, owned by CSIRO Australia, has been developed by the CSIRO 
   Intelligent Interactive Technology group. 
   For more information, please see http://www.cmis.csiro.au/iit/.

   The Software is a beta-test version for internal research and evaluation purposes 
   by the Licensee. The Software is still at a development stage. 
   It has not undergone complete testing and may have inherent errors,
   bugs or deficiencies that can affect the operation of the Software. 

   We encourage your feedback and suggestions. Any bugs / suggestions found please email to
   Shijian.Lu@cmis.csiro.au and Thomas.Lo@cmis.csiro.au. 

   After you have signed the Beta-Test Software License Agreement with the CSIRO,
   CSIRO grants you a non-exclusive, non-transferable license to use it only for your 
   personal, non-commercial and lawful end use. Implied licenses are negated. 

   Warranty Disclaimer : 

   CSIRO supplies the Software on an "as is" basis and makes no warranties that use of
   the Software by the Licensee will not infringe any third party’s 
   Intellectual Property Rights nor that the Software will be of merchantable quality,
   or suitable for a particular purpose. CSIRO excludes all terms, conditions and
   warranties implied by custom, the general law or statute in relation to the
   Software.
*/

package taskModellingTool;

import java.io.IOException;
import javax.swing.JOptionPane;
//import java.util.Locale;

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
//import org.xml.sax.ErrorHandler;

import org.apache.xerces.msg.XMLMessages;
import org.apache.xerces.msg.ImplementationMessages;

//public class TamotDOMParser extends DOMParser implements ErrorHandler {
public class TamotDOMParser extends DOMParser {

    public TamotDOMParser() {
        super();
        
        try {
            setFeature("http://xml.org/sax/features/validation", true);
            
            /*setLocale(new Locale("English","Australia"));
                
            System.out.println("Country = "+Locale.getDefault().getDisplayCountry());
            System.out.println("Language = "+Locale.getDefault().getDisplayLanguage());
            System.out.println("Variant = "+Locale.getDefault().getDisplayVariant());
            
            Locale[] locales = new Locale[10];
            locales = Locale.getDefault().getAvailableLocales();
            for (int i = 0; i < locales.length; i++) {
                if (locales[i] != null) {
                    System.out.println("Country = "+locales[i].getDisplayCountry());
                    System.out.println("Language = "+locales[i].getDisplayLanguage());
                    System.out.println("Variant = "+locales[i].getDisplayVariant());
                }
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        //setErrorHandler(this);
    }
    
    /*public void warning(SAXParseException exception) throws SAXException {
        System.err.println("warning : "+exception);
    }
    
    public void error(SAXParseException exception) throws SAXException {
        System.err.println("error : "+exception);
    }
    
    public void fatalError(SAXParseException exception) throws SAXException {
        System.err.println("fatalError : "+exception);
    }*/
    
    private void displayErrors(Exception e,String fileName) {
        int lineNum = getLineNumber();
        int columnNum = getColumnNumber()-1;
        
        //System.err.println("validateDocument --- an error occurred : ");
        //System.err.println("     "+e);
        //System.err.println("     at line no. = "+lineNum+", column no. = "+columnNum);
        
        JOptionPane.showMessageDialog(MainFrame.mainFrame,
            "The following error occurs when validating the XML document : \n"+
            e+"\n"+
            "\n"+
            "File = "+fileName+"\n"+
            "Error occurs around line "+lineNum+", column "+columnNum,
            "Error",JOptionPane.ERROR_MESSAGE);

        //e.printStackTrace();
    }
    
    public boolean validateDocument(String fileName) {
        boolean result = true;
        
        try {
            parse(fileName);
        }
        catch (Exception e) {
            displayErrors(e,fileName);
            result = false;
        }
        
        return result;
    }
}