Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DefaultInterpreter.java

00001 /*
00002  * DefaultInterpreter.java
00003  *
00004  * Created on 28 November 2002, 13:56
00005  */
00006 
00007 package GuiComponents.scripting;
00008 import GuiComponents.Console.*;
00009 import org.apache.bsf.*;
00010 import java.io.*;
00011 import java.awt.event.*;
00012 import java.util.*;
00013 
00028 public class DefaultInterpreter implements ActionListener, NameCompletionListener, Interpreter {
00029     protected JConsole console; 
00030     protected BSFEngine engine;
00031     protected NameCompleter completer;
00032  
00033     protected String defaultPath;
00034     protected String consoleCommandString = ".";
00035     protected char endCommandChar = '\n';
00036     protected char lineContinueChar = '\\';
00037     protected char blockStartChar = '{';
00038     protected char blockEndChar = '}';
00039     
00040     private int nestLevel = 0;
00041     private StringBuffer buffered = new StringBuffer();
00042     
00044     public DefaultInterpreter(JConsole console, BSFEngine engine) {
00045         this(console,  engine, null);
00046     }
00047     
00048     public DefaultInterpreter(JConsole console, BSFEngine engine, NameCompleter name) {
00049         this.completer = name;
00050         this.console = console;
00051         console.addActionListener(this);
00052         console.addNameCompletionListener(this);
00053         console.setPrompt("bsf> ");
00054         this.engine = engine;
00055         printStartMaterial();
00056         console.initialize();
00057         defaultPath = System.getProperty("sct.daq.root") + java.io.File.pathSeparatorChar + "share" +java.io.File.pathSeparatorChar + "scripts" + java.io.File.pathSeparatorChar ;
00058     }
00059     
00060     public JConsole getConsole() {
00061         return console;
00062     }
00063     
00068     protected boolean printObject(Object val) {
00069         if (val == null) return false;
00070         return true;
00071     }
00072     
00076     protected boolean startBlock(String line) {        
00077         StringBuffer sb = new StringBuffer(line.trim());
00078         if ((sb.length() > 0) && (sb.charAt(sb.length()-1) == blockStartChar)) return true;
00079         return false;
00080     }
00081     
00085     protected boolean endBlock(String line) {        
00086         StringBuffer sb = new StringBuffer(line.trim());
00087         if ((sb.length() > 0) && (sb.charAt(sb.length()-1) == blockEndChar)) return true;
00088         return false;
00089     }
00090     
00094     protected boolean continueLine(String line) {        
00095         StringBuffer sb = new StringBuffer(line.trim());
00096         if ((sb.length() > 0) && (sb.charAt(sb.length()-1) == lineContinueChar)) return true;
00097         return false;
00098     }
00099     
00100     public String toString() {
00101         return "DefaultInterpreter";
00102     }
00103       
00104     protected int nestLevel() {
00105         return nestLevel;
00106     }
00107     
00108         
00112     protected void incIndent() {
00113         nestLevel++;
00114         setIndent(nestLevel);
00115     }
00116     
00117     protected void decIndent() {        
00118         nestLevel--;
00119         if (nestLevel < 0) nestLevel = 0;            
00120         setIndent(nestLevel);
00121     }
00122     
00123     protected void setIndent(int level) {
00124         String indent="";
00125         for (int i=0; i<level; i++)
00126             indent += "\t";
00127         console.setPostPrompt(indent);
00128     }
00129     
00134     protected void printStartMaterial() {        
00135     }
00136     
00140     protected void printHelp() {
00141         
00142     }
00143     
00148     protected void consoleCommand(String command)  {
00149         if (command.startsWith(".x")) 
00150             loadFile(command.substring(2).trim());
00151             
00152     }
00153     
00157     protected void loadFile(String file) {
00158         File f = new File(file);
00159         if (!f.exists() || !f.isFile()) {
00160             if (file.indexOf(java.io.File.pathSeparatorChar) == -1) {
00161                 loadFile(defaultPath+file);
00162             } else {
00163                 console.println("Not a valid filename: " + file);
00164             }
00165             return;
00166         }
00167         
00168         //Read the whole file.
00169         try {
00170             FileReader fr = new FileReader(f);
00171             char[] buffer = new char[(int)f.length()];      //Can't read long files!
00172             fr.read(buffer);
00173             
00174             //Read, so execute it in the interpreter
00175             exec(new String(buffer), file, 0, 0);
00176         
00177         } catch (IOException ioe) {
00178             console.println("Error opening file: " + ioe.toString());
00179         }               
00180     }
00181     
00185     public List completeName(String curCmd) {
00186         if (completer == null) return null;
00187         
00188         //Find last occurrence of a space after triming
00189         curCmd = curCmd.trim();
00190         int index = curCmd.lastIndexOf(" ");
00191         String context;
00192         String part;
00193         if (index != -1) {
00194             context = curCmd.substring(0, index);
00195             part = curCmd.substring(index + 1);
00196         } else {
00197             context = "";
00198             part = curCmd;
00199         }
00200                 
00201         List l = completer.completeName(context, part); 
00202         //System.out.println("Completions");
00203         for (int i=0; i<l.size(); ++i) {
00204             l.set(i, context + (String)l.get(i));
00205             //System.out.println(l.get(i));
00206         }
00207         //System.out.println();
00208         return l;
00209     }
00210     
00211     
00215     public void exec(String s, String source, int row, int column) {
00216         try {            
00217             Object retVal = engine.eval(source, row, column, s);
00218             //engine.exec(source, row, column, s);
00219             if (printObject(retVal))
00220                 console.println(retVal.toString());
00221             else 
00222                 console.println("[null]");
00223         } catch (BSFException be) {
00224             console.println(be.toString());
00225             //System.out.println(be.getMessage());
00226             //be.printStackTrace();
00227         }            
00228     }
00229     
00230     public void exec(String s) {        
00231         exec(s, this.toString(), -1, -1);
00232     }
00233     
00237     public void actionPerformed(ActionEvent e) {
00238         String line = e.getActionCommand();        
00239         
00240         if (line.startsWith(consoleCommandString)) {
00241             consoleCommand(line);     
00242             return;
00243         }
00244             
00245         if (continueLine(line)) {
00246             buffered.append(line);
00247             return;
00248         }
00249 
00250         if (startBlock(line)) {
00251             buffered.append(line);
00252             incIndent();
00253             return;
00254         }
00255 
00256         if (endBlock(line)) {
00257             decIndent();
00258         }
00259 
00260         if (nestLevel > 0) {
00261             buffered.append(line);
00262             return;
00263         }
00264 
00265         line = buffered + line;
00266         buffered.delete(0, buffered.length());      //Empty buffer
00267         
00268         exec(line);                                 //Execute!
00269     }        
00270 }

Generated on Thu Jul 15 09:55:41 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5