users@jaxb.java.net

Re: JAXB generated Java Model + JXPath

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Wed, 03 Aug 2005 13:25:47 +0200

Hi.

> I would like to select instances from the JXAB generated Java Bean Tree
> by passing XPath.
>
> JXPath, an Apache's project allow exactly that.
>
> E.g:
>
> Address address = (Address)JXPathContext.newContext(vendor).
> getValue("locations[address/zipCode='90210']/address");
>
> Unfortunately JXPath's capabilites are limited to querrying a Java Bean
> Model which will fail even on moderately customized JAXB generated Java
> Bean Tree.
>
> /some-root/class is actually SomeRoot/Klass and so on.
>
> To extend JXPath
> <http://jakarta.apache.org/commons/jxpath/users-guide.html#Custom%20Pointers%20and%20Iterators>
> and seamlessly operate between XPath and JAXB's Java Bean Tree :
>
> 1. I would need to be aware of all the customizations "xjc" makes while
> generating the Java sources.
>
> What is a beatiful way to get this information ?

I have researched the possibility of applying JXPath to JAXB-generated
classes few month ago and I'm glad that someone follows the same route.
That would be a very nice extension. ;)

Here's a small roadmap for you.

The information about mapping between elements/attributes and
classes/properties is almost unavailable after the schema is compiled.
Therefore you'll have to write an add-on (a CodeAugmenter, actually) to
generate your very custom NodePointer, NodeIterator and
NodePointerFactory implementations alongside with classes generated by JAXB.

CodeAugmenters are provided with AnnotatedGrammar object instance, which
is richly filled with information on what is what. JAXB organizes the
processed schema as a tree of expressions and the easiest way to
traverse the tree (while generating Node** things you need) is to use
visitors, for instance ExpressionVisitor and JavaItemVisitor. They have
methods like onElement(ElementExp exp), where ElementExp will carry the
information you actually need to understand how was this element
actually called in the schema. From the other side, you have also
onClass/onField where you get ClassItem and FieldItem. Having these
objects you may determine how is this schema construct represented with
classes or properties.

Finally, you'll have to generate the code. For this purpose, JAXB uses
so-called "code model" which is a beatiful set of classes that represent
the syntax of Java code.

Needless to say, your task is uneasy, but very interesting. I would do
this myself if I had little more time.

I also recommend that you take a look at jaxbcommons and jaxbvalidation
projects on dev.java.net. They both have several add-on implemented.
jaxbvalidation might be especially interesting for you. It generates
"validator" classes that perform object-level validation against the
initial schema. You task is in many places similar.

Bye.
/lexi