/*
 * Copyright (c) 2004, ADAPT project partners (IST-2001-37126).
 *
 * XMLCheck.java
 */

package org.eu.adapt.tools;

import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.ErrorHandler;
import org.xml.sax.EntityResolver;

public class XMLCheck
{
    public static void main(String[] args)
    {
        String uri = args[0];

        StringBuffer externalSchemaLocations = new StringBuffer();

        externalSchemaLocations.append("http://www.w3.org/2001/XMLSchema ../../w3_org/2001/xml_scheme-1.0.xsd ");
        externalSchemaLocations.append("http://www.w3.org/2001/xml.xsd ../../w3_org/2001/xml.xsd ");
        externalSchemaLocations.append("http://schemas.xmlsoap.org/wsdl/ ../../xmlsoap_org/wsdl-1.1.xsd ");
        externalSchemaLocations.append("http://schemas.xmlsoap.org/wsdl/soap/ ../../xmlsoap_org/wsdl/soap-1.1.xsd ");
        externalSchemaLocations.append("http://schemas.adapt.org/interaction-constraints-definition-1.0/ ../../adapt_eu_org/sequencing-constraints-1.0.xsd ");
        externalSchemaLocations.append("http://schemas.adapt.eu.org/service-protocol-defintion-1.0/ ../../adapt_eu_org/service-protocol-definition-1.0.xsd ");
        externalSchemaLocations.append("http://schemas.adapt.org/process-definition-2.1/ ../../adapt_eu_org/process_definition-2.1.xsd");

        try
        {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setIgnoringElementContentWhitespace(true);
            documentBuilderFactory.setValidating(true);
            documentBuilderFactory.setAttribute("http://xml.org/sax/features/validation", Boolean.TRUE);
            documentBuilderFactory.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
            documentBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", Boolean.TRUE);
            documentBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema-full-checking", Boolean.TRUE);
            documentBuilderFactory.setAttribute("http://apache.org/xml/features/dom/include-ignorable-whitespace", Boolean.FALSE);
            documentBuilderFactory.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation", externalSchemaLocations.toString());
            documentBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", Boolean.TRUE);

            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            documentBuilder.setErrorHandler(new BasicErrorHandler());
            documentBuilder.setEntityResolver(new BasicEntityResolver());

            Document document = documentBuilder.parse(uri);
        }
        catch (Exception exception)
        {
            System.err.print("XMLCheck.main: ");
            exception.printStackTrace(System.err);
        }
        catch (Error error)
        {
            System.err.print("XMLCheck.main: ");
            error.printStackTrace(System.err);
        }
    }

    private static class BasicErrorHandler implements ErrorHandler
    {
        public void error(SAXParseException saxParseException)
        {
            System.err.println("XML Error:       [" + saxParseException + "]");
            System.err.println("    message      [" + saxParseException.getMessage() + "]");
            System.err.println("    exception    [" + saxParseException.getException() + "]");
            System.err.println("    publicId     [" + saxParseException.getPublicId() + "]");
            System.err.println("    systemId     [" + saxParseException.getSystemId() + "]");
            System.err.println("    lineNumber   [" + saxParseException.getLineNumber() + "]");
            System.err.println("    columnNumber [" + saxParseException.getColumnNumber() + "]");
        }

        public void fatalError(SAXParseException saxParseException)
        {
            System.err.println("XML Exception:   [" + saxParseException + "]");
            System.err.println("    message      [" + saxParseException.getMessage() + "]");
            System.err.println("    exception    [" + saxParseException.getException() + "]");
            System.err.println("    publicId     [" + saxParseException.getPublicId() + "]");
            System.err.println("    systemId     [" + saxParseException.getSystemId() + "]");
            System.err.println("    lineNumber   [" + saxParseException.getLineNumber() + "]");
            System.err.println("    columnNumber [" + saxParseException.getColumnNumber() + "]");
         }

        public void warning(SAXParseException saxParseException)
        {
            System.err.println("XML Warning:     [" + saxParseException + "]");
            System.err.println("    message      [" + saxParseException.getMessage() + "]");
            System.err.println("    exception    [" + saxParseException.getException() + "]");
            System.err.println("    publicId     [" + saxParseException.getPublicId() + "]");
            System.err.println("    systemId     [" + saxParseException.getSystemId() + "]");
            System.err.println("    lineNumber   [" + saxParseException.getLineNumber() + "]");
            System.err.println("    columnNumber [" + saxParseException.getColumnNumber() + "]");
         }
    }

    private static class BasicEntityResolver implements EntityResolver
    {
        public InputSource resolveEntity(String publicId, String systemId)
        {
//          System.err.println("resolve entity: [" + publicId + "," + systemId + "]");

            return null;
        }
    }
}
