随笔 - 458  文章 - 0  评论 - 11  阅读 - 39万

JAXB - XML Schema Types, Defining Subtypes

Although object orientation isn't a key feature of XML or the XML Schema language, it's still possible to apply the fundamental OO paradigm when designing a schema: inheritance. This is based on the schema element xsd:extension which lets you add both child elements and attributes to some elsewhere defined type acting as the base type. The example given below presents the components for defining a simple menu (this time it's for a graphical user interface) where menu entries may come in several flavours: simple items, check boxes, radio buttons and sub-menus.

复制代码
<xsd:complexType name="EntryType">
    <xsd:attribute name="Text" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="ItemType">
    <xsd:complexContent>
        <xsd:extension base="EntryType">
            <xsd:sequence>
                <xsd:element name="Command" type="xsd:string"/>
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="CheckBoxType">
    <xsd:complexContent>
        <xsd:extension base="ItemType">
            <xsd:attribute name="State" type="xsd:boolean"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="RadioButtonType">
    <xsd:complexContent>
        <xsd:extension base="ItemType">
            <xsd:attribute name="Group" type="xsd:string"/>            
            <xsd:attribute name="State" type="xsd:boolean"/>
            <xsd:attribute name="Value" type="xsd:string"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="MenuType">
    <xsd:complexContent>
        <xsd:extension base="EntryType">
            <xsd:choice maxOccurs="unbounded">
                <xsd:element name="Item" type="ItemType"/>
                <xsd:element name="CheckBox" type="CheckBoxType"/>
                <xsd:element name="RadioButton" type="RadioButtonType"/>
                <xsd:element name="Menu" type="MenuType"/>
            </xsd:choice>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
复制代码

The base class EntryType is extended in several ways:

  • ItemType adds a command definition to the base type.
  • CheckBoxType extends ItemType, inheriting the command and adding an attribute for the initial state of the check box.
  • RadioButtonType is another extension of ItemType, again adding some attributes. Group is the button group's identification, and Value defines the string to be used for indicating the selection.
  • MenuType reflects the recursive structure of menus by being both another subclass of ItemType (so that it may represent cascades) as well as a container for all kinds of menu entries, including itself.

Before we look at the generated Java code, we should note that the definition of MenuType isn't quite what an OO aficionado would expect. After all the pains taken to establish this little class hierarchy, one still must explicitly put all the subclasses into the choice list. Using just the supertype EntryType in an xsd:sequence would result in very dull menus.

The JAXB compiler, however, rewards you with a set of class definitions that uses extends wherever we have xsd:extension in the schema. A look at the (much abbreviated) code shows the expected inheritance structure.

复制代码
public class EntryType {
    protected String text;

    // ...(getText, setText)
}

public class ItemType extends EntryType {
    protected String command;

    // ...(getCommand, setCommand)
}

public class CheckBoxType extends ItemType {
    protected Boolean state;

    // ...(isState, setState)
}

public class RadioButtonType extends ItemType {
    protected String group;
    protected Boolean state;
    protected String value;

    // ...(getters and setters)
}
复制代码

Finally there is MenuType, which contains a java.util.List<EntryType>. JAXB has briefly reflected upon the element types bunched into the choice and has, literally, reverse engineered the common superclass. A reminder that the list is a mixture is embedded in the name of the getter which is made up from the first three tags.

复制代码
public class MenuType extends EntryType {
    protected List<EntryType> itemOrCheckBoxOrRadioButton;

    public List<EntryType> getItemOrCheckBoxOrRadioButton() {
        if (itemOrCheckBoxOrRadioButton == null) {
            itemOrCheckBoxOrRadioButton = new ArrayList<EntryType>();
        }
        return this.itemOrCheckBoxOrRadioButton;
    }
}
复制代码

 

posted on   huey2672  阅读(243)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
历史上的今天:
2015-05-18 Spring - IoC(12): 属性占位符
2015-05-18 Tomcat - 设置 HTTP 基本认证
2015-05-18 HTTP - 基本认证
2015-05-18 HTTP - 首部
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示