Example usage for org.w3c.dom.bootstrap DOMImplementationRegistry addSource

List of usage examples for org.w3c.dom.bootstrap DOMImplementationRegistry addSource

Introduction

In this page you can find the example usage for org.w3c.dom.bootstrap DOMImplementationRegistry addSource.

Prototype

public void addSource(final DOMImplementationSource s) 

Source Link

Document

Register an implementation.

Usage

From source file:com.espertech.esper.event.xml.XSDSchemaMapper.java

private static XSModel readSchemaInternal(String schemaResource, String schemaText)
        throws IllegalAccessException, InstantiationException, ClassNotFoundException, ConfigurationException,
        URISyntaxException {//from w  w  w  . j  a v a2s.  c  om
    LSInputImpl input = null;
    String baseURI = null;
    if (schemaResource != null) {
        URL url = ResourceLoader.resolveClassPathOrURLResource("schema", schemaResource);
        baseURI = url.toURI().toString();
    } else {
        input = new LSInputImpl(schemaText);
    }

    // Uses Xerxes internal classes
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    registry.addSource(new DOMXSImplementationSourceImpl());
    Object xsImplementation = registry.getDOMImplementation("XS-Loader");
    if (xsImplementation == null) {
        throw new ConfigurationException(
                "Failed to retrieve XS-Loader implementation from registry obtained via DOMImplementationRegistry.newInstance, please check that registry.getDOMImplementation(\"XS-Loader\") returns an instance");
    }
    if (!JavaClassHelper.isImplementsInterface(xsImplementation.getClass(), XSImplementation.class)) {
        String message = "The XS-Loader instance returned by the DOM registry class '"
                + xsImplementation.getClass().getName() + "' does not implement the interface '"
                + XSImplementation.class.getName()
                + "'; If you have a another Xerces distribution in your classpath please ensure the classpath order loads the JRE Xerces distribution or set the DOMImplementationRegistry.PROPERTY system property";
        throw new ConfigurationException(message);
    }
    XSImplementation impl = (XSImplementation) xsImplementation;
    XSLoader schemaLoader = impl.createXSLoader(null);
    XSModel xsModel;
    if (input != null) {
        xsModel = schemaLoader.load(input);
    } else {
        xsModel = schemaLoader.loadURI(baseURI);
    }

    if (xsModel == null) {
        throw new ConfigurationException("Failed to read schema via URL '" + schemaResource + '\'');
    }

    return xsModel;
}