KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > miniinstaller > ScriptReader


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.miniinstaller;
27
28 import net.yagga.util.*;
29 import java.io.*;
30 import java.util.*;
31
32 /**
33   Reads a script file
34   and give commands in sequence.
35
36   A script file is an ordered list of actions:<BR>
37   each action is:<BR>
38   COMMAND,CMD_NAME,PARAM1,PARAM2,.. PARAMN<BR>
39
40   commands and params (cmd_name is omittede here for simplicity) available are:
41     <PRE>
42     SET varname value
43     START Title MsgString
44     START_IMG Title ImgName
45     INPUT_DIR Title MsgString RetVariable [Default]
46     INPUT Title MsgString RetVariable [Default]
47     INPUT_FILE Title MsgString RetVariable [Default]
48     CONSOLE_INPUT Title MsgString RetVariable
49     UNZIP Descr zipfile destdir
50         COPY src_dir filename_filter(s) dest_dir
51     EXEC_JAR_METHOD jarfile classname methodname param(s)
52     EXEC_JAVA classname methodname params
53     EXEC_JAR jarfile params
54     WRITE msg
55     WRITE_CONSOLE msg
56     DISPLAY filename
57     DISPLAY_CONSOLE filename
58     SHOW imgName
59     COPY srcDir fileNames/fileMask destDir
60     FINAL title,msg [,shell command,shell command caption]
61    </PRE>
62   values can be strings (with spaces, but without commas, and quted strings..
63   with the usual value "...."<BR>
64   in param values there is variable expansion
65   in the form $VAR_NAME$ where $VARNAME is one set in:<BR>
66   - INPUT/CONSOLE INPUT<BR>
67   - SET<BR>
68 <BR>
69   There are some Predefined variables:
70     <PRE>
71   $USER_NAME$ name of uswer logged on to system
72   $USER_HOME$ user's home directory
73   $JAVA_HOME$ java home directory, be it jre home or jdk home
74   $JAVA_VERSION$ java version
75   $OS_NAME$ operating system name
76   $OS_ARCH$ operating system hardware architecture
77   $OS_VERSION$ operating system version
78     $_$ result of last command execution (Java execution..)
79    </PRE>
80     *
81     * @author Walter Gamba
82 */

