1 25 26 package net.yagga.miniinstaller; 27 28 import net.yagga.util.*; 29 import java.io.*; 30 import java.util.*; 31 32 83 public class ScriptReader { 84 private Vector cmds=new Vector(); 85 public ScriptCommand scriptCommands[]; 86 87 private final static String changeSep="_SEP_STR="; 88 public final static String DEBUG="DEBUG"; 89 private static String sepString=","; 90 91 int lineNum; 92 public ScriptReader(String scriptFile) { 93 Reader r=new InputStreamReader(ResourceMgr.openResource(scriptFile)); 95 try{ 96 BufferedReader read=new BufferedReader(r); 97 String line=""; 98 boolean inComment=false; 99 boolean justComment=false; 100 int idx=-1; 101 while((line=read.readLine())!=null){ 102 lineNum++; 103 line=AssociativeFile.myTrim(line); 105 if(line.length()==0) 107 continue; 108 109 if((idx=line.indexOf("//"))!=-1) 111 line=line.substring(0,idx); 112 113 if((idx=line.indexOf("#"))!=-1) 115 line=line.substring(0,idx); 116 117 if((idx=line.indexOf("/*"))!=-1){ 119 inComment=true; 120 justComment=true; 121 line=line.substring(0,idx); 122 } 123 124 if((idx=line.indexOf("*/"))!=-1){ 126 inComment=false; 127 line=line.substring(idx+2); 128 } 129 130 if(inComment && !justComment) 131 continue; 132 133 justComment=false; 134 135 line=AssociativeFile.myTrim(line); 136 137 if(line.length()==0) 139 continue; 140 addCommand(line); 141 } 142 }catch(IOException e){} 143 for(int j=0;j<cmds.size();j++){ 145 ScriptCommand sc=(ScriptCommand)cmds.elementAt(j); 146 if(sc.meta || sc.cmdType==ScriptCommand.SET) 147 continue; 148 if(sc.cmdType!=ScriptCommand.START && sc.cmdType!=ScriptCommand.START_IMG){ 151 152 Vector v=new Vector(); 153 v.add("Start"); 154 v.add("Installing application"); 155 v.add("This wizard will guide you through the installation process"); 156 sc=new ScriptCommand("START",v); 157 cmds.insertElementAt(sc,j); 158 159 break; 160 } 161 else 162 break; 163 } 164 165 scriptCommands=new ScriptCommand[cmds.size()]; 166 int prevSC=-1; 169 for(int j=0;j<scriptCommands.length;j++){ 170 scriptCommands[j]=(ScriptCommand)cmds.get(j); 171 172 scriptCommands[j].prevInteractiveCommand=prevSC; 173 if(scriptCommands[j].isInteractive){ 174 prevSC=j; 175 } 176 } 177 } 178 179 180 private void addCommand(String str){ 181 if(str.startsWith(this.changeSep)){ 183 185 sepString=str.substring(this.changeSep.length(),str.length()); 186 return; 187 } 188 StringQuoteTokenizer st=new StringQuoteTokenizer(str,sepString,StringQuoteTokenizer.ESCAPE_IN_QUOTES); 190 String cmd=st.nextToken(); 191 Vector params=new Vector(); 192 while(st.hasMoreTokens()){ 193 String tk=st.nextToken(); 194 tk=AssociativeFile.myTrim(tk); 195 params.addElement(tk); 196 } 197 cmds.addElement(new ScriptCommand(cmd,params)); 198 } 199 200 public void debug(){ 201 System.out.println("SCRIPT:"); 202 for(int i=0;i<scriptCommands.length;i++){ 203 ScriptCommand sc=scriptCommands[i]; 204 System.out.println(i+"]"+sc.debug()); 205 } 206 System.out.println("SCRIPT end:"); 207 } 208 } 209 210 216 class ScriptCommand 217 { 218 219 public final static int SET=0; 220 public final static int INPUT=1; 221 public final static int INPUT_CONSOLE=2; 222 public final static int UNZIP=3; 223 public final static int EXEC_JAR_METHOD=4; 224 public final static int EXEC_JAVA=5; 225 public final static int WRITE=6; 226 public final static int WRITE_CONSOLE=7; 227 public final static int DISPLAY=8; 228 public final static int DISPLAY_CONSOLE=9; 229 public final static int SHOW=10; 230 public final static int INPUT_FILE=11; 231 public final static int FINAL=12; 232 public final static int EXEC_JAR=13; 233 public final static int COPY=14; 234 public final static int INPUT_DIR=15; 235 public final static int START=16; 236 public final static int START_IMG=17; 237 238 239 public final static int META=50; 240 public final static int _DIMS =META+0; 241 public final static int _TITLE =META+1; 242 public final static int _HIDE =META+2; 243 public final static int _SHOW =META+3; 244 public final static int _SET_COL =META+4; 245 public final static int _LOOK_AND_FEEL = META+5; 246 public final static int _SET_FONT = META+6; 247 public final static int _STEP_HIDE = META+7; 248 public final static int _STEP_SHOW = META+8; 249 250 252 public final static String metaStart="_"; 253 254 public final static int UNKNOWN=99; 255 private String cmd_tags[]={"SET","INPUT","INPUT_CONSOLE","UNZIP","EXEC_JAR_METHOD","EXEC_JAVA", 257 "WRITE","WRITE_CONSOLE","DISPLAY","DISPLAY_CONSOLE","SHOW","INPUT_FILE","FINAL", 258 "EXEC_JAR","COPY","INPUT_DIR","START","START_IMG"}; 259 260 private String meta_cmd_tags[]={"_DIMS", "_TITLE","_HIDE","_SHOW","_SET_COL", 261 "_LOOK_AND_FEEL","_SET_FONT","_STEP_HIDE","_STEP_SHOW"}; 262 public int cmdType=UNKNOWN; 264 public String name=""; public String params[]; 266 270 public String value=null; 271 boolean meta=false; 272 275 public boolean isStep=true; 276 280 public boolean isInteractive=false; 281 287 public int prevInteractiveCommand=-1; 288 289 295 ScriptCommand(String cmd, Vector p){ 296 if(cmd.startsWith(metaStart)) 298 { 299 parseMeta(cmd,p); 300 return; 301 } 302 try{ 303 for(int i=0;i<cmd_tags.length;i++){ 304 if(cmd.equals(cmd_tags[i])){ 305 cmdType=i; 307 switch(cmdType){ 308 case WRITE: 311 case WRITE_CONSOLE: 312 isInteractive=true; 313 params=new String [2]; 314 name=(String )p.elementAt(0); 315 if(p.size()==3){ 316 params[0]=(String )p.elementAt(1); 317 params[1]=(String )p.elementAt(2); 318 } 319 else{ 320 params[0]=null; 321 params[1]=(String )p.elementAt(1); 322 } 323 break; 324 case DISPLAY: 325 case DISPLAY_CONSOLE: 326 case SHOW: 327 isInteractive=true; 328 params=new String [1]; 329 name=(String )p.elementAt(0); 330 params[0]=(String )p.elementAt(1); 331 break; 332 case SET: 334 isStep=false; 335 params=new String [2]; 336 params[0]=(String )p.elementAt(0); 337 params[1]=(String )p.elementAt(1); 338 break; 339 case START: 341 case START_IMG: 342 isInteractive=true; 343 name=(String )p.elementAt(0); 344 params=new String [2]; 345 params[0]=(String )p.elementAt(1); 346 params[1]=(String )p.elementAt(2); 347 break; 348 case FINAL: 350 name=(String )p.elementAt(0); 351 params=new String [4]; 352 params[0]=(String )p.elementAt(1); 353 params[1]=(String )p.elementAt(2); 354 if(p.size()>=4) 355 params[2]=(String )p.elementAt(3); 356 else 357 params[2]=null; 358 if(p.size()>=5) 359 params[3]=(String )p.elementAt(4); 360 else 361 params[3]=null; 362 break; 363 case INPUT_CONSOLE: 365 isInteractive=true; 366 case UNZIP: 367 case COPY: 368 isInteractive=true; 369 name=(String )p.elementAt(0); 370 params=new String [3]; 371 params[0]=(String )p.elementAt(1); 372 params[1]=(String )p.elementAt(2); 373 params[2]=(String )p.elementAt(3); 374 break; 375 case INPUT: 377 case INPUT_FILE: 378 case INPUT_DIR: 379 isInteractive=true; 380 name=(String )p.elementAt(0); 381 params=new String [4]; 382 params[0]=(String )p.elementAt(1); 383 params[1]=(String )p.elementAt(2); 384 params[2]=(String )p.elementAt(3); 385 if(p.size()<=4) 386 params[3]=""; 387 else 388 params[3]=(String )p.elementAt(4); 389 break; 390 case EXEC_JAR_METHOD: 392 case EXEC_JAVA: 393 case EXEC_JAR: 394 isInteractive=true; 396 name=(String )p.elementAt(0); 397 params=new String [p.size()-1]; 398 for(i=1;i<p.size();i++) 399 params[i-1]=(String )p.elementAt(i); 400 break; 401 } 402 return; 403 } 404 } 405 cmdType=UNKNOWN; 406 } 407 catch(ArrayIndexOutOfBoundsException aob){ 408 Ut.error("Error at config file, command:"+cmd+":"+aob); 409 } 410 } 411 412 private void parseMeta(String cmd, Vector p){ 413 try{ 414 for(int i=0;i<meta_cmd_tags.length;i++){ 415 isStep=false; 416 if(cmd.equals(meta_cmd_tags[i])){ 417 cmdType=META+i; 419 switch(cmdType){ 420 case _HIDE: 422 case _SHOW: 423 case _STEP_SHOW: 424 case _STEP_HIDE: 425 params=new String [0]; 426 break; 427 case _TITLE: 429 case _LOOK_AND_FEEL: 430 params=new String [1]; 431 params[0]=(String )p.elementAt(0); 432 break; 433 case _DIMS: 435 params=new String [2]; 436 params[0]=(String )p.elementAt(0); 437 params[1]=(String )p.elementAt(1); 438 break; 439 case _SET_COL: 441 case _SET_FONT: 442 params=new String [4]; 443 params[0]=(String )p.elementAt(0); 444 params[1]=(String )p.elementAt(1); 445 params[2]=(String )p.elementAt(2); 446 params[3]=(String )p.elementAt(3); 447 break; 448 449 } 450 meta=true; 451 return; 452 453 } 454 } 455 cmdType=UNKNOWN; 456 } 457 catch(ArrayIndexOutOfBoundsException aob){ 458 Ut.error("Error at config file, command:"+cmd+":"+aob); 459 } 460 } 461 462 public boolean isMeta(){ 463 return meta; 464 } 465 466 public String debug(){ 467 String buf=""; 468 if(cmdType==UNKNOWN){ 469 return "UNKNOWN"; 470 } 471 if(this.meta) 472 cmdType-=this.META; 473 buf=cmd_tags[cmdType]+" ("+params.length+" args):"; 474 if(this.meta) 475 cmdType+=this.META; 476 for(int i=0;i<params.length;i++) 477 buf+="["+i+"]'"+params[i]+"' "; 478 buf+=isStep? "STEP " : "noS "; 479 buf+=isInteractive? "INTER " : "noI "; 480 buf+=" prev="+this.prevInteractiveCommand; 481 482 return buf; 483 } 485 } | Popular Tags |