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
|
|
24 import org.tn5250j.tools.logging.TN5250jLogFactory;
|
|
25 import org.tn5250j.tools.logging.TN5250jLogger;
|
|
26
|
|
27
|
|
28 public class KeyStrokenizer {
|
|
29
|
|
30 private StringBuffer keyStrokes;
|
|
31 private StringBuffer sb;
|
|
32 private int index;
|
|
33 private int length;
|
|
34
|
|
35 private final TN5250jLogger log = TN5250jLogFactory.getLogger(this.getClass());
|
|
36
|
|
37 public KeyStrokenizer() {
|
|
38
|
|
39 sb = new StringBuffer();
|
|
40 setKeyStrokes(null);
|
|
41 }
|
|
42
|
|
43 public void setKeyStrokes (String strokes) {
|
|
44
|
|
45 if (strokes != null) {
|
|
46 keyStrokes.setLength(0);
|
|
47 log.debug("set "+ keyStrokes);
|
|
48 length = strokes.length();
|
|
49 }
|
|
50 else {
|
|
51
|
|
52 keyStrokes = new StringBuffer();
|
|
53 length = 0;
|
|
54
|
|
55 }
|
|
56 keyStrokes.append(strokes);
|
|
57 index = 0;
|
|
58
|
|
59 }
|
|
60
|
|
61 public boolean hasMoreKeyStrokes() {
|
|
62 return length > index;
|
|
63 }
|
|
64
|
|
65 public String nextKeyStroke() {
|
|
66
|
|
67 String s = "";
|
|
68 boolean gotOne = false;
|
|
69 if(length > index) {
|
|
70 sb.setLength(0);
|
|
71
|
|
72 char c = keyStrokes.charAt(index);
|
|
73 switch(c) {
|
|
74 case '[':
|
|
75 sb.append(c);
|
|
76 index++;
|
|
77
|
|
78 // we need to throw an error here
|
|
79 if(index >= length) {
|
|
80 log.warn(" mnemonic key was incomplete :1 " +
|
|
81 "at position " + index + " len " + length );
|
|
82 }
|
|
83 else {
|
|
84 c = keyStrokes.charAt(index);
|
|
85
|
|
86 if(c == '[')
|
|
87 index++;
|
|
88 else {
|
|
89 while(!gotOne) {
|
|
90
|
|
91 if(c == ']') { // did we find an ending
|
|
92 sb.append(c);
|
|
93 index++;
|
|
94 gotOne = true;
|
|
95 }
|
|
96 else {
|
|
97 sb.append(c);
|
|
98 index++;
|
|
99 // we need to throw an error here because we did not
|
|
100 // find an ending for the potential mnemonic
|
|
101 if(index >= length) {
|
|
102 log.warn(
|
|
103 " mnemonic key was incomplete ending not found :2 " +
|
|
104 "at position " + index);
|
|
105 }
|
|
106 c = keyStrokes.charAt(index);
|
|
107 }
|
|
108 }
|
|
109 }
|
|
110 }
|
|
111 break;
|
|
112
|
|
113 case ']':
|
|
114 index++;
|
|
115 if(index >= length) {
|
|
116 log.warn(
|
|
117 " mnemonic key was incomplete ending not found :3 " +
|
|
118 "at position " + index);
|
|
119 sb.append(c);
|
|
120 index++;
|
|
121
|
|
122 }
|
|
123 else {
|
|
124 c = keyStrokes.charAt(index);
|
|
125 if(c == ']') {
|
|
126 sb.append(c);
|
|
127 index++;
|
|
128 }
|
|
129 else {
|
|
130 log.warn(
|
|
131 " mnemonic key was incomplete beginning not found :4 " +
|
|
132 "at position " + index);
|
|
133 }
|
|
134 }
|
|
135 break;
|
|
136 default:
|
|
137 sb.append(c);
|
|
138 index++;
|
|
139 break;
|
|
140 }
|
|
141 if(sb != null) {
|
|
142 s = new String(sb);
|
|
143 }
|
|
144
|
|
145 }
|
|
146 log.debug("next "+ keyStrokes);
|
|
147
|
|
148 return s;
|
|
149 }
|
|
150
|
|
151 public String getUnprocessedKeyStroked() {
|
|
152 if(index >= length) {
|
|
153 return null;
|
|
154 }
|
|
155 return keyStrokes.substring(index);
|
|
156 }
|
|
157
|
|
158 } |