JCLF
Utilities

Description of some JCLF utilities

Carlos Amengual

Last Update: Nov 23, 2021

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 more complete description of its use.

Package io.sf.jclf.app

CommandLine

This class not only makes easier to handle command-line parameters used to start a Java application, it can also unify 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:

  • Only single-letter parameters can have name=value assignations on it.
  • Only double-dash parameters can have values assigned by equals.
  • Double-dash options can't have contiguous values (e.g. -p2 is valid but not --p2).

For example,

  • it is legal: -s -p2 -Dname1=value1 -Dname2=value2 --path=/usr/bin
  • It is not legal: -p=2 -q=3

While these modifiers prefixed by a "-" or "--" are indistinctly called "parameters" or "options", in the documentation of some applications the word "option" is reserved 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 io.sf.jclf.io

WildcardFilter

This class 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 io.sf.jclf.io.Dir class is provided as an example of the use of WildcardFilter to get directory listings.

Package io.sf.jclf.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", "24-inch monitor"); m.put("code", new Integer(11209)); m.put("price", new Double(234.92)); String formatted_output = fmt.format(m);

Package io.sf.jclf.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()