|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.object.Translator
A Translator is a class to translate strings to internal format fields in DAOs. It is used by classes that convert from strings to an internal representation. It supports all primatives, arrays of primatives and Strings in this the default form. The main power derives from sub-classing the Translator. _translate_* can be over-ridden to change the way various field types are translated or methods with the same name as objects being translated can be created to tune translations on a field-by-field basis. The default translation for collections is a collection of strings. Maps expect a string of the type key=value - both as strings. getInstance() can be used to extract the translator from inside the class - where it has access to the complete class.
private static class TestObject
{
int intValue;
String stringValue, string2Value;
int[] intArray;
ArrayList collection;
HashMap map;
public class TestObject2 { int intValue; }
TestObject2 testObject2;
}
private class CustomTranslator extends Translator
{
public int intValue( String from) { return Integer.parseInt( from) + 5678; }
public String stringValue( String from) { return _translate_S( from) + " customised"; }
public int intArray( String from) { return Integer.parseInt( from) + 5678; }
public String _translate_S( String from) { return convertToFrench( from); }
public Object[] map( String from) { return new Object[] { from, "empty" }; }
}
In the rare case where you are translating multiple sub-objects with the same
field name, and you need to do a specific translation on one of them, use the
protected Object translationObject to determine the source.
public int intValue( String from)
{
int add = (translationObject instanceOf TestObject2) ? 5678 : 9012;
return Integer.parseInt( from) + add;
}
Once individual field translations are complete, validate() is called for user provided
cross-field validation.
ObjectScraper.fromProperties(Object, Properties, Translator),
ObjectXML.fromXML( Database, BufferedReader, boolean, Translator)| Constructor Summary | |
Translator()
|
|
| Method Summary | |
byte |
_translate_b(String from)
Translate from a string to a 8 bit unsigned integer. |
boolean |
_translate_B(String from)
Translate from a string to a boolean. |
char |
_translate_c(String from)
This is the default code for translating individual characters. |
Object |
_translate_C(String from)
Translate from a string to a Collection entry. |
double |
_translate_d(String from)
Translate from a string to a double precision decimal. |
float |
_translate_f(String from)
Translate from a string to a single precision decimal. |
int |
_translate_i(String from)
Translate from a string to a 32 bit integer. |
long |
_translate_l(String from)
Translate from a string to a 64 bit integer. |
Object[] |
_translate_M(String from)
Translate from a string to a Map entry. |
Object |
_translate_O(String from)
Translate from a string to an object generates an error. |
short |
_translate_s(String from)
Translate from a string to a 16 bit integer. |
String |
_translate_S(String from)
Translate from a string to a string. |
Object |
_translate_T(String from)
Translate from a string for epoch seconds to a timeStamp. |
static Translator |
getInstance(Object object,
com.marringtons.object.ClassDirectory.Entry entry)
Get an instance of a translator - from the class being translated or the default if no others exist. |
Messages |
translate(Object object,
com.marringtons.object.ClassDirectory.Entry entry,
int fieldIndex,
String from,
int arrayIndex,
int arrayLength)
Translates from string to internal form. |
Messages |
validate(Object object,
com.marringtons.object.ClassDirectory.Entry entry,
Properties fields)
Once all the fields have been scraped into the POJO/DAO, validate is called for cross-field validation. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public Translator()
| Method Detail |
public static Translator getInstance(Object object,
com.marringtons.object.ClassDirectory.Entry entry)
object - to translate (that may have a sub-class of translator.entry - for object - for caching translators.
public Messages translate(Object object,
com.marringtons.object.ClassDirectory.Entry entry,
int fieldIndex,
String from,
int arrayIndex,
int arrayLength)
object - that we are setting data withinentry - details for this objectfieldIndex - index of the field to setfrom - string to translate into the fieldarrayIndex - Location in array (-1 means whole array is csv of this field)arrayLength - Length of array
public Messages validate(Object object,
com.marringtons.object.ClassDirectory.Entry entry,
Properties fields)
object - to be verified. It will need to be cast to the correct type.entry - details for this objectfields - name/value pairs that have been scraped. Useful to see which were done.
public char _translate_c(String from)
from - string representation of a character (or \nnn where nnn are digits).
public double _translate_d(String from)
from - string to translate from
public float _translate_f(String from)
from - string to translate from
public int _translate_i(String from)
from - string to translate from
public long _translate_l(String from)
from - string to translate from
public short _translate_s(String from)
from - string to translate from
public byte _translate_b(String from)
from - string to translate from
public boolean _translate_B(String from)
throws Convert.Exception
from - string to translate from
Convert.Exception - if it can't see a boolean in the string.Convert.toBoolean(String)public String _translate_S(String from)
from - string to translate from
public Object _translate_C(String from)
from - string to translate from
public Object[] _translate_M(String from)
from - string in the form name=value
public Object _translate_T(String from)
from - string in the form name=value
public Object _translate_O(String from)
from - string to translate from
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||