com.ccg.util
Class LookupCreate

java.lang.Object
  extended by com.ccg.util.LookupCreate

public class LookupCreate
extends Object

Static methods to form Lookup objects from standard Java objects. Unfortunately, many Java classes don't implement the Lookup (or some such similar interface). This class contains static methods which allow one to create standard Lookup objects by putting a small wrapper class around these standard Java objects.

Since:
1.0
Version:
$Revision: 1.1.1.1 $
Author:
$Author: pkb $
See Also:
Lookup

Constructor Summary
LookupCreate()
           
 
Method Summary
static LookupKeyed empty()
          Create a empty Lookup object.
static Lookup forOS(Lookup from)
          Create a Operating System specific Lookup table.
static Lookup fromApplet(Applet a)
          Create a Lookup object from any Applet derived object This method is used to make a Lookup object out of java.applet.Applet derived object.
static LookupKeyed fromBracketString(String items)
          Create a LookupKeyed object from a "bracket string".
static LookupKeyed fromDictionary(Dictionary dict)
          Create a Lookup object from any Dictionary derived object This method is used to make a Lookup object out of java.util.Dictionary derived objects (like java.util.Hashtable an java.util.Properties).
static Lookup fromLookups(Lookup primary, Lookup fallback)
          Chain together two Lookup objects.
static LookupKeyed fromResourceBundle(ResourceBundle rb)
          Create a Lookup object from any ResourceBundle derived object This method is used to make a Lookup object out of java.util.ResourceBundle derived object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LookupCreate

public LookupCreate()
Method Detail

fromDictionary

public static LookupKeyed fromDictionary(Dictionary dict)
Create a Lookup object from any Dictionary derived object This method is used to make a Lookup object out of java.util.Dictionary derived objects (like java.util.Hashtable an java.util.Properties).

Parameters:
other - A Dictionary object to put a Lookup wrapper on.
Returns:
Lookup object for fetching values.
Since:
1.0
See Also:
LookupKeyed

fromResourceBundle

public static LookupKeyed fromResourceBundle(ResourceBundle rb)
Create a Lookup object from any ResourceBundle derived object This method is used to make a Lookup object out of java.util.ResourceBundle derived object. It should be noted, that a ResourceBundles can throw Exceptions when used to fetch objects. The Lookup wrapper traps this and prevents it and will return null if the ResourceBundle does not have a value associated with the key we want to lookup.

Parameters:
other - A ResourceBundle object to put a Lookup wrapper on.
Returns:
Lookup object for fetching values.
Since:
1.0
See Also:
Lookup

fromApplet

public static Lookup fromApplet(Applet a)
Create a Lookup object from any Applet derived object This method is used to make a Lookup object out of java.applet.Applet derived object. It uses the applet's getParameter(String) method to do its lookups with.

Parameters:
other - A Applet object to put a Lookup wrapper on.
Returns:
Lookup object for fetching values.
Since:
1.0
See Also:
Lookup

fromLookups

public static Lookup fromLookups(Lookup primary,
                                 Lookup fallback)
Chain together two Lookup objects. This method is extremely useful. It allows all sorts of possible ways to chain together various lookup tables. For example, you could you this method to join a java.util.Hashtable, java.util.ResourceBundle, and CommandLine object together as shown in the following example:
 static Lookup foo(CommandLine cl, Hashtable ht, ResourceBundle rb) {
   Lookup  l = LookupCreate.fromDictionary(ht);
                                // join lookups such that we check
                                // 1. The command line arguments
                                // 2. The Hashtable arguments
                                // 3. Finally the ResourceBundle
   l = LookupCreate.fromLookups(cl,l);
   l = LookupCreate.fromLookups(l,LookupCreate.fromResourceBundle(rb));
   return l;
 }
 

The above code fragment demonstrates how the recursiveness of this method can be used to chain together more than just two Lookup tables.

This method handles null values gracefully and optimizes the returned lookup table based on the values passed (if both values are passed, you'll get a empty lookup table back).

Parameters:
primary - First Lookup object to search for value in (null is OK - you'll end up getting primary or empty lookup back).
secondary - Secondary Lookup object to search for value in (null is OK - you'll end up getting primary or empty lookup back).
Returns:
Lookup object that is two Lookup objects chained together.
Since:
1.0
See Also:
Lookup

fromBracketString

public static LookupKeyed fromBracketString(String items)
Create a LookupKeyed object from a "bracket string". This method takes a single strings as input and parses the contents of the string to produce a LookupKeyed table. This is very handy when reading choices from resource bundles or property files.

As of this time, the "<" and ">" characters are used to separate key values from data values and must not appear in either key fields or data fields. In the future a escape character may be added (like "\" to enable this). For this reason, currently the escape character is reservered. So, for maximum safety, make sure your key and data items do not contain "<", ">", or "\". That said, the input string to this method should look something like:

 <key0>data0<key1>data1...<keyN>dataN
 

For example:

 <Red>1<Green>2<Blue>4
 

NOTE: If sometimes, the "<" and ">" aren't the best characters (depeding upon your environment - they are a pain in XML/HTML documents). You can automatically switch to using "[]", "{}" or "()" by passing a string that starts with "[", "{" or "(". For example, all of the following evaluate the same:

 <Red>1<Green>2<Blue>4
 [Red]1[Green]2[Blue]4
 {Red}1{Green}2{Blue}4
 (Red)1(Green)2(Blue)4
 

Parameters:
bracketString - String to parse into a LookupKeyed table. You can pass null - you'll be returned a empty lookup table.
Returns:
LookupKeyed table of choices.
Since:
1.0
See Also:
LookupKeyed, MenuChoice.create(com.ccg.util.Lookup, java.lang.String)

empty

public static LookupKeyed empty()
Create a empty Lookup object. This method is used to make a empty Lookup/LookupKeyed object. It is handy when you want to pass a non-null Lookup object. Any attempt to lookup a value will result in null being returned (told you it was empty). If you fetch the keys from this object, you will get an empty enumeration (not null - just no elements).

Returns:
LookupKeyed object for fetching values - though it has no values.
Since:
1.0
See Also:
LookupKeyed

forOS

public static Lookup forOS(Lookup from)
Create a Operating System specific Lookup table. This method is used to create a lookup table which will first check for specific operating system values BEFORE checking for the standard value associated with a key. This allows one to set operating specific parameters in their lookup tables when necessary. For example, if you had loaded your Lookup table from a property file containing the following lines:
 editor=notepad.exe
 editor.linux=emacs
 editor.mac=simpletext
 

Then, if you invoke this method passing the key value of "editor" (as in getString("emacs")), it will return "emacs" on a system whose "os.name" is "linux" and "simpletext" on a operating system whose name matches "mac", on all other operating systems the value "notepad.exe" will be returned. This provides a way to configure operating specific settings in your configuration files.

The operating specific tag is constructed appending a period followed by the lower case value of System.getProperty("os.name") to the key you provide during your lookup.

There are two special cases currently. If the lower case value of "os.name" starts with "mac", then the tag will be ".mac". If the lower case value of "os.name" starts with "win", then the tag will be ".windows".

Parameters:
lookup - Lookup table to search the value for. Note, that all searches will first be done with the operating specific tag and then without.
Returns:
Lookup table which checks for for a operating specific setting before using the default setting.
Since:
1.0
See Also:
ProcessInvoker


Copyright 1998-1998-2006 null. All Rights Reserved.