Our Data Objects (Beans):
The goal here is how can we convert Java data objects (Beans) into a structured XML tags or records.
The following questions must be addressed:
•
|
Can we store Beans directly into XML field (as text) in the database?
|
•
|
Can we converted back to the same beans?
|
•
|
Would others understand our data types and values?
|
•
|
We should not create a new language or new technologies
|
•
|
Should our JAR and documentation be easily understood and used by other venders and platforms?
|
Target Data Types
The following are our target Java types:
•
|
Primitive Data Types
|
•
|
Array of Primitive Data Types
|
•
|
Java Date/Time Object
|
•
|
Array of Data Objects
|
•
|
Java Collections
|
•
|
Java Enum
|
These target types should be either part of a Java Bean or composed of Java Beans.
Why Java Beans?
Java Beans is simply composed of the following:
•
|
A set of private data fields
|
•
|
A set of public get() and set() methods that access these data fields
|
•
|
toString() method
|
We will be setting our
Basic Conversion Unit as a Java Bean. Therefore, we will be
converting Beans to XML tags for storing and XML tags to beans for retrieval. Java
Reflection enables to access the get() and set() methods and the beans data fields.
Target Data Types and Java Beans
How can we tie the target data types and Beans?
The following steps would help us in the conversion of Java beans to XML tags and values.
•
|
Identify the data object by its name (package and class name)
|
•
|
Identify each data type
|
•
|
Give each target Data Type an index for processing
|
•
|
Convert each type to string value to stored into an XML tag
|
The best way to illustrate our conversion that we are presenting is by doing an
example.
The following table has one-to-one conversion from MyFirstBean
Class to MyFirstBeanXMLFileConversion.xml
MyFirstBean.java
|
MyFirstBeanXMLFileConversion.xml
|
package sample_data_objects;
/**
*
* @author sameldin
*/
public class MyFirstBean
{
private int intField = 10;
private String stringField = "Same I am";
private double [] doubleArrayField = {22.22, 33.33, 44.44};
/**
* get() and set() mehtods ...
*/
}
|
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
-<Root>
- <DATA_OBJECT CLASS_NAME="sample_data_objects.MyFirstBean">
- <FIELD_NAME FIELD="intField">
- <RETURN_TYPE INT="7">10</RETURN_TYPE>
- </FIELD_NAME>
- <FIELD_NAME FIELD="stringField">
- <RETURN_TYPE STRING="2">Sam I am</RETURN_TYPE>
- </FIELD_NAME>
- <FIELD_NAME FIELD="doubleArrayField">
- <RETURN_TYPE ARRAY_DOUBLE="210">22.22,33.33,44.44</RETURN_TYPE>
- </FIELD_NAME>
- </DATA_OBJECT>
-</Root>
|
Constants and Index for Processing
Before we can explain the tag and bean relationship, we recommend the creation of the following Constants class:
/**
*
* @author sameldin
*/
public class Constants
{
//---------------------------- Tag Names -------------------------------------------
|
public final static String
|
ERROR_TAG_NAME
|
= "ERROR_FOUND";
|
|
public final static String
|
DATA_OBJECT_TAG_NAME
|
= "DATA_OBJECT";
|
|
public final static String
|
FIELD_TAG_NAME
|
= "FIELD_NAME";
|
|
public final static String
|
RETURN_TYPE_TAG_NAME
|
= "RETURN_TYPE";
|
//---------------------------- Labels -------------------------------------------
|
public final static String
|
CLASS_NAME_LABEL
|
= "CLASS_NAME";
|
|
public final static String
|
FIELD_LABEL
|
= "FIELD";
|
|
public final static String
|
RETUN_VALUE_LABEL
|
= "RETUN_VALUE";
|
//---------------------------- Names & Index -------------------------------------------
|
public final static String
|
UNKNOWN_TYPE_NAME
|
= "UNKNOWN";
|
|
public final static int
|
UNKNOWN_TYPE_INDEX
|
= -1;
|
|
public final static String
|
VOID_TYPE_NAME
|
= "VOID";
|
|
public final static int
|
VOID_TYPE_INDEX
|
= 0;
|
|
...
|
...
|
...
|
|
public final static String
|
STRING_TYPE_NAME
|
= "STRING";
|
|
public final static int
|
STRING_TYPE_INDEX
|
= 2;
|
|
...
|
...
|
...
|
|
public final static String
|
INT_TYPE_NAME
|
= "INT";
|
|
public final static int
|
INT_TYPE_INDEX
|
= 7;
|
|
...
|
...
|
...
|
|
public final static String
|
DOUBLE_TYPE_NAME
|
= "DOUBLE";
|
|
public final static int
|
DOUBLE_TYPE_INDEX
|
= 10;
|
|
...
|
...
|
...
|
//---------------------------- Arrays Names & Index -------------------------------------------
|
public final static String
|
ARRAY_DOUBLE_TYPE_NAME
|
= "ARRAY_DOUBLE";
|
|
public final static int
|
ARRAY_DOUBLE_TYPE_INDEX
|
= 210;
|
|
...
|
...
|
...
|
}
In this Constants class we are defining the name and index for each type which will be converted
to XML tag field. This is our design to help with the naming and the processing of each Java bean
field including arrays, Java Collection and enumeration. The name is created for readability and
index for processing the type in a "switch" statement as follows:
switch(typeIndex)
{
case Constants.INT_TYPE_INDEX:
...
break;
case Constants.DOUBLE_TYPE_INDEX:
...
break;
...
}
Looking at the MyFirstBean.java Class and MyFirstBeanXMLFileConversion.xml files, we have the Java bean converted to XML tags, where we have:
-<Root>
|
Start of xml representation of Java bean class file.
|
- <DATA_OBJECT CLASS_NAME="sample_data_objects.MyFirstBean">
|
The start of a new Java bean object and the name of the bean class including the package.
|
- <FIELD_NAME FIELD="intField">
|
The start of the field within the bean and the name of the field
|
- <RETURN_TYPE INT="7">10</RETURN_TYPE>
|
The type of the field which is the return type of the method "getIntField()". "INT" is the return type name, "7" is the index value
|
- <RETURN_TYPE STRING="2">Sam I am</RETURN_TYPE>
|
String field type, index value and assigned value
|
- <RETURN_TYPE ARRAY_DOUBLE="210">22.22,33.33,44.44</RETURN_TYPE>
|
Array of double field type, name and index value and assigned values to the elements of the array
|
With what we presented so far, adding more beans and different beans objects would be done by simply adding
more " DATA_OBJECT". The nesting of arrays of beans can also be done using the nesting of XML tag with
the "DATA_OBJECT" tag. The architect-designer may need to set the constant names and index value so they will not
cause any conflicts, confusion or miscommunication.
If the reader is interested in getting the code for some or all of the tool classes, they can email a request to: