Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB - flag 'required' from XmlAttribute being ignored on primitive types
    text
    copied!<p>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 <code>@XmlAttribute</code> is not being parsed properlly.</p> <pre><code>- src - teste - entity </code></pre> <p>So, the problem is that for some classes, the flag <code>required</code> in <code>XmlAttribute</code> is being ignored completly by the schemagen task.</p> <p>I'll paste here some examples of classes and the generated schema so you can understand what is going on</p> <pre><code>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() { } } </code></pre> <p>My package-info.java</p> <pre><code>@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; </code></pre> <p>When I run the <code>schemagen</code> task, I got this output:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;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"&gt; &lt;xs:complexType name="class1"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="tns:class2"&gt; &lt;xs:sequence/&gt; &lt;xs:attribute name="integerRequired" type="xs:int" use="required"/&gt; &lt;xs:attribute name="integerNotRequired" type="xs:int"/&gt; &lt;xs:attribute name="intNotRequired" type="xs:int" use="required"/&gt; &lt;xs:attribute name="intRequired" type="xs:int" use="required"/&gt; &lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="class2" abstract="true"&gt; &lt;xs:sequence/&gt; &lt;xs:attribute name="stringNotRequired" type="xs:string"/&gt; &lt;xs:attribute name="stringRequired" type="xs:string" use="required"/&gt; &lt;xs:attribute name="anotherIntNotRequired" type="xs:int" use="required"/&gt; &lt;xs:attribute name="anotherIntRequired" type="xs:int" use="required"/&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>Here is my Ant Task</p> <pre><code>&lt;target name="generate-schema" &gt; &lt;path id="mycp"&gt; &lt;fileset dir="lib/jaxb/lib\"&gt; &lt;include name="*.jar"/&gt; &lt;/fileset&gt; &lt;fileset dir="lib" &gt; &lt;include name="*.jar"/&gt; &lt;/fileset&gt; &lt;fileset dir="dist" &gt; &lt;include name="*.jar"/&gt; &lt;/fileset&gt; &lt;/path&gt; &lt;taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask"&gt; &lt;classpath refid="mycp"/&gt; &lt;/taskdef&gt; &lt;mkdir dir="schema"/&gt; &lt;schemagen srcdir="src/teste/entity" destdir="schema" &gt; &lt;classpath refid="mycp"/&gt; &lt;schema namespace="http://test.com" file="full.xsd" /&gt; &lt;/schemagen&gt; &lt;/target&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload