|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.ws4d.java.service.parameter.ParameterValue
public class ParameterValue
This class allows object representation of XML instance documents.
XML Schema describes the structure of content for XML instance documents.
Those definitions are used inside WSDL documents to describe a message's
content. 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 dedicated 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 look 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 person element defined above can be used as input
or output parameter of an operation. This will allow to use
this parameter within a service. As shown in the XML Schema part, an element
defined inside a XML Schema will be used to create XML instance documents.
The Framework allows to create those XML instance documents with this class.
A parameter value can be created from an element with the
createElementValue(Element)
method, or will be
pass-through within action invocation.
The ParameterValue
class allows nested structures like seen in
XML. An object of this class represents a single entry in a XML instance
document. The XML shown above, has an root element named "person" containing
three inner-elements, firstname, lastname and age.
This would lead to an parameter value with three nested inner-elements. The
ParameterValue
class allows to access the element directly and
any inner-element. To access the value of a parameter it is necessary
to check the type of the parameter and cast to the correct
implementation. The framework comes along with the implementation of
xs:string StringValue
, xs:QNAME QNameValue
and
xs:base64binary AttachmentValue
. It is possible to register own
implementation of XML Schema datatypes. If no implementation matches the
given data type a it will be handles as xs:string (fallback). The following
lines of code, will show the usage for the structure defined above:
// create ParameterValue from element ParameterValue personInstance = ParameterValue.createElementValue(person); // as person does not have any values to set, set the value of the // inner-elements. // direct access using the path (something like XPath). ParameterValue fname = personInstance.get("firstname"); ParameterValue lname = personInstance.setValue("lastname"); ParameterValue a = personInstance.setValue("age"); // check for correct type, cast and set the value if (fname.getValueType() == ParameterValue.TYPE_STRING) { StringValue firstname = (StringValue) fname; // set value for the string firstname.set("John"); } if (lname.getValueType() == ParameterValue.TYPE_STRING) { StringValue lastname = (StringValue) lname; // set value for the string lastname.set("Doe"); } // As there is not implementation for xs:integer we must use the xs:string // fallback here if (a.getValueType() == ParameterValue.TYPE_STRING) { StringValue age = (StringValue) a; // set value for the string lastname.set("66"); } // check for correct type, cast and set the value if (fname.getValueType() == ParameterValue.TYPE_STRING) { StringValue firstname = (StringValue) fname; // set value for the string String fn = firstname.get(); }
The path value used in different methods, allows direct access the inner-elements. Let us assume the XML content below:
<?xml version="1.0"?> <person> <firstname>John</firstname> <lastname>Doe</lastname> <age>66</age> <address> <street>Mainstreet 20</firstname> <city>Los Wochos</lastname> <phone>555-123-780-JOHNDOE</phone> <phone>555-123-780-XML</phone> </address> </person>
To access the elements like street, or even the both phone elements, it
necessary to extend the path. The path is always relative to the current
element. Every next entry in the path is divided by a slash (\). No set path
points the current element. You can use the #setValue(String)
and
#getValue()
methods for direct access without path. If an entry
exists more then once, like the phone element in the example above, an
specific element can be accessed by using an index. The index starts with 0.
Omitting the index is like using 0.
path syntax: child[index]/child-from-child[index]/child-from-child-from-chil[index]/ ... and so on.
// create ParameterValue from element ParameterValue personInstance = ParameterValue.createElementValue(person); // as person does not have any values to set, set the value of the // inner-elements. // direct access using the path (something like XPath). personInstance.get("firstname");
The ParameterUtil
class offers shortcut methods for the most common
cast, get and set operations for the build-in implementation of datatypes.
Element
,
Operation
,
StringValue
,
QNameValue
,
AttachmentValue
,
ParameterUtil
Field Summary | |
---|---|
static boolean |
ALLOW_NOINDEX
|
static int |
TYPE_ATTACHMENT
|
static int |
TYPE_COMPLEX
|
static int |
TYPE_QNAME
|
static int |
TYPE_STRING
|
static int |
TYPE_UNKNOWN
|
Constructor Summary | |
---|---|
ParameterValue()
|
Method Summary | |
---|---|
void |
add(ParameterAttribute attribute)
|
void |
add(ParameterValue value)
Adds an inner-element to this parameter value. |
void |
addAnyAttribute(QName name,
java.lang.String value)
|
Iterator |
attributeNames()
Returns an iterator over the qualified names of all attributes within this parameter value. |
Iterator |
attributes()
Returns an iterator of attributes for this parameter value. |
static int |
childCount(ParameterValue pv,
java.lang.String childLocalName)
Returns the number of direct children of pv with a
local name of childLocalName . |
Iterator |
children()
Returns an iterator of inner-elements for this parameter value. |
Iterator |
childrenFromType()
Returns an iterator of types for all inner-elements. |
ParameterValue |
createChild(java.lang.String path)
|
ParameterValue |
createChild(java.lang.String path,
Type instanceType)
|
static ParameterValue |
createElementValue(Element element)
Creates an XML instance document representation from a given XML Schema element. |
static ParameterValue |
createElementValue(Element element,
Type instanceType)
|
ParameterValue |
get(java.lang.String path)
|
java.lang.String |
getAttributeValue(java.lang.String attribute)
Returns the value of an attribute for this parameter value. |
Iterator |
getChildren(java.lang.String path)
Returns an iterator of inner-elements for the parameter value given by the path. |
int |
getChildrenCount()
Returns the number of inner-elements for the parameter value. |
int |
getChildrenCount(java.lang.String path)
Returns the number of inner-elements for the parameter value given by the path. |
ListIterator |
getChildrenList()
Returns an listiterator of inner-elements for this parameter value. |
Type |
getInstanceType()
Returns the instance type of this parameter value (in accordance to xsi:Type attribute). |
int |
getMaxOccurs()
Returns the the maximum occurrence for this parameter value. |
int |
getMinOccurs()
Returns the the minimum occurrence for this parameter value. |
QName |
getName()
Returns the name of the parameter value. |
List |
getNamespaces()
Returns the namespaces used by this parameter value. |
Type |
getType()
Returns the type of this parameter value. |
int |
getValueType()
Returns the VALUE TYPE for this parameter. |
boolean |
hasAttributes()
Returns true if this parameter value has attributes,
false otherwise. |
boolean |
hasChildren()
Returns true if this parameter value has inner-elements,
false otherwise. |
boolean |
hasChildrenFromType()
Returns true if this parameter value is based on a complex
type, false otherwise. |
boolean |
isNil()
Returns whether the XML instance nil value is set or not. |
boolean |
isOverriden()
Returns whether this parameter value is overridden or not. |
void |
overrideSerialization(java.lang.String value)
Allows to override the serialization of this parameter. |
static java.lang.String |
register(Type type,
java.lang.String clazz)
Register a class for a XML Schema datatype Type . |
void |
remove(ParameterValue value)
|
ParameterValue |
removeChild(java.lang.String path)
|
void |
resolveTypes(Schema s)
Resolve the types based on the given XML schema. |
void |
serialize(java.io.OutputStream out)
Serializes the parameter value into an XML instance document on a given stream. |
void |
serialize(org.xmlpull.v1.XmlSerializer serializer)
Serializes the parameter value with a XML serializer. |
void |
setAttributeValue(java.lang.String attribute,
java.lang.String value)
Sets the value of an attribute of this parameter value with given value. |
void |
setInstanceType(Type instanceType)
|
void |
setNil(boolean nil)
Set whether this parameter should carry values or not. |
java.lang.String |
toString()
|
static java.lang.String |
unregister(Type type)
Unregister a class for a XML Schema datatype Type . |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final boolean ALLOW_NOINDEX
public static final int TYPE_UNKNOWN
public static final int TYPE_COMPLEX
public static final int TYPE_STRING
public static final int TYPE_ATTACHMENT
public static final int TYPE_QNAME
Constructor Detail |
---|
public ParameterValue()
Method Detail |
---|
public List getNamespaces()
This method allows to collect all namespaces and use it if necessary.
List
of QName
.public static java.lang.String register(Type type, java.lang.String clazz)
Type
.
type
- the type which should be used.clazz
- the class which should be used to handle that type.
public static java.lang.String unregister(Type type)
Type
.
type
- the type which should be used.
public int getValueType()
A VALUE TYPE should be a unique representation of a
ParameterValue
implementation which allows to identify the
implementation and cast correctly.
public void overrideSerialization(java.lang.String value)
String
can contain anything but
SHOULD contain correct XML data. This method should be used for
debug purposes. A nested parameter can be overriden too.
Set to null
to disable the override.
value
- the value which should override the parameter serialization,
or null
if the parameter should not be
overridden.public boolean isOverriden()
true
the parameter serialization is overridden,
false
otherwise.public void setAttributeValue(java.lang.String attribute, java.lang.String value)
attribute
- the name of the attribute.value
- the value of the attribute.public java.lang.String getAttributeValue(java.lang.String attribute)
attribute
- the attribute to get the value of.
public void add(ParameterAttribute attribute)
public void addAnyAttribute(QName name, java.lang.String value)
public boolean hasAttributes()
true
if this parameter value has attributes,
false
otherwise.
true
if this parameter value has attributes,
false
otherwise.public Iterator attributes()
public Iterator attributeNames()
QName
instances, which represent the
names of this parameter value's attributespublic void setInstanceType(Type instanceType)
public void setNil(boolean nil)
nil
- true
this parameter will not have any values and
the XML instance nil will be set.
xsi:nil="true"public boolean isNil()
true
if the XML instance nil value
is set, false
otherwise.public Type getType()
public Type getInstanceType()
public int getMinOccurs()
The "minOccurs" attribute in XML Schema describes the minimum occurrence of this element inside the created XML instance document.
public int getMaxOccurs()
The "maxOccurs" attribute in XML Schema describes the maximum occurrence of this element inside the created XML instance document.
public QName getName()
public void add(ParameterValue value)
value
- the parameter value to add.public void remove(ParameterValue value)
public boolean hasChildren()
true
if this parameter value has inner-elements,
false
otherwise.
true
if this parameter value has inner-elements,
false
otherwise.public int getChildrenCount(java.lang.String path)
path
- the path to access the inner-element.
public int getChildrenCount()
path
- the path to access the inner-element.
public Iterator getChildren(java.lang.String path)
path
- the path to access the inner-element.
public boolean hasChildrenFromType()
true
if this parameter value is based on a complex
type, false
otherwise.
true
if this parameter value is based on a complex
type, false
otherwise.public Iterator childrenFromType()
public Iterator children()
public ListIterator getChildrenList()
public void resolveTypes(Schema s)
s
- the XML schema which contains the types for this parameter
value.public ParameterValue removeChild(java.lang.String path)
public ParameterValue createChild(java.lang.String path)
public ParameterValue createChild(java.lang.String path, Type instanceType)
public void serialize(org.xmlpull.v1.XmlSerializer serializer) throws java.io.IOException
serializer
- the XML serializer.
java.io.IOException
- throws an exception if the parameter value could not
be serialized correctly.public void serialize(java.io.OutputStream out) throws java.io.IOException
out
- the stream to serialize to.
java.io.IOException
- throws an exception if the parameter value could not
be serialized correctly.public ParameterValue get(java.lang.String path) throws java.lang.IndexOutOfBoundsException, java.lang.IllegalArgumentException
java.lang.IndexOutOfBoundsException
java.lang.IllegalArgumentException
public static ParameterValue createElementValue(Element element)
element
- the element to create the representation from.
public static ParameterValue createElementValue(Element element, Type instanceType)
public static int childCount(ParameterValue pv, java.lang.String childLocalName)
pv
with a
local name of childLocalName
. Returns 0
, if
either pv
or childLocalName
are
null
.
pv
- the parameter value instance, which of to count the direct
children with the given local namechildLocalName
- the local name of children to look for
pv
with the
specified local namepublic java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |