Example usage for org.w3c.dom DOMError getLocation

List of usage examples for org.w3c.dom DOMError getLocation

Introduction

In this page you can find the example usage for org.w3c.dom DOMError getLocation.

Prototype

public DOMLocator getLocation();

Source Link

Document

The location of the error.

Usage

From source file:org.alfresco.web.forms.xforms.SchemaUtil.java

public static XSModel parseSchema(final Document schemaDocument, final boolean failOnError)
        throws FormBuilderException {
    try {/*from  w  ww. j a v  a  2s .  com*/
        // Get DOM Implementation using DOM Registry
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMXSImplementationSourceImpl");

        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        final DOMImplementationLS lsImpl = (DOMImplementationLS) registry
                .getDOMImplementation("XML 1.0 LS 3.0");
        if (lsImpl == null) {
            throw new FormBuilderException("unable to create DOMImplementationLS using " + registry);
        }
        final LSInput in = lsImpl.createLSInput();
        in.setStringData(XMLUtil.toString(schemaDocument));

        final XSImplementation xsImpl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
        final XSLoader schemaLoader = xsImpl.createXSLoader(null);
        final DOMConfiguration config = (DOMConfiguration) schemaLoader.getConfig();
        final LinkedList<DOMError> errors = new LinkedList<DOMError>();
        config.setParameter("error-handler", new DOMErrorHandler() {
            public boolean handleError(final DOMError domError) {
                errors.add(domError);
                return true;
            }
        });

        final XSModel result = schemaLoader.load(in);
        if (failOnError && errors.size() != 0) {
            final HashSet<String> messages = new HashSet<String>();
            StringBuilder message = null;
            for (DOMError e : errors) {
                message = new StringBuilder();
                final DOMLocator dl = e.getLocation();
                if (dl != null) {
                    message.append("at line ").append(dl.getLineNumber()).append(" column ")
                            .append(dl.getColumnNumber());
                    if (dl.getRelatedNode() != null) {
                        message.append(" node ").append(dl.getRelatedNode().getNodeName());
                    }
                    message.append(": ").append(e.getMessage());
                }
                messages.add(message.toString());
            }

            message = new StringBuilder();
            message.append(messages.size() > 1 ? "errors" : "error").append(" parsing schema: \n");
            for (final String s : messages) {
                message.append(s).append("\n");
            }

            throw new FormBuilderException(message.toString());
        }

        if (result == null) {
            throw new FormBuilderException("invalid schema");
        }
        return result;
    } catch (ClassNotFoundException x) {
        throw new FormBuilderException(x);
    } catch (InstantiationException x) {
        throw new FormBuilderException(x);
    } catch (IllegalAccessException x) {
        throw new FormBuilderException(x);
    }
}