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:
ItemTypeadds 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;
}
}
JAXB - XML Schema Types, Defining Subtypes的更多相关文章
- JAXB - XML Schema Types, Defining an Enumeration
If you want a data type that enumerates discrete values you should use a restriction of the schema t ...
- JAXB - XML Schema Types, Defining Types for XML Elements With Content
Content: A Value The content of an XML element may be some value, or one or more subordinate element ...
- JAXB - XML Schema Types, Defining Types for XML Elements Without Content
Types for XML elements are constructed using xsd:complexType, even if they do not have content. The ...
- JAXB - XML Schema Types, Date and Time
JAXB binds all three of the schema types xsd:date, xsd:time and xsd:dateTime to XMLGregorianCalendar ...
- JAXB - XML Schema Types, Binary Data
Data that has no "natural" representation with printable characters must, for inclusion in ...
- XML Schema and XMLspy notes
Introduction An xml documents consists of elements, attributes and text. There are two structures in ...
- [BTS] System.Xml.Schema.XmlSchemaException: The complexType has already been declared when generate IDoc schema.
I use wcf-sap adapter for generate the schema of IDoc that named "YHREMPMASTER". but throw ...
- [CXF REST标准实战系列] 一、JAXB xml与javaBean的转换
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.不认识到犯错,然后得到 ...
- [CXF REST标准实战系列] 一、JAXB xml与javaBean的转换(转)
转自:[CXF REST标准实战系列] 一.JAXB xml与javaBean的转换 文章Points: 1.不认识到犯错,然后得到永久的教训. 2.认识JAXB 3.代码实战 1.不认识到犯错,然后 ...
随机推荐
- Magento 前台的logo更改
进入后台: 系统-配置, 然后选择左栏的“设计”, 选择右栏的“页眉”里面, 一般logo的路径在: skin/frontend/base/default/images/media/logo.png ...
- 4.1Reduction模型
1. Reduction Reduction是一种广泛使用的计算模型,特别是在并行计算领域.简单地来说,Reduction就是一系列的划分(Partition)和汇总(Summarize)操作的集合: ...
- CentOS 6.5 安装配置VSFTP
1.下载安装VSFTP 首先查看当前系统是否已经安装VSFTP,若未安装则使用yum安装. chkconfig --list | grep vsftpd #查看是否安装 yum install vsf ...
- 如何搭建ss服务器
由于是Red Hat系统,缺少epel包,需要先安装该包才能安装python-pip,命令如下: rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x8 ...
- hdoj 2829 Lawrence 四边形不等式优化dp
dp[i][j]表示前i个,炸j条路,并且最后一个炸在i的后面时,一到i这一段的最小价值. dp[i][j]=min(dp[i][k]+w[k+1][i]) w[i][j]表示i到j这一段的价值. # ...
- Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 【Stage3D学习笔记续】真正的3D世界(一):透视矩阵
如果各位看官跟着我的学习笔记一路看过来的话,一定会吐槽我的,这都是什么3D啊?从头到尾整个都是在使用GPU绘制一堆2D图像而已,的确,之前我们一直使用正交矩阵利用GPU加速来实现2D世界的展示,算不上 ...
- [置顶] MapReduce 编程之 倒排索引
本文调试环境: ubuntu 10.04 , hadoop-1.0.2 hadoop装的是伪分布模式,就是只有一个节点,集namenode, datanode, jobtracker, tasktra ...
- Windows Azure Website类别、限制条件
在Azure中Website是一个典型的SAAS,用户可以很容易地搭建自己想要的网站,可以基于多种编程语言(ASP.NET/ASP/PHP/Node.JS等)和网站框架. Website有三种分类分别 ...
- MySQL的安装——源码方式(实验环境下测试用,真实环境请忽略此文)
#虚拟机是最初的的系统,我们在虚拟机里安装scp [root@serv01 ~]# yum install /usr/bin/scp -y #安装过程略 #我们拷贝MySQL的源码包到目标机的/roo ...