com.taco.text
Class StringToInstanceConverter

java.lang.Object
  extended by com.taco.text.AbstractStringToObjectConverter
      extended by com.taco.text.StringToInstanceConverter
All Implemented Interfaces:
ICommonRegexConstants, IStringToObjectConverter, IObjectMapper, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
StringToBeanShellInstanceConverter

public class StringToInstanceConverter
extends AbstractStringToObjectConverter
implements java.lang.Cloneable, java.io.Serializable, ICommonRegexConstants

An implementation of IStringToObjectConverter that converts a string, interpreted as a fully qualified class name, into a new instance of the class denoted by s by interpreting the string as a Java literal or using reflection to create an instance of an object.

See Also:
Serialized Form

Field Summary
protected  java.lang.Class _returnType
          The type returned objects must be instances of.
static StringToInstanceConverter instance
          The singleton instance of this class, which does not impose any type constraint.
 
Fields inherited from interface com.taco.text.ICommonRegexConstants
BOOLEAN_REGEX_STRING, CHAR_REGEX_STRING, FLOAT_REGEX_STRING, HEX_NUMBER_REGEX_STRING, INTEGER_REGEX_STRING, JAVA_CLASS_NAME_REGEX_STRING, LONG_HEX_REGEX_STRING, LONG_INTEGER_REGEX_STRING, NON_NEGATIVE_FLOAT_REGEX_STRING, PROPERTY_PREFIX_PATTERN, PROPERTY_PREFIX_REGEX_STRING, QUOTED_STRING_REGEX_STRING, WHITESPACE_PATTERN
 
Constructor Summary
protected StringToInstanceConverter()
          Construct an instance that does not impose a type constraint on returned objects.
  StringToInstanceConverter(java.lang.Class returnType)
          Construct an instance which checks that returned objects are instances of subclasses or implementations of the argument class or interface.
 
Method Summary
protected  void _checkType(java.lang.Object obj)
          Check that obj is an instance of each of the classes in classArray.
protected  java.lang.Object _parseInstanceString(java.lang.String s)
          Use s as a class name.
protected  java.lang.Object _parseJavaLiteral(java.lang.String s)
          Parse s as a Java literal and return the String or primitive wrapper that corresponds to it.
protected  java.lang.Object _toObject(java.lang.String s)
          Convert s to an object as in toObject(), but don't do any type checking.
 java.lang.Object clone()
           
static void main(java.lang.String[] args)
          A simple test program that gets and instance of the class denoted by the first command-line argument.
 java.lang.Object toObject(java.lang.String s)
           
 
Methods inherited from class com.taco.text.AbstractStringToObjectConverter
map
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_returnType

protected final java.lang.Class _returnType
The type returned objects must be instances of.


instance

public static final StringToInstanceConverter instance
The singleton instance of this class, which does not impose any type constraint.

Constructor Detail

StringToInstanceConverter

protected StringToInstanceConverter()
Construct an instance that does not impose a type constraint on returned objects.


StringToInstanceConverter

public StringToInstanceConverter(java.lang.Class returnType)
Construct an instance which checks that returned objects are instances of subclasses or implementations of the argument class or interface. If the argument is null, do not perform any type checking.

Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

toObject

public java.lang.Object toObject(java.lang.String s)
                          throws java.text.ParseException
Specified by:
toObject in interface IStringToObjectConverter
Throws:
java.text.ParseException

_toObject

protected java.lang.Object _toObject(java.lang.String s)
                              throws java.text.ParseException

Convert s to an object as in toObject(), but don't do any type checking.

Try to parse s as a Java literal using _parseJavaLiteral(). Failing that, try to parse s as a instance string using _parseInstanceString(). If that fails, it throw a ParseException.

Throws:
java.text.ParseException

_parseJavaLiteral

protected java.lang.Object _parseJavaLiteral(java.lang.String s)
                                      throws java.text.ParseException
Parse s as a Java literal and return the String or primitive wrapper that corresponds to it. If s cannot be parsed as a Java literal, throw a ParseException

Throws:
java.text.ParseException

_parseInstanceString

protected java.lang.Object _parseInstanceString(java.lang.String s)
                                         throws java.text.ParseException
Use s as a class name. If the class has a static field named "instance", return it. Otherwise, create a new instance of the class using the default constructor.

Throws:
java.text.ParseException

_checkType

protected void _checkType(java.lang.Object obj)
                   throws java.text.ParseException
Check that obj is an instance of each of the classes in classArray. If not, throw a ParseException.

Throws:
java.text.ParseException

main

public static void main(java.lang.String[] args)
A simple test program that gets and instance of the class denoted by the first command-line argument.