com.objectwave.utility
Class ObjectFormatter

java.lang.Object
  |
  +--com.objectwave.utility.ObjectFormatter
Direct Known Subclasses:
ObjectFormatter

public class ObjectFormatter
extends java.lang.Object

This class converts data from one type to another. Used extensively for persistence manipulation.

Version:
$Id: ObjectFormatter.java,v 2.4 2002/07/31 15:55:23 dave_hoag Exp $
Author:
Dave Hoag

Nested Class Summary
static class ObjectFormatter.Test
          Unit tests.
 
Field Summary
static java.lang.String Native_Array_Delimiter
           
protected static java.lang.ThreadLocal outputStreams
           
static int zipBufferSize
           
 
Constructor Summary
ObjectFormatter()
           
 
Method Summary
protected  void bytesToString(byte[] data, java.lang.StringBuffer buffer)
          Convert the byte array into a string buffer representing the hexidecimal equivalent of the data.
 java.lang.String convertArray(java.lang.Object dataArray)
          Convert a native array into a string value.
protected  java.lang.Object convertPrimitive(java.lang.Class c, java.lang.String str)
          Convert the string value to the correct primitive type.
 java.lang.Object convertString(java.lang.Class c, java.lang.String str)
          When converting to an array, we need every element to be inserted into the array to be of the correct object type.
 java.lang.Object convertType(java.lang.Class c, java.lang.Object obj)
          Convert the object from its database representation to its actual value in the business object.
protected  java.lang.Object createArray(java.lang.Class clazz, java.lang.String vals)
          Convert a string value into a Native Array.
 java.lang.Number createNumber(java.lang.Class c, java.lang.Number value)
          If the SQL type was "NUMERIC", then we need to check to see if the Double that NUMBERIC values are converted to should really be an Integer or a Long.
protected  java.lang.Object createScalarType(java.lang.Class c, java.lang.String value)
          To support custom 'scalar' types (types that will be stored in the database as a string, but will in fact be an object in the object model) a ScalarFactory was introduced.
protected  void formatDate(java.util.Date value, java.lang.StringBuffer buf)
          To facilitate overriding of this method, the formatting is broken out.
 void formatSerializable(java.io.Serializable value, java.lang.StringBuffer buf)
          To facilitate overriding of this method, the formatting is broken out.
 java.lang.String formatValue(java.lang.Object value)
          Convert the Object to a string.
 void formatValue(java.lang.Object value, java.lang.StringBuffer buf)
          Common formatting routine used by most of the sqlAssembler objects.
 java.lang.Object readSerializable(byte[] bytes)
          Create an object from the bytes read from the database.
static java.lang.String replace1QuoteWith2(java.lang.String fromString)
           
 byte[] serializeObject(java.io.Serializable value)
          Serialize the object into a byte []
 byte[] stringToBytes(java.lang.String string)
          Take a string of bytes in Hex format and create a byte array from the string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

zipBufferSize

public static final int zipBufferSize
See Also:
Constant Field Values

Native_Array_Delimiter

public static final java.lang.String Native_Array_Delimiter
See Also:
Constant Field Values

outputStreams

protected static java.lang.ThreadLocal outputStreams
Constructor Detail

ObjectFormatter

public ObjectFormatter()
Method Detail

replace1QuoteWith2

public static java.lang.String replace1QuoteWith2(java.lang.String fromString)
Parameters:
fromString -
Returns:

convertString

public java.lang.Object convertString(java.lang.Class c,
                                      java.lang.String str)
When converting to an array, we need every element to be inserted into the array to be of the correct object type. Since the original Object type is a String this wouldn't work for an Array of ints. We convert the string to an Integer and the reflection array processing takes care of the rest. This method is different than convertType in that we know we are starting from a String object.

Parameters:
c - Class to which the string is going to be converted.
str - A String representation of the class.
Returns:

convertType

public java.lang.Object convertType(java.lang.Class c,
                                    java.lang.Object obj)
Convert the object from its database representation to its actual value in the business object. This is the most likely method to be used in other processes. If the 'c' is a primtitive type, we force the value to zero.

Parameters:
c - Class to which the object is going to be converted.
obj - An unkown object type that may need to be converted to be a type of the class 'c'.
Returns:
An instance of the passed in type.

readSerializable

public java.lang.Object readSerializable(byte[] bytes)
Create an object from the bytes read from the database. It is assumed that the bytes were written via JGrinder. JGrinder compresses the bytes during the insert/update and we decompress the bytes here.

Parameters:
bytes - byte[] Zipped byte array that contains a serialized object
Returns:
See Also:
serializeObject(java.io.Serializable)

convertArray

public java.lang.String convertArray(java.lang.Object dataArray)
Convert a native array into a string value.

Parameters:
dataArray - an object representing an array. The value is assumed to be non-null.
Returns:

formatValue

public java.lang.String formatValue(java.lang.Object value)
                             throws java.io.IOException
Convert the Object to a string.

Parameters:
value -
Returns:
Throws:
java.io.IOException

formatValue

public void formatValue(java.lang.Object value,
                        java.lang.StringBuffer buf)
                 throws java.io.IOException
Common formatting routine used by most of the sqlAssembler objects.

Parameters:
value -
buf -
Throws:
java.io.IOException

serializeObject

public byte[] serializeObject(java.io.Serializable value)
                       throws java.io.IOException
Serialize the object into a byte []

Parameters:
value -
Returns:
Throws:
java.io.IOException

stringToBytes

public byte[] stringToBytes(java.lang.String string)
Take a string of bytes in Hex format and create a byte array from the string. The string must have 2 digits for each byte, therefore bytes with a value less than 0x10 must begin with a zero. Example: 0xaff would be 0aff in the byte string

Parameters:
string -
Returns:
The array of bytes from the string

createNumber

public java.lang.Number createNumber(java.lang.Class c,
                                     java.lang.Number value)
If the SQL type was "NUMERIC", then we need to check to see if the Double that NUMBERIC values are converted to should really be an Integer or a Long.

Parameters:
c -
value -
Returns:

formatSerializable

public void formatSerializable(java.io.Serializable value,
                               java.lang.StringBuffer buf)
                        throws java.io.IOException
To facilitate overriding of this method, the formatting is broken out.

Parameters:
buf - StringBuffer upon which to write the data.
value - Serializable object to convert to a string value.
Throws:
java.io.IOException

bytesToString

protected void bytesToString(byte[] data,
                             java.lang.StringBuffer buffer)
Convert the byte array into a string buffer representing the hexidecimal equivalent of the data. Two hexidecimal characters will be created for each byte.

Parameters:
data -
buffer -

convertPrimitive

protected java.lang.Object convertPrimitive(java.lang.Class c,
                                            java.lang.String str)
Convert the string value to the correct primitive type.

Parameters:
c - Class to which the string is going to be converted.
str - A String representation of the class.
Returns:

createArray

protected java.lang.Object createArray(java.lang.Class clazz,
                                       java.lang.String vals)
Convert a string value into a Native Array. The data came out of the database as a string, but the class itself is an array. We attempt to convert every string value in the found in the data value to be an element in the correct scalar array. I've used '\n' the new line character to sperate entries. This could cause problems but many of the other characters were having troubles getting in an out of the datatbase. If an exception occurs while building array, return null for the array value.

Parameters:
clazz - java.lang.Class The class of the native array being built.
vals -
Returns:

createScalarType

protected java.lang.Object createScalarType(java.lang.Class c,
                                            java.lang.String value)
To support custom 'scalar' types (types that will be stored in the database as a string, but will in fact be an object in the object model) a ScalarFactory was introduced. This factory should convert the String found in the database to the correct custom ScalarType.

Parameters:
c -
value -
Returns:

formatDate

protected void formatDate(java.util.Date value,
                          java.lang.StringBuffer buf)
To facilitate overriding of this method, the formatting is broken out.

Parameters:
value - Date object to format.
buf - StringBuffer upon which to write the data.