3

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.

Deelazee
  • 493
  • 2
  • 9
  • 18

1 Answers1

2

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

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Nice solution. Definitely highlights that XML/Schema is not OO. – Alan Escreet May 06 '11 at 06:59
  • Yeah, while doing my initial search I already found these two links you mention. However, I don't think this is what I need. I have one attribute that I want to have different values inside the different classes. Maybe I don't understand to well what is stated in the linked Stack Overflow topic. I'll edit the question with a bit more clear of the outcome I need. – Deelazee May 06 '11 at 07:31
  • I edited my initial question, maybe you can clear up for me if I can used your linked approach somehow... Much appreciated. – Deelazee May 06 '11 at 07:37
  • Thanks for the help Blaise, things are getting much clearer in my head now :) – Deelazee May 09 '11 at 07:05