|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.Convert
com.ccg.util.TagLookup
public class TagLookup
General purpose lookup and string convertor.
It seems like you are always running into a condition where you
need to convert string values into some sort of java object. These
string values might come from command line arguments, applet
parameters, property files, etc. This class works in conjunction
with Lookup objects to perform its task.
Basically, you use one of the static methods in LookupCreate (or some other class) to make a generic Lookup object out of your applet, properites file, resource
bundle, etc. You can then use the created Lookup object
(and an optional tag) to create one of these objects.
Once created, you can use this object to then lookup strings,
numbers, dates from your Lookup table. The conversion of
strings to their other representations is isolated here in this
class.
To see an example of using this class, take a look at TagLookupEx.java.
LookupCreate| Constructor Summary | |
|---|---|
TagLookup(Class forClass,
String tag)
Easy way to Create a TagLookup for a particular class. |
|
TagLookup(Lookup l)
Create a "untagged" instance. |
|
TagLookup(Lookup l,
String tag)
Create a "tagged" instance. |
|
| Method Summary | |
|---|---|
Object |
get(Object key)
Lookup the Object associated with a key. |
Boolean |
getBoolean(String key,
Boolean def)
Lookup value for a key and convert it to a Boolean
This method looks up the string value for a particular key and
attempts to convert it to a standard Boolean
object. |
int |
getChoice(String key,
String[] choices)
Lookup a "choice" from a set of string values. |
Color |
getColor(String key,
Color dc)
Lookup value for a key and convert it to a Color
This method looks up the string value for a particular key and
attempts to convert it to a standard Color
object. |
Date |
getDate(String key,
Date def)
Lookup value for a key and convert it to a Date
This method looks up the string value for a particular key and
attempts to convert it to a standard Date
object. |
Date |
getDate(String key,
Date def,
DateFormat df)
Lookup value for a key and convert it to a Date
This method looks up the string value for a particular key and
attempts to convert it to a standard Date
object. |
Font |
getFont(String key,
Font df)
Lookup value for a key and convert it to a Font
This method looks up the string value for a particular key and
attempts to convert it to a standard Font
object. |
static String[] |
getIndexedList(Lookup l,
String id)
Lookup a variable length array prefixed by "tag.N". |
InputStream |
getInputStream(Object key,
InputStream def,
boolean allowSystemIn)
Lookup a specified InputStream. |
Locale |
getLocale(String key,
Locale df)
Get a Locale from a specified string. |
Lookup |
getLookup()
Get the base settings used for looking up values |
Number |
getNumber(String key,
Number def)
Lookup value for a key and convert it to a Number
This method looks up the string value for a particular key and
attempts to convert it to a standard Number
object. |
Number |
getNumber(String key,
Number def,
NumberFormat nf)
Lookup value for a key and convert it to a Number
This method looks up the string value for a particular key and
attempts to convert it to a standard Number
object. |
OutputStream |
getOutputStream(Object key,
OutputStream def,
boolean allowSystem,
boolean noModify,
boolean appendIfFound)
Lookup a specified OutputStream. |
String |
getString(Object key)
Lookup the String associated with a key. |
String |
getString(String key,
String def)
Lookup the String associated with a key. |
static String[] |
getStrings(Lookup l,
String[] tags)
Lookup a array of strings given an array of keys. |
String |
getTag()
Get set the "tag" which is used to prefix key names. |
TimeSpan |
getTimeSpan(String skey,
String ekey,
TimeSpan def)
Lookup two date values from a pair of keys and convert it to a TimeSpan
This method looks up the string value for two particular key
values and attempts to convert them into a TimeSpan object (you use this method if you allow a time span to
be specified). |
TimeSpan |
getTimeSpan(String skey,
String ekey,
TimeSpan def,
DateFormat df)
Lookup two date values from a pair of keys and convert it to a TimeSpan
This method looks up the string value for two particular key
values and attempts to convert them into a TimeSpan object (you use this method if you allow a time span to
be specified). |
void |
setLookup(Lookup val)
Set the base settings used for looking up values Note, this has "ripple" affects if your object is shared by different threads/objects. |
void |
setTag(String val)
Set set the "tag" which is used to prefix key names. |
| Methods inherited from class com.ccg.util.Convert |
|---|
choiceValue, getDateFormat, getNumberFormat, setDateFormat, setNumberFormat, toBoolean, toColor, toDate, toFont, toInputStream, toLocale, toNumber, toOutputStream, toString, toTimeSpan |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public TagLookup(Lookup l)
x=12 y=33
lookup - Lookup table used to fetch string values associated
with keys from.getString(java.lang.Object),
getNumber(String,Number)
public TagLookup(Lookup l,
String tag)
Menu1.label=File Menu1.pos=1 Menu1.help=File operations Menu2.label=Edit Menu2.pos=2 Menu2.help=Edit operations Menu2.button=edit.gif
You could then use methods like getString("Menu2.","Edit") to look up
values associated with "Menu2".
lookup - Lookup table used to fetch string values associated
with keys from.tag - Tag to prefix your keys with (or null if you don't want a
prefix).getString(java.lang.Object),
getNumber(String,Number)
public TagLookup(Class forClass,
String tag)
TagLookup for a particular class.
Assume that you create a new class mypkg.MyObject, if you also
create a associated resource bundle MyObjectStrings.properties in
the same package, this constructor simplifies the entire process
of working with the TagLookup class. Assuming your class
isn't interested in using the "tag" feature, then you could do
something like the following:
MyObject mobj = new MyObject();
TagLookup _strings = new TagLookup(mobj.getClass(),null);
String editLabel = _strings.getString("edit","Edit");
Number scale = _strings.getNumber("scale",new Double(1.0));
class - What class you want to load the resources for.tag - Tag to prefix your keys with (or null if you don't want a
prefix).| Method Detail |
|---|
public String getString(Object key)
String associated with a key.
This method attempts to lookup the string associated with a
particular key. If not found, then null is returned.
getString in interface Lookupkey - Key to use for lookup (should not be null).
getNumber(String,Number)public Object get(Object key)
Object associated with a key.
This method attempts to lookup the object associated with a
particular key. If not found, then null is returned. This method
was required to implement the Lookup interface.
get in interface Lookupkey - Key to use for lookup (should not be null).
getString(Object)
public String getString(String key,
String def)
String associated with a key.
This method attempts to lookup the string associated with a
particular key. If not found, then the default value passed will
be returned.
key - Key to use for lookup (should not be null).default - Default value to return if value not found (this can be null
if desired).
getNumber(String,Number)
public final Number getNumber(String key,
Number def)
throws ParseException
Number
This method looks up the string value for a particular key and
attempts to convert it to a standard Number
object. If unable to do so, then the default value you specify
(which can be null) will be returned. The default number parser
for the default locale will be used.
key - Key to use for lookup (should not be null).default - Default value to return if value is missing or not a valid
number (this can be null).
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getNumber(String,Number,NumberFormat)
public final Number getNumber(String key,
Number def,
NumberFormat nf)
throws ParseException
Number
This method looks up the string value for a particular key and
attempts to convert it to a standard Number
object. If unable to do so, then the default value you specify
(which can be null) will be returned. This method allows you to
specify a specific NumberFormat to use for
parsing the number.
key - Key to use for lookup (should not be null).default - Default value to return if value is missing or not a valid
number (this can be null).nf - NumberFormat object to use for parsing
numbers with. This can be null in which case this method
behaves like the other getNumber() method.
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getNumber(String,Number)
public final Boolean getBoolean(String key,
Boolean def)
throws ParseException
Boolean
This method looks up the string value for a particular key and
attempts to convert it to a standard Boolean
object. If unable to do so, then the default value you specify
(which can be null) will be returned.
key - Key to use for lookup (should not be null).default - Default value to return if value is missing or not a valid
number (this can be null).
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getNumber(String,Number)
public final Date getDate(String key,
Date def)
throws ParseException
Date
This method looks up the string value for a particular key and
attempts to convert it to a standard Date
object. If unable to do so, then the default value you specify
(which can be null) will be returned. The default date parser
for the default locale will be used.
key - Key to use for lookup (should not be null).default - Default value to return if value is missing or not a valid
date (this can be null).
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getDate(String,Date,DateFormat)
public final Date getDate(String key,
Date def,
DateFormat df)
throws ParseException
Date
This method looks up the string value for a particular key and
attempts to convert it to a standard Date
object. If unable to do so, then the default value you specify
(which can be null) will be returned. This method allows you to
specify a specific DateFormat to use for
parsing the date.
key - Key to use for lookup (should not be null).default - Default value to return if value is missing or not a valid
date (this can be null).df - DateFormat object to use for parsing dates
with (may be null in which case this method behaves like the
other getDate() implementation.
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getDate(String,Date)
public final TimeSpan getTimeSpan(String skey,
String ekey,
TimeSpan def)
throws ParseException
TimeSpan
This method looks up the string value for two particular key
values and attempts to convert them into a TimeSpan object (you use this method if you allow a time span to
be specified). If unable to do so, then the default value you
specify (which can be null) will be returned. The date parser
currently associated for the object will be used to parse the
dates.
Note it is considered valid to specify either the start or end
time (or both). If just the end time is omitted, it will default
to the current time. If just the start time is omitted, it will
default to a time way in the past. So, if you are using this from
a command line lookup table, and you wanted
to use "start" and "end" as your key tags, your users could
specify something like:
skey - Key to use for lookup of the start time (should not be null).ekey - Key to use for lookup of the end time (should not be null).default - Default TimeSpan to return if value is
missing or not valid (this can be null).
TimeSpan object parsed from the lookup table, or your
default value.
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getTimeSpan(String,String,TimeSpan,DateFormat)
public TimeSpan getTimeSpan(String skey,
String ekey,
TimeSpan def,
DateFormat df)
throws ParseException
TimeSpan
This method looks up the string value for two particular key
values and attempts to convert them into a TimeSpan object (you use this method if you allow a time span to
be specified). If unable to do so, then the default value you
specify (which can be null) will be returned. The date parser
currently associated for the object will be used to parse the
dates.
Note it is considered valid to specify either the start or end
time (or both). If just the end time is omitted, it will default
to the current time. If just the start time is omitted, it will
default to a time way in the past. So, if you are using this from
a command line lookup table, and you wanted
to use "start" and "end" as your key tags, your users could
specify something like:
skey - Key to use for lookup of the start time (should not be null).ekey - Key to use for lookup of the end time (should not be null).default - Default TimeSpan to return if value is
missing or not valid (this can be null).df - DateFormat object to use for parsing dates
with. May be null in which case this method behaves like the
other getTimeSpan() method.
TimeSpan object parsed from the lookup table, or your
default value.
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getTimeSpan(String,String,TimeSpan,DateFormat)
public InputStream getInputStream(Object key,
InputStream def,
boolean allowSystemIn)
throws IOException
InputStream.
This method attempts to provide a flexible mechanism to open a
InputStream. It first looks up the ASCII name
of the source via the getString(Object) method. If a ASCII source name is found, then
Utility.getInputStream(String) will be used to convert the
String into a InputStream. If that fails, then your default value
will be returned.
key - Key to use for lookup (should not be null).def - Default InputStream to return if we are
unable to create one from the our lookup. Most likely you will
pass null for this parameter (which allows you to detect when
the input was omitted or incorrectly specified).allowSystemIn - Set to true if you want to allow this method to return System.in if the value specified corresponds to "-".
InputStream object. It may be null, if
our attempt to open the stream failed and you supplied null as
the default return value.
IOException - If a non-default value was found in the lookup (corresponding
to the 'key') table and could not be converted to a InputStream.getString(Object)
public OutputStream getOutputStream(Object key,
OutputStream def,
boolean allowSystem,
boolean noModify,
boolean appendIfFound)
throws IOException
OutputStream.
This method attempts to provide a flexible mechanism to open a
OutputStream. It first looks up the ASCII name
of the source via the getString(Object) method. If a ASCII source name is found, the
following is done in an effort to convert it to a OutputStream:
System.outSystem.out and the lookup value is "-" or
"out", then System.out will be returned.
System.errSystem.err and the lookup value is "err",
then System.err will be returned.
FileOutputStream class.
key - Key to use for lookup (should not be null).def - Default OutputStream to return if we are
unable to create one from the our lookup. Most likely you will
pass null for this parameter (which allows you to detect when
the output was omitted or incorrectly specified).allowSystem - Set to true if you want to allow this method to return System.out or System.err if the value specified
corresponds to "-", "out" or "err".noModify - Set to true if we should reject any existing file (prevent the modification of existing
files).allowAppend - Set to true if you want the output stream appended to any
existing file found, set to false if you just want to
overwrite an existing file which has the same name. This
parameter is only looked at if 'noModify' is set false.
OutputStream object. It may be null, if
our attempt to open the stream failed and you supplied null as
the default return value.
IOException - If a non-default value was found in the lookup (corresponding
to the 'key') table and could not be converted to a OutputStream.getString(Object)
public Color getColor(String key,
Color dc)
throws ParseException
Color
This method looks up the string value for a particular key and
attempts to convert it to a standard Color
object. If unable to do so, then the default value you specify
(which can be null) will be returned. The default number parser
for the default locale will be used. Colors may be specified as
integers (hex works well for this using 0xRRGGBB - where RR is
two hex digits 00-ff for red component, GG is two hex digits for
green component, and BB is two hex digits for blue component), or
by simple names (like "red", "green", etc.).
key - Key to use for lookup (should not be null).default - Default value to return if value is missing or not a valid
number (this can be null).
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getFont(String,Font)
public Font getFont(String key,
Font df)
throws ParseException
Font
This method looks up the string value for a particular key and
attempts to convert it to a standard Font
object. If unable to do so, then the default value you specify
(which can be null) will be returned. The Font.decode(java.lang.String)
method is used to decode the string specified. Refer to Font.decode(java.lang.String) for details on forming font names. Here are some
valid examples (that should work on all platforms):
key - Key to use for lookup (should not be null).default - Default value to return if value is missing or not a valid
font (this can be null).
Font object from the lookup table, or your
default value.
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)getColor(String,Color)
public Locale getLocale(String key,
Locale df)
throws ParseException
Locale from a specified string.
This method is intended to lookup a simple string value and
convert it to a standard Locale object. The strings must
be in the form:ll[_CC[_VARIANT]
Locale class. We
will force it to lower case (that is we aren't case sensitive) as
required by the Locale class. If "ll" isn't exactly two
characters, we will throw a parse exception.
Locale class. If "CC" is not exactly two characters, we
will throw a ParseException.
To see what locale's are available, try running the following:
java com.ccg.applet.AppletJVM
If you scroll down the window, you should see a list of locale
values supported by your JVM environment. It should look
something like:
... # # Info from: java.util.Locale # Default Locale:en_US Available: en en_US ar ar_AE ar_BH ar_DZ ... de_LU_EURO ...
In addition, you can look at the source code for TagLookupEx.java which
demonstrates how one might use this method. If you compile this
class, you can also use it from the command line to test out the
parsing of locale strings. Invoke it something like:
java TagLookupEx -local en_US java TagLookupEx -local nl java TagLookupEx -local de_nl_euro
key - Key to use for lookup (should not be null).default - Default value to return if value is not found (this can be
null).
Locale object which we looked up, or your
default value if one wasn't found.
ParseException - If value is specified but fails to convert properly (since a
value was specified, we don't want to return the default, but
we do want to let you know about the problem)Locale
public int getChoice(String key,
String[] choices)
This method looks up the string value for a particular key and then scans through the list of valid choices which you supplied. The results will be one of the following:
Here's a quick example of how one might use this method:
public static void foo(TagLookup tl) {
String[] modes = { "verbose", "quiet" };
int mode = tl.getChoice("mode",modes);
// if "mode" specified, go set
if (mode >= 0) setNewMode(mode);
}
key - Key to use for lookup (should not be null - if you pass null
you will get -3 returned).choices - String array of valid values recognized (if you pass null, you
will get -3 returned to you).
getString(Object)
public static String[] getIndexedList(Lookup l,
String id)
applets and in standard
property files. For example,
consider a property file with the following entries:
name.0=Paul Blankenbaker name.1=Megan Brown name.2=Scott Blankenbaker name.3=Erik Blankenbaker
Using this method, you can retrieve all four of the "name.#" entries shown above with a single call. In addition, the number of entries is controlled by the contents of the property file and determined at run-time. As an example, the following code fragment demonstrates how one could use this method to lookup ALL of the names entered:
public static String[] getNames(Lookup l) {
return TagLookup.getStrings(l,"name");
}
l - The lookup table to fetch values from. Pass null and get a
zero length array back.tag - The "tag" identifier which will precede each entry. We will
form a "key" value by appending the period ('.') character and
index ID to lookup values from. Pass null and you get a zero
length array back.
getStrings(com.ccg.util.Lookup, java.lang.String[])
public static String[] getStrings(Lookup l,
String[] tags)
public static String[] getInfo(Lookup l) {
String[] keys = { "first", "last", "email" };
return TagLookup.getStrings(l,keys);
}
The above routine would always return a String array with 3
elements. If the lookup table didn't have an entry
for one of the keys, then the returned array would have a null at
the position of the missing element.
l - The lookup table to fetch values from. Pass null and get a
zero length array back.tags - Array of "tag" identifiers (keys) which you want to look up
the corresponding values for. Pass null and you get a zero
length array back.
getIndexedList(com.ccg.util.Lookup, java.lang.String)public void setLookup(Lookup val)
val - New Lookup value to assign - if you pass null, we will ignore
your value.getLookup()public Lookup getLookup()
"empty" lookup table.setLookup(com.ccg.util.Lookup)public void setTag(String val)
val - New String value to assign.getTag()public String getTag()
setTag(java.lang.String)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||