/*
 * [ValidateJNLP.java]
 *
 * Summary: Validate a JNLP document with an XSD schema.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-04-11 initial version
 */
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.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;

import static java.lang.System.*;

/**
 * Validate a JNLP document with an XSD schema.
 * <p/>
 * Using an XSD schema based on the Vampqh JNLP XSD schema
 * jnlp1-xml-schema.xsd version 1.0
 * or the CMP schema jnlp6.xsd version 6.0 in the current directory.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-04-11 initial version
 * @since 2009-04-11
 */
public class ValidateJNLP
    {
    /**
     * validate one jnlp file
     *
     * @param args first: name of xsd schema, e.g. jnlp6.xsd
     *             second: name of name of the jnlp file to validate,
     *             e.g. E:\com\mindprod\affirm\affirm.jnlp
     */
    public static void main( String[] args )
        {
        try
            {
            // build an XSD-aware SchemaFactory
            final SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
            // hook up mindless org.xml.sax.ErrorHandler implementation.
            schemaFactory.setErrorHandler( new JNLPErrorHandler() );
            // get the custom xsd schema describing the required format for the jnlp XML files.
            final Schema schemaXSD = schemaFactory.newSchema( new File( args[ 0 ] ) );
            // Create a Validator capable of validating JNLP files according to to the custom schema.
            final Validator validator = schemaXSD.newValidator();
            // Create a generic XML parser.
            final DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            // parse the JNLP file on the command line purely as XML and get a DOM tree representation.
            final Document document = parser.parse( new File( args[ 1 ] ) );
            // Validate the JNLP tree against the stricter XSD schema
            validator.validate( new DOMSource( document ) );
            }
        catch ( Exception e )
            {
            err.println();
            e.printStackTrace( err );
            err.println();
            }
        } // end main
    } // end ValidateJNLP

/**
 * minimalist error handler for parser
 */
class JNLPErrorHandler implements ErrorHandler
    {
    /**
     * default constructor
     */
    public JNLPErrorHandler()
        {
        }

    /**
     * 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
        {
        err.println( 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
        {
        err.println( 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
        {
        err.println( exception );
        }
    }