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>
Probably. I’ve created a setup similar to yours.
Test.javapackage-info.javabuild.xml(snippet, based on OP’s input)test.xsd(output)(I’ve used the prefix
tnsbecause JAXB will generate and use that instead of any prefix that was specified in@XmlNs.)Given the files above XJC’s
SchemaGenproduces the desired results—if I have a required attribute, then it’ll be generated as such; if I setrequired=false, then it won’t.The main thing here is the
@XmlTypeannotation on theTestclass. You just can’t live without that if you handcraft your classes. (The@XmlRootElementisn’t necessary, so it depends on your use-case if you want it or not.) This tells JAXB which namespace does theTestclass (which represents a schema type by the way) belongs whenSchemaGenprocesses it.The
package-info.javaserves almost the same purpose but at a schema level. If a package contains this file (and the annotations shown above) andSchemaGenencounters, 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,SchemaGenuses 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 alwaysschema1.xsd(or similar).Multiple packages
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.javasnippet above—the namespace prefix used must betns, because of the JAXB limitation described earlier).schemagenof course needs to be ordered to compile both packages. In yourbuild.xmltheschemagentask’ssrcdirshould encompass both packages.If you have a structure similar to this
you can tell the
schemagentask to compile onlyAandClike thisOptional and required attributes
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
(I’ve found a mailing list thread regarding this same issue, maybe you’re interested in it.)