I have the following XML code:
<stereotypes>
<stereotype1/>
<stereotype2/>
</stereotypes>
The problem is that I need a general attribute for each stereotype that has a different value for each stereotype type.
Actualy I'm not sure this is even posible or if I can implement such a thing.
I tried this using the following schema fragments (setting the path attribute). What I would like is to give this attribute a fixed value for each stereotype type. The goal would be to have the getPath generated on the AbstractStereotype class and to use it in a generic way. The problem is that I can't seem to find a way of defining the attribute value in the specific Stereotypes.
<xs:element name="stereotypes" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="stereotype1" type="Stereotype1" />
<xs:element name="stereotype2" type="Stereotype2"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="AbstractStereotype" abstract="true">
<xs:attribute name="path" type="amf-base:FQN" use="required"></xs:attribute>
</xs:complexType>
<xs:complexType name="Stereotype1">
<xs:complexContent>
<xs:extension base="AbstractStereotype">
<!-- <xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class1"/> -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Stereotype2">
<xs:complexContent>
<xs:extension base="AbstractStereotype">
<!-- <xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class2"/> -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
Any other sugestion that would let me to "have a getPath method generated on the AbstractStereotype class and to use it in a generic way" would be much appreciated.
EDIT: Maybe to be more clear of the outcome I need.
public abstract class AbstractStereotype {
public String getPath();
}
public class Stereotype1 extends AbstractStereotype {
public String getPath() {
return "Path1";
}
}
public class Stereotype2 extends AbstractStereotype {
public String getPath() {
return "Path2";
}
}
I need this because I want to treat these Stereotypes the same way:
public void someMethod() {
for(AbstractStereotype stereotype: getStereotypes()) {
System.out.println(stereotype.getPath());
}
}
As I said before not even sure this is possible using this approach.
Are you looking to use the path attribute as the inheritance indicator? If so the following will help:
I'm still not 100% sure I understand your use case but what about the following:
Stereotypes
import java.util.List;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Stereotypes {
private List<AbstractStereotype> sterotypes;
@XmlElementRef
public List<AbstractStereotype> getSterotypes() {
return sterotypes;
}
public void setSterotypes(List<AbstractStereotype> sterotypes) {
this.sterotypes = sterotypes;
}
}
AbstractStereotype
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlSeeAlso({Stereotype1.class, Stereotype2.class})
public abstract class AbstractStereotype {
@XmlAttribute
public abstract String getPath();
}
Stereotype1
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Stereotype1 extends AbstractStereotype {
public String getPath() {
return "Path1";
}
}
Stereotype2
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Stereotype2 extends AbstractStereotype {
public String getPath() {
return "Path2";
}
}
Demo
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Stereotypes.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Stereotypes stereotypes = (Stereotypes) unmarshaller.unmarshal(new File("input.xml"));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(stereotypes, System.out);
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<stereotypes>
<stereotype1/>
<stereotype2/>
</stereotypes>
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<stereotypes>
<stereotype1 path="Path1"/>
<stereotype2 path="Path2"/>
</stereotypes>
For More Information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With