tags:

views:

360

answers:

1

I have a hierarchy of Line<--SPLine, Line<--DID_Line Line<--TollFree etc. Then for each line there is a type of action. So I have it looking like this in the xsd:

<xsd:complexType name="line" abstract="true">
    <xsd:complexContent>
        <xsd:extension base="tns:executable">
            <xsd:sequence>
                ...
            </xsd:sequence>
            <xsd:attribute name="type" type="tns:line_type" />
            <xsd:attribute name="switch_type" type="tns:switch_type" />
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="toll_free">
    <xsd:complexContent>
        <xsd:extension base="tns:line">
            <xsd:sequence>
                ...
            </xsd:sequence>
            <xsd:attribute name="action" type="tns:basic_actions" />
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="did_line">
    <xsd:complexContent>
        <xsd:extension base="tns:line">
            <xsd:attribute name="action" type="tns:suspendable_actions"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="spLine">
    <xsd:complexContent>
        <xsd:extension base="tns:line">
            <xsd:attribute name="action" type="tns:suspendable_actions" />
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
<xsd:simpleType name="line_action" >
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="add" />
        <xsd:enumeration value="delete">
        </xsd:enumeration>
        <xsd:enumeration value="remove">
        <xsd:enumeration value="suspend" />
        <xsd:enumeration value="restore" />
        <xsd:enumeration value="update" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="basic_actions">
    <xsd:restriction base="tns:line_action">
        <xsd:enumeration value="add" />
        <xsd:enumeration value="remove" />
    </xsd:restriction>
</xsd:simpleType>

Of there are more action types and line types. But my mine issue is all the casting I have to do to get the action. So lets say someone sends me a spline and I want to get the action, well the web service accepts a line objects so I take that line object check the type and then do the appropriate cast. I would like to just move the action attribute to line and then eliminate the cast. The only thing that I can see that I can do is this:

...

<xsd:complexType name="toll_free">
    <xsd:complexContent>
        <xsd:extension base="tns:line">
            <xsd:sequence>...
            </xsd:sequence>
            <xsd:attribute ref="tns:basic_actions" />
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

But that just gives me a bunch of extra methods and I still need the cast. So how can I get this polymorphic method to work such that I can just say line.getAction and it will call the subclass to return the action that was set. If I have to turn the attribute to an element that is fine.

A: 

If you want to have a different set of restrictions per subtype you need to cast. You can drop the restrictions from the XSD (keep them in code only) and then put the attribute definition in the line type.

David Rabinowitz