001: /*
002: * JavaPropCmd.java --
003: *
004: * Implements the built-in "java::prop" command.
005: *
006: * Copyright (c) 1997 Sun Microsystems, Inc.
007: *
008: * See the file "license.terms" for information on usage and
009: * redistribution of this file, and for a DISCLAIMER OF ALL
010: * WARRANTIES.
011: *
012: * RCS: @(#) $Id: JavaPropCmd.java,v 1.3 1999/05/09 22:23:46 dejong Exp $
013: *
014: */
015:
016: package tcl.lang;
017:
018: /**
019: * This class implements the built-in "java::prop" command.
020: * The java::prop command is used to manipulate Java Bean properties from Tcl.
021: */
022:
023: class JavaPropCmd implements Command {
024:
025: /*----------------------------------------------------------------------
026: *
027: * cmdProc --
028: *
029: * This procedure is invoked to process the "java::prop" Tcl
030: * command. See the user documentation for details on what they
031: * do.
032: *
033: * Results:
034: * None.
035: *
036: * Side effects:
037: * A standard Tcl result is stored in the interpreter.
038: *
039: *----------------------------------------------------------------------
040: */
041:
042: public void cmdProc(Interp interp, // Current interpreter.
043: TclObject argv[]) // Argument list.
044: throws TclException // A standard Tcl exception.
045: {
046: boolean query;
047: int objIndex;
048: boolean convert;
049:
050: if (argv.length < 3) {
051: throw new TclException(interp, usage(argv[0]));
052: }
053:
054: // Check the validity of the arguments. N.B., the -noconvert flag
055: // is allowed only in value query.
056:
057: String arg1 = argv[1].toString();
058: if ((arg1.length() >= 2) && ("-noconvert".startsWith(arg1))) {
059: convert = false;
060: objIndex = 2;
061:
062: if (argv.length == 4) {
063: query = true;
064: } else {
065: throw new TclException(interp, usage(argv[0]));
066: }
067: } else {
068: convert = true;
069: objIndex = 1;
070:
071: if (argv.length == 3) {
072: query = true;
073: } else if ((argv.length % 2) == 0) {
074: query = false;
075: } else {
076: throw new TclException(interp, usage(argv[0]));
077: }
078: }
079:
080: if (query) {
081: // Query one property.
082:
083: interp.setResult(JavaInvoke.getProperty(interp,
084: argv[objIndex], argv[objIndex + 1], convert));
085: } else {
086: // Set one or more properties.
087:
088: for (int i = objIndex + 1; i < argv.length; i += 2) {
089: JavaInvoke.setProperty(interp, argv[objIndex], argv[i],
090: argv[i + 1]);
091: }
092:
093: interp.resetResult();
094: }
095: }
096:
097: /*----------------------------------------------------------------------
098: *
099: * usage --
100: *
101: * Returns the usage string for the java::prop command.
102: *
103: * Results:
104: * Returns the usage string for the java::prop command.
105: *
106: * Side effects:
107: * None.
108: *
109: *----------------------------------------------------------------------
110: */
111:
112: private static final String usage(TclObject cmd) // The command name.
113: {
114: return "wrong # args: should be \""
115: + cmd
116: + " ?-noconvert? javaObj property ?value property value ...?\"";
117: }
118:
119: } // end JavaPropCmd
|