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;
}
}

最新文章

  1. 4、jvm内存回收&mdash;&mdash;器
  2. C#操作Word生成目录
  3. JMS之开源实现ActiveMQ
  4. tomcat server.xml配置文件 解析
  5. ANDROID_MARS学习笔记_S02_014_GSON解析JSON串为对象
  6. 【原创】史上最全的Android开发环境搭建
  7. qt 3d 绘图
  8. azkaben任务调度器
  9. 论文笔记(9):Multiscale Combinatorial Grouping
  10. [Swift]LeetCode239. 滑动窗口最大值 | Sliding Window Maximum
  11. Django使用第三方模块django-password-reset重置密码
  12. (转)支持 PS/2 与 USB 的键盘过滤驱动(可卸载)
  13. freeswitch笔记
  14. RPC通信原理
  15. Python学习笔记(一)简介总览
  16. Hadoop生态圈-Kafka的旧API实现生产者-消费者
  17. 训练赛第二场E题 Cottage Village
  18. @RunWith和 SpringJUnit4ClassRunner ----&gt;junit4和Spring一起使用
  19. Struts2(四)Action一接收参数
  20. C++编程思想

热门文章

  1. Python引用传值总结
  2. Navicat通过云主机内网连接阿里云RDS
  3. Java集合的小抄 Java初学者必备
  4. url 中文编解码
  5. [Objective-c 基础 - 1.1] OC类
  6. 爬去知乎百万用户信息之UserTask
  7. server2008服务器iis设置的一些经验
  8. How good software makes us stupid?
  9. SQLite使用教程11 表达式
  10. [转]Java 动态代理机制分析及扩展