Reputation: 1022
I have some problem while trying to use the schemagen tool from JAXB library to generate a Schema for my project. The problem is that the annotation @XmlAttribute
is not being parsed properlly.
- src
- teste
- entity
So, the problem is that for some classes, the flag required
in XmlAttribute
is being ignored completly by the schemagen task.
I'll paste here some examples of classes and the generated schema so you can understand what is going on
package teste.entity;
import javax.xml.bind.annotation.XmlAttribute;
public abstract class Class2 {
@XmlAttribute(required=false)
public String stringNotRequired;
@XmlAttribute(required=true)
public String stringRequired;
@XmlAttribute(required=false)
public int anotherIntNotRequired;
@XmlAttribute(required=true)
public int anotherIntRequired;
}
package teste.entity;
import javax.xml.bind.annotation.XmlAttribute;
public class Class1 extends Class2 {
@XmlAttribute(required=true)
public Integer integerRequired;
@XmlAttribute(required=false)
public Integer integerNotRequired;
@XmlAttribute(required=false)
public int intNotRequired;
@XmlAttribute(required=true)
public int intRequired;
public Class1() {
}
}
My package-info.java
@XmlSchema(xmlns = @XmlNs(prefix = "t", namespaceURI = "http://test.com"),
namespace = "http://test.com",
elementFormDefault = XmlNsForm.UNQUALIFIED,
attributeFormDefault = XmlNsForm.UNQUALIFIED)
package teste.entity;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
When I run the schemagen
task, I got this output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" version="1.0" targetNamespace="http://test.com" xmlns:t="http://test.com" xmlns:tns="http://test.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="class1">
<xs:complexContent>
<xs:extension base="tns:class2">
<xs:sequence/>
<xs:attribute name="integerRequired" type="xs:int" use="required"/>
<xs:attribute name="integerNotRequired" type="xs:int"/>
<xs:attribute name="intNotRequired" type="xs:int" use="required"/>
<xs:attribute name="intRequired" type="xs:int" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="class2" abstract="true">
<xs:sequence/>
<xs:attribute name="stringNotRequired" type="xs:string"/>
<xs:attribute name="stringRequired" type="xs:string" use="required"/>
<xs:attribute name="anotherIntNotRequired" type="xs:int" use="required"/>
<xs:attribute name="anotherIntRequired" type="xs:int" use="required"/>
</xs:complexType>
</xs:schema>
Here is my Ant Task
<target name="generate-schema" >
<path id="mycp">
<fileset dir="lib/jaxb/lib\">
<include name="*.jar"/>
</fileset>
<fileset dir="lib" >
<include name="*.jar"/>
</fileset>
<fileset dir="dist" >
<include name="*.jar"/>
</fileset>
</path>
<taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask">
<classpath refid="mycp"/>
</taskdef>
<mkdir dir="schema"/>
<schemagen srcdir="src/teste/entity" destdir="schema" >
<classpath refid="mycp"/>
<schema namespace="http://test.com" file="full.xsd" />
</schemagen>
</target>
Upvotes: 11
Views: 12000
Reputation: 10151
Do I need annotations on the classes for the namespace to get output correctly?
Probably. I've created a setup similar to yours.
Test.java
package test;
...
@XmlType(name = "test", namespace = "http://test.com", propOrder = "b")
@XmlRootElement(name = "test", namespace = "http://test.com")
public final class Test {
@XmlAttribute(required = false)
public String a;
@XmlElement
public String b;
public Test() {}
}
package-info.java
@XmlSchema(xmlns = @XmlNs(prefix = "tns", namespaceURI = "http://test.com"),
namespace = "http://test.com",
elementFormDefault = XmlNsForm.QUALIFIED,
attributeFormDefault = XmlNsForm.QUALIFIED)
package test;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
build.xml
(snippet, based on OP's input)<schemagen srcdir="<path-to-test-package>" destdir=".">
<classpath refid="<classpath-refid>" />
<schema namespace="http://test.com" file="test.xsd" />
</schemagen>
test.xsd
(output)<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema
attributeFormDefault="qualified"
elementFormDefault="qualified" version="1.0"
targetNamespace="http://test.com"
xmlns:tns="http://test.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="test" type="tns:test"/>
<xs:complexType name="test" final="extension restriction">
<xs:sequence>
<xs:element name="b" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="tns:a"/>
</xs:complexType>
<xs:attribute name="a" type="xs:string"/>
</xs:schema>
(I've used the prefix tns
because JAXB will generate and use that instead of any prefix that was specified in @XmlNs
.)
Given the files above XJC's SchemaGen
produces the desired results—if I have a required attribute, then it'll be generated as such; if I set required=false
, then it won't.
The main thing here is the @XmlType
annotation on the Test
class. You just can't live without that if you handcraft your classes. (The @XmlRootElement
isn't necessary, so it depends on your use-case if you want it or not.) This tells JAXB which namespace does the Test
class (which represents a schema type by the way) belongs when SchemaGen
processes it.
The package-info.java
serves almost the same purpose but at a schema level. If a package contains this file (and the annotations shown above) and SchemaGen
encounters, then it'll know that the classes (schema types) in that package belongs to the namespace specified in the package level annotations. (Again, if you doing things by hand this is a must.) Other than this, SchemaGen
uses the namespace declared in this file to send the output to the file you specified in <schema namespace="http://test.com" file="test.xsd" />
. Without it the generated file's name is always schema1.xsd
(or similar).
If you want to group several classes located in multiple packages into one namespace, then you'll have to apply the same package level annotations on all of the packages (like in the package-info.java
snippet above—the namespace prefix used must be tns
, because of the JAXB limitation described earlier).
schemagen
of course needs to be ordered to compile both packages. In your build.xml
the schemagen
task's srcdir
should encompass both packages.
If you have a structure similar to this
.
|-- x
| |-- A.java # JAXB
| |-- B.java # POJO
| `-- package-info.java # http://test.com
`-- y
|-- C.java # JAXB
`-- package-info.java # http://test.com
you can tell the schemagen
task to compile only A
and C
like this
<schemagen srcdir="." destdir="." >
<schema namespace="http://test.com" file="test.xsd" />
<include name="x/A.java" />
<include name="y/C.java" />
</schemagen>
There is a short section in the official JAXB tutorial on attributes, which unfortunately doesn't say a thing about primitive Java types and XML Schema types in particular.
The shortcoming you're experiencing isn't a JAXB defect in my opinion, rather a Java oddity: primitive types can't be null, which is good and bad at the same time.
You can solve this by changing your primitive attribute
@XmlAttribute
public int attribute;
@XmlAttribute
public Integer attribute;
(I've found a mailing list thread regarding this same issue, maybe you're interested in it.)
Upvotes: 9