users@jersey.java.net

Re: [Jersey] Custom error message for JAXB validation

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Thu, 28 Oct 2010 12:55:16 +0200

Hi,

I think the problem is the way JAXB processes runtime exceptions
thrown from the ValidationEventHandler.handleEvent method:

http://download.oracle.com/javase/6/docs/api/javax/xml/bind/ValidationEventHandler.html#handleEvent%28javax.xml.bind.ValidationEvent%29

"If an unchecked runtime exception is thrown from this method, the
JAXB provider will treat it as if the method returned false and
interrupt the current unmarshal, validate, or marshal operation."

It may even be a JAXB bug. What exception is being thrown the JAXB
runtime for a fatal error?

Paul.

On Oct 23, 2010, at 1:07 PM, Prashanth Patnam wrote:

> Hi,
>
>
> I am using JAXB to validate the XML input against Schema.
> I am extending the JAXBContext and have written CustomEventCollector
> to handle validation event.
>
> The custom event handler will throw runtime exception which is
> mapped to ExceptionMapper to return error message which I’ve set.
>
> It is working fine if the ValidationEvent Severity is ERROR but when
> it is FATAL_ERROR I am not getting the custom message, Jersey is
> giving its own message “Input is syntactically incorrect”.
>
>
> Below is the code:
>
>
> public class ValidatingJAXBContext extends JAXBContext {
> private final JAXBContext jaxbContext;
> private final String schemasFolderPath;
>
> /**
> * This will override ValidationEventCollector to send custom
> error messages.
> * @
> *
> */
> private static class MyValidationEventCollector extends
> ValidationEventCollector {
> @Override
> public boolean handleEvent(ValidationEvent event) throws
> RuntimeException
> {
>
> ValidationEventLocator vel =
> event.getLocator();
> if (event.getSeverity() ==
> ValidationEvent.ERROR || event.getSeverity() ==
> ValidationEvent.FATAL_ERROR) {
> String error = "XML Validation Exception: " + event.getMessage() +
> " at row: " + vel.getLineNumber() + " column: " +
> vel.getColumnNumber();
> System.out.println(error);
> throw new ParserExcpetion(error);
> }
>
> return false;
> }
>
> }
>
> /**
> * Constructor
> * @param schemasFolderPath
> * @param classesToBeBound
> * @throws JAXBException
> */
> public ValidatingJAXBContext(String schemasFolderPath,
> Class... classesToBeBound) throws JAXBException {
> jaxbContext = JAXBContext.newInstance(classesToBeBound);
> this.schemasFolderPath = schemasFolderPath;
> }
>
>
> /**
> * Constructor
> * @param classesToBeBound
> * @param properties
> * @param schemasFolderPath
> * @throws JAXBException
> */
> public ValidatingJAXBContext(Class[] classesToBeBound,
> Map<String, Object> properties, String schemasFolderPath)
> throwsJAXBException {
> jaxbContext = JAXBContext.newInstance(classesToBeBound,
> properties);
> this.schemasFolderPath = schemasFolderPath;
> }
>
> /**
> * Creates Unmarshaller
> * @return Unmarshaller
> */
> @Override
> public Unmarshaller createUnmarshaller() throws JAXBException {
> Unmarshaller unmarshaller =
> jaxbContext.createUnmarshaller();
> MyValidationEventCollector vec = new
> MyValidationEventCollector();
> unmarshaller.setEventHandler(vec);
> unmarshaller.setSchema(getSchema());
> return unmarshaller;
> }
>
>
> /**
> * Creates Marshaller
> * @return Marshaller
> */
> @Override
> public Marshaller createMarshaller() throws JAXBException {
> return jaxbContext.createMarshaller();
> }
>
> /**
> * Creates Validator
> * @return Validator
> */
> @SuppressWarnings("deprecation")
> @Override
> public Validator createValidator() throws JAXBException {
> return jaxbContext.createValidator();
> }
>
> /**
> * Loads schema
> * @return Schema
> */
> private Schema getSchema()
> {
> String schemaFilePath = schemasFolderPath +
> File.separator + "CreditMemo.xsd";
>
> Schema mySchema;
>
> SchemaFactory sf =
> SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
> try {
> File schemaFile = new File(schemaFilePath);
> System.out.println(schemaFile);
> mySchema = sf.newSchema(schemaFile);
> } catch (Exception saxe) {
> System.out.println("Error in loading schema
> located at " + schemaFilePath);
> saxe.printStackTrace();
> mySchema = null;
> }
>
> return mySchema;
> }
>
> }
>
>
> Regards,
> Prashanth Patnam| Module lead| Persistent Systems
> prashanth_patnam_at_persistent.co.in | Cell: +91 9032332242 |
> Innovation in software product design, development and delivery- www.persistentsys.com
>
> DISCLAIMER ========== This e-mail may contain privileged and
> confidential information which is the property of Persistent Systems
> Ltd. It is intended only for the use of the individual or entity to
> which it is addressed. If you are not the intended recipient, you
> are not authorized to read, retain, copy, print, distribute or use
> this message. If you have received this communication in error,
> please notify the sender and delete all copies of this message.
> Persistent Systems Ltd. does not accept any liability for virus
> infected mails.
>