1 3 5 22 23 package edu.neu.ccs.jmk; 24 25 33 final class Command 34 { 35 private Operator operator; 36 private Function operands; private boolean ignore = false; 39 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 63 void run(Rule target, Rule first, Rule[] newer, String match, 64 boolean justPrinting, java.io.PrintWriter out) 65 throws CommandFailedException 66 { 67 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 Value[] params = new Value [] { at, lt, qm, pc }; 80 81 Value result = null; 83 try { 84 result = operands.invoke(params, null); 85 } 86 catch (Exception ex) { 87 String msg = "Command failed: " + ex.getMessage(); 88 throw new CommandFailedException(msg); 89 } 90 91 if (!StringList.isStringList(result)) { 92 String msg 93 = "Command failed: Cannot convert operands to a list of strings"; 94 throw new CommandFailedException(msg); 95 } 96 97 StringList sl = (StringList)result; 99 int len = StringList.length(sl); 100 String [] args = new String [len]; 101 for (int i = 0; sl != null; sl = sl.getRest()) 102 args[i++] = StringUtils.localizePaths(sl.getString()); 103 104 out.print(operator.getName()); 106 for (int i = 0; i < args.length; i++) 107 out.print(" " + args[i]); 108 out.println(); 109 110 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 t) { 122 throw new CommandFailedException(t.toString()); 123 } 124 } 125 126 129 final static String [] commandArgs = {"@", "<", "?", "%"}; 130 } 131 | Popular Tags |