com.skaringa.javaxml.serializers
Class AbstractSerializer

java.lang.Object
  extended by com.skaringa.javaxml.serializers.AbstractSerializer
All Implemented Interfaces:
ComponentSerializer
Direct Known Subclasses:
ArraySerializer, CollectionSerializer, DateSerializer, LocaleSerializer, MapEntrySerializer, MapSerializer, ObjectSerializer, PrimitiveTypeSerializer, SetSerializer

public abstract class AbstractSerializer
extends java.lang.Object
implements ComponentSerializer

Abstract class that provides methods common for all serializers.


Field Summary
protected static java.lang.reflect.Field[] FIELD_TEMPLATE
          This is used down in getFieldsToSerialize as a template for toArray().
 
Constructor Summary
AbstractSerializer()
           
 
Method Summary
static java.lang.reflect.Field getField(java.lang.Object obj, java.lang.String fieldName)
          Get a field of an object.
static java.lang.reflect.Field[] getFieldsToSerialize(java.lang.Class type, java.util.Comparator fieldComparator)
          Get all fields to serialize for a type.
static java.lang.Class getFieldType(java.lang.Object obj, java.lang.String fieldName)
          Get the type of a field of an object.
protected  void printEncodedStr(java.lang.String text, java.io.PrintStream output)
           
protected  void startElement(java.lang.Object obj, java.lang.String type, java.lang.String name, java.util.Map propertyMap, DocumentOutputHandlerInterface output)
          Write an element start tag to the output.
protected static java.lang.String subst(java.lang.String target, java.lang.String oldString, java.lang.String newString)
          Substitute all occurences of oldString in target by newString.
protected  void writeXMLCollectionDef(java.lang.String componentElementName, java.lang.Class componentElementType, DocumentOutputHandlerInterface output)
          Write the XML schema definition for a collection.
protected  void writeXMLExtensionDef(DocumentOutputHandlerInterface output, java.lang.String baseType)
          Write the XML schema definition for a type that only extends a base type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.skaringa.javaxml.serializers.ComponentSerializer
addUsedClasses, endDeserialize, getXMLTypeName, serialize, setMember, startDeserialize, toJson, writeXMLTypeDefinition
 

Field Detail

FIELD_TEMPLATE

protected static final java.lang.reflect.Field[] FIELD_TEMPLATE
This is used down in getFieldsToSerialize as a template for toArray().

Constructor Detail

AbstractSerializer

public AbstractSerializer()
Method Detail

startElement

protected final void startElement(java.lang.Object obj,
                                  java.lang.String type,
                                  java.lang.String name,
                                  java.util.Map propertyMap,
                                  DocumentOutputHandlerInterface output)
                           throws SerializerException
Write an element start tag to the output. It includes test for null and setting the type attribute.

Parameters:
obj - The object to write.
type - The type of the object.
name - The name of the object.
propertyMap - The output properties.
output - The handler used to write the output.
Throws:
SerializerException - If the element can't be written.

getFieldType

public static java.lang.Class getFieldType(java.lang.Object obj,
                                           java.lang.String fieldName)
Get the type of a field of an object.

Parameters:
obj - The object.
fieldName - Then field name.
Returns:
The type of the field or null if the field doesn't exist.

getField

public static java.lang.reflect.Field getField(java.lang.Object obj,
                                               java.lang.String fieldName)
                                        throws java.lang.NoSuchFieldException
Get a field of an object. The field is identified by its name.

Parameters:
obj - The object.
fieldName - Then field name.
Returns:
The field.
Throws:
java.lang.NoSuchFieldException - If the object doesn't have a field with the specified name.

getFieldsToSerialize

public static java.lang.reflect.Field[] getFieldsToSerialize(java.lang.Class type,
                                                             java.util.Comparator fieldComparator)
Get all fields to serialize for a type. These are all fields that are not internal, static, nor transient.

Parameters:
type - The type.
fieldComparator - The comparator used to order the fields.
Returns:
Array of fields to serialize.

writeXMLCollectionDef

protected final void writeXMLCollectionDef(java.lang.String componentElementName,
                                           java.lang.Class componentElementType,
                                           DocumentOutputHandlerInterface output)
                                    throws SerializerException
Write the XML schema definition for a collection.

Parameters:
componentElementName - The name of the elements of the collection.
componentElementType - The type of the elements of the collection.
output - The handler used to write the output.
Throws:
SerializerException - If the definition can't be written.

writeXMLExtensionDef

protected final void writeXMLExtensionDef(DocumentOutputHandlerInterface output,
                                          java.lang.String baseType)
                                   throws SerializerException
Write the XML schema definition for a type that only extends a base type.

Parameters:
output - The handler used to write the output.
baseType - The XML type name of the base type.
Throws:
SerializerException - If the definition can't be written.

printEncodedStr

protected void printEncodedStr(java.lang.String text,
                               java.io.PrintStream output)

subst

protected static java.lang.String subst(java.lang.String target,
                                        java.lang.String oldString,
                                        java.lang.String newString)
Substitute all occurences of oldString in target by newString.

Parameters:
target - The target of the substitution.
oldString - The old string to be replaced.
newString - The new string.
Returns:
The target with all occurences of oldString replaced by newString.