Example usage for javax.xml.validation ValidatorHandler startDocument

List of usage examples for javax.xml.validation ValidatorHandler startDocument

Introduction

In this page you can find the example usage for javax.xml.validation ValidatorHandler startDocument.

Prototype

public void startDocument() throws SAXException;

Source Link

Document

Receive notification of the beginning of a document.

Usage

From source file:stroom.pipeline.server.filter.SchemaFilter.java

private ValidatorHandler getValidator() throws SAXException {
    ValidatorHandler validatorHandler = null;

    if (schemaValidation && schemaPool != null) {
        // Put back the old schema if we need to.
        returnCurrentSchema();//from  w  ww.ja  v a  2s.c o m

        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        sb.append("<xsd:schema xmlns:xsd=\"");
        sb.append(schemaLanguage);
        sb.append("\"");
        final CharBuffer namespaces = new CharBuffer(100);
        final CharBuffer locations = new CharBuffer(100);
        for (final String prefix : prefixes.keySet()) {
            final String namespace = prefixes.get(prefix);
            final String location = schemaLocations.get(namespace);
            if (location != null) {
                namespaces.append(" xmlns");
                if (prefix.length() > 0) {
                    namespaces.append(":");
                    namespaces.append(prefix);
                }
                namespaces.append("=\"");
                namespaces.append(namespace);
                namespaces.append("\"");

                locations.append("<xsd:import namespace=\"");
                locations.append(namespace);
                locations.append("\" schemaLocation=\"");
                locations.append(location);
                locations.append("\"/>\n");
            }
        }
        sb.append(namespaces.toString());
        sb.append(">\n");
        sb.append(locations.toString());
        sb.append("</xsd:schema>");

        final String data = sb.toString();
        sb.clear();

        // Get another schema.
        final SchemaKey schemaKey = new SchemaKey(schemaLanguage, data);
        poolItem = schemaPool.borrowObject(schemaKey, true);
        final StoredSchema storedSchema = poolItem.getValue();

        // Replay errors generated when creating schema.
        try {
            storedSchema.getErrorReceiver().replay(errorReceiverProxy);
        } catch (final Exception e) {
            errorHandler.fatalError(new SAXParseException(e.getMessage(), null));
        }

        // Create a validator handler.
        validatorHandler = storedSchema.getSchema().newValidatorHandler();
        validatorHandler.setDocumentLocator(locator);
        validatorHandler.setErrorHandler(errorHandler);

        validatorHandler.startDocument();
        for (final String prefix : prefixes.keySet()) {
            validatorHandler.startPrefixMapping(prefix, prefixes.get(prefix));
        }
    }

    return validatorHandler;
}