| Home >> All >> org >> incenter >> [ ngbclient Javadoc ] |
Source code: org/incenter/ngbclient/Macro.java
1 package org.incenter.ngbclient; 2 import org.incenter.ngbclient.*; 3 // macro object 4 5 import java.lang.*; 6 import java.util.*; 7 8 public class Macro extends Object 9 { 10 private String name; // macros name 11 private String def; // definition string from res file 12 private Vector args; // args supplied by user 13 private Vector commands; // individual commands sep by ; 14 private int status, current; 15 public static final int M_NotEnoughCmds = -1; 16 public static final int M_NotEnoughArgs = -2; 17 public static final int M_OK = 0; 18 private macroProvider myparent; 19 20 public Macro(String _def) { 21 def = new String(_def); 22 args = commands = null; 23 name = null; 24 current = 0; 25 myparent = null; 26 parse(); 27 } 28 29 public void setParent(macroProvider g) { 30 myparent = g; 31 } 32 33 public Vector getCommands() { 34 return commands; 35 } 36 37 public String getDef() { 38 return def; 39 } 40 41 public Vector getCommand(String _args) { 42 if(status != M_OK) 43 return null; 44 45 Vector runcmds = new Vector(); 46 StringTokenizer tok = new StringTokenizer(_args); 47 args = new Vector(); 48 String buf, tmp = "", cmd = "", saved = ""; 49 int i, pos, argn = -1; 50 51 while(tok.hasMoreTokens()) 52 args.addElement(tok.nextToken()); 53 54 for(i=0;i<commands.size();i++) { 55 buf = (String)commands.elementAt(i); 56 tok = new StringTokenizer(buf); 57 while(tok.hasMoreTokens()) { 58 buf = tok.nextToken(); 59 if(buf.startsWith("#")) { 60 saved = "#"; 61 buf = buf.substring(1); 62 } else 63 saved = ""; 64 if(buf.startsWith("$")) { 65 // found a sub to make 66 try { 67 tmp = buf.substring(1); 68 argn = new Integer(tmp).intValue(); 69 cmd = cmd + " " + saved + (String)args.elementAt(argn); 70 } 71 catch(NumberFormatException e) { 72 // a special arg 73 if(tmp.startsWith("*")) 74 cmd = cmd + " " + saved + _args; 75 else if(tmp.startsWith("b")) { 76 // handle builtship 77 if(myparent != null) 78 if(myparent.getBuiltship() != null) { 79 cmd = cmd + " " + saved + 80 myparent.getBuiltship(); 81 } 82 } 83 84 } 85 catch(ArrayIndexOutOfBoundsException e) { 86 // bad argnumber 87 System.out.println("Bad arg number:" + argn); 88 } 89 } else 90 cmd = cmd + " " + saved + buf; 91 } 92 parsed:; 93 cmd = cmd.trim(); 94 runcmds.addElement(cmd); 95 cmd = ""; 96 } 97 return runcmds; 98 } 99 100 private void parse() { 101 StringTokenizer tok = new StringTokenizer(def); 102 String buf; 103 int pos; 104 105 if(tok.countTokens() < 2) { 106 status = M_NotEnoughCmds; 107 return; 108 } 109 commands = new Vector(); 110 name = tok.nextToken(); 111 tok = new StringTokenizer(def, ";"); 112 buf = tok.nextToken(); 113 pos = buf.indexOf(' '); 114 buf = buf.substring(pos+1); 115 commands.addElement(buf); 116 while(tok.hasMoreTokens()) { 117 buf = tok.nextToken(); 118 commands.addElement(buf); 119 } 120 status = M_OK; 121 } 122 123 public String getName() { 124 return name; 125 } 126 127 public void debug() { 128 int i; 129 130 if(status != M_OK) 131 System.out.println("Did not parse correctly"); 132 133 if(name != null) 134 System.out.println("Name:" + name); 135 136 if(commands != null) 137 for(i=0;i<commands.size();i++) 138 System.out.println("CMD:" + commands.elementAt(i)); 139 else 140 System.out.println("No commands"); 141 142 if(args != null) 143 for(i=0;i<args.size();i++) 144 System.out.println("ARG:" + args.elementAt(i)); 145 else 146 System.out.println("No args"); 147 148 } 149 150 public static void main(String s[]) { 151 Macro m = new Macro("m map $*"); 152 Macro n = new Macro("o ord $0 dest $1;repo"); 153 String cmd; 154 Vector cmds; 155 int i; 156 m.debug(); 157 n.debug(); 158 159 cmds = m.getCommand("/FG dsds"); 160 System.out.println("Parsed[/m /Fg dsds]:"); 161 for(i=0;i<cmds.size();i++) 162 System.out.println((String)cmds.elementAt(i) + " "); 163 System.out.print('\n'); 164 165 cmds = n.getCommand("100 /Oly"); 166 System.out.println("Parsed[/n 100 /Oly]:"); 167 for(i=0;i<cmds.size();i++) 168 System.out.println((String)cmds.elementAt(i) + " "); 169 System.out.print('\n'); 170 171 } 172 }