1 19 package org.netbeans.mdrshell; 20 21 import java.util.*; 22 import java.io.*; 23 24 import org.openide.util.Lookup; 25 import org.netbeans.api.mdr.*; 26 27 import javax.jmi.reflect.*; 28 import javax.jmi.model.ModelElement; 29 30 import org.netbeans.mdr.storagemodel.*; 31 import org.netbeans.mdr.handlers.BaseObjectHandler; 32 33 38 public class Shell extends Object { 39 40 private static final char CONTINUE_CHAR = '\\'; 41 private static final char NEW_LINE = '\n'; 42 43 44 private Preprocessor preprocessor; 45 46 47 private boolean exit; 48 49 50 private Stack ioStack; 51 52 55 public PrintStream out; 56 57 58 private boolean resultPrint; 59 60 61 private boolean commandEcho; 62 63 64 private String prompt; 65 66 private Object current; 67 68 70 public Shell() { 71 preprocessor = new Preprocessor( this ); 72 exit = false; 73 out = System.out; 74 ioStack = new Stack(); 75 resultPrint = false; 76 commandEcho = true; 77 current = null; 78 } 79 80 private void execCommand(String command) throws Exception { 81 String preprocessedLine = preprocessor.processLine( command.toString() ); 82 83 85 if ( commandEcho ) { 86 out.println( preprocessedLine ); 87 } 88 89 Object result = Interpreter.processLine( preprocessedLine ); 90 if ( resultPrint ) { 91 Support.defaultPrint( out, result ); 92 } 93 } 94 95 97 99 public void exit() { 100 System.out.println( "Exiting" ); 101 exit = true; 102 } 103 104 106 public void print( Object o ) { 107 Support.defaultPrint( out, o ); 108 } 109 110 public void print( boolean what ) { 111 out.println( what ); 112 } 113 114 public void print( byte what ) { 115 out.println( what ); 116 } 117 118 public void print( short what ) { 119 out.println( what ); 120 } 121 122 public void print( char what ) { 123 out.println( what ); 124 } 125 126 public void print( int what ) { 127 out.println( what ); 128 } 129 130 public void print( long what ) { 131 out.println( what ); 132 } 133 134 public void print( float what ) { 135 out.println( what ); 136 } 137 138 public void print( double what ) { 139 out.println( what ); 140 } 141 142 public Object setActive(Object object) throws Exception { 143 retype(object); 144 return object; 145 } 146 147 public void retype(Object object) throws Exception { 148 execCommand("#define C$ ((" + object.getClass().getName() + ")CurrentObject)"); 149 } 150 151 public MDRepository getRepository() throws Exception { 152 return MDRManager.getDefault().getDefaultRepository(); 153 } 154 155 157 public String getPrompt() { 158 return prompt; 159 } 160 161 162 164 public void setPrompt( String prompt ) { 165 this.prompt = prompt; 166 } 167 168 170 public boolean isCommandEcho() { 171 return commandEcho; 172 } 173 174 176 public void setCommandEcho( boolean commandEcho ) { 177 this.commandEcho = commandEcho; 178 } 179 180 182 public boolean isResultPrint() { 183 return resultPrint; 184 } 185 186 188 public void setResultPrint( boolean resultPrint ) { 189 this.resultPrint = resultPrint; 190 } 191 192 193 195 199 public void run( InputStream is ) { 200 run( new InputStream[] { is } ); 201 } 202 203 207 public void run( InputStream[] iss ) { 208 209 if ( iss == null ) { 210 pushInput(System.in); 211 } else { 212 for ( int i = iss.length-1; i >= 0; i-- ) { 213 pushInput(iss[i]); 214 } 215 } 216 217 DJava.initialize(); 218 DJava.declareVariable( "SHELL", this ); 219 220 StringBuffer commandBuffer = new StringBuffer (); 221 while ( !exit ) { 222 try { 223 if ( getPrompt() != null && ioStack.size() == 1 ) { 224 out.print( getPrompt() ); 225 } 226 227 String line = ((BufferedReader)ioStack.peek()).readLine(); 228 229 if ( line == null ) { 230 ioStack.pop(); 231 continue; 232 } 233 if ( line.equals( "" ) || line.trim().startsWith( "//" ) ) { 234 continue; 235 } 236 237 commandBuffer.append( line ); 238 239 if ( line.charAt( line.length() - 1 ) == CONTINUE_CHAR ) { 240 commandBuffer.deleteCharAt( commandBuffer.length() - 1 ).append( NEW_LINE ); 241 continue; 242 } 243 244 String command = commandBuffer.toString(); 245 commandBuffer = new StringBuffer (); 246 247 execCommand(command.toString()); 248 } 249 catch ( Throwable e ) { 250 e.printStackTrace( out ); 251 } 252 253 } 254 } 255 256 258 void pushInput( Reader reader ) { 259 ioStack.push( new BufferedReader( reader ) ); 260 } 261 262 void pushInput( InputStream is ) { 263 ioStack.push( new BufferedReader( new InputStreamReader( is ) ) ); 264 } 265 266 267 } 268 | Popular Tags |