Example usage for javax.xml.bind ValidationEventLocator getNode

List of usage examples for javax.xml.bind ValidationEventLocator getNode

Introduction

In this page you can find the example usage for javax.xml.bind ValidationEventLocator getNode.

Prototype

public org.w3c.dom.Node getNode();

Source Link

Document

Return a reference to the DOM Node if available

Usage

From source file:com.rapid.server.RapidHttpServlet.java

public static Unmarshaller getUnmarshaller() throws JAXBException, IOException {

    // initialise the unmarshaller
    Unmarshaller unmarshaller = _jaxbContext.createUnmarshaller();
    // add the encrypted xml adapter
    unmarshaller.setAdapter(_encryptedXmlAdapter);

    // add a validation listener (this makes for better error messages)
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override/*  www. j  a  v a  2 s  .com*/
        public boolean handleEvent(ValidationEvent event) {

            // get the location
            ValidationEventLocator location = event.getLocator();

            // log
            _logger.debug("JAXB validation event - " + event.getMessage()
                    + (location == null ? ""
                            : " at line " + location.getLineNumber() + ", column " + location.getColumnNumber()
                                    + ", node " + location.getNode()));

            // messages with "unrecognized type name" are very useful they're not sever themselves must almost always followed by a severe with a less meaningful message 
            if (event.getMessage().contains("unrecognized type name")
                    || event.getSeverity() == ValidationEvent.FATAL_ERROR) {
                return false;
            } else {
                return true;
            }

        }
    });

    // return
    return unmarshaller;
}