package com.mindprod.example;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import static java.lang.System.err;

/**
 * Validate the syntax of an XML document.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0,  2007-07-30
 */
@SuppressWarnings( { "UnusedAssignment" } )
public final class TestXMLDTDValidator
    {
    // --------------------------- main() method ---------------------------

    /**
     * Validate the syntax of an XML document that contains a DOCTYPE DTD to define the structure. XML docs to validate
     * must start something like this:
     * <p/>
     * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
     * <p/>
     * &lt;!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN'
     * 'http://java.sun.com/j2ee/dtds/web-app_2_2.dtd'&lt;
     *
     * @param args arg[0] is name of XML file.
     */
    public static void main( String[] args )
        {
        try
            {
            final String filename = args[ 0 ];
            System.out.println( "Parsing XML document: " + filename );

            // parse XML to form DOM object tree.
            final DocumentBuilderFactory dbf =
                    DocumentBuilderFactory.newInstance();
            // validate XML as you parse
            dbf.setValidating( true );

            final DocumentBuilder db = dbf.newDocumentBuilder();
            db.setErrorHandler( new SimpleErrorHandler() );

            final Document doc = db.parse( new File( filename ) );
            }
        catch ( IOException e )
            {
            err.println( "trouble opening file: " + args[ 0 ] );
            err.println( e );
            }
        catch ( ParserConfigurationException e )
            {
            err.println( "Parser configuration problem" );
            err.println( e );
            }
        catch ( SAXException e )
            {
            err.println( "Syntax error in XML file" );
            err.println( e );
            }
        System.out.println( "done" );
        }
    }

/**
 * handles errors discovered in the XML syntax
 */
final class SimpleErrorHandler implements ErrorHandler
    {
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * default constructor
     */
    public SimpleErrorHandler()
        {
        }

    /**
     * Receive notification of a recoverable error. <p/> <p>This corresponds to the definition of "error" in section 1.2
     * of the W3C XML 1.0 Recommendation.  For example, a validating parser would use this callback to report the
     * violation of a validity constraint.  The default behaviour is to take no action.</p> <p/> <p>The SAX parser must
     * continue to provide normal parsing events after invoking this method: it should still be possible for the
     * application to process the document through to the end. If the application cannot do so, then the parser should
     * report a fatal error even if the XML recommendation does not require it to do so.</p> <p/> <p>Filters may use
     * this method to report other, non-XML errors as well.</p>
     *
     * @param exception The error information encapsulated in a SAX parse exception.
     * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
     * @see org.xml.sax.SAXParseException
     */
    public void error( SAXParseException exception ) throws SAXException
        {
        System.err.println( "error: " + exception );
        }

    /**
     * Receive notification of a non-recoverable error. <p/> <p><strong>There is an apparent contradiction between the
     * documentation for this method and the documentation for {@link org.xml.sax.ContentHandler#endDocument}. Until
     * this ambiguity is resolved in a future major release, clients should make no assumptions about whether
     * endDocument() will or will not be invoked when the parser has reported a fatalError() or thrown an
     * exception.</strong></p> <p/> <p>This corresponds to the definition of "fatal error" in section 1.2 of the W3C XML
     * 1.0 Recommendation.  For example, a parser would use this callback to report the violation of a well-formedness
     * constraint.</p> <p/> <p>The application must assume that the document is unusable after the parser has invoked
     * this method, and should continue (if at all) only for the sake of collecting additional error messages: in fact,
     * SAX parsers are free to stop reporting any other events once this method has been invoked.</p>
     *
     * @param exception The error information encapsulated in a SAX parse exception.
     * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
     * @see org.xml.sax.SAXParseException
     */
    public void fatalError( SAXParseException exception ) throws SAXException
        {
        System.err.println( "fatal: " + exception );
        }

    /**
     * Receive notification of a warning. <p/> <p>SAX parsers will use this method to report conditions that are not
     * errors or fatal errors as defined by the XML recommendation.  The default behaviour is to take no action.</p>
     * <p/> <p>The SAX parser must continue to provide normal parsing events after invoking this method: it should still
     * be possible for the application to process the document through to the end.</p> <p/> <p>Filters may use this
     * method to report other, non-XML warnings as well.</p>
     *
     * @param exception The warning information encapsulated in a SAX parse exception.
     * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
     * @see org.xml.sax.SAXParseException
     */
    public void warning( SAXParseException exception ) throws SAXException
        {
        System.err.println( "warning: " + exception );
        }
    }// end SimpleErrorHandler