com.taco.text
Class StringConverterUtilities

java.lang.Object
  extended by com.taco.text.StringConverterUtilities

public class StringConverterUtilities
extends java.lang.Object

Utility functions for converting interpolated strings.


Field Summary
protected static java.lang.String _ARGUMENT_MAP_REFERENCE_PREFIX_REGEX_STRING
           
protected static java.lang.String _BUNDLE_KEY_REFERENCE_REGEX_STRING
           
protected static java.util.regex.Pattern _DEFAULT_VALUE_END_PATTERN
           
protected static java.lang.String _DOTTED_NAME_REGEX_STRING
           
protected static java.util.regex.Pattern _GLOBAL_ASSIGNMENT_PATTERN
          The pattern compiled from _GLOBAL_NAME_ASSIGNMENT_REGEX_STRING.
protected static java.lang.String _GLOBAL_ASSIGNMENT_REGEX_STRING
          A regex string for the prefix before a literal indicating a property value.
protected static java.lang.String _GLOBAL_REFERENCE_REGEX_STRING
           
protected static java.util.regex.Pattern _VARIABLE_REFERENCE_PATTERN
           
protected static java.lang.String _VARIABLE_REFERENCE_REGEX_STRING
           
static java.util.regex.Pattern END_OF_STRING_PATTERN
          A pattern passed to nextValue() that indicates that the entire sequence should be used for the next value.
 
Constructor Summary
StringConverterUtilities()
           
 
Method Summary
protected static int _finishSequence(java.lang.CharSequence cs, int startIndex, java.util.regex.Pattern endPattern)
          Give a character sequence and an index at which a value end pattern should match, return the index into the character sequence at which the end of the value sequence lies.
protected static int _parseArgumentMapSuffix(java.lang.CharSequence cs, int prefixEndOffset, InterpolatedValueMatchResult result)
          Given a character sequence that begins with a reference to an argument map key, the location of the end of the reference, and an instance of InterpolatedValueMatchResult which has its startIndex field set to the index in the original character sequence at which cs starts, set the fromMapValueConverterString, toMapValueConverterString fields of result.
protected static int _parseLiteral(java.lang.CharSequence cs, java.util.regex.Pattern endPattern, InterpolatedValueMatchResult result)
          Given that cs begins with a string literal, parse it an fill in the appropriate fields of result.
static java.lang.CharSequence extractPropertiesSubSequence(java.lang.CharSequence cs)
          Given a literal string to convert, return the part of the string that is a series of property name / value pairs.
static int intValueForProperty(java.lang.Object propertyValue, IStringToObjectConverter converter, int defaultValue)
          Given a property value and a converter from strings to instances of Number, return the int value of the property.
static void main(java.lang.String[] args)
          Send args[0] to valueForSequence and print the result.
static InterpolatedValueMatchResult nextValue(java.lang.CharSequence cs, int startIndex)
          Parse the sequence starting at the argument index, looking for the default end pattern to terminate the subsequence used for the value.
static InterpolatedValueMatchResult nextValue(java.lang.CharSequence cs, int startIndex, java.util.regex.Pattern endPattern)
          Given a character sequence that starting from startIndex, contains a subsequence specifying a (possibly interpolated) value, return a new instance of InterpolatedValueMatchResult.
static java.lang.Object test(IInterpolatingStringToObjectConverter converter, java.lang.String[] args)
          Call test(IInterpolatingStringToObjectConverter, String[], ResourceBundle, INoReturnMap) with a null bundle and a new observable map.
static java.lang.Object test(IInterpolatingStringToObjectConverter converter, java.lang.String[] args, java.util.ResourceBundle bundle, INoReturnMap map)
          A simple test program for an interpolating string to object converter.
static java.lang.Object testOrdinaryConverter(IStringToObjectConverter converter, java.lang.String[] args)
          A simple test program for an ordinary string to object converter.
static java.lang.String unescape(java.lang.String s)
          Unescape a leading '%', '$', '&', or '#' character in a string when they are followed by '\'.
static void usage(java.lang.Class c)
           
static InterpolatedValueMatchResult valueForSequence(java.lang.CharSequence cs)
          Parse the entire sequence to get a single interpolated value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

END_OF_STRING_PATTERN

public static final java.util.regex.Pattern END_OF_STRING_PATTERN
A pattern passed to nextValue() that indicates that the entire sequence should be used for the next value.


_DEFAULT_VALUE_END_PATTERN

protected static final java.util.regex.Pattern _DEFAULT_VALUE_END_PATTERN

_DOTTED_NAME_REGEX_STRING

protected static final java.lang.String _DOTTED_NAME_REGEX_STRING
See Also:
Constant Field Values

_BUNDLE_KEY_REFERENCE_REGEX_STRING

protected static final java.lang.String _BUNDLE_KEY_REFERENCE_REGEX_STRING
See Also:
Constant Field Values

_GLOBAL_REFERENCE_REGEX_STRING

protected static final java.lang.String _GLOBAL_REFERENCE_REGEX_STRING
See Also:
Constant Field Values

_ARGUMENT_MAP_REFERENCE_PREFIX_REGEX_STRING

protected static final java.lang.String _ARGUMENT_MAP_REFERENCE_PREFIX_REGEX_STRING
See Also:
Constant Field Values

_VARIABLE_REFERENCE_REGEX_STRING

protected static final java.lang.String _VARIABLE_REFERENCE_REGEX_STRING
See Also:
Constant Field Values

_VARIABLE_REFERENCE_PATTERN

protected static final java.util.regex.Pattern _VARIABLE_REFERENCE_PATTERN

