KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > terminalemulator > InterpDumb


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 Terminal Emulator.
16  * The Initial Developer of the Original Software is Sun Microsystems, Inc..
17  * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001-2004.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Ivan Soleimanipour.
21  */

22
23 package org.netbeans.lib.terminalemulator;
24
25 import java.util.Stack JavaDoc;
26
27 /**
28  * Input stream interpreter
29  * Decodes incoming characters into cursor motion etc.
30  */

31 public class InterpDumb extends AbstractInterp {
32
33     protected static class InterpTypeDumb {
34     public final State st_base = new State("base"); // NOI18N
35

36     protected final Actor act_nop = new ACT_NOP();
37     protected final Actor act_pause = new ACT_PAUSE();
38     protected final Actor act_err = new ACT_ERR();
39     protected final Actor act_regular = new ACT_REGULAR();
40     protected final Actor act_cr = new ACT_CR();
41     protected final Actor act_lf = new ACT_LF();
42     protected final Actor act_bs = new ACT_BS();
43     protected final Actor act_tab = new ACT_TAB();
44     protected final Actor act_beL = new ACT_BEL();
45
46     protected InterpTypeDumb() {
47         st_base.setRegular(st_base, act_regular);
48
49         for (char c = 0; c < 128; c++)
50         st_base.setAction(c, st_base, act_regular);
51
52         st_base.setAction((char) 0, st_base, act_pause);
53         st_base.setAction('\r', st_base, act_cr);
54         st_base.setAction('\n', st_base, act_lf);
55         st_base.setAction('\b', st_base, act_bs);
56         st_base.setAction('\t', st_base, act_tab);
57         st_base.setAction((char) 7, st_base, act_beL);
58     }
59
60     protected static final class ACT_NOP implements Actor {
61         public String JavaDoc action(AbstractInterp ai, char c) {
62         return null;
63         }
64     };
65
66     protected static final class ACT_PAUSE implements Actor {
67         public String JavaDoc action(AbstractInterp ai, char c) {
68         ai.ops.op_pause();
69         return null;
70         }
71     };
72
73     protected static final class ACT_ERR implements Actor {
74         public String JavaDoc action(AbstractInterp ai, char c) {
75         return "ACT ERROR"; // NOI18N
76
}
77     };
78
79     protected static final class ACT_REGULAR implements Actor {
80         public String JavaDoc action(AbstractInterp ai, char c) {
81         ai.ops.op_char(c);
82         return null;
83         }
84     };
85
86
87     protected static final class ACT_CR implements Actor {
88         public String JavaDoc action(AbstractInterp ai, char c) {
89         ai.ops.op_carriage_return();
90         return null;
91         }
92     };
93
94     protected static final class ACT_LF implements Actor {
95         public String JavaDoc action(AbstractInterp ai, char c) {
96         ai.ops.op_line_feed();
97         return null;
98         }
99     };
100
101
102     protected static final class ACT_BS implements Actor {
103         public String JavaDoc action(AbstractInterp ai, char c) {
104         ai.ops.op_back_space();
105         return null;
106         }
107     };
108
109     protected static final class ACT_TAB implements Actor {
110         public String JavaDoc action(AbstractInterp ai, char c) {
111         ai.ops.op_tab();
112         return null;
113         }
114     };
115
116     protected static final class ACT_BEL implements Actor {
117         public String JavaDoc action(AbstractInterp ai, char c) {
118         ai.ops.op_bel();
119         return null;
120         }
121     }
122     }
123
124     /*
125      * A stack for State
126      */

127     private Stack JavaDoc stack = new Stack JavaDoc();
128
129     protected void push_state(State s) {
130     stack.push(s);
131     }
132
133     protected State pop_state() {
134     return (State) stack.pop();
135     }
136
137     protected void pop_all_states() {
138     while(!stack.empty())
139         stack.pop();
140     }
141
142
143     protected String JavaDoc ctl_sequence = null;
144
145     private InterpTypeDumb type;
146
147     public static final InterpTypeDumb type_singleton = new InterpTypeDumb();
148
149     public InterpDumb(Ops ops) {
150     super(ops);
151     this.type = type_singleton;
152     setup();
153     ctl_sequence = null;
154     }
155
156     protected InterpDumb(Ops ops, InterpTypeDumb type) {
157     super(ops);
158     this.type = type;
159     setup();
160     ctl_sequence = null;
161     }
162
163     public String JavaDoc name() {
164     return "dumb"; // NOI18N
165
}
166
167     public void reset() {
168     super.reset();
169     pop_all_states();
170     state = type.st_base;
171     ctl_sequence = null;
172     }
173
174     private void setup() {
175     state = type.st_base;
176     }
177
178
179     private void reset_state_bad() {
180         /*
181     DEBUG
182     System.out.println("Unrecognized sequence in state " + state.name()); // NOI18N
183     if (ctl_sequence != null) {
184         for (int sx = 0; sx < ctl_sequence.length(); sx++)
185         System.out.print(String.valueOf((int)ctl_sequence.charAt(sx)) +
186             " "); // NOI18N
187         System.out.print("\n\t"); // NOI18N
188         for (int sx = 0; sx < ctl_sequence.length(); sx++)
189         System.out.print(ctl_sequence.charAt(sx) + " "); // NOI18N
190
191         System.out.println(); // NOI18N
192     }
193         */

194     reset();
195     }
196
197     public void processChar(char c) {
198
199     // If we're collecting stuff into a control sequence remember the char
200
if (ctl_sequence != null)
201         ctl_sequence += c;
202
203     try {
204         State.Action a = state.getAction(c);
205         /* DEBUG
206         if (a == null) {
207         System.out.println("null action in state " + state.name() + // NOI18N
208                            " for char " + c + " = " + (int) c); // NOI18N
209         }
210         if (a.actor == null) {
211         System.out.println("null a.actor in state " + state.name() + // NOI18N
212                    " for char " + c + " = " + (int) c); // NOI18N
213         }
214         */

215         String JavaDoc err_str = a.actor.action(this, c);
216         if (err_str != null) {
217         /* DEBUG
218         System.out.println("action error: " + err_str); // NOI18N
219         */

220         reset_state_bad();
221         return;
222         }
223
224         if (a.new_state != null)
225         state = a.new_state;
226         else
227         ; // must be set by action, usually using pop_state()
228

229     } finally {
230         if (state == type.st_base)
231         ctl_sequence = null;
232     }
233     }
234
235     /*
236      * Actions ..............................................................
237      */

238
239 }
240
Popular Tags