The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » Metro and JAXB

Thread: need improved validation error details when marshalling

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 6 - Last Post: Apr 17, 2007 4:17 PM by: kohsuke
podenski

Posts: 7
need improved validation error details when marshalling
Posted: Feb 28, 2007 3:51 PM
  Click to reply to this thread Reply

I am using validation when marshalling. I start by creating new objects and then marshall them with a ValidationEventHandler setup. The validation works okay as far as it goes.

For example, here is the error message for a test case:

Line:Col[-1:-1]:cvc-type.3.1.3: The value 'ABC' of element 'zipcode' is not valid.
theInvalidObject: org.example.Address@419d05

I am able to successfully obtain the above information via a ValidationEventLocator. However, what I really would like is to be able to explicitly know 'which' object out the entire object graph is in error. Another way of saying this is that I would like to know the explicit XPath of the object/field along with the validation error.

The reason I need this is that I am working toward the ability to map the error back into a JSF error message that corresponds to a field on a web page. Of course this is more complicated when you have lists of things.

For example, if I have an order with 25 line items (each of which is an object itself with multiple fields), I would like to be able to point specifically at the offending item.

Although the validation provides the object instance, I don't think it would be all that efficient to have to walk the entire object graph looking for the offending object using '=='.

Doesn't the validation process know the XPath of the current object being validated against the XML Schema?

I did find an older jaxb project that appears to enhance validation, but it looks like it was designed only for JAXB v1, and likely wouldn't work with JAXB v2.

Please provide any pointers you might have as to how to obtain XPath for an invalid object field.

Thanks,

Pat Podenski

kohsuke

Posts: 3,962
Re: need improved validation error details when marshalling
Posted: Feb 28, 2007 4:31 PM   in response to: podenski
  Click to reply to this thread Reply

I wonder if ValidationEventLocator.getObject() reports anything useful, and I think you are saying that it does return the object.

If you want to know XPath, we need a filter that keeps track of the current location in terms of XPath.

jjj110200

Posts: 6
Re: need improved validation error details when marshalling
Posted: Feb 28, 2007 7:38 PM   in response to: kohsuke
  Click to reply to this thread Reply

I think the exact XPATH in ValidationEvent is really important so that the users can pinpoint the exact node in the content tree that causes the problem. I would imagine marshalling is top-down process, the marshaller should have the exact path to the object being marshalled. Maybe just add one more method in ValidationEvent with the signature public Object[] getXPath()

kohsuke

Posts: 3,962
Re: need improved validation error details when marshalling
Posted: Mar 1, 2007 3:57 PM   in response to: jjj110200
  Click to reply to this thread Reply

Your wish is my command...

https://jaxb2-commons.dev.java.net/xpath-tracker/

podenski

Posts: 7
Re: need improved validation error details when marshalling
Posted: Mar 1, 2007 7:40 PM   in response to: kohsuke
  Click to reply to this thread Reply

kohsuke,

I am impressed by your fast response!

Thanks for adding an XPathTracker! I will check it out tomorrow first thing.

Pat Podenski

ramrod1460

Posts: 5
Re: need improved validation error details when marshalling
Posted: Apr 13, 2007 10:11 AM   in response to: podenski
  Click to reply to this thread Reply

Does anyone know if XPathTracker can be used in conjunction with UNMarshalling ..if so, can someone point me to some sample code.

Thanks.

kohsuke

Posts: 3,962
Re: need improved validation error details when marshalling
Posted: Apr 17, 2007 4:17 PM   in response to: ramrod1460
  Click to reply to this thread Reply

Sure you can. You need to do a bit of SAX programming, though.

XPathTracker works as an XMLFilter, so you'll set up your SAX event pipeline like:

parser -> XPathTracker -> UnmarshallerHandler

There should be plenty of documentation on the web about how to do this kind of stuff in SAX.




 XML java.net RSS