Example usage for org.xml.sax XMLReader setErrorHandler

List of usage examples for org.xml.sax XMLReader setErrorHandler

Introduction

In this page you can find the example usage for org.xml.sax XMLReader setErrorHandler.

Prototype

public void setErrorHandler(ErrorHandler handler);

Source Link

Document

Allow an application to register an error event handler.

Usage

From source file:org.roda.core.common.validation.ValidationUtils.java

public static ValidationReport isXMLValid(ContentPayload xmlPayload) {
    ValidationReport ret = new ValidationReport();

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);//from  ww  w. j a  va2  s. c  om
    factory.setNamespaceAware(true);

    RodaErrorHandler errorHandler = new RodaErrorHandler();

    try (Reader reader = new InputStreamReader(new BOMInputStream(xmlPayload.createInputStream()))) {
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setEntityResolver(new RodaEntityResolver());
        InputSource inputSource = new InputSource(reader);

        xmlReader.setErrorHandler(errorHandler);
        xmlReader.parse(inputSource);
        ret.setValid(errorHandler.getErrors().isEmpty());
        for (SAXParseException saxParseException : errorHandler.getErrors()) {
            ret.addIssue(convertSAXParseException(saxParseException));
        }
    } catch (SAXException e) {
        ret.setValid(false);
        for (SAXParseException saxParseException : errorHandler.getErrors()) {
            ret.addIssue(convertSAXParseException(saxParseException));
        }
    } catch (IOException e) {
        ret.setValid(false);
        ret.setMessage(e.getMessage());
    }
    return ret;
}

From source file:org.sakaiproject.warehouse.util.db.DbLoader.java

protected void readProperties(XMLReader parser, InputStream properties) throws SAXException, IOException {
    propertiesHandler = new PropertiesHandler();
    parser.setContentHandler(propertiesHandler);
    parser.setErrorHandler(propertiesHandler);
    parser.parse(new InputSource(properties));
}

From source file:org.slc.sli.ingestion.parser.impl.EdfiRecordParserImpl2.java

private void parseAndValidate(InputStream input, Schema schema) throws XmlParseException, IOException {
    ValidatorHandler vHandler = schema.newValidatorHandler();
    vHandler.setContentHandler(this);
    vHandler.setErrorHandler(this);

    InputSource is = new InputSource(new InputStreamReader(input, "UTF-8"));
    is.setEncoding("UTF-8");

    try {/*from w  ww . j a  v  a2  s .  com*/
        XMLReader parser = XMLReaderFactory.createXMLReader();
        parser.setContentHandler(vHandler);
        parser.setErrorHandler(this);

        vHandler.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);

        parser.setFeature("http://apache.org/xml/features/validation/id-idref-checking", false);
        parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
        parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

        parser.parse(is);
    } catch (SAXException e) {
        throw new XmlParseException(e.getMessage(), e);
    }
}

From source file:org.unitils.dbunit.datasetfactory.impl.MultiSchemaXmlDataSetFactory.java

/**
 * Parses the data sets from the given files.
 * Each schema is given its own data set and each row is given its own table.
 *
 * @param dataSetFiles The data set files, not null
 * @return The read data set, not null/*from ww w  .j a v  a  2s . c  om*/
 */
public MultiSchemaDataSet createDataSet(List<File> dataSetFiles) {
    DataSetContentHandler dataSetContentHandler = new DataSetContentHandler(defaultSchemaName);
    XMLReader xmlReader = createXMLReader();
    xmlReader.setContentHandler(dataSetContentHandler);
    xmlReader.setErrorHandler(dataSetContentHandler);

    for (File dataSetFile : dataSetFiles) {
        readDataSetFile(xmlReader, dataSetFile);
    }
    return dataSetContentHandler.getMultiSchemaDataSet();
}

From source file:org.unitils.dbunit.util.MultiSchemaXmlDataSetReader.java

/**
 * Parses the datasets from the given files.
 * Each schema is given its own dataset and each row is given its own table.
 *
 * @param dataSetFiles The dataset files, not null
 * @return The read data set, not null//ww w.j a  v  a  2s .c  om
 */
public MultiSchemaDataSet readDataSetXml(File... dataSetFiles) {
    try {
        DataSetContentHandler dataSetContentHandler = new DataSetContentHandler(defaultSchemaName);
        XMLReader xmlReader = createXMLReader();
        xmlReader.setContentHandler(dataSetContentHandler);
        xmlReader.setErrorHandler(dataSetContentHandler);

        for (File dataSetFile : dataSetFiles) {
            InputStream dataSetInputStream = null;
            try {
                dataSetInputStream = new FileInputStream(dataSetFile);
                xmlReader.parse(new InputSource(dataSetInputStream));
            } finally {
                closeQuietly(dataSetInputStream);
            }
        }
        return dataSetContentHandler.getMultiSchemaDataSet();

    } catch (Exception e) {
        throw new UnitilsException("Unable to parse data set xml.", e);
    }

}

