users@jaxb.java.net

Re: ValidationEventHandler returns only parent object through ValidationEvent.getLocator().getObject()

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 04 Oct 2004 09:47:25 -0700

Aleksei Valikov wrote:
> One question: is it somehow possible to produce datatype validation
> expressions from the information contained in grammar? I mean,
> expression tree may be compiled into a a set of predicates (getA() must
> not be null, getB() can be null, getC() mut not be null and
> getC().size() must be greater than 2). But we also need to check
> datatypes (strings matching patterns, number values matching given
> ranges and so on). Could this information be somehow exported in a form
> of code model expressions or something similar?

The easiest approach is, as you mentioned, to serialize the datatype
object. I call this freeze-drying. Datatype objects are all
serializable. But this produces rather ugly code.

Another possibility is to introspect the
com.sun.msv.datatype.xsd.XSDatatype object and list up the facets it
have. This is more work, but in this way you can generate the code that
reconstructs the same datatype at the runtime.

The potential issues are:
        - internally facets are converted into objects, so you have to
          take those values and do convertToLexicalValue() to get their
          lexical form back.
        - QName needs a prefix -> URI association, making it tricker to
          do the above.
        - you've got 5 different kinds of facets to worry about.

That said, this approach would still probably work.


The last but more ambitious approach is, for the most of the major
datatypes, there's relatively straight-forward Java code to check the
value object directly (IOW, check Java int instead of java.lang.String
that looks like xs:int.)

For example, checking the range of int is as easy as "x<100". So if you
are willing to confine yourself to a subset of datatypes, then you can
actually try to generate the code that validates datatype values directly.

This would look really nice, but you would probably find it difficult to
support all the datatypes in this way. But then, that's probably OK in
practice, as not all the datatypes are used equally, and not all the
facets are used equally.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net