Hi,
yes, we have also considered using JSR 303, but the problem is that the
existing standard annotations do not provide exactly the same
expressivity as XSD facets.
A possible (incomplete) mapping from XSD facets to standard JSR 303
annotations could be:
* <enumeration value="X"/> -- (not needed, automatically handled by JAXB)
* <minInclusive value="X"/> -- @Min(X) or @DecimalMin("X")
* <maxInclusive value="X"/> -- @Max(X) or @DecimalMax("X")
* <pattern value="X"/> -- @Pattern(regexp="X")
The following facets cannot be mapped directly, but using a workaround:
* <fractionDigits value="X"/> -- @Digits(integer=a,fraction=X) ,
attribute "integer" is required, and has to be set to some value!
* <totalDigits value="X"/> -- @Digits(integer=a, fraction=b) , assuming
that X=a+b
* <length value="X"/> -- @Size(min=X, max=X) , both min and max need to
be set
* <minLength value="X"/> -- @Min(X) , problem: minLength is for strings
etc, whereas @Min is generally used for numeric values
* <maxLength value="X"/> -- @Max(X) , same problem as for minLength
The following facets cannot be mapped directly:
* <minExclusive value="X"/> -- no possibility to express exclusive value
* <maxExclusive value="X"/> -- no possibility to express exclusive value
* <whiteSpace value="X"/> -- no suitable annotation in JSR 303
However, JSR 303 is an extensible standard and allows for the definition
of new annotations and related validators. For the time being, we could
support the above mapping, but generally I believe that a new @Facets
annotation is preferable, otherwise the semantics are somewhat mixed up
and unclear. For instance, consider an application which assumes that
@Min is only used for numeric values. If we use @Min to specify the
"minLength" facet for a string, this application might then fail with an
exception when trying to cast the target value.
Another important extension (which I forgot to mention in my previous
post) is the support for explicit specification of the XSD attributes
"minOccurs" and "maxOccurs", for which we defined annotations @MinOccurs
and @MaxOccurs.
What is your opinion on that, did I forget to mention something or do
you have any other suggestions?
Regards,
Waldemar
Martin Grebac wrote:
> Hi,
> nice work. Wrt the annotations used, have you been thinking about using standard Bean Validation (JSR 303) annotations?
> MartiNG
>
> On Jan 2, 2011, at 7:37 PM, <hummer_at_infosys.tuwien.ac.at> <hummer_at_infosys.tuwien.ac.at> wrote:
>
>
>> Hi,
>>
>> apologies for my previous empty post.. ;)
>>
>> We are currently developing a framework for API coverage of Web
>> services, and one of the core issues in this project is the exact
>> specification of service interfaces in terms of the input/output
>> messages using XSD facets. To the best of our knowledge, annotations
>> for XSD facets are still not supported in JAXB, yet this seems to be an
>> often requested feature [1][2][3].
>>
>> We believe to have come up with a simple solution for annotating JAXB
>> classes with facets, and automatic generation of the resulting XSD
>> schema.
>>
>> We defined an @Facets annotation, which can be used, e.g., as follows:
>>
>> @XmlRootElement
>> public class Person {
>> @Facets(pattern="[a-zA-Z0-9]+")
>> public String username;
>> @Facets(minInclusive=18, maxInclusive=120)
>> public int age;
>> }
>>
>> The following 2 new lines of code have been added to the class
>> com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator:
>>
>> private Tree handleElementProp(final ElementPropertyInfo<T,C> ep) {
>> ...
>> if(!XsdFacetsGenerator.hasFacets(t, e)) // new line of code #1
>> writeTypeRef(e,t, "type");
>> ...
>> writeOccurs(e,isOptional,repeated);
>> XsdFacetsGenerator.addFacets(t, e); // new line of code #2
>> ...
>> }
>>
>> The class XsdFacetsGenerator contains the implementation to generate
>> the XSD code for the facet, e.g.:
>> <xs:element name="username">
>> <xs:simpleType>
>> <xs:restriction base="xs:string">
>> <xs:pattern value="[a-zA-Z0-9]+"/>
>> </xs:restriction>
>> </xs:simpleType>
>> </xs:element>
>>
>> In sum, the following extensions to JAXB RI would be required:
>> * new annotation class @javax.xml.bind.annotation.Facets
>> * new class com.sun.xml.bind.v2.schemagen.XsdFacetsGenerator
>> * 2 new lines of code in
>> com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator
>>
>> We still need to test the approach thoroughly, but the results so far
>> look promising. Are you interested in merging our code into the JAXB
>> RI, and if so, how should we proceed?
>>
>> Best regards,
>> Waldemar Hummer
>>
>> [1] http://jaxb.java.net/guide/Generating_Schema_that_you_want.html
>> [2] http://java.net/jira/browse/JAXB-392
>> [3]
>> http://old.nabble.com/forum/Search.jtp?query=facet&local=y&forum=13500&
>> daterange=0&startdate=&enddate=
>>