From source file:org.xchain.framework.servlet.XChainManager.java

/**
 * Handles requests to the render url screen.
 *//*from  w w  w. j  ava 2 s.  c o m*/
protected void render(HttpServletRequest request, HttpServletResponse response, List<String> pathSegments)
        throws ServletException, IOException {
    // get the url to render.
    String systemId = request.getParameter("system-id");

    if (systemId == null) {
        return;
    }

    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("text/xml");

    URL url = null;
    InputSource inputSource = null;

    try {
        // get the url to copy out.
        url = UrlFactory.getInstance().newUrl(systemId);

        // get the input source for the url.
        inputSource = UrlSourceUtil.createSaxInputSource(url);

        // create the XMLReader.
        XMLReader reader = XmlFactoryLifecycle.newXmlReader();
        reader.setErrorHandler(new FailingErrorHandler());

        // set up the source filter.
        XChainDeclFilter sourceFilter = new XChainDeclFilter();
        sourceFilter.setParent(reader);
        sourceFilter.setErrorHandler(new FailingErrorHandler());

        // create a serializer for the response.
        Properties outputProperties = OutputPropertiesFactory.getDefaultMethodProperties("xml");
        outputProperties.setProperty("media-type", "text/xml");
        Serializer serializer = SerializerFactory.getSerializer(outputProperties);
        serializer.setOutputStream(response.getOutputStream());
        sourceFilter.setContentHandler(serializer.asContentHandler());

        // parser the url.
        sourceFilter.parse(inputSource);
    } catch (Exception e) {
        errorScreen(request, response, e);
    } finally {
        close(inputSource);
        close(response.getOutputStream());
    }
}

From source file:org.xchain.framework.strategy.CatalogConsumerStrategy.java

public Catalog consume(String systemId, SourceStrategy<InputSource> sourceStrategy, DependencyTracker tracker)
        throws Exception {
    // Get the input source
    InputSource inputSource = sourceStrategy.getSource(systemId);

    // create the XMLReader.
    XMLReader reader = XmlFactoryLifecycle.newXmlReader();

    reader.setErrorHandler(new FailingErrorHandler());

    XChainDeclFilter sourceFilter = new XChainDeclFilter();
    sourceFilter.setParent(reader);/*  w  w w.  j a  v  a2s .c o m*/
    sourceFilter.setErrorHandler(new FailingErrorHandler());

    // create the jsl filter.
    SaxTemplateHandler xmlFilter = new SaxTemplateHandler();
    xmlFilter.setParent(sourceFilter);
    xmlFilter.setErrorHandler(new FailingErrorHandler());

    // create the digester, passing the jsl filter.
    Digester digester = new Digester(xmlFilter);

    // set the digester onto the xml filter.
    xmlFilter.setDigester(digester);

    // add the annotation rule set to the digester.
    digester.addRuleSet(new AnnotationRuleSet(systemId));

    digester.setErrorHandler(new FailingErrorHandler());

    // get the catalog object.
    Catalog catalog = null;

    try {
        catalog = (Catalog) digester.parse(inputSource);
    } catch (Exception e) {
        if (log.isErrorEnabled()) {
            log.error("Could not create catalog for system id '" + inputSource.getSystemId() + "'.", e);
        }
        throw e;
    }

    return catalog;
}

From source file:org.yawlfoundation.yawl.unmarshal.YawlXMLSpecificationValidator.java

/**
 * Sets the checker up for a run./*from  w w  w.  jav  a2  s  .  c o  m*/
 * @param version the version of the schema
 * @return a reader configured to do the checking.
 * @throws SAXException
 */
private XMLReader setUpChecker(String version) throws SAXException {
    XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    if (YSpecification.isValidVersion(version)) {
        parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
                getSchemaLocation(version));
    } else {
        throw new RuntimeException("Version [" + version + "] is not valid version.");
    }
    parser.setContentHandler(this);
    parser.setErrorHandler(this);
    parser.setFeature("http://xml.org/sax/features/validation", true);
    parser.setFeature("http://apache.org/xml/features/validation/schema", true);
    parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
    return parser;
}

From source file:petascope.util.XMLUtil.java

private static Builder newBuilder(boolean ignoreDTD) {
    XMLReader xmlReader = null;
    try {/*from www.ja  va  2s.  co m*/
        xmlReader = factory.newSAXParser().getXMLReader();
        if (ignoreDTD) {
            xmlReader.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId)
                        throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
            xmlReader.setErrorHandler(new ErrorHandler() {

                @Override
                public void warning(SAXParseException saxpe) throws SAXException {
                    log.warn("XML parser warning: ", saxpe.getMessage());
                }

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

                @Override
                public void fatalError(SAXParseException saxpe) throws SAXException {
                    throw saxpe;
                }
            });
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return new Builder(xmlReader);
}