common-util

local.tomas.common.xml
Class XMLTypeHandler

java.lang.Object
  extended by local.tomas.common.xml.XMLTypeHandler
Direct Known Subclasses:
DefaultTypeHandler, EnumTypeHandler, PrimitiveTypeHandler

public abstract class XMLTypeHandler
extends Object

The type handlers are used to convert specific types to and from XML.

Author:
tomas

Field Summary
protected  Class<?> arrayType
          The corresponding array type.
protected  XMLTypeHandler arrayTypeHandler
          Handler for corresponding array type.
protected  String name
          Type name
protected  Class<?> type
          The type
 
Constructor Summary
protected XMLTypeHandler(Class<?> type)
          Construct type handler from type.
protected XMLTypeHandler(Class<?> type, Class<?> arrayType)
          Construct type handler from type and array type.
 
Method Summary
protected abstract  int getArraySize(Object src)
          Get size of array.
 Class<?> getArrayType()
          Get type of array of base type.
 XMLTypeHandler getArrayTypeHandler()
          Get handler for type of array of base type.
 String getName()
          Get name of type handled by this instance.
 Class<?> getType()
          Get type handled by this instance.
 void marshall(String name, Class<?> type, Object src, XMLTokenGenerator gen)
          Create XML description of an object.
 void marshall(String name, int index, Class<?> type, Object src, XMLTokenGenerator gen)
          Create XML description of an object.
protected abstract  void marshallArrayElements(Class type, Object src, XMLTokenGenerator gen)
          Generate XML description of array elements.
protected  void marshallNullPrefix(String name, int index, Class<?> type, XMLTokenGenerator gen)
          Generate start token of null value.
protected  void marshallNullSuffix(String name, Class<?> type, XMLTokenGenerator gen)
          Generate end token of null value.
protected  void marshallPrefix(String name, int index, Class<?> type, Object src, XMLTokenGenerator gen)
          Generate start token.
 boolean marshallRef(String name, int index, Class<?> type, Object src, XMLTokenGenerator gen)
          Try to create an XML reference for an object.
protected  void marshallSuffix(String name, Class<?> type, XMLTokenGenerator gen)
          Generate end token.
protected abstract  void marshallValue(Class type, Object src, XMLTokenGenerator gen)
          Generate XML description of object value.
 boolean match(Class<?> type)
          Determine if type can be handled by this instance.
 Object newArray(Class<?> type, int size)
          Create an array of the type handled.
 Object newInstance(Class<?> type)
          Create an instance of the type handled.
 Object newInstance(Class<?> type, int size)
          Create an instance of the type handled with a given size.
 Object newInstance(Class<?> type, String value)
          Create an instance of the type handled initialized with a string value.
protected abstract  void setArrayElement(Object dest, int index, Object value)
          Set array component.
 void setProperty(Object dest, String name, int index, Object value)
          Set property of an object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

protected String name
Type name


type

protected Class<?> type
The type


arrayType

protected Class<?> arrayType
The corresponding array type.


arrayTypeHandler

protected XMLTypeHandler arrayTypeHandler
Handler for corresponding array type.

Constructor Detail

XMLTypeHandler

protected XMLTypeHandler(Class<?> type,
                         Class<?> arrayType)
Construct type handler from type and array type.

Parameters:
type - type
arrayType - array type

XMLTypeHandler

protected XMLTypeHandler(Class<?> type)
Construct type handler from type.

Parameters:
type - the type
Method Detail

getArrayType

public Class<?> getArrayType()
Get type of array of base type.

Returns:
array type

getArrayTypeHandler

public XMLTypeHandler getArrayTypeHandler()
Get handler for type of array of base type.

Returns:
handler of array type

getName

public String getName()
Get name of type handled by this instance.

Returns:
the name.

getType

public Class<?> getType()
Get type handled by this instance.

Returns:
the type.

match

public boolean match(Class<?> type)
Determine if type can be handled by this instance.

Parameters:
type - - test candidate
Returns:
true if type can be handled

newInstance

public Object newInstance(Class<?> type)
                   throws XMLException
Create an instance of the type handled.

Parameters:
type - - type of instance
Returns:
the instance
Throws:
XMLException

newInstance

