com.marringtons.object
Class Translator

java.lang.Object
  extended bycom.marringtons.object.Translator

public class Translator
extends Object

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.

Author:
Paul Marrington
See Also:
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

Translator

public Translator()
Method Detail

getInstance

public 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.

Parameters:
object - to translate (that may have a sub-class of translator.
entry - for object - for caching translators.
Returns:
Translator to use.

translate

public Messages translate(Object object,
                          com.marringtons.object.ClassDirectory.Entry entry,
                          int fieldIndex,
                          String from,
                          int arrayIndex,
                          int arrayLength)
Translates from string to internal form. Will look for specific translation methods. If not found, default translation rules are applied.

Parameters:
object - that we are setting data within
entry - details for this object
fieldIndex - index of the field to set
from - string to translate into the field
arrayIndex - Location in array (-1 means whole array is csv of this field)
arrayLength - Length of array
Returns:
List of problems encountered (none() if none).

validate

public 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. The default does nothing. Only used by scraper.

Parameters:
object - to be verified. It will need to be cast to the correct type.
entry - details for this object
fields - name/value pairs that have been scraped. Useful to see which were done.
Returns:
List of problems encountered (none() for none).

_translate_c

public char _translate_c(String from)
This is the default code for translating individual characters. If the string being translated from is 1 character long, this character is taken verbatim. Otherwise the string is trimmed and the first character checked. If it is slosh (\) followed by a digit, the string is treated as a decimal ASCII representation of the character - otherwise the first character of the string is returned. Override to change the default translation.

Parameters:
from - string representation of a character (or \nnn where nnn are digits).
Returns:
a single character

_translate_d

public double _translate_d(String from)
Translate from a string to a double precision decimal. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_f

public float _translate_f(String from)
Translate from a string to a single precision decimal. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_i

public int _translate_i(String from)
Translate from a string to a 32 bit integer. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_l

public long _translate_l(String from)
Translate from a string to a 64 bit integer. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_s

public short _translate_s(String from)
Translate from a string to a 16 bit integer. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_b

public byte _translate_b(String from)
Translate from a string to a 8 bit unsigned integer. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_B

public boolean _translate_B(String from)
                     throws Convert.Exception
Translate from a string to a boolean. Can be case independant and starts with yes/true/on/oui/da/si or no/false/off/non. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation
Throws:
Convert.Exception - if it can't see a boolean in the string.
See Also:
Convert.toBoolean(String)

_translate_S

public String _translate_S(String from)
Translate from a string to a string. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_C

public Object _translate_C(String from)
Translate from a string to a Collection entry. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation

_translate_M

public Object[] _translate_M(String from)
Translate from a string to a Map entry. Override to change the default translation.

Parameters:
from - string in the form name=value
Returns:
result as an array of 1 or 2 objects (name and optional value).

_translate_T

public Object _translate_T(String from)
Translate from a string for epoch seconds to a timeStamp. Override to change the default translation.

Parameters:
from - string in the form name=value
Returns:
result as an array of 1 or 2 objects (name and optional value).

_translate_O

public Object _translate_O(String from)
Translate from a string to an object generates an error. Override to change the default translation.

Parameters:
from - string to translate from
Returns:
result extracted from the tranlation


Copyright © 2005 Paul Marrington http://library.marringtons.com