KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdrshell > Shell


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /** Shell for metadata repository.
34  *
35  * @author Petr Hrebejk
36  * @version
37  */

38 public class Shell extends Object JavaDoc {
39
40     private static final char CONTINUE_CHAR = '\\';
41     private static final char NEW_LINE = '\n';
42
43     /** Holds the preprocesor for this Shell */
44     private Preprocessor preprocessor;
45     
46     /** Value indicating wether shell should exit */
47     private boolean exit;
48
49     /** Stack of streams to read from */
50     private Stack ioStack;
51     
52     /** Print stream used for this shell. Note that this variable can be
53     * changed from inside the shell.
54     */

55     public PrintStream out;
56     
57     /** Says wether the result of commands should be printed */
58     private boolean resultPrint;
59     
60     /** Should the commands be echoed */
61     private boolean commandEcho;
62     
63     /** String used for prompting the user */
64     private String JavaDoc prompt;
65     
66     private Object JavaDoc current;
67
68     /** Creates new shell
69     */

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 JavaDoc command) throws Exception JavaDoc {
81         String JavaDoc preprocessedLine = preprocessor.processLine( command.toString() );
82
83         //Support.defaultPrint( 0, Interpreter.processLine( preprocessedLine ) );
84

85         if ( commandEcho ) {
86             out.println( preprocessedLine );
87         }
88
89         Object JavaDoc result = Interpreter.processLine( preprocessedLine );
90         if ( resultPrint ) {
91             Support.defaultPrint( out, result );
92         }
93     }
94
95     // Methods to be used from inside the shell --------------------------------
96

97     /** Exits the shell
98     */

99     public void exit() {
100         System.out.println( "Exiting" );
101         exit = true;
102     }
103     
104     /** Prints object in default format
105     */

106     public void print( Object JavaDoc 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 JavaDoc setActive(Object JavaDoc object) throws Exception JavaDoc {
143         retype(object);
144         return object;
145     }
146     
147     public void retype(Object JavaDoc object) throws Exception JavaDoc {
148         execCommand("#define C$ ((" + object.getClass().getName() + ")CurrentObject)");
149     }
150     
151     public MDRepository getRepository() throws Exception JavaDoc {
152         return MDRManager.getDefault().getDefaultRepository();
153     }
154     
155     /** get the current prompt
156     */

157     public String JavaDoc getPrompt() {
158         return prompt;
159     }
160     
161     
162     /** Set the string for prompting
163     */

164     public void setPrompt( String JavaDoc prompt ) {
165         this.prompt = prompt;
166     }
167     
168     /** Gets the of current command echoing
169     */

170     public boolean isCommandEcho() {
171         return commandEcho;
172     }
173     
174     /** Sets the command echo
175     */

176     public void setCommandEcho( boolean commandEcho ) {
177         this.commandEcho = commandEcho;
178     }
179     
180     /** Gets the value of result printing
181     */

182     public boolean isResultPrint() {
183         return resultPrint;
184     }
185     
186     /** Sets the command echo
187     */

188     public void setResultPrint( boolean resultPrint ) {
189         this.resultPrint = resultPrint;
190     }
191     
192     
193     // Public methods ----------------------------------------------------------
194

195     /** Starts interpretting commands in the InputStream.
196     * @param is InputStream to read commands from. If <code>null</code>
197      * <code>System.in</code> is used.
198     */

199     public void run( InputStream is ) {
200         run( new InputStream[] { is } );
201     }
202     
203     /** Starts interpretting commands in the InputStreams.
204     * @param iss InputStreams to read commands from. If <code>null</code>
205     * <code>System.in</code> is used.
206     */

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 JavaDoc commandBuffer = new StringBuffer JavaDoc();
221         while ( !exit ) {
222             try {
223                 if ( getPrompt() != null && ioStack.size() == 1 ) {
224                     out.print( getPrompt() );
225                 }
226                 
227                 String JavaDoc 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 JavaDoc command = commandBuffer.toString();
245                 commandBuffer = new StringBuffer JavaDoc();
246      
247                 execCommand(command.toString());
248             }
249             catch ( Throwable JavaDoc e ) {
250                 e.printStackTrace( out );
251             }
252            
253         }
254     }
255
256     // Package private methods -------------------------------------------------
257

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