83 public class ScriptReader {
84   private Vector cmds=new Vector();
85   public ScriptCommand scriptCommands[];
86
87   private final static String JavaDoc changeSep="_SEP_STR=";
88     public final static String JavaDoc DEBUG="DEBUG";
89   private static String JavaDoc sepString=",";
90
91   int lineNum;
92   public ScriptReader(String JavaDoc scriptFile) {
93         //Reader r=new InputStreamReader(Ut.openIn(scriptFile));
94
Reader r=new InputStreamReader(ResourceMgr.openResource(scriptFile));
95         try{
96             BufferedReader read=new BufferedReader(r);
97             String JavaDoc line="";
98             boolean inComment=false;
99             boolean justComment=false;
100             int idx=-1;
101             while((line=read.readLine())!=null){
102                 lineNum++;
103                 //trimm spaces
104
line=AssociativeFile.myTrim(line);
105                 //empty lines
106
if(line.length()==0)
107                     continue;
108
109                 //comments like C++ "//"
110
if((idx=line.indexOf("//"))!=-1)
111                     line=line.substring(0,idx);
112
113                 //comments like Perl "#"
114
if((idx=line.indexOf("#"))!=-1)
115                     line=line.substring(0,idx);
116
117                 //comments like c "/*"
118
if((idx=line.indexOf("/*"))!=-1){
119                     inComment=true;
120                     justComment=true;
121                     line=line.substring(0,idx);
122                 }
123
124                 //comments like stile c "*/"
125
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                 //check for empty lines again
138
if(line.length()==0)
139                     continue;
140         addCommand(line);
141             }
142         }catch(IOException e){}
143     //check that first NON meta cmd is Start, else set it by default
144
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 we arrive here we are inspecting
149
//the first non META or non SET) command..
150
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     //stores ScriptCommand as an array
167
//and saves also prevInteractiveCommand
168
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 JavaDoc str){
181     //check for sepchar
182
if(str.startsWith(this.changeSep)){
183       //sets separator
184

185       sepString=str.substring(this.changeSep.length(),str.length());
186       return;
187     }
188     //
189
StringQuoteTokenizer st=new StringQuoteTokenizer(str,sepString,StringQuoteTokenizer.ESCAPE_IN_QUOTES);
190     String JavaDoc cmd=st.nextToken();
191     Vector params=new Vector();
192     while(st.hasMoreTokens()){
193       String JavaDoc 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 /**
211  * Simple helper class that handles every command.
212  * It provides handy definitions, and convenience methods.
213  * @author Walter Gamba
214  * @version 1.0
215  */

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 // public final static int _SEP_CHAR=META+2;
251

252   public final static String JavaDoc metaStart="_";
253
254   public final static int UNKNOWN=99;
255   //in the same order!!
256
private String JavaDoc 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 JavaDoc meta_cmd_tags[]={"_DIMS", "_TITLE","_HIDE","_SHOW","_SET_COL",
261         "_LOOK_AND_FEEL","_SET_FONT","_STEP_HIDE","_STEP_SHOW"};
262   //
263
public int cmdType=UNKNOWN;
264   public String JavaDoc name=""; //name of the section, can be null for SET and _META
265
public String JavaDoc params[];
266   /**
267    * Default value or value inputted by user.
268    * Used mainly by back function.
269    */

270   public String JavaDoc value=null;
271   boolean meta=false;
272   /**
273    * Denotes state that require highlighting in wizard frame as a step
274    */

275   public boolean isStep=true;
276   /**
277    * Denotes state that require user interaction.
278    * Every state that is Interactev MUST also be a Step!
279    */

280   public boolean isInteractive=false;
281   /**
282    * stores first interactive ScriptCommand preceding this one.
283    * Used during backwards navigation.<BR>
284    * This value is to be set once all th SCriptComamnds are read in.
285    * @note this attribute is valid only in interactive ScriptCommands
286    */

287   public int prevInteractiveCommand=-1;
288
289     /**
290      * Build a command given a command name and its arguments
291      * @param cmd the command name, as defined in this class
292      * @param p a vector of String s representing command arguemnts
293      * @author Walter Gamba
294      */

295   ScriptCommand(String JavaDoc cmd, Vector p){
296     //System.out.println("look for "+cmd);
297
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         //System.out.println("FOUND "+cmd);
306
cmdType=i;
307         switch(cmdType){
308         //0 params..
309
//1 param + 1 di titolo + 1 opzionale
310
case WRITE:
311         case WRITE_CONSOLE:
312                     isInteractive=true;
313           params=new String JavaDoc[2];
314           name=(String JavaDoc)p.elementAt(0);
315                     if(p.size()==3){
316                         params[0]=(String JavaDoc)p.elementAt(1);
317                         params[1]=(String JavaDoc)p.elementAt(2);
318                     }
319                     else{
320                         params[0]=null;
321                         params[1]=(String JavaDoc)p.elementAt(1);
322                     }
323                     break;
324         case DISPLAY:
325         case DISPLAY_CONSOLE:
326         case SHOW:
327                     isInteractive=true;
328           params=new String JavaDoc[1];
329           name=(String JavaDoc)p.elementAt(0);
330           params[0]=(String JavaDoc)p.elementAt(1);
331           break;
332         //2 params
333
case SET:
334           isStep=false;
335           params=new String JavaDoc[2];
336           params[0]=(String JavaDoc)p.elementAt(0);
337           params[1]=(String JavaDoc)p.elementAt(1);
338           break;
339         //2 params + 1 di titolo
340
case START:
341         case START_IMG:
342                     isInteractive=true;
343           name=(String JavaDoc)p.elementAt(0);
344           params=new String JavaDoc[2];
345           params[0]=(String JavaDoc)p.elementAt(1);
346           params[1]=(String JavaDoc)p.elementAt(2);
347          break;
348          //2 params + 2 opzionale (stringa shell + caption)
349
case FINAL:
350           name=(String JavaDoc)p.elementAt(0);
351           params=new String JavaDoc[4];
352           params[0]=(String JavaDoc)p.elementAt(1);
353           params[1]=(String JavaDoc)p.elementAt(2);
354                     if(p.size()>=4)
355                                 params[2]=(String JavaDoc)p.elementAt(3);
356                     else
357                     params[2]=null;
358                     if(p.size()>=5)
359                                 params[3]=(String JavaDoc)p.elementAt(4);
360                     else
361                     params[3]=null;
362                     break;
363         //3 params + 1 di titolo
364
case INPUT_CONSOLE:
365           isInteractive=true;
366         case UNZIP:
367         case COPY:
368                     isInteractive=true;
369           name=(String JavaDoc)p.elementAt(0);
370           params=new String JavaDoc[3];
371           params[0]=(String JavaDoc)p.elementAt(1);
372           params[1]=(String JavaDoc)p.elementAt(2);
373           params[2]=(String JavaDoc)p.elementAt(3);
374           break;
375         //3params + (1 di titolo) + opzionale DEFAULT ultimo parametro
376
case INPUT:
377         case INPUT_FILE:
378         case INPUT_DIR:
379           isInteractive=true;
380           name=(String JavaDoc)p.elementAt(0);
381           params=new String JavaDoc[4];
382           params[0]=(String JavaDoc)p.elementAt(1);
383           params[1]=(String JavaDoc)p.elementAt(2);
384           params[2]=(String JavaDoc)p.elementAt(3);
385           if(p.size()<=4)
386             params[3]="";
387           else
388             params[3]=(String JavaDoc)p.elementAt(4);
389           break;
390           //special
391
case EXEC_JAR_METHOD:
392         case EXEC_JAVA:
393         case EXEC_JAR:
394           //
395
isInteractive=true;
396           name=(String JavaDoc)p.elementAt(0);
397           params=new String JavaDoc[p.size()-1];
398           for(i=1;i<p.size();i++)
399             params[i-1]=(String JavaDoc)p.elementAt(i);
400           break;
401         }
402         return;
403       }
404     }
405     cmdType=UNKNOWN;
406     }
407     catch(ArrayIndexOutOfBoundsException JavaDoc aob){
408       Ut.error("Error at config file, command:"+cmd+":"+aob);
409     }
410   }
411
412   private void parseMeta(String JavaDoc 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         //System.out.println("FOUND "+cmd);
418
cmdType=META+i;
419         switch(cmdType){
420         //0 params..
421
case _HIDE:
422         case _SHOW:
423         case _STEP_SHOW:
424         case _STEP_HIDE:
425           params=new String JavaDoc[0];
426           break;
427         //1 param
428
case _TITLE:
429                 case _LOOK_AND_FEEL:
430           params=new String JavaDoc[1];
431           params[0]=(String JavaDoc)p.elementAt(0);
432           break;
433         //2 params
434
case _DIMS:
435           params=new String JavaDoc[2];
436           params[0]=(String JavaDoc)p.elementAt(0);
437           params[1]=(String JavaDoc)p.elementAt(1);
438           break;
439         //4 params
440
case _SET_COL:
441         case _SET_FONT:
442           params=new String JavaDoc[4];
443           params[0]=(String JavaDoc)p.elementAt(0);
444           params[1]=(String JavaDoc)p.elementAt(1);
445           params[2]=(String JavaDoc)p.elementAt(2);
446           params[3]=(String JavaDoc)p.elementAt(3);
447           break;
448
449         }
450         meta=true;
451         return;
452
453       }
454     }
455     cmdType=UNKNOWN;
456     }
457     catch(ArrayIndexOutOfBoundsException JavaDoc 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 JavaDoc debug(){
467     String JavaDoc 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     //return "";
484
}
485 }
Popular Tags