Example usage for javax.xml.validation ValidatorHandler setDocumentLocator

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

Introduction

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

Prototype

public void setDocumentLocator(Locator locator);

Source Link

Document

Receive an object for locating the origin of SAX document events.

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();// w  w w . j  a  va 2 s  .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;
}