KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > panther > util > ConsoleInterface


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.panther.util;
56
57 import java.io.FilterOutputStream JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.OutputStream JavaDoc;
60 import java.net.Socket JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.Iterator JavaDoc;
63
64 import org.lateralnz.common.telnet.BaseHandler;
65 import org.lateralnz.common.telnet.Constants;
66 import org.lateralnz.common.telnet.Server;
67 import org.lateralnz.common.util.InteractiveInterpreter;
68
69 import org.python.core.PyDictionary;
70 import org.python.core.PySystemState;
71
72 public class ConsoleInterface extends BaseHandler {
73
74   private ConsoleConnector parent;
75   private ArrayList JavaDoc func;
76   private ArrayList JavaDoc history = new ArrayList JavaDoc();
77   int historyIdx = -1;
78   private InteractiveInterpreter interp;
79   private StringBuffer JavaDoc current = new StringBuffer JavaDoc();
80
81   public ConsoleInterface(Server parent, Socket JavaDoc sock) throws IOException JavaDoc {
82     super(parent, sock);
83
84     this.parent = (ConsoleConnector)parent;
85     
86     PySystemState.initialize();
87     PySystemState ss = new PySystemState();
88     this.interp = new InteractiveInterpreter(new PyDictionary(), ss);
89     this.interp.setOut(new LinefeedFilterStream(out));
90     this.interp.setErr(new LinefeedFilterStream(out));
91
92     init();
93   }
94   
95   private void init() {
96     func = (ArrayList JavaDoc)this.parent.functions.clone();
97     Iterator JavaDoc iter = func.iterator();
98     while (iter.hasNext()) {
99       String JavaDoc s = (String JavaDoc)iter.next();
100             
101       interp.exec(s);
102     }
103   }
104   
105   public boolean handleControlChar(int c) throws IOException JavaDoc {
106     if (super.handleControlChar(c)) {
107       return true;
108     }
109     else if (c == 18) {
110       this.parent.init();
111       init();
112       return true;
113     }
114     else {
115       return false;
116     }
117   }
118   
119   public boolean handleEscape(int esc, int mod) throws IOException JavaDoc {
120     if (super.handleEscape(esc, mod)) {
121       return true;
122     }
123     if (esc == Constants.ESC_UP) {
124       while (deleteChar());
125       if (historyIdx > 0) {
126         historyIdx--;
127       }
128       if (historyIdx < history.size() && historyIdx >= 0) {
129         line.append((String JavaDoc)history.get(historyIdx));
130         out.write(line.toString().getBytes());
131       }
132     }
133     else if (esc == Constants.ESC_DOWN) {
134       while (deleteChar());
135       if (historyIdx < history.size()-1) {
136         historyIdx++;
137       }
138       if (historyIdx < history.size() && historyIdx >= 0) {
139         line.append((String JavaDoc)history.get(historyIdx));
140         out.write(line.toString().getBytes());
141       }
142     }
143     return true;
144   }
145   
146   public boolean handleInput(String JavaDoc line) throws IOException JavaDoc {
147     interp.resetbuffer();
148     current.append(line);
149     history.add(line);
150     historyIdx = history.size()-1;
151     if (interp.runsource(current.toString())) {
152       current.append("\n");
153     }
154     else {
155       current.delete(0, current.length());
156     }
157     return true;
158   }
159   
160   // weird problem with jython, output stream send line feed but not
161
// carriage return
162
class LinefeedFilterStream extends FilterOutputStream JavaDoc {
163     
164     public LinefeedFilterStream(OutputStream JavaDoc out) {
165       super(out);
166     }
167     
168     public void write(int i) throws IOException JavaDoc {
169       super.write(i);
170       if (i == 10) {
171         super.write(13);
172       }
173     }
174   }
175 }
Popular Tags