3
|
1 /*
|
|
2 * @(#)KeyStrokenizer.java
|
|
3 * Copyright: Copyright (c) 2001
|
|
4 *
|
|
5 * This program is free software; you can redistribute it and/or modify
|
|
6 * it under the terms of the GNU General Public License as published by
|
|
7 * the Free Software Foundation; either version 2, or (at your option)
|
|
8 * any later version.
|
|
9 *
|
|
10 * This program is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 * GNU General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License
|
|
16 * along with this software; see the file COPYING. If not, write to
|
|
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
18 * Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 */
|
|
21
|
|
22 package org.tn5250j.framework.tn5250;
|
|
23
|
25
|
24 import android.util.Log;
|
|
25
|
3
|
26
|
|
27
|
|
28 public class KeyStrokenizer {
|
27
|
29 private static final String TAG = "KeyStrokenizer";
|
48
|
30 private StringBuffer keyStrokes;
|
|
31 private StringBuffer sb;
|
|
32 private int index;
|
|
33 private int length;
|
27
|
34
|
25
|
35
|
27
|
36
|
112
|
37 public KeyStrokenizer() {
|
|
38 sb = new StringBuffer();
|
|
39 setKeyStrokes(null);
|
|
40 }
|
3
|
41
|
112
|
42 public void setKeyStrokes(String strokes) {
|
|
43 if (strokes != null) {
|
|
44 keyStrokes.setLength(0);
|
|
45 Log.d(TAG, "set " + keyStrokes);
|
|
46 length = strokes.length();
|
|
47 }
|
|
48 else {
|
|
49 keyStrokes = new StringBuffer();
|
|
50 length = 0;
|
|
51 }
|
3
|
52
|
112
|
53 keyStrokes.append(strokes);
|
|
54 index = 0;
|
|
55 }
|
3
|
56
|
112
|
57 public boolean hasMoreKeyStrokes() {
|
|
58 return length > index;
|
|
59 }
|
3
|
60
|
112
|
61 public String nextKeyStroke() {
|
|
62 String s = "";
|
|
63 boolean gotOne = false;
|
|
64
|
|
65 if (length > index) {
|
|
66 sb.setLength(0);
|
|
67 char c = keyStrokes.charAt(index);
|
3
|
68
|
112
|
69 switch (c) {
|
|
70 case '[':
|
|
71 sb.append(c);
|
|
72 index++;
|
3
|
73
|
112
|
74 // we need to throw an error here
|
|
75 if (index >= length) {
|
|
76 Log.w(TAG, " mnemonic key was incomplete :1 " +
|
|
77 "at position " + index + " len " + length);
|
|
78 }
|
|
79 else {
|
|
80 c = keyStrokes.charAt(index);
|
3
|
81
|
112
|
82 if (c == '[')
|
|
83 index++;
|
|
84 else {
|
|
85 while (!gotOne) {
|
|
86 if (c == ']') { // did we find an ending
|
|
87 sb.append(c);
|
|
88 index++;
|
|
89 gotOne = true;
|
|
90 }
|
|
91 else {
|
|
92 sb.append(c);
|
|
93 index++;
|
3
|
94
|
112
|
95 // we need to throw an error here because we did not
|
|
96 // find an ending for the potential mnemonic
|
|
97 if (index >= length) {
|
|
98 Log.w(TAG,
|
|
99 " mnemonic key was incomplete ending not found :2 " +
|
|
100 "at position " + index);
|
|
101 }
|
3
|
102
|
112
|
103 c = keyStrokes.charAt(index);
|
|
104 }
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108
|
|
109 break;
|
|
110
|
|
111 case ']':
|
|
112 index++;
|
3
|
113
|
112
|
114 if (index >= length) {
|
|
115 Log.w(TAG,
|
|
116 " mnemonic key was incomplete ending not found :3 " +
|
|
117 "at position " + index);
|
|
118 sb.append(c);
|
|
119 index++;
|
|
120 }
|
|
121 else {
|
|
122 c = keyStrokes.charAt(index);
|
3
|
123
|
112
|
124 if (c == ']') {
|
|
125 sb.append(c);
|
|
126 index++;
|
3
|
127 }
|
|
128 else {
|
112
|
129 Log.w(TAG,
|
|
130 " mnemonic key was incomplete beginning not found :4 " +
|
|
131 "at position " + index);
|
3
|
132 }
|
112
|
133 }
|
|
134
|
|
135 break;
|
3
|
136
|
112
|
137 default:
|
|
138 sb.append(c);
|
|
139 index++;
|
|
140 break;
|
|
141 }
|
3
|
142
|
112
|
143 if (sb != null) {
|
|
144 s = new String(sb);
|
|
145 }
|
|
146 }
|
3
|
147
|
112
|
148 Log.d(TAG, "next " + keyStrokes);
|
|
149 return s;
|
|
150 }
|
3
|
151
|
112
|
152 public String getUnprocessedKeyStroked() {
|
|
153 if (index >= length) {
|
|
154 return null;
|
|
155 }
|
|
156
|
|
157 return keyStrokes.substring(index);
|
|
158 }
|
3
|
159
|
|
160 } |