ContentsJava Class DesignUsing Other Commands
Previous: Setting Variables | Next: System Administration

Using Other Commands

Java commands may use other commands to perform their work.  This is the ultimate in code reuse and abstraction.  The commands may be written in Java, or may be procedures which are defined in a TCL file.

The Interp object provides an eval() method that will interpret the string passed to it and set the interpreter's result.  You may want to intercept this result (with Interp.getResult()) and act on it, or you may simply want to leave it be the result of your command.

This command is part of WELD, and is provided as a simple way for web page designers to produce the current time in a specified format.

package us.oh.state.common.commands;

import tcl.lang.*;

public class NowCmd implements Command {

public void cmdProc(Interp interp, TclObject argv[]) throws TclException {
  StringBuffer sb = null;
  for (int i = 1; i < argv.length; ++i) {
    if (sb == null) sb = new StringBuffer(" -format {");
    sb.append(' ');
    sb.append(argv[i]);
  }
  String format = null;
  if (sb != null) {
    sb.append("}");
    format = sb.toString();
  }
  else format = "";
  interp.eval("clock format [clock seconds]" + format);
}

}

As an alternative, this command could have been written in TCL and included as part of the session.tcl file (see the "script" property in the servlet properties file).  The TCL equivalent would look like this:

proc now {args} {
	if [llength $args] {
		return [clock format [clock seconds] -format $args]
		}
	return [clock format [clock seconds]]
	}

ContentsJava Class DesignUsing Other Commands
Previous: Setting Variables | Next: System Administration

Modified: Wed Apr 19 10:38:59 EDT 2000