org.ws4d.java.schema
Class Element

java.lang.Object
  extended by org.ws4d.java.schema.NamedObject
      extended by org.ws4d.java.schema.AnyElement
          extended by org.ws4d.java.schema.Element
All Implemented Interfaces:
SchemaConstants, Any, Attributable

public class Element
extends AnyElement
implements Attributable

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

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

Framework

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

Details

The following examples will show how to use the element to create different XML Schema structures.

See Also:
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
 
Fields inherited from interface org.ws4d.java.constants.SchemaConstants
ATTRIBUTE_ABSTRACT, ATTRIBUTE_DEFAULT, ATTRIBUTE_FIXED, ATTRIBUTE_USE, ATTRIBUTE_XSINIL, ATTRIBUTE_XSITYPE, DOCUMENTATION_LANG, ELEMENT_ALL, ELEMENT_CHOICE, ELEMENT_DEFAULT, ELEMENT_FIXED, ELEMENT_MAXOCCURS, ELEMENT_MINOCCURS, ELEMENT_NILLABLE, ELEMENT_PARENT, ELEMENT_RESTRICTIONS, ELEMENT_SEQUENCE, ELEMENT_SUBSTITUTIONS, ELEMENT_UNIONS, FACET_ENUMERATION, FACET_FRACTIONDIGITS, FACET_LENGTH, FACET_MAXEXCLUSIVE, FACET_MAXINCLUSIVE, FACET_MAXLENGTH, FACET_MINEXCLUSIVE, FACET_MININCLUSIVE, FACET_MINLENGTH, FACET_PATTERN, FACET_TOTALDIGITS, FACET_WHITESPACE, LIST_ITEMTYPE, MAXOCCURS_UNBOUNDED, SCHEMA_ANNOTATION, SCHEMA_ANY, SCHEMA_ANYATTRIBUTE, SCHEMA_APP_INFO, SCHEMA_ATTRIBUTE, SCHEMA_ATTRIBUTEFORMDEFAULT, SCHEMA_ATTRIBUTEGROUP, SCHEMA_BASE, SCHEMA_COMPLEXCONTENT, SCHEMA_COMPLEXTYPE, SCHEMA_DOCUMENTATION, SCHEMA_ELEMENT, SCHEMA_ELEMENTFORMDEFAULT, SCHEMA_EXTENSION, SCHEMA_FACETS, SCHEMA_FORM, SCHEMA_GROUP, SCHEMA_IMPORT, SCHEMA_INCLUDE, SCHEMA_ITEMLIST, SCHEMA_ITEMTYPE, SCHEMA_LIST, SCHEMA_LOCATION, SCHEMA_MEMBERTYPES, SCHEMA_NAME, SCHEMA_NAMESPACE, SCHEMA_NONAMESPACESCHEMALOCATION, SCHEMA_NOTATION, SCHEMA_PUBLIC, SCHEMA_QUALIFIED, SCHEMA_REDEFINE, SCHEMA_REF, SCHEMA_RESTRICTION, SCHEMA_SCHEMA, SCHEMA_SIMPLECONTENT, SCHEMA_SIMPLETYPE, SCHEMA_STYPES, SCHEMA_SUBSTITUTIONGROUP, SCHEMA_SYSTEM, SCHEMA_TARGETNAMESPACE, SCHEMA_TYPE, SCHEMA_UNION, SCHEMA_UNQUALIFIED, SCHEMA_VALUE, SCHEMA_VALUEVECTOR, USE_OPTIONAL, USE_PROHIBITED, USE_REQUIRED, XMLSCHEMA_NAMESPACE, XMLSCHEMA_PREFIX, XSD_ALLMODEL, XSD_ANYATTRIBUTE, XSD_ANYELEMENT, XSD_ATTRIBUTE, XSD_ATTRIBUTEGROUP, XSD_CHOICEMODEL, XSD_COMPLEXTYPE, XSD_ELEMENT, XSD_EXTENDEDCOMPLEXCONTENT, XSD_EXTENDEDSIMPLECONTENT, XSD_GROUP, XSD_NOTATION, XSD_RESTRICTEDCOMPLEXCONTENT, XSD_RESTRICTEDSIMPLECONTENT, XSD_RESTRICTEDSIMPLETYPE, XSD_SCHEMA, XSD_SEQUENCEMODEL, XSD_SIMPLETYPE, XSI_NAMESPACE
 
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

Element

public Element(java.lang.String name,
               java.lang.String namespace)
Creates an element with the given name and namespace.

This constructor will generate an appropriate qualified name.

Parameters:
name - the name of the element.
namespace - the namespace.

Element

public Element(java.lang.String name,
               java.lang.String namespace,
               Type type)
Creates an element with given name, namespace and type.

This constructor will generate an appropriate qualified name.

Parameters:
name - the name of the element.
namespace - the namespace.
type - the type of the element.

Element

public Element(QName name)
Creates an element with given qualified name.

Parameters:
name - the qualified name of the element.

Element

public Element(java.lang.String elementName,
               Type type)
Creates an element with given element name and type.

Parameters:
name - the qualified name of the element.
type - the type of the element.

Element

