Description of some JCLF utilities

Carlos Amengual

Revision date: Feb. 17, 2006

This document describes some generic utility classes present in the JCLF packages. While this information may be enough to let you use the classes described here, you are invited to read the API docs to get a complete description of its use.

Package info.informatica.app

CommandLine

This class not only makes easier to handle command-line parameters used to start a Java application, it also unifies the way command-line elements are used across your applications.

The idea is that when you start an application through a command line interface, after specifying the name of the application, you have a series of command-line arguments, for example:

java MyApp -p2 -Dname1=value1 -Dname2=value2 [...] final arguments

where the first ones are "modifiers" or "parameters", and the last ones are the real "arguments". The modifiers are identified as such because they are preceded by a single or double dash.

When processing "modifiers", which can be options and parameters, the following rules are followed:

For example,

While these modifiers prefixed by a "-" or "--" are indistinctly called "parameters" or "options", in the documentation of some applications I reserve the word "option" only for boolean parameters.

Use the getParam and getAssignParam methods to retrieve parameter values, and the hasParameter method to test for options. When you do not know in advance which parameters could be set, you should use an uppercase letter to label parameter definitions (typically a "D"), and use the getParameterValues method to retrieve them.

Usage example:

Arguments: -afirst_arg -Dname1=value -Dlogic --dir=/var/tmp

 public static void main(String[] args){
   CommandLine cmdline = new CommandLine(args);
   String opt_a = cmdline.getParam("a");  // opt_a = "first_arg"
   Enumeration enum = cmdline.getParameterValues("D");
   while(enum.hasMoreElements()){
     String s = (String)enum.nextElement();  // s = "name1=value"
     ...                                     // s = "logic"
   }
   String param_dir = cmdline.getAssignParam("dir");  // param_dir = "/var/tmp"
 }
 

Package info.informatica.io

WildcardFilter

When the Java Servlet API became available and I started the migration from my Perl CGI scripts, I noticed the lack of any method to read directory contents according to familiar filename wildcards. While some Swing dialogs have the capability to filter filenames according to simple wildcards, there is no standard wildcard-enabled implementation of the FilenameFilter interface available in Java.

That is the reason why I wrote this class, which allows to specify a wildcard and then read directory contents according to it.

When using the constructor without arguments on a Unix™ or MS Windows™ system, the class will try to figure out which system is and set the appropriate functionality.

To set the wildcard, you just have to call the setWildcardPath method.

The info.informatica.io.Dir class is provided as an example of the use of WildcardFilter to get directory listings.

Package info.informatica.text

TokenParser

This class is a replacement for the StringTokenizer standard tokenizer, with enhanced functionality and a more modern interface. Otherwise, its use is the same. If you use the default constructor, for example with:

TokenParser tp = new TokenParser("first parameter,second parameter,\"This is one parameter, not two\",fourth,last param");

When you call the Iterator methods you get four parameters, where the third is "This is one parameter, not two".

Of course you can construct your TokenParser with your desired separator or grouping characters.

FormatPrinter

This class can be used instead of java.text.Format and related classes to print formatted values.

Read the API documentation for a more detailed description of what the different formatters can do (or write your own). The formats are an implementation to printf's format specifiers, with some additions, though not the 100% of the format specifiers have been implemented.

The following example is self-explanatory:

FormatPrinter fmt = new FormatPrinter("Cost of item number ${code,7.1d} (${item,-40.50s}) is ${price,8.2f}.");
Map m = new HashMap(); // Any implementation of Map is valid m.put("item", "15-inch monitor"); m.put("code", new Integer(11209)); m.put("price", new Double(234.92)); String formatted_output = fmt.format(m);

Package info.informatica.util

EnvironProperties

EnvironProperties is a direct replacement for java.util.Properties which supports the use of environment variables. This means that if you properties file is like this:

year=2003
month=05
date=${year}-${month}
file=myfile
dir=/path/to/${file}

When you use getProperty to get the date property, "2003-05" is returned, and dir returns "/path/to/myfile".

The setParse method can be used to turn parsing off and on, or you can use getUnparsedProperty to get the plain unparsed property (in our example, applying it to dir would return "/path/to/${file}").

As this class extends the standard Properties class, you can preserve its interface in your code, if you do not need the EnvironProperties-specific methods. For example, you can replace:

Properties p = new Properties()

with:

Properties p = new EnvironProperties()