users@jaxb-verification.java.net

Re: Custom Data Type Support

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Fri, 17 Mar 2006 10:43:35 +0100

Hi Marcus,

> I have been using jaxb-verification extensively in my internal projects
> and it's revolutionised our error reporting, so firstly a big thank-you.

Thanks for the feedback.

> Now, I'm considering introducing it into a customer project but have
> come across a limitation which I want to understand a bit more before
> deciding what to do.

First of all, jaxb-verification is a prequel of the jaxbvalidation
project. The latter is a bit refactored and extended. I suggest that you
move to jaxbvalidation (must not be complex), since all the further
developments will take place there.

> My customer wants the contents of the XML elements to be trimmed (i.e.
> leading and trailing white removed). The schema whitespace support
> isn't good enough for me, because I don't want multiple spaces within
> the string to be reduced to a single space. Thus, I have created a
> custom datatype, as follows:
>
> <xsd:simpleType name="MyString">
> <xsd:annotation>
> <xsd:appinfo>
> <jxb:javaType name="java.lang.String"
>
> parseMethod="mypackage.DataTypeHandler.parseString"
>
> printMethod="mypackage.DataTypeHandler.printString"/>
> </xsd:appinfo>
> </xsd:annotation>
> <xsd:restriction base="xsd:string"/>
> </xsd:simpleType>
>
> And this appears to work fine as far as trimming the data is concerned.
> However, jaxb-verification doesn't appear to support the custom type and
> adds a comment to say so:
>
> java.lang.String realValue = ((java.lang.String) value);
> // Check primitive value
> // Custom type conversion is defined, no check is possible
>
> What I'd like it to do, is:
>
> java.lang.String realValue =
> ((java.lang.String)mypackage.DataTypeHandler.parseString(value));
> // Check primitive value
> {
> // Perform the check
> // Checking class com.sun.msv.datatype.xsd.MaxLengthFacet datatype
> de.fzi.dbs.verification.event.datatype.ValueProblem problem =
> null;
> if (((null == realValue)? 0 :realValue.length())>= 1) {
> // Value length is correct
> [...]
>
>
> Is this in anyway possible, or am I asking for something really silly
> that's just impossible?

The problem is that parse/print allow you converting string
representation of the value into just anything you like. Your
space-separated string could become java.util.List, for example.

jaxbvalidation in its turn checks real values stored in objects rather
than string representations. Therefore, when you customize,
jaxbvalidation will generally have no idea how to handle the real data.
I'll have to think about the solution. Seems like I have to use the
custom type handler to convert the value into String, then use standard
type handler to convert it back into the object of the expected type and
then check that value. Would this be fine for you?

Bye.
/lexi