public Element(Type type)
Creates an element with given type.

Parameters:
name - the qualified name of the element.
type - the type of the element.

Element

public Element(QName name,
               Type type)
Creates an element with given qualified name and type.

Parameters:
name - the qualified name of the element.
type - the type of the element.

Element

public Element(java.lang.String name,
               java.lang.String namespace,
               Type type,
               int min,
               int max)
Creates an element with given name, namespace and type.

This constructor will generated an appropriate qualified name.

Parameters:
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.

Element

public Element(QName name,
               Type type,
               int min,
               int max)
Creates an element with given qualified name, type and occurrences.

Parameters:
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.

Element

public Element(Element reference)
Creates a reference based on a specified element.

Parameters:
reference - the element which should be referenced.

Element

public Element(Element reference,
               int min,
               int max)
Creates a reference based on a specified element.

Parameters:
reference - the element which should be referenced.
min - the minimum occurrence of this element.
max - the maximum occurrence of this element.
Method Detail

getElementCount

public static int getElementCount()
Returns the number of elements created by the framework. This can be used for debug purposes.

Returns:
the number of elements created by the framework.

toString

public java.lang.String toString()
Overrides:
toString in class NamedObject

getSchemaIdentifier

public int getSchemaIdentifier()
Specified by:
getSchemaIdentifier in interface Any
Overrides:
getSchemaIdentifier in class AnyElement

getAttribute

public CustomAttributeValue getAttribute(QName name)
Description copied from interface: Attributable
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).

Specified by:
getAttribute in interface Attributable
Parameters:
name - the name of the attribute of which to query the value
Returns:
the value of the named attribute or null

setAttribute

public void setAttribute(QName name,
                         CustomAttributeValue value)
Description copied from interface: Attributable
Sets the value for the attribute with the specified name. Throws a java.lang.IllegalArgumentException in case name is null.

Specified by:
setAttribute in interface Attributable
Parameters:
name - the name of the attribute to set, must not be null
value - the value to set the named attribute to (may be null

setAttribute

public void setAttribute(QName name,
                         java.lang.String value)
Description copied from interface: Attributable
Sets the value 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)).

Specified by:
setAttribute in interface Attributable
Parameters:
name - the name of the attribute to set, must not be null
value - the value to set the named attribute to (may be null

getAttributes

public HashMap getAttributes()
Description copied from interface: Attributable
Returns all attributes explicitly set for this Attributable 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.

Specified by:
getAttributes in interface Attributable
Returns:
all already set attributes

setAttributes

public void setAttributes(HashMap attributes)
Description copied from interface: Attributable
Sets all attributes at once to those contained within argument attributes. 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.

Specified by:
setAttributes in interface Attributable
Parameters:
attributes - the new attributes to set

hasAttributes

public boolean hasAttributes()
Description copied from interface: Attributable
Returns true only if this instance has at least one attribute set. Returns false in any other case.

Specified by:
hasAttributes in interface Attributable
Returns:
true only if there is at least one attribute set within this instance

serializeAttributes

public void serializeAttributes(org.xmlpull.v1.XmlSerializer serializer)
                         throws java.io.IOException
Description copied from interface: Attributable
Serializes the attributes stored within this instance, if any.

Specified by:
serializeAttributes in interface Attributable
Parameters:
serializer - the serializer to which to send output
Throws:
java.io.IOException - in case writing to serializer fails for any reason

getType

public Type getType()
Returns the type of this element.

Overrides:
getType in class AnyElement
Returns:
the type of this element.

isFixed

public boolean isFixed()
Returns whether this element value is fixed or not.

Returns:
true if the value of this element cannot be changed, false otherwise.

getFixedValue

public java.lang.String getFixedValue()
Returns the fixed value for this element.

The fixed value cannot be changed inside a XML instance document.

Returns:
the fixed value for this element.

setSubstitutionGroup

public void setSubstitutionGroup(QName group)
Sets the name of the substitution group for this element.

This affects only elements which are root elements of a XML Schema.

Parameters:
group - the qualified name for the group.

getSubstitutionGroup

public QName getSubstitutionGroup()
Returns the name of the substitution group for this element.

Returns:
the qualified name of the group.

setType

public void setType(Type type)
Sets the type of the element.

Parameters:
type - the type of the element.

isNillable

public boolean isNillable()
Returns whether the instance of this element can handle nil values or not.

Returns:
true if the instance can handle nil values, false otherwise.

setNillable

public void setNillable(boolean nillable)
Set whether the instance of this element can handle nil values or not.

Parameters:
nillable - true if the instance can handle nil values, false otherwise.

getName

public QName getName()
Description copied from class: NamedObject
Returns the qualified name.

Overrides:
getName in class NamedObject
Returns:
the qualified name.

setName

public void setName(QName name)
Description copied from class: NamedObject
Sets the qualified name.

Overrides:
setName in class NamedObject
Parameters:
name - the qname to set.

isReference

public boolean isReference()
Returns true if the element is a reference for another schema object, false otherwise.

Returns:
true if the element is a reference for another schema object, false otherwise.

getReference

public org.ws4d.java.schema.Reference getReference()