Example usage for javax.xml.validation ValidatorHandler startPrefixMapping

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

Introduction

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

Prototype

public void startPrefixMapping(String prefix, String uri) throws SAXException;

Source Link

Document

Begin the scope of a prefix-URI Namespace mapping.

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();//ww  w.ja v  a  2  s  .  co  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;
}