Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB generated classes for fixed attribute in subtype
    primarykey
    data
    text
    <p>My java classes are being generated from xsd file.<br/> The goal I want to accomplish is to have some "known" properties based on the type of elements. For instance, I have a list of animals. After the xml is being parsed, I want to know in the code how many legs have my animals. But the number of legs is a characteristic of the animal type, so, in case I have a cat it will have 4 legs and the kangaroo will have 2 legs.<br/> In case I define the xsd like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt; &lt;xs:element name="Animals" type="animals" /&gt; &lt;xs:complexType name="animals"&gt; &lt;xs:sequence&gt; &lt;xs:element name="Animal" maxOccurs="unbounded" type="animal"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="animal" abstract="true"&gt; &lt;xs:simpleContent&gt; &lt;xs:extension base="xs:string"&gt; &lt;xs:attribute name="name" /&gt; &lt;/xs:extension&gt; &lt;/xs:simpleContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="catType"&gt; &lt;xs:simpleContent&gt; &lt;xs:extension base="animal"&gt; &lt;xs:attribute name="nbOfLegs" type="xs:integer" fixed="4" /&gt; &lt;/xs:extension&gt; &lt;/xs:simpleContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="kangarooType"&gt; &lt;xs:simpleContent&gt; &lt;xs:extension base="animal"&gt; &lt;xs:attribute name="nbOfLegs" type="xs:integer" fixed="2" /&gt; &lt;/xs:extension&gt; &lt;/xs:simpleContent&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>The generated classes are as expected (I removed the annotations):</p> <pre><code>public abstract class Animal { protected String name; public String getName() {return name;} public void setName(String value) {this.name = value;} } public class CatType extends Animal { protected BigInteger nbOfLegs; public BigInteger getNbOfLegs() { if (nbOfLegs == null) { return new BigInteger("4"); } else { return nbOfLegs; } } public void setNbOfLegs(BigInteger value) {this.nbOfLegs = value;} } </code></pre> <p>This way, in case the user sets the number of legs for cats in the xml, it can only be 4, and if he doesn't, in code I will receive 4 anyway. Similar for kangaroo I always receive 2 legs.<br/> But the problem with this approach is that I cannot use polymoprhism like this:</p> <pre><code>for(Animal animal : animals) { System.out.println(animal.getNbOfLegs()); } </code></pre> <p><br/>So I tried a different approach:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt; &lt;xs:element name="Animals" type="animals" /&gt; &lt;xs:complexType name="animals"&gt; &lt;xs:sequence&gt; &lt;xs:element name="Animal" maxOccurs="unbounded" type="animal"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="animal" abstract="true"&gt; &lt;xs:simpleContent&gt; &lt;xs:extension base="xs:string"&gt; &lt;xs:attribute name="name" /&gt; &lt;xs:attribute name="nbOfLegs" type="xs:integer"/&gt; &lt;/xs:extension&gt; &lt;/xs:simpleContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="catType"&gt; &lt;xs:simpleContent&gt; &lt;xs:restriction base="animal"&gt; &lt;xs:attribute name="nbOfLegs" type="xs:integer" fixed="4" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="kangarooType"&gt; &lt;xs:simpleContent&gt; &lt;xs:restriction base="animal"&gt; &lt;xs:attribute name="nbOfLegs" type="xs:integer" fixed="2" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleContent&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>The Animal class is generated as expected:</p> <pre><code>public abstract class Animal { protected String name; protected BigInteger nbOfLegs; public String getName() {return name;} public void setName(String value) {this.name = value;} public BigInteger getNbOfLegs() {return nbOfLegs;} public void setNbOfLegs(BigInteger value) {this.nbOfLegs = value;} } </code></pre> <p>But the generated CatType <strong>is empty</strong>.<br/> I expected it to be like this:</p> <pre><code>public class CatType extends Animal { @Override public BigInteger getNbOfLegs() { if (nbOfLegs == null) { return new BigInteger("4"); } else { return nbOfLegs; } } } </code></pre> <p>Is it possible to customize the bindings file in order to achieve the desired generated CatType class?<br/> Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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