users@jersey.java.net

Custom error message for JAXB validation

From: Prashanth Patnam <prashanth_patnam_at_persistent.co.in>
Date: Sat, 23 Oct 2010 16:37:32 +0530

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) throws JAXBException {
            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<mailto:prashanth_patnam_at_persistent.co.in> | Cell: +91 9032332242 |
Innovation in software product design, development and delivery- www.persistentsys.com<http://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.