|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.ws4d.java.schema.NamedObject
org.ws4d.java.schema.Type
org.ws4d.java.schema.ComplexType
public class ComplexType
This class allows object representation of XML Schema complex types.
Those types are part of the XML Schema definition and are used inside WSDL
documents to describe the content of a message. It is possible to define XML
Schema structures with the classes Schema
, Element
,
Attribute
, SimpleType
, ComplexType
, Group
and
AttributeGroup
. This is at least necessary to invoke SOAP operations
(like used in DPWS).
A complex type consists of a qualified name and the description of the
content structure.
XML Schema describes the structure of the content for a XML instance document. Each element is linked to a specific data type. XML Schema comes with built-in primitive data types like string, boolean, decimal and derived data types like byte, int, token and positiveInteger. It is also possible to define one's own derived data types. An XML Schema could looks like this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org"> <xs:complexType name="personType"> <xs:sequence> <xs:element name="firstname" type="xs:string" /> <xs:element name="lastname" type="xs:string" /> <xs:element name="age" type="xs:int" /> </xs:sequence> </xs:complexType> <xs:element name="person" type="personType" /> </xs:schema>
The XML Schema above defines a derived data type called personType which contains inner-elements. The derived data type is used by the element person. This XML schema allows the creation of the following XML instance document:
<?xml version="1.0"?> <person> <firstname>John</firstname> <lastname>Doe</lastname> <age>66</age> </person>
You can learn more about XML Schema at http://www.w3.org/XML/Schema
If you want to create the complex type described above, it is necessary to
create the derived data type too and use the primitive data type
string. If you can access predefined primitive data types with the
SchemaUtil.getSchemaType(String)
method.
The created code should look like this:
// get primitive data types Type xsString = SchemaUtil.getSchemaType("string"); Type xsInt = SchemaUtil.getSchemaType("int"); // create inner elements for personType Element firstname = new Element(new QName("firstname", "http://www.example.org"), xsString); Element lastname = new Element(new QName("lastname", "http://www.example.org"), xsString); Element age = new Element(new QName("age", "http://www.example.org"), xsInt); // create personType and add inner elements ComplexType personType = new ComplexType(new QName("personType", "http://www.example.org"), ComplexType.CONTAINER_SEQUENCE); personType.addElement(firstname); personType.addElement(lastname); personType.addElement(age); // create element Element person = new Element(new QName("person", "http://www.example.org"), personType);
The following examples will show how to use the complex type to create different XML Schema structures.
Different model groups allow the assignment of an order to the content structure. The content order can be all, sequence or choice. The all model group does not restrict the order of the content. The sequence model group requires the correct order of the content. The choice model group allows only one element to be chosen for content.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org"> <element name="all" type="allType" /> <element name="sequence" type="sequenceType" /> <element name="choice" type="choiceType" /> <xs:complexType name="allType"> <xs:all> <xs:element name="a" type="xs:string" /> <xs:element name="b" type="xs:string" /> </xs:all> </xs:complexType> <xs:complexType name="sequenceType"> <xs:sequence> <xs:element name="a" type="xs:string" /> <xs:element name="b" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="choiceType"> <xs:choice> <xs:element name="a" type="xs:string" /> <xs:element name="b" type="xs:string" /> </xs:choice> </xs:complexType> </xs:schema>
The XML Schema listed above allows three different elements to be used as
root element for an XML instance. In case of the element "all", the inner
elements do not need any order. The order can be: a, b or b, a.
<?xml version="1.0"?> <all> <a>some text</a> <b>more text</b> </all>
<?xml version="1.0"?> <all> <b>more text</b> <a>some text</a> </all>In case of the element "sequence", the inner elements can only be used in the given order. The order can be: a, b
<?xml version="1.0"?> <sequence> <a>some text</a> <b>more text</b> </sequence>In case of the element "choice", one element must be chosen from the given elements. The elements can be: a or b, not both.
<?xml version="1.0"?> <choice> <a>some text</a> </choice>
<?xml version="1.0"?> <choice> <b>more text</b> </choice>
The framework supports the all, sequence and choice model groups.
// get primitive data types Type xsString = SchemaUtil.getSchemaType("string"); // create inner elements for the complex types Element a = new Element(new QName("a", "http://www.example.org"), xsString); Element b = new Element(new QName("b", "http://www.example.org"), xsString); // create some complex type (all) and add inner elements ComplexType allType = new ComplexType(new QName("allType", "http://www.example.org"), ComplexType.CONTAINER_ALL); allType.addElement(a); allType.addElement(b); // create some complex type (sequence) and add inner elements ComplexType sequenceType = new ComplexType(new QName("sequenceType", "http://www.example.org"), ComplexType.CONTAINER_SEQUENCE); sequenceType.addElement(a); sequenceType.addElement(b); // create some complex type (choice) and add inner elements ComplexType choiceType = new ComplexType(new QName("choiceType", "http://www.example.org"), ComplexType.CONTAINER_CHOICE); choiceType.addElement(a); choiceType.addElement(b);
CONTAINER_ALL
,
CONTAINER_SEQUENCE
,
CONTAINER_CHOICE
Field Summary | |
---|---|
static int |
CONTAINER_ALL
This is the constant field value for the all model group. |
static int |
CONTAINER_CHOICE
This is the constant field value for the choice model group. |
static int |
CONTAINER_SEQUENCE
This is the constant field value for the sequence model group. |
Fields inherited from interface org.ws4d.java.schema.Any |
---|
ATTRIBUTE_NAME, ATTRIBUTE_TYPE, ATTRIBUTE_VALUE_FALSE, ATTRIBUTE_VALUE_TRUE, TAG_ANY, TAG_ANYATTRIBUTE |
Constructor Summary | |
---|---|
ComplexType(int containerType)
This constructor must be used when declaring inline types, which must not have a name. |
|
ComplexType(QName name,
int containerType)
This constructor must be used when declaring top-level types, which have a non-empty name. |
|
ComplexType(java.lang.String name,
java.lang.String namespace,
int containerType)
This constructor must be used when declaring top-level types, which have a non-empty name. |
Method Summary | |
---|---|
void |
addElement(Element e)
Adds an element to the enclosed container. |
Iterator |
elements()
Returns an iterator of elements from the enclosed container. |
ElementContainer |
getContainer()
Returns the enclosed container for this complex type. |
int |
getContainerMaxOccurs()
Returns the maximum occurrence for the enclosed container. |
int |
getContainerMinOccurs()
Returns the minimum occurrence for the enclosed container. |
Element |
getElementByName(QName name)
Returns an element with matching name from the container. |
Element |
getElementByName(java.lang.String name)
Returns an element with matching name from the container. |
int |
getElementCount()
Returns the element count in the enclosed container. |
int |
getSchemaIdentifier()
|
boolean |
hasElements()
Returns true if the enclosed container has element
definitions, false otherwise. |
void |
setContainerMaxOccurs(int max)
Sets the maximum occurrence for the enclosed container. |
void |
setContainerMinOccurs(int min)
Sets the minimum occurrence for the enclosed container. |
Methods inherited from class org.ws4d.java.schema.Type |
---|
addAttribute, addAttributeGroup, allAttributes, allowAnyAttribute, attributeGroups, attributes, denyAnyAttribute, getAttribute, getAttributeCount, getAttributeGroup, getAttributeGroupCount, getKownSubtypes, getTypeCount, hasAnyAttribute, hasAttributeGroups, hasAttributes, isComplexType |
Methods inherited from class org.ws4d.java.schema.NamedObject |
---|
checkNamespace, equals, getName, getParentSchema, hashCode, isAbstract, setAbstract, setName, toString |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int CONTAINER_ALL
Should be used to define complex types without relevant element order.
public static final int CONTAINER_SEQUENCE
Should be used to define complex types with relevant element order.
public static final int CONTAINER_CHOICE
Should be used to define complex types where one element must be chosen.
Constructor Detail |
---|
public ComplexType(java.lang.String name, java.lang.String namespace, int containerType)
name
- the name of the type.namespace
- the namespace.containerType
- the type of the enclosed container.CONTAINER_ALL
,
CONTAINER_SEQUENCE
,
CONTAINER_CHOICE
public ComplexType(int containerType)
containerType
- the type of the enclosed container.CONTAINER_ALL
,
CONTAINER_SEQUENCE
,
CONTAINER_CHOICE
public ComplexType(QName name, int containerType)
name
- the qualified name of the typecontainerType
- the type of the enclosed container.CONTAINER_ALL
,
CONTAINER_SEQUENCE
,
CONTAINER_CHOICE
Method Detail |
---|
public int getSchemaIdentifier()
public int getContainerMinOccurs()
The "minOccurs" attribute in XML Schema describes the minimum occurrence of the enclosed container inside the created XML instance document.
public int getContainerMaxOccurs()
The "maxOccurs" attribute in XML Schema describes the maximum occurrence of the enclosed container inside the created XML instance document.
public void setContainerMinOccurs(int min)
The "minOccurs" attribute in XML Schema describes the minimum occurrence of the enclosed container inside the created XML instance document.
min
- the minimum occurrence for the enclosed container.public void setContainerMaxOccurs(int max)
The "maxOccurs" attribute in XML Schema describes the maximum occurrence of the enclosed container inside the created XML instance document.
max
- the maximum occurrence for the enclosed container.public boolean hasElements()
true
if the enclosed container has element
definitions, false
otherwise.
true
if the enclosed container has element
definitions, false
otherwise.public void addElement(Element e)
e
- the element to add.public Element getElementByName(QName name)
name
- the qualified name of the element which should be returned.
public Element getElementByName(java.lang.String name)
This method will NOT verify the namespace.
name
- the name of the element which should be returned.
public int getElementCount()
public Iterator elements()
public ElementContainer getContainer()
SequenceContainer
,
AllContainer
,
ChoiceContainer
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |