/*
 * Copyright 2015 Adaptris Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package com.adaptris.util.text.xml;

import java.io.InputStream;
import java.io.Reader;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * @author Stuart Ellidge
 * 
 * Convenience class which validates an XML document against a specified schema
 */
public class Validator {
  private DOMParser parser = null;

  /**
   * Constructor that creates a new Validator object
   * 
   * @param schema a valid url to the schema to process
   * @throws Exception if a parse error is encountered
   */
  public Validator(String schema) throws Exception {
    parser = createParser(schema);
  }

  /**
   * Constructor that creates a new Validator object
   * 
   * @param schema a valid url to the schema to process
   * @param resolver an EntityResolver to use to retrieve included / imported
   *          documents
   * @throws Exception if a parse error is encountered
   */
  public Validator(String schema, EntityResolver resolver) throws Exception {
    this(schema);
    parser.setEntityResolver(resolver);
  }

  private DOMParser createParser(String schema) throws Exception {
    DOMParser result = new DOMParser();

    Class poolClass = Class.forName("org.apache.xerces.util.XMLGrammarPoolImpl");
    Object grammarPool = poolClass.newInstance();
    result.setProperty("http://apache.org/xml/properties/internal/grammar-pool", grammarPool);

    result.setFeature("http://xml.org/sax/features/validation", true);
    result.setFeature("http://apache.org/xml/features/validation/schema", true);
    result.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", schema);
    result.setErrorHandler(new ErrorChecker());
    return result;
  }

  /**
   * method which parses an xml document from an input stream, validates it and
   * returns the subsequent Document object.
   * 
   * @param xml the xml document as input stream
   * @return the resultant Document (if parsing successful)
   * @throws Exception if the document fails to be validated
   */
  public Document parse(InputStream xml) throws Exception {
    return parse(new InputSource(xml));
  }

  /**
   * method which parses an xml document from a Reader, validates it and returns
   * the subsequent Document object.
   * 
   * @param xml the xml document as reader
   * @return the resultant Document (if parsing successful)
   * @throws Exception if the document fails to be validated
   */
  public Document parse(Reader xml) throws Exception {
    return parse(new InputSource(xml));
  }

  /**
   * method which parses an xml document from an input source, validates it and
   * returns the subsequent Document object.
   * 
   * @param xml the xml document as input source
   * @return the resultant Document (if parsing successful)
   * @throws Exception if the document fails to be validated
   */
  public Document parse(InputSource xml) throws Exception {
    parser.parse(xml);
    return parser.getDocument();
  }

  private class ErrorChecker implements ErrorHandler {

    @Override
    public void error(SAXParseException e) throws SAXException {
      throw e;
    }

    @Override
    public void warning(SAXParseException e) throws SAXException {
      throw e;
    }

    @Override
    public void fatalError(SAXParseException e) throws SAXException {
      throw e;
    }

  }
}

@@ -30,18 +30,17 @@
  Ltd.%0A *
- 
 %0A
+
  * Licen
@@ -211,17 +211,16 @@
 se at%0A *
- 
 %0A *     
@@ -264,17 +264,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -678,17 +678,16 @@
 Reader;%0A
-%0A
 import o
@@ -955,17 +955,16 @@
 lidge%0A *
- 
 %0A * Conv
@@ -1065,16 +1065,18 @@
 dator %7B%0A
+%0A%0A
   privat
@@ -1158,33 +1158,32 @@
 ator object%0A   *
- 
 %0A   * @param sch
@@ -1436,25 +1436,24 @@
  object%0A   *
- 
 %0A   * @param
@@ -1912,16 +1912,124 @@
 ();%0A%0A   
+ // pointless class.forName() here, since we can't do any validation%0A    // if xerces isn't available%0A    //
  Class p
@@ -2102,16 +2102,19 @@
 l%22);%0A   
+ //
  Object 
@@ -2232,18 +2232,56 @@
 r-pool%22,
- g
+%0A        new org.apache.xerces.util.XMLG
 rammarPo
@@ -2282,20 +2282,25 @@
 mmarPool
+Impl()
 );%0A
-%0A
     resu
@@ -2752,33 +2752,32 @@
 ent object.%0A   *
- 
 %0A   * @param xml
@@ -3162,33 +3162,32 @@
 ent object.%0A   *
- 
 %0A   * @param xml
@@ -3572,21 +3572,20 @@
 object.%0A
+
    *
- 
 %0A   * @p

Remove class.forname on xerces Grammar Pool