users@jaxb.java.net

Re: JAXB plugin help

From: Kohsuke Kawaguchi <kk_at_kohsuke.org>
Date: Fri, 27 Oct 2006 11:37:14 -0700

Raj Pitchaimani wrote:
> Hi Kohsuke,
>
> I have been reading your blog and also looked at the various plugins that
> had mentioned in your blog.
>
> I'm trying to come with a plugin which should add annotations for
> validation.

Cool. Would you be interested in hosting this on
http://jaxb2-commons.dev.java.net/ ?

There has been substantial interest in this. I think other people might
be interested in contributing to this project.

>
> For example, lets take the following schema:
>
> ...
> <xs:attribute name="intValue" use="required">
> <xs:simpleType>
> <xs:restriction base="xs:int">
> <xs:minInclusive value="1"/>
> <xs:maxInclusive value="600"/>
> </xs:restriction>
> </xs:simpleType>
> </xs:attribute>
>
> <xs:attribute name="stringValue" use="required">
> <xs:simpleType name="FamilyId">
> <xs:restriction base="xs:string">
> <xs:minLength value="1"/>
> <xs:maxLength value="100"/>
> </xs:restriction>
> </xs:simpleType>
> </xs:attribute>
> ...
>
> In the code, I like to add the annotation like below:
>
> public class MyClass {
> public int intValue;
> public String stringValue;
>
> @Min(1)
> @Max(100)
> public int getIntValue() {
> return intValue;
> }
>
> @Length(min="1", max="100)
> public String getStringValue() {
> return stringValue;
> }
> }
>
> I'm already using these type annotations in our code but I would like to add
> these in the JAXB generated code as well, so I can validate the bean using
> our current annotation based validator.
>
> Is there a easy way to do this? Can you please give me small example of how
> to do this? If that works, I will do the rest.

Sure. The basic idea is to look at CPropertyInfo.getSchemaComponent(),
and then determine what facets you want to copy over.

Then use methods on JAnnotatable to put annotations into the generated code.

-- 
Kohsuke Kawaguchi