KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > Command


1 // $Id: Command.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // A command specified by a rule.
4

5 /*
6  * Copyright 1997 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 /**
26  * A command specified by a rule.
27  * Each command has an operator which is applied to an array of
28  * strings generated by a function. To produce the strings, the
29  * function is applied to the variables set when a rule is invoked.
30  * @version May 1999
31  * @author John D. Ramsdell
32  */

33 final class Command
34 {
35   private Operator operator;
36   private Function operands; // A function that produces a string list
37
private boolean ignore = false; // Ignore errors?
38

39   /**
40    * Creates a command from an operator and a function.
41    */

42   Command(Operator operator, Function operands) {
43     this.operator = operator;
44     this.operands = operands;
45   }
46
47   void setIgnore(boolean ignore) {
48     this.ignore = ignore;
49   }
50
51   /**
52    * Runs a command.
53    * It produces an argument list from the operands and then applies
54    * the operator to the arguments. Each command variable is
55    * replaced by its value.
56    * @param target the origin for the value of '@' command variable
57    * @param first the origin for the value of '<' command variable
58    * @param newer the origin for the value of '?' command variable
59    * @param match the origin for the value of '%' command variable
60    * @param justPrinting if true, print but do not run commands
61    * @param out the place to write messages
62    */

63   void run(Rule target, Rule first, Rule[] newer, String JavaDoc match,
64        boolean justPrinting, java.io.PrintWriter JavaDoc out)
65        throws CommandFailedException
66   {
67     // Construct the function's arguments
68
StringList at = new StringList(target.getTarget());
69     StringList lt = null;
70     if (first != null)
71       lt = new StringList(first.getTarget());
72     StringList qm = null;
73     for (int i = newer.length - 1; i >= 0; i--)
74       qm = new StringList(newer[i].getTarget(), qm);
75     StringList pc = null;
76     if (match != null)
77       pc = new StringList(match);
78     // This must match Command.commandArgs
79
Value[] params = new Value [] { at, lt, qm, pc };
80
81     // Generate operands
82
Value result = null;
83     try {
84       result = operands.invoke(params, null);
85     }
86     catch (Exception JavaDoc ex) {
87       String JavaDoc msg = "Command failed: " + ex.getMessage();
88       throw new CommandFailedException(msg);
89     }
90
91     if (!StringList.isStringList(result)) {
92       String JavaDoc msg
93     = "Command failed: Cannot convert operands to a list of strings";
94       throw new CommandFailedException(msg);
95     }
96
97     // Convert operand into a localized array of strings
98
StringList sl = (StringList)result;
99     int len = StringList.length(sl);
100     String JavaDoc[] args = new String JavaDoc[len];
101     for (int i = 0; sl != null; sl = sl.getRest())
102       args[i++] = StringUtils.localizePaths(sl.getString());
103
104     // Print the command after command variable substitution.
105
out.print(operator.getName());
106     for (int i = 0; i < args.length; i++)
107       out.print(" " + args[i]);
108     out.println();
109
110     // Run the command.
111
try {
112       if (!justPrinting)
113     operator.exec(args, out);
114     }
115     catch (CommandFailedException ex) {
116       if (ignore)
117     out.println("Command failed: " + ex.getMessage() + " (ignored)");
118       else
119     throw ex;
120     }
121     catch (Throwable JavaDoc t) {
122       throw new CommandFailedException(t.toString());
123     }
124   }
125
126   /*
127    * argument list used by the loader.
128    */

129   final static String JavaDoc[] commandArgs = {"@", "<", "?", "%"};
130 }
131
Popular Tags