users@jaxb.java.net

Re: jaxb validation errors

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Thu, 26 Oct 2006 16:47:57 +0200

Hi.

> I am trying to get some better errors from the jaxb validation whilst
> constructing an xml document via the generated java.
>
> I have seen that in JAXB I can substitute the ValidationEventHandler
> with my own. I have tried to do so using :
>
> JAXBContext jaxbContext =
> JAXBContext.newInstance("generated");
> this.marshaller = jaxbContext.createMarshaller();
> this.marshaller.setEventHandler(new
> ValidationEventHandler ( ));
> this.logger.debug ( "validator : " +
> this.marshaller.getEventHandler().getClass().getName() );
>
>
> this.objectFactory = new generated.ObjectFactory();
> this.validator = jaxbContext.createValidator();
> this.validator.setEventHandler(new ValidationEventHandler
> ( ));
> this.logger.debug ( "validator : " +
> this.validator.getEventHandler().getClass().getName() );
>
>
> When I run the code the dubug shows the validator to be :
> 2006-10-26 16:27:10,640 DEBUG [it.jrc.fish.datacollection.xml.XLS2XML]
> - <validator : it.jrc.fish.datacollection.xml.ValidationEventHandler>
> 2006-10-26 16:27:10,723 DEBUG [it.jrc.fish.datacollection.xml.XLS2XML]
> - <validator : it.jrc.fish.datacollection.xml.ValidationEventHandler>
>
> from this code. However when I run the validator manually :
>
> // validate this part
> this.validator.validate(object);
>
> The debug I put in my validator does not show, and the stack dump from
> an invalid document shows that the validator is
>
> generated.impl.runtime.ValidatorImpl
>
> com.sun.xml.bind.serializer.AbortSerializationException: the value is
> not a member of the enumeration.
> at generated.impl.runtime.ValidationContext.reportEvent(Unknown
> Source)
> at generated.impl.runtime.ValidationContext.reportEvent(Unknown
> Source)
> at generated.impl.runtime.MSVValidator.writePendingText(Unknown
> Source)
> at generated.impl.runtime.MSVValidator.endElement(Unknown Source)
> at generated.impl.SamplingParamsTypeImpl.serializeBody(Unknown
> Source)
> at generated.impl.runtime.MSVValidator._validate(Unknown Source)
> at generated.impl.runtime.MSVValidator.validate(Unknown Source)
> at generated.impl.runtime.ValidationContext.validate(Unknown Source)
> at generated.impl.runtime.MSVValidator.childAsElementBody(Unknown
> Source)
> at generated.impl.runtime.MSVValidator.childAsBody(Unknown Source)
> at generated.impl.VESSELSAGETypeImpl.serializeBody(Unknown Source)
> at generated.impl.VESSELSAGEImpl.serializeBody(Unknown Source)
> at generated.impl.runtime.MSVValidator._validate(Unknown Source)
> at generated.impl.runtime.MSVValidator.validate(Unknown Source)
> at generated.impl.runtime.ValidationContext.validate(Unknown Source)
> at generated.impl.runtime.ValidatorImpl.validate(Unknown Source)
> at generated.impl.runtime.ValidatorImpl.validate(Unknown Source)
>
>
>
> Is there a way of substituting the validator to get more information,
> or of getting more error information from teh validation.
>
> The error :
> ' the value is not a member of the enumeration'
>
> is not enough for us, and I would like the element name and the values
> it could be and the value it has been set to.
>
> Thanks

Take a look at the http://jaxbvalidation.dev.java.net project. It does exactly
what you need. There's an add-on that generates object validators which can be
used instead of XML-style validation.

User guide is in process, take a look at sample projects instead:

https://jaxbvalidation.dev.java.net/nonav/repository/org/jvnet/jaxbvalidation/jaxbvalidation-sample/1.0.4/jaxbvalidation-sample-1.0.4-src.zip
https://jaxbvalidation.dev.java.net/nonav/repository/org/jvnet/jaxbvalidation/jaxbvalidation-sample-ant/1.0.4/jaxbvalidation-sample-ant-1.0.4-src.zip
Bye.
/lexi