Example usage for org.xml.sax.helpers DefaultHandler setDocumentLocator

List of usage examples for org.xml.sax.helpers DefaultHandler setDocumentLocator

Introduction

In this page you can find the example usage for org.xml.sax.helpers DefaultHandler setDocumentLocator.

Prototype

public void setDocumentLocator(Locator locator) 

Source Link

Document

Receive a Locator object for document events.

Usage

From source file:org.n52.ifgicopter.spf.xml.SchematronValidator.java

/**
 * Here the real validation process is started.
 * /*from ww  w  .j a v a 2  s. c  om*/
 * @return true if no schematron rule was violated.
 */
public boolean validate() {
    Transform trans = new Transform();

    /*
     * transform the schematron
     */
    String[] arguments = new String[] { "-x", "org.apache.xerces.parsers.SAXParser", "-w1", "-o",
            DOCS_FOLDER + "/tmp/tmp.xsl", this.schematronFile, DOCS_FOLDER + "/iso_svrl_for_xslt2.xsl",
            "generate-paths=yes" };
    trans.doTransform(arguments, "java net.sf.saxon.Transform");

    /*
     * transform the instance
     */
    String report = DOCS_FOLDER + "/tmp/"
            + this.xmlInstanceFile.substring(this.xmlInstanceFile.lastIndexOf("/") + 1) + ".report.xml";
    arguments = new String[] { "-x", "org.apache.xerces.parsers.SAXParser", "-w1", "-o", report,
            this.xmlInstanceFile, DOCS_FOLDER + "/tmp/tmp.xsl" };
    trans.doTransform(arguments, "java net.sf.saxon.Transform");

    LocatorImpl locator = new LocatorImpl();
    /*
     * an extension of DefaultHandler
     */
    DefaultHandler handler = new DefaultHandler() {

        private String failTmp;
        private Locator locator2;
        private boolean insideFail = false;

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if (qName.endsWith("failed-assert")) {
                this.failTmp = "Assertion error at \"" + attributes.getValue("test") + "\" (line "
                        + this.locator2.getLineNumber() + "): ";
                this.insideFail = true;
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (qName.endsWith("failed-assert")) {
                SchematronValidator.this.assertFails.add(this.failTmp);
                this.failTmp = null;
                this.insideFail = false;
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (this.insideFail) {
                this.failTmp += new String(ch, start, length).trim();
            }
        }

        @Override
        public void setDocumentLocator(Locator l) {
            this.locator2 = l;
        }

    };
    handler.setDocumentLocator(locator);

    try {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(new File(report), handler);
    } catch (SAXException e) {
        log.warn(e.getMessage(), e);
    } catch (IOException e) {
        log.warn(e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        log.warn(e.getMessage(), e);
    }

    return (this.assertFails.size() == 0) ? true : false;
}