comparison app/src/main/java/org/tn5250j/framework/tn5250/KeyStrokenizer.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/org/tn5250j/framework/tn5250/KeyStrokenizer.java@e2a56e383bad
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
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
24 import android.util.Log;
25
26
27
28 public class KeyStrokenizer {
29 private static final String TAG = "KeyStrokenizer";
30 private StringBuffer keyStrokes;
31 private StringBuffer sb;
32 private int index;
33 private int length;
34
35
36
37 public KeyStrokenizer() {
38 sb = new StringBuffer();
39 setKeyStrokes(null);
40 }
41
42 public void setKeyStrokes(String strokes) {
43 if (strokes != null) {
44 keyStrokes.setLength(0);
45 Log.d(TAG, "set " + strokes);
46 length = strokes.length();
47 }
48 else {
49 keyStrokes = new StringBuffer();
50 length = 0;
51 }
52
53 keyStrokes.append(strokes);
54 index = 0;
55 }
56
57 public boolean hasMoreKeyStrokes() {
58 return length > index;
59 }
60
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);
68
69 switch (c) {
70 case '[':
71 sb.append(c);
72 index++;
73
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);
81
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++;
94
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 }
102
103 c = keyStrokes.charAt(index);
104 }
105 }
106 }
107 }
108
109 break;
110
111 case ']':
112 index++;
113
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);
123
124 if (c == ']') {
125 sb.append(c);
126 index++;
127 }
128 else {
129 Log.w(TAG,
130 " mnemonic key was incomplete beginning not found :4 " +
131 "at position " + index);
132 }
133 }
134
135 break;
136
137 default:
138 sb.append(c);
139 index++;
140 break;
141 }
142
143 if (sb != null) {
144 s = new String(sb);
145 }
146 }
147
148 Log.d(TAG, "next " + keyStrokes);
149 return s;
150 }
151
152 public String getUnprocessedKeyStroked() {
153 if (index >= length) {
154 return null;
155 }
156
157 return keyStrokes.substring(index);
158 }
159
160 }