import java.io.IOException;

// SAX
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;

//SAX and external XSD
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;

public class SaxIgnorableWhiteSpaceTest {

    private SaxIgnorableWhiteSpaceTest() {
    }

    // validate SAX and external XSD
    public static boolean validateWithExtXSDUsingSAX(String xml, String xsd) throws ParserConfigurationException, IOException {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(true);

            SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
            SAXParser parser = null;
            try {
                factory.setSchema(schemaFactory.newSchema(new Source[]{new StreamSource(xsd)}));
                parser = factory.newSAXParser();
            } catch (SAXException se) {
                System.out.println("SCHEMA : " + se.getMessage()); 
                return false;
            }

            System.out.println("factory = " + factory);
            System.out.println("parser = " + parser);

            XMLReader reader = parser.getXMLReader();
            reader.setErrorHandler(new ErrorHandler() {
                public void warning(SAXParseException e) throws SAXException {
                    System.out.println("WARNING: " + e.getMessage()); 
                }

                public void error(SAXParseException e) throws SAXException {
                    System.out.println("ERROR : " + e.getMessage());
                    throw e;
                }

                public void fatalError(SAXParseException e) throws SAXException {
                    System.out.println("FATAL : " + e.getMessage());
                    throw e;
                }
            });
            reader.setContentHandler(new ContentHandler() {
                public void setDocumentLocator(Locator locator) {}
                public void startDocument() throws SAXException {}
                public void endDocument() throws SAXException {}
                public void startPrefixMapping(String prefix, String uri) throws SAXException {}
                public void endPrefixMapping(String prefix) throws SAXException {}

                public void startElement(
                        String uri,
                        String localName,
                        String qName,
                        Attributes atts) throws SAXException {
                    System.out.println("startElement " + qName);
                }

                 public void endElement(
                        String uri,
                        String localName,
                        String qName) throws SAXException {
                    System.out.println("endElement " + qName);
                }

                public void characters(char[] ch, int start, int length)
                        throws SAXException {
                    System.out.println("characters = \""
                            + new String(ch, start, length) + "\"");
                }

                 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
                    System.out.println("ignorableWhitespace = \""
                            + new String(ch, start, length) + "\"");
                }

                public void processingInstruction(String target, String data) throws SAXException {}

                public void skippedEntity(String name) throws SAXException {}
            });

            reader.parse(new InputSource(xml));
            return true;
        } catch (ParserConfigurationException pce) {
            throw pce;
        } catch (IOException io) {
            throw io;
        } catch (SAXException se) {
            return false;
        }
    }

    public static void main(String args[]) throws Exception {
        System.out.println(SaxIgnorableWhiteSpaceTest
                        .validateWithExtXSDUsingSAX(
         "C:/temp/howto.xml",
         "C:/temp/howto.xsd"));
    }
}