public Object newInstance(Class<?> type,
                          String value)
                   throws XMLException
Create an instance of the type handled initialized with a string value.

Parameters:
type - - type of instance
value - - the string value
Returns:
the instance
Throws:
XMLException

newInstance

public Object newInstance(Class<?> type,
                          int size)
                   throws XMLException
Create an instance of the type handled with a given size.

Parameters:
type - - type of instance
size - - the size
Returns:
the instance
Throws:
XMLException

newArray

public Object newArray(Class<?> type,
                       int size)
                throws XMLException
Create an array of the type handled.

Parameters:
type - - component type
size - - size of array
Returns:
the array
Throws:
XMLException

marshall

public void marshall(String name,
                     int index,
                     Class<?> type,
                     Object src,
                     XMLTokenGenerator gen)
              throws XMLException
Create XML description of an object.

Parameters:
name - - XML tag name
index - - index attribute, negative values are omitted
type - - type of object
src - - the object
gen - - used to generate XML tokens
Throws:
XMLException

marshall

public void marshall(String name,
                     Class<?> type,
                     Object src,
                     XMLTokenGenerator gen)
              throws XMLException
Create XML description of an object.

Parameters:
name - - XML tag name
type - - type of object
src - - the object
gen - - used to generate XML tokens
Throws:
XMLException

marshallRef

public boolean marshallRef(String name,
                           int index,
                           Class<?> type,
                           Object src,
                           XMLTokenGenerator gen)
                    throws XMLException
Try to create an XML reference for an object.

Parameters:
name - - XML tag name
index - - index attribute, negative values are omitted
type - - type of object
src - - the object
gen - - used to generate XML tokens
Returns:
true if reference was created
Throws:
XMLException

marshallNullPrefix

protected void marshallNullPrefix(String name,
                                  int index,
                                  Class<?> type,
                                  XMLTokenGenerator gen)
                           throws XMLException
Generate start token of null value.

Parameters:
name - - XML tag name
type - - value type
gen - - used to generate XML tokens
Throws:
XMLException

marshallNullSuffix

protected void marshallNullSuffix(String name,
                                  Class<?> type,
                                  XMLTokenGenerator gen)
                           throws XMLException
Generate end token of null value.

Parameters:
name - - XML tag name
type - - value type
gen - - used to generate XML tokens
Throws:
XMLException

marshallPrefix

protected void marshallPrefix(String name,
                              int index,
                              Class<?> type,
                              Object src,
                              XMLTokenGenerator gen)
                       throws XMLException
Generate start token.

Parameters:
name - - token name
index - - index attribute, negative values are omitted
type - - value type
src - - value
gen - - used to generate XML tokens
Throws:
XMLException

marshallSuffix

protected void marshallSuffix(String name,
                              Class<?> type,
                              XMLTokenGenerator gen)
                       throws XMLException
Generate end token.

Parameters:
name - - token name
type - - type of object
gen - - used to generate XML tokens
Throws:
XMLException

marshallValue

protected abstract void marshallValue(Class type,
                                      Object src,
                                      XMLTokenGenerator gen)
                               throws XMLException
Generate XML description of object value.

Parameters:
type - - object type
src - - the object
gen - - used to generate XML tokens
Throws:
XMLException

getArraySize

protected abstract int getArraySize(Object src)
Get size of array.

Parameters:
src - - the array
Returns:
the size

marshallArrayElements

protected abstract void marshallArrayElements(Class type,
                                              Object src,
                                              XMLTokenGenerator gen)
                                       throws XMLException
Generate XML description of array elements.

Parameters:
type - - the array type
src - - the array
gen - - used to generate XML tokens
Throws:
XMLException

setArrayElement

protected abstract void setArrayElement(Object dest,
                                        int index,
                                        Object value)
                                 throws XMLException
Set array component.

Parameters:
dest - - the array
index - - the index within the array
value - - the value to assign
Throws:
XMLException

setProperty

public void setProperty(Object dest,
                        String name,
                        int index,
                        Object value)
                 throws XMLException
Set property of an object.

Parameters:
dest - - the object
name - - the property name
index - - a property index
value - - the new property value
Throws:
XMLException

common-util

Copyright © 2007 null. All Rights Reserved.