|
|||||||||
| 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.AnyElement
org.ws4d.java.schema.Element
public class Element
This class allows object representation of XML Schema elements.
Those elements 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).
An element consists of a qualified name (local part and namespace) and a
type.
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 like to create the element 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 element to create different XML Schema structures.
Element references allow to reference global elements.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org">
<xs:element name="a" type="xs:string" />
<xs:complexType name="b">
<xs:sequence>
<xs:element ref="a" />
</xs:sequence>
</xs:complexType>
</xs:schema>
// get primitive data types
Type xsString = SchemaUtil.getSchemaType("string");
// create element a
Element a = new Element(new QName("a", "http://example.org"), xsString);
// create reference for element a
Element aref = new Element(a);
// create type b
ComplexType b = new ComplexType(new QName("b", "http://example.org"), ComplexType.CONTAINER_SEQUENCE);
b.addElement(aref);
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org">
<xs:element name="a" type="xs:string" />
<xs:element name="b" substitutionGroup="a" />
</xs:schema>
// get primitive data types
Type xsString = SchemaUtil.getSchemaType("string");
// create element a
Element a = new Element(new QName("a", "http://example.org"), xsString);
// create a substituted element b
Element b = new Element(new QName("b", "http://example.org"));
b.setSubstitutionGroup(new QName("a", "http://example.org"));
Schema,
Attribute,
SimpleType,
ComplexType,
Group,
AttributeGroup| Field Summary |
|---|
| Fields inherited from interface org.ws4d.java.schema.Any |
|---|
ATTRIBUTE_NAME, ATTRIBUTE_VALUE_FALSE, ATTRIBUTE_VALUE_TRUE, TAG_ANY, TAG_ANYATTRIBUTE |
| Constructor Summary | |
|---|---|
Element(Element reference)
Creates a reference based on a specified element. |
|
Element(Element reference,
int min,
int max)
Creates a reference based on a specified element. |
|
Element(QName name)
Creates an element with given qualified name. |
|
Element(QName name,
Type type)
Creates an element with given qualified name and type. |
|
Element(QName name,
Type type,
int min,
int max)
Creates an element with given qualified name, type and occurrences. |
|
Element(java.lang.String name,
java.lang.String namespace)
Creates an element with the given name and namespace. |
|
Element(java.lang.String name,
java.lang.String namespace,
Type type)
Creates an element with given name, namespace and type. |
|
Element(java.lang.String name,
java.lang.String namespace,
Type type,
int min,
int max)
Creates an element with given name, namespace and type. |
|
Element(java.lang.String elementName,
Type type)
Creates an element with given element name and type. |
|
Element(Type type)
Creates an element with given type. |
|
| Method Summary | |
|---|---|
CustomAttributeValue |
getAttribute(QName name)
Returns the value of the attribute with the given name or
null, if this attribute is not available (or if its value is
actually explicitly set to null). |
HashMap |
getAttributes()
Returns all attributes explicitly set for this Attributable
instance. |
static int |
getElementCount()
Returns the number of elements created by the framework. |
java.lang.String |
getFixedValue()
Returns the fixed value for this element. |
QName |
getName()
Returns the qualified name. |
org.ws4d.java.schema.Reference |
getReference()
|
int |
getSchemaIdentifier()
|
QName |
getSubstitutionGroup()
Returns the name of the substitution group for this element. |
Type |
getType()
Returns the type of this element. |
boolean |
hasAttributes()
Returns true only if this instance has at least one
attribute set. |
boolean |
isFixed()
Returns whether this element value is fixed or not. |
boolean |
isNillable()
Returns whether the instance of this element can handle nil values or not. |
boolean |
isReference()
Returns true if the element is a reference for another
schema object, false otherwise. |
void |
serializeAttributes(org.xmlpull.v1.XmlSerializer serializer)
Serializes the attributes stored within this instance, if any. |
void |
setAttribute(QName name,
CustomAttributeValue value)
Sets the value for the attribute with the specified
name. |
void |
setAttribute(QName name,
java.lang.String value)
Sets the value for the attribute with the specified
name. |
void |
setAttributes(HashMap attributes)
Sets all attributes at once to those contained within argument attributes. |
void |
setName(QName name)
Sets the qualified name. |
void |
setNillable(boolean nillable)
Set whether the instance of this element can handle nil values or not. |
void |
setSubstitutionGroup(QName group)
Sets the name of the substitution group for this element. |
void |
setType(Type type)
Sets the type of the element. |
java.lang.String |
toString()
|
| Methods inherited from class org.ws4d.java.schema.AnyElement |
|---|
getMaxOccurs, getMinOccurs, setMaxOccurs, setMinOccurs |
| Methods inherited from class org.ws4d.java.schema.NamedObject |
|---|
checkNamespace, equals, getParentSchema, hashCode, isAbstract, setAbstract |
| Methods inherited from class java.lang.Object |
|---|
getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public Element(java.lang.String name,
java.lang.String namespace)
This constructor will generate an appropriate qualified name.
name - the name of the element.namespace - the namespace.
public Element(java.lang.String name,
java.lang.String namespace,
Type type)
This constructor will generate an appropriate qualified name.
name - the name of the element.namespace - the namespace.type - the type of the element.public Element(QName name)
name - the qualified name of the element.
public Element(java.lang.String elementName,
Type type)
name - the qualified name of the element.type - the type of the element.public Element(Type type)
name - the qualified name of the element.type - the type of the element.
public Element(QName name,
Type type)
name - the qualified name of the element.type - the type of the element.
public Element(java.lang.String name,
java.lang.String namespace,
Type type,
int min,
int max)
This constructor will generated an appropriate qualified name.
name - the name of the element.namespace - the namespace.type - the type of the element.min - the minimum occurrence of this element.max - the maximum occurrence of this element.
public Element(QName name,
Type type,
int min,
int max)
name - the qualified name of the element.type - the type of the element.min - the minimum occurrence of this element.max - the maximum occurrence of this element.public Element(Element reference)
reference - the element which should be referenced.
public Element(Element reference,
int min,
int max)
reference - the element which should be referenced.min - the minimum occurrence of this element.max - the maximum occurrence of this element.| Method Detail |
|---|
public static int getElementCount()
public java.lang.String toString()
toString in class NamedObjectpublic int getSchemaIdentifier()
getSchemaIdentifier in interface AnygetSchemaIdentifier in class AnyElementpublic CustomAttributeValue getAttribute(QName name)
Attributablename or
null, if this attribute is not available (or if its value is
actually explicitly set to null).
getAttribute in interface Attributablename - the name of the attribute of which to query the value
null
public void setAttribute(QName name,
CustomAttributeValue value)
Attributablevalue for the attribute with the specified
name. Throws a
java.lang.IllegalArgumentException in case name
is null.
setAttribute in interface Attributablename - the name of the attribute to set, must not be
nullvalue - the value to set the named attribute to (may be
null
public void setAttribute(QName name,
java.lang.String value)
Attributablevalue for the attribute with the specified
name. The value will be represented as plain String. It will
be wrapped within a new instance of StringAttributeValue. This
method throws a java.lang.IllegalArgumentException in case
name is null.
This is a shorthand for
setAttribute(name, new StringAttributeValue(value)).
setAttribute in interface Attributablename - the name of the attribute to set, must not be
nullvalue - the value to set the named attribute to (may be
nullpublic HashMap getAttributes()
AttributableAttributable
instance. Note that depending on the actual implementation the returned
reference may point at the 'life map', i .e. the actual storage for the
attributes. Thus, modifications to that map should be performed with care
and keeping this in mind.
getAttributes in interface Attributablepublic void setAttributes(HashMap attributes)
Attributableattributes. Note that depending on the actual implementation
it is possible that the map attributes points at may be used
for the actual internal storage of the attributes (i.e. without copying
it). That is why, after passing it to this method, modifications to this
map should be made with care. This method throws a
java.lang.IllegalArgumentException in cases where
attributes is null.
setAttributes in interface Attributableattributes - the new attributes to setpublic boolean hasAttributes()
Attributabletrue only if this instance has at least one
attribute set. Returns false in any other case.
hasAttributes in interface Attributabletrue only if there is at least one attribute set
within this instance
public void serializeAttributes(org.xmlpull.v1.XmlSerializer serializer)
throws java.io.IOException
Attributable
serializeAttributes in interface Attributableserializer - the serializer to which to send output
java.io.IOException - in case writing to serializer fails for
any reasonpublic Type getType()
getType in class AnyElementpublic boolean isFixed()
true if the value of this element cannot be changed,
false otherwise.public java.lang.String getFixedValue()
The fixed value cannot be changed inside a XML instance document.
public void setSubstitutionGroup(QName group)
This affects only elements which are root elements of a XML Schema.
group - the qualified name for the group.public QName getSubstitutionGroup()
public void setType(Type type)
type - the type of the element.public boolean isNillable()
true if the instance can handle nil
values, false otherwise.public void setNillable(boolean nillable)
nillable - true if the instance can handle
nil values, false otherwise.public QName getName()
NamedObject
getName in class NamedObjectpublic void setName(QName name)
NamedObject
setName in class NamedObjectname - the qname to set.public boolean isReference()
true if the element is a reference for another
schema object, false otherwise.
true if the element is a reference for another
schema object, false otherwise.public org.ws4d.java.schema.Reference getReference()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||