_GLOBAL_ASSIGNMENT_REGEX_STRING

protected static final java.lang.String _GLOBAL_ASSIGNMENT_REGEX_STRING
A regex string for the prefix before a literal indicating a property value. The prefix contains an assignment to a global variable. The name of the global variable (without the '&' prefix) is captured in the first group.

See Also:
Constant Field Values

_GLOBAL_ASSIGNMENT_PATTERN

protected static final java.util.regex.Pattern _GLOBAL_ASSIGNMENT_PATTERN
The pattern compiled from _GLOBAL_NAME_ASSIGNMENT_REGEX_STRING.

Constructor Detail

StringConverterUtilities

public StringConverterUtilities()
Method Detail

unescape

public static final java.lang.String unescape(java.lang.String s)
Unescape a leading '%', '$', '&', or '#' character in a string when they are followed by '\'.


valueForSequence

public static final InterpolatedValueMatchResult valueForSequence(java.lang.CharSequence cs)
                                                           throws java.text.ParseException
Parse the entire sequence to get a single interpolated value. Throw a parse error if the sequence doesn't parse correctly.

Throws:
java.text.ParseException

nextValue

public static final InterpolatedValueMatchResult nextValue(java.lang.CharSequence cs,
                                                           int startIndex)
                                                    throws java.text.ParseException
Parse the sequence starting at the argument index, looking for the default end pattern to terminate the subsequence used for the value.

Throws:
java.text.ParseException

nextValue

public static final InterpolatedValueMatchResult nextValue(java.lang.CharSequence cs,
                                                           int startIndex,
                                                           java.util.regex.Pattern endPattern)
                                                    throws java.text.ParseException
Given a character sequence that starting from startIndex, contains a subsequence specifying a (possibly interpolated) value, return a new instance of InterpolatedValueMatchResult.

Throws:
java.text.ParseException - if the sequence is not a variable and the opening brace is not found or not matched with a closing brace.

_finishSequence

protected static int _finishSequence(java.lang.CharSequence cs,
                                     int startIndex,
                                     java.util.regex.Pattern endPattern)
                              throws java.text.ParseException
Give a character sequence and an index at which a value end pattern should match, return the index into the character sequence at which the end of the value sequence lies. If endPattern has a capturing group and the group captures something, return the index at which the capturing group starts. Otherwise, return the index after the entire matched sequence.

Throws:
java.text.ParseException

_parseLiteral

protected static int _parseLiteral(java.lang.CharSequence cs,
                                   java.util.regex.Pattern endPattern,
                                   InterpolatedValueMatchResult result)
                            throws java.text.ParseException
Given that cs begins with a string literal, parse it an fill in the appropriate fields of result.

Throws:
java.text.ParseException

_parseArgumentMapSuffix

protected static int _parseArgumentMapSuffix(java.lang.CharSequence cs,
                                             int prefixEndOffset,
                                             InterpolatedValueMatchResult result)
                                      throws java.text.ParseException
Given a character sequence that begins with a reference to an argument map key, the location of the end of the reference, and an instance of InterpolatedValueMatchResult which has its startIndex field set to the index in the original character sequence at which cs starts, set the fromMapValueConverterString, toMapValueConverterString fields of result. If no converters are specified, set these fields to the empty string. Return the index just after the reference, if there are no converters, or just after the last '#' if there are converters.

Throws:
java.text.ParseException

main

public static void main(java.lang.String[] args)
Send args[0] to valueForSequence and print the result.


usage

public static final void usage(java.lang.Class c)

test

public static final java.lang.Object test(IInterpolatingStringToObjectConverter converter,
                                          java.lang.String[] args)
Call test(IInterpolatingStringToObjectConverter, String[], ResourceBundle, INoReturnMap) with a null bundle and a new observable map.


test

public static final java.lang.Object test(IInterpolatingStringToObjectConverter converter,
                                          java.lang.String[] args,
                                          java.util.ResourceBundle bundle,
                                          INoReturnMap map)
A simple test program for an interpolating string to object converter.

Parameters:
converter - The converter to use to create an object.
args - A list of arguments from the command line.
map - The argument map.
Returns:
The result of conversion.

testOrdinaryConverter

public static final java.lang.Object testOrdinaryConverter(IStringToObjectConverter converter,
                                                           java.lang.String[] args)
A simple test program for an ordinary string to object converter.


extractPropertiesSubSequence

public static final java.lang.CharSequence extractPropertiesSubSequence(java.lang.CharSequence cs)
                                                                 throws java.text.ParseException
Given a literal string to convert, return the part of the string that is a series of property name / value pairs. See if the string begins with a C-style opening punctuation character. If so, discard it and its matching character. Next, if the beginning of the remaining string is an alphanumeric word, discard it as well as any whitespace after it.

Throws:
java.text.ParseException

intValueForProperty

public static int intValueForProperty(java.lang.Object propertyValue,
                                      IStringToObjectConverter converter,
                                      int defaultValue)

Given a property value and a converter from strings to instances of Number, return the int value of the property. If the value cannot be converted, return defaultValue.

To be more precise, if propertyValue is null, defaultValue is returned. Otherwise, if propertyValue is a string, use the argument converter to convert it to an instance of Number. If the conversion succeeds, return the int value of the Number. If the conversion fails, return defaultValue.

Otherwise, if propertyValue is an instance of Number, return its int value. If propertyValue is not a String or Number, return defaultValue.

Parameters:
propertyValue - the value to be converter, may be null
converter - the string to object converter to use to convert propertyValue if it's a string
defaultValue - the value to return in case of error
Returns:
the value represented by propertyValue, or defaultValue in case or conversion error