ContentsJava Class DesignSetting Variables
Previous: Using Variables | Next: Using Other Commands

Setting Variables

It is the frequent duty of commands to set variables as the result of execution.  By doing this, the web page designer is free to use the variables as needed and allows a simpler use of the returned result.

The names and meanings of all variables which are gotten or set must be documented along with command usage.

By convention, all variables which Java commands interact with should be given a name that starts with an upper-case character.

When there are several variables that need to be made available to the web page designer, consider using an array instead of individual variables.  Arrays are much more efficient and they help the web page designer keep things together.

Here's another version of the previous command (changes highlighted), which also sets array variables for each of the color components and the name.

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(interp, args[1].toString(), colorFile);
  if (colorCode == null) interp.setResult(args[1]);
  else interp.setResult(TclString.newInstance(colorCode));
}

String lookup(Interp p, String color, String fname) throws TclException {
  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);
          p.setVar("Color", "Red",
            TclString.newInstance(Integer.toString(red)), TCL.GLOBAL_ONLY);
          p.setVar("Color", "Green",
            TclString.newInstance(Integer.toString(green)), TCL.GLOBAL_ONLY);
          p.setVar("Color", "Blue",
            TclString.newInstance(Integer.toString(blue)), TCL.GLOBAL_ONLY);
          p.setVar("Color", "Name",
            TclString.newInstance(color), TCL.GLOBAL_ONLY);
          break;
        }
      }
    }
    rdr.close();
  }
  catch (IOException e) {
  }
  return htmlCode;
}

}

After this command is used, the web page designer will have access to four new variables:

$Color(Red)the decimal value of the red component
$Color(Green)the decimal value of the green component
$Color(Blue)the decimal value of the blue component
$Color(Name)the name of the color

ContentsJava Class DesignSetting Variables
Previous: Using Variables | Next: Using Other Commands

Modified: Wed Apr 19 10:10:51 EDT 2000