ContentsJava Class DesignUsing Variables
Previous: Registering a New Command | Next: Setting Variables

Using Variables

The best way for a command to receive information from the web page is by the use of command parameters.  Sometimes, however, it is more convenient to read variables which were previously set by the execution of commands or by the WELD framework.

The Interp object which is passed to your cmdProc() provides two methods to access variables (overloads of Interp.getVar()).  One will get either a scalar variable or an array member, while the other is only useful for array members.  The first parameter(s) specify the variable name, and the last identifies the scope that the variable is found in (this is TCL.GLOBAL_ONLY for WELD).

The return type for getVar() is a TclObject, but may be immediately converted to String by TclObject.toString().

This example accepts a parameter which specifies a color name, then searches for that color in a file specified by the ColorFile variable.  If the color is found in that file, the HTML color code is given as the result.  If not, the result is set to the name that was passed in:

package us.oh.state.dot.OHPS.commands;

import java.io.*;
import java.util.*;
import tcl.lang.*;

public class ColorCmd implements Command {

public void cmdProc(Interp interp, TclObject [] args) throws TclException {
  if (args.length != 2)
    throw new TclNumArgsException(interp, 1, args, "name");
  String colorFile = null;
  colorFile = interp.getVar("ColorFile", TCL.GLOBAL_ONLY).toString();
  String colorCode = lookup(args[1].toString(), colorFile);
  if (colorCode == null) interp.setResult(args[1]);
  else interp.setResult(TclString.newInstance(colorCode));
}

String lookup(String color, String fname) {
  color = color.toLowerCase();
  String htmlCode = null;
  try {
    BufferedReader rdr = new BufferedReader(new FileReader(fname));
    String rec = null;
    while (true) {
      rec = rdr.readLine();
      if (rec == null) break;
      if (rec.startsWith("!")) continue;
      if (rec.indexOf(color) > -1) {
        StringTokenizer st = new StringTokenizer(rec.toLowerCase());
        if (st.countTokens() < 4) continue;
        int red = Integer.parseInt(st.nextToken());
        int green = Integer.parseInt(st.nextToken());
        int blue = Integer.parseInt(st.nextToken());
        StringBuffer sb = new StringBuffer(st.nextToken());
        while (st.hasMoreTokens()) {
          sb.append(' ');
          sb.append(st.nextToken());
        }
        if (color.equals(sb.toString())) {
          htmlCode = "#" + Integer.toHexString(red)
             + Integer.toHexString(green) + Integer.toHexString(blue);
          break;
        }
      }
    }
    rdr.close();
  }
  catch (IOException e) {
  }
  return htmlCode;
}

}

If the ColorFile variable is not set, a TclException will be thrown and an error message will appear in the web browser.  Variables like this one are best set in the servlet properties file, which may contain variable initialization such as follows:

# Variables
var.CounterFile=/vqserver/counters.dat
var.IncludeDir=/vqserver/include
var.ColorFile=r:/rgb.txt

ContentsJava Class DesignUsing Variables
Previous: Registering a New Command | Next: Setting Variables

Modified: Wed Apr 19 09:32:49 EDT 2000