calabash-android 0.4.0.pre1 → 0.4.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/bin/calabash-android-build.rb +6 -0
  2. data/bin/calabash-android-setup.rb +2 -3
  3. data/lib/calabash-android/canned_steps.md +1 -1
  4. data/lib/calabash-android/helpers.rb +35 -2
  5. data/lib/calabash-android/lib/TestServer.apk +0 -0
  6. data/lib/calabash-android/operations.rb +13 -6
  7. data/lib/calabash-android/version.rb +1 -1
  8. data/test-server/instrumentation-backend/.classpath +2 -1
  9. data/test-server/instrumentation-backend/antlr.sh +2 -0
  10. data/test-server/instrumentation-backend/antlr/UIQuery.g +70 -0
  11. data/test-server/instrumentation-backend/antlr/UIQuery.tokens +12 -0
  12. data/test-server/instrumentation-backend/build-libs/antlr-3.4-complete.jar +0 -0
  13. data/test-server/instrumentation-backend/build-libs/junit.jar +0 -0
  14. data/test-server/instrumentation-backend/build.xml +56 -0
  15. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRFileStream.java +78 -0
  16. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRInputStream.java +70 -0
  17. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRReaderStream.java +95 -0
  18. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRStringStream.java +230 -0
  19. data/test-server/instrumentation-backend/src/org/antlr/runtime/BaseRecognizer.java +894 -0
  20. data/test-server/instrumentation-backend/src/org/antlr/runtime/BitSet.java +325 -0
  21. data/test-server/instrumentation-backend/src/org/antlr/runtime/BufferedTokenStream.java +272 -0
  22. data/test-server/instrumentation-backend/src/org/antlr/runtime/CharStream.java +57 -0
  23. data/test-server/instrumentation-backend/src/org/antlr/runtime/CharStreamState.java +45 -0
  24. data/test-server/instrumentation-backend/src/org/antlr/runtime/ClassicToken.java +141 -0
  25. data/test-server/instrumentation-backend/src/org/antlr/runtime/CommonToken.java +191 -0
  26. data/test-server/instrumentation-backend/src/org/antlr/runtime/CommonTokenStream.java +153 -0
  27. data/test-server/instrumentation-backend/src/org/antlr/runtime/DFA.java +250 -0
  28. data/test-server/instrumentation-backend/src/org/antlr/runtime/EarlyExitException.java +41 -0
  29. data/test-server/instrumentation-backend/src/org/antlr/runtime/FailedPredicateException.java +54 -0
  30. data/test-server/instrumentation-backend/src/org/antlr/runtime/IntStream.java +122 -0
  31. data/test-server/instrumentation-backend/src/org/antlr/runtime/LegacyCommonTokenStream.java +394 -0
  32. data/test-server/instrumentation-backend/src/org/antlr/runtime/Lexer.java +340 -0
  33. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedNotSetException.java +41 -0
  34. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedRangeException.java +45 -0
  35. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedSetException.java +44 -0
  36. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedTokenException.java +45 -0
  37. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedTreeNodeException.java +49 -0
  38. data/test-server/instrumentation-backend/src/org/antlr/runtime/MissingTokenException.java +56 -0
  39. data/test-server/instrumentation-backend/src/org/antlr/runtime/NoViableAltException.java +57 -0
  40. data/test-server/instrumentation-backend/src/org/antlr/runtime/Parser.java +98 -0
  41. data/test-server/instrumentation-backend/src/org/antlr/runtime/ParserRuleReturnScope.java +52 -0
  42. data/test-server/instrumentation-backend/src/org/antlr/runtime/RecognitionException.java +180 -0
  43. data/test-server/instrumentation-backend/src/org/antlr/runtime/RecognizerSharedState.java +144 -0
  44. data/test-server/instrumentation-backend/src/org/antlr/runtime/RuleReturnScope.java +42 -0
  45. data/test-server/instrumentation-backend/src/org/antlr/runtime/SerializedGrammar.java +204 -0
  46. data/test-server/instrumentation-backend/src/org/antlr/runtime/Token.java +92 -0
  47. data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenRewriteStream.java +569 -0
  48. data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenSource.java +54 -0
  49. data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenStream.java +75 -0
  50. data/test-server/instrumentation-backend/src/org/antlr/runtime/UnbufferedTokenStream.java +82 -0
  51. data/test-server/instrumentation-backend/src/org/antlr/runtime/UnwantedTokenException.java +53 -0
  52. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/BlankDebugEventListener.java +77 -0
  53. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventHub.java +292 -0
  54. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventListener.java +323 -0
  55. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventRepeater.java +88 -0
  56. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventSocketProxy.java +358 -0
  57. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugParser.java +101 -0
  58. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTokenStream.java +156 -0
  59. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeAdaptor.java +250 -0
  60. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeNodeStream.java +155 -0
  61. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeParser.java +112 -0
  62. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/ParseTreeBuilder.java +109 -0
  63. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/Profiler.java +772 -0
  64. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/RemoteDebugEventSocketListener.java +541 -0
  65. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/TraceDebugEventListener.java +108 -0
  66. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/Tracer.java +69 -0
  67. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/DoubleKeyMap.java +62 -0
  68. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/FastQueue.java +100 -0
  69. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/IntArray.java +87 -0
  70. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/LookaheadStream.java +161 -0
  71. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/Stats.java +189 -0
  72. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BaseTree.java +349 -0
  73. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BaseTreeAdaptor.java +279 -0
  74. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BufferedTreeNodeStream.java +489 -0
  75. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonErrorNode.java +108 -0
  76. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTree.java +185 -0
  77. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTreeAdaptor.java +168 -0
  78. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTreeNodeStream.java +171 -0
  79. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/ParseTree.java +119 -0
  80. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteCardinalityException.java +47 -0
  81. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteEarlyExitException.java +39 -0
  82. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteEmptyStreamException.java +35 -0
  83. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleElementStream.java +210 -0
  84. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleNodeStream.java +70 -0
  85. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java +86 -0
  86. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleTokenStream.java +76 -0
  87. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/Tree.java +127 -0
  88. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeAdaptor.java +263 -0
  89. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeFilter.java +135 -0
  90. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeIterator.java +132 -0
  91. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeNodeStream.java +106 -0
  92. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeParser.java +169 -0
  93. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreePatternLexer.java +135 -0
  94. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreePatternParser.java +154 -0
  95. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeRewriter.java +124 -0
  96. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeRuleReturnScope.java +41 -0
  97. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeVisitor.java +69 -0
  98. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeVisitorAction.java +47 -0
  99. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeWizard.java +531 -0
  100. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/ClearAppData.java +22 -7
  101. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/InstrumentationBackend.java +4 -4
  102. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/Result.java +0 -1
  103. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/HttpServer.java +161 -129
  104. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/activity/SetActivityOrientation.java +5 -7
  105. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/view/GetViewProperty.java +1 -7
  106. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/Query.java +70 -61
  107. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/UIQuery.tokens +12 -0
  108. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ViewMapper.java +63 -0
  109. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQuery.tokens +10 -0
  110. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQueryLexer.java +945 -0
  111. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQueryParser.java +463 -0
  112. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/InvalidUIQueryException.java +10 -0
  113. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryAST.java +8 -0
  114. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryASTClassName.java +115 -0
  115. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryASTWith.java +157 -0
  116. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryDirection.java +5 -0
  117. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryEvaluator.java +205 -0
  118. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryUtils.java +135 -0
  119. data/test-server/instrumentation-backend/tests/sh/calaba/instrumentationbackend/query/tests/UIQueryTest.java +134 -0
  120. metadata +106 -3
  121. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/activity/SetOrientation.java +0 -39
@@ -0,0 +1,541 @@
1
+ /*
2
+ [The "BSD license"]
3
+ Copyright (c) 2005-2009 Terence Parr
4
+ 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
+ 1. Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ 2. Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ 3. The name of the author may not be used to endorse or promote products
15
+ derived from this software without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+ package org.antlr.runtime.debug;
29
+
30
+ import org.antlr.runtime.RecognitionException;
31
+ import org.antlr.runtime.Token;
32
+ import org.antlr.runtime.CharStream;
33
+ import org.antlr.runtime.tree.BaseTree;
34
+ import org.antlr.runtime.tree.Tree;
35
+
36
+ import java.io.*;
37
+ import java.net.ConnectException;
38
+ import java.net.Socket;
39
+ import java.util.StringTokenizer;
40
+
41
+ public class RemoteDebugEventSocketListener implements Runnable {
42
+ static final int MAX_EVENT_ELEMENTS = 8;
43
+ DebugEventListener listener;
44
+ String machine;
45
+ int port;
46
+ Socket channel = null;
47
+ PrintWriter out;
48
+ BufferedReader in;
49
+ String event;
50
+ /** Version of ANTLR (dictates events) */
51
+ public String version;
52
+ public String grammarFileName;
53
+ /** Track the last token index we saw during a consume. If same, then
54
+ * set a flag that we have a problem.
55
+ */
56
+ int previousTokenIndex = -1;
57
+ boolean tokenIndexesInvalid = false;
58
+
59
+ private static final String TAG = "ANTLR:DEBUG:RemoteDebugEventSocketListener";
60
+
61
+ public static class ProxyToken implements Token {
62
+ int index;
63
+ int type;
64
+ int channel;
65
+ int line;
66
+ int charPos;
67
+ String text;
68
+ public ProxyToken(int index) { this.index = index; }
69
+ public ProxyToken(int index, int type, int channel,
70
+ int line, int charPos, String text)
71
+ {
72
+ this.index = index;
73
+ this.type = type;
74
+ this.channel = channel;
75
+ this.line = line;
76
+ this.charPos = charPos;
77
+ this.text = text;
78
+ }
79
+ public String getText() {
80
+ return text;
81
+ }
82
+ public void setText(String text) {
83
+ this.text = text;
84
+ }
85
+ public int getType() {
86
+ return type;
87
+ }
88
+ public void setType(int ttype) {
89
+ this.type = ttype;
90
+ }
91
+ public int getLine() {
92
+ return line;
93
+ }
94
+ public void setLine(int line) {
95
+ this.line = line;
96
+ }
97
+ public int getCharPositionInLine() {
98
+ return charPos;
99
+ }
100
+ public void setCharPositionInLine(int pos) {
101
+ this.charPos = pos;
102
+ }
103
+ public int getChannel() {
104
+ return channel;
105
+ }
106
+ public void setChannel(int channel) {
107
+ this.channel = channel;
108
+ }
109
+ public int getTokenIndex() {
110
+ return index;
111
+ }
112
+ public void setTokenIndex(int index) {
113
+ this.index = index;
114
+ }
115
+ public CharStream getInputStream() {
116
+ return null;
117
+ }
118
+ public void setInputStream(CharStream input) {
119
+ }
120
+ public String toString() {
121
+ String channelStr = "";
122
+ if ( channel!=Token.DEFAULT_CHANNEL ) {
123
+ channelStr=",channel="+channel;
124
+ }
125
+ return "["+getText()+"/<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+",@"+index+"]";
126
+ }
127
+ }
128
+
129
+ public static class ProxyTree extends BaseTree {
130
+ public int ID;
131
+ public int type;
132
+ public int line = 0;
133
+ public int charPos = -1;
134
+ public int tokenIndex = -1;
135
+ public String text;
136
+
137
+ public ProxyTree(int ID, int type, int line, int charPos, int tokenIndex, String text) {
138
+ this.ID = ID;
139
+ this.type = type;
140
+ this.line = line;
141
+ this.charPos = charPos;
142
+ this.tokenIndex = tokenIndex;
143
+ this.text = text;
144
+ }
145
+
146
+ public ProxyTree(int ID) { this.ID = ID; }
147
+
148
+ public int getTokenStartIndex() { return tokenIndex; }
149
+ public void setTokenStartIndex(int index) { }
150
+ public int getTokenStopIndex() { return 0; }
151
+ public void setTokenStopIndex(int index) { }
152
+ public Tree dupNode() { return null; }
153
+ public int getType() { return type; }
154
+ public String getText() { return text; }
155
+ public String toString() {
156
+ return "fix this";
157
+ }
158
+ }
159
+
160
+ public RemoteDebugEventSocketListener(DebugEventListener listener,
161
+ String machine,
162
+ int port) throws IOException
163
+ {
164
+ this.listener = listener;
165
+ this.machine = machine;
166
+ this.port = port;
167
+
168
+ if( !openConnection() ) {
169
+ throw new ConnectException();
170
+ }
171
+ }
172
+
173
+ protected void eventHandler() {
174
+ try {
175
+ handshake();
176
+ event = in.readLine();
177
+ while ( event!=null ) {
178
+ dispatch(event);
179
+ ack();
180
+ event = in.readLine();
181
+ }
182
+ }
183
+ catch (Exception e) {
184
+ //System.err.println(e);
185
+ //e.printStackTrace(System.err);
186
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(e));
187
+ }
188
+ finally {
189
+ closeConnection();
190
+ }
191
+ }
192
+
193
+ protected boolean openConnection() {
194
+ boolean success = false;
195
+ try {
196
+ channel = new Socket(machine, port);
197
+ channel.setTcpNoDelay(true);
198
+ OutputStream os = channel.getOutputStream();
199
+ OutputStreamWriter osw = new OutputStreamWriter(os, "UTF8");
200
+ out = new PrintWriter(new BufferedWriter(osw));
201
+ InputStream is = channel.getInputStream();
202
+ InputStreamReader isr = new InputStreamReader(is, "UTF8");
203
+ in = new BufferedReader(isr);
204
+ success = true;
205
+ } catch(Exception e) {
206
+ //System.err.println(e);
207
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(e));
208
+ }
209
+ return success;
210
+ }
211
+
212
+ protected void closeConnection() {
213
+ try {
214
+ in.close(); in = null;
215
+ out.close(); out = null;
216
+ channel.close(); channel=null;
217
+ }
218
+ catch (Exception e) {
219
+ //System.err.println(e);
220
+ //e.printStackTrace(System.err);
221
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(e));
222
+ }
223
+ finally {
224
+ if ( in!=null ) {
225
+ try {in.close();} catch (IOException ioe) {
226
+ //System.err.println(ioe);
227
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(ioe));
228
+ }
229
+ }
230
+ if ( out!=null ) {
231
+ out.close();
232
+ }
233
+ if ( channel!=null ) {
234
+ try {channel.close();} catch (IOException ioe) {
235
+ //System.err.println(ioe);
236
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(ioe));
237
+ }
238
+ }
239
+ }
240
+
241
+ }
242
+
243
+ protected void handshake() throws IOException {
244
+ String antlrLine = in.readLine();
245
+ String[] antlrElements = getEventElements(antlrLine);
246
+ version = antlrElements[1];
247
+ String grammarLine = in.readLine();
248
+ String[] grammarElements = getEventElements(grammarLine);
249
+ grammarFileName = grammarElements[1];
250
+ ack();
251
+ listener.commence(); // inform listener after handshake
252
+ }
253
+
254
+ protected void ack() {
255
+ out.println("ack");
256
+ out.flush();
257
+ }
258
+
259
+ protected void dispatch(String line) {
260
+ //System.out.println("event: "+line);
261
+ String[] elements = getEventElements(line);
262
+ if ( elements==null || elements[0]==null ) {
263
+ //System.err.println("unknown debug event: "+line);
264
+ android.util.Log.e(TAG, "unknown debug event: "+line);
265
+ return;
266
+ }
267
+ if ( elements[0].equals("enterRule") ) {
268
+ listener.enterRule(elements[1], elements[2]);
269
+ }
270
+ else if ( elements[0].equals("exitRule") ) {
271
+ listener.exitRule(elements[1], elements[2]);
272
+ }
273
+ else if ( elements[0].equals("enterAlt") ) {
274
+ listener.enterAlt(Integer.parseInt(elements[1]));
275
+ }
276
+ else if ( elements[0].equals("enterSubRule") ) {
277
+ listener.enterSubRule(Integer.parseInt(elements[1]));
278
+ }
279
+ else if ( elements[0].equals("exitSubRule") ) {
280
+ listener.exitSubRule(Integer.parseInt(elements[1]));
281
+ }
282
+ else if ( elements[0].equals("enterDecision") ) {
283
+ listener.enterDecision(Integer.parseInt(elements[1]), elements[2].equals("true"));
284
+ }
285
+ else if ( elements[0].equals("exitDecision") ) {
286
+ listener.exitDecision(Integer.parseInt(elements[1]));
287
+ }
288
+ else if ( elements[0].equals("location") ) {
289
+ listener.location(Integer.parseInt(elements[1]),
290
+ Integer.parseInt(elements[2]));
291
+ }
292
+ else if ( elements[0].equals("consumeToken") ) {
293
+ ProxyToken t = deserializeToken(elements, 1);
294
+ if ( t.getTokenIndex() == previousTokenIndex ) {
295
+ tokenIndexesInvalid = true;
296
+ }
297
+ previousTokenIndex = t.getTokenIndex();
298
+ listener.consumeToken(t);
299
+ }
300
+ else if ( elements[0].equals("consumeHiddenToken") ) {
301
+ ProxyToken t = deserializeToken(elements, 1);
302
+ if ( t.getTokenIndex() == previousTokenIndex ) {
303
+ tokenIndexesInvalid = true;
304
+ }
305
+ previousTokenIndex = t.getTokenIndex();
306
+ listener.consumeHiddenToken(t);
307
+ }
308
+ else if ( elements[0].equals("LT") ) {
309
+ Token t = deserializeToken(elements, 2);
310
+ listener.LT(Integer.parseInt(elements[1]), t);
311
+ }
312
+ else if ( elements[0].equals("mark") ) {
313
+ listener.mark(Integer.parseInt(elements[1]));
314
+ }
315
+ else if ( elements[0].equals("rewind") ) {
316
+ if ( elements[1]!=null ) {
317
+ listener.rewind(Integer.parseInt(elements[1]));
318
+ }
319
+ else {
320
+ listener.rewind();
321
+ }
322
+ }
323
+ else if ( elements[0].equals("beginBacktrack") ) {
324
+ listener.beginBacktrack(Integer.parseInt(elements[1]));
325
+ }
326
+ else if ( elements[0].equals("endBacktrack") ) {
327
+ int level = Integer.parseInt(elements[1]);
328
+ int successI = Integer.parseInt(elements[2]);
329
+ listener.endBacktrack(level, successI==DebugEventListener.TRUE);
330
+ }
331
+ else if ( elements[0].equals("exception") ) {
332
+ String excName = elements[1];
333
+ String indexS = elements[2];
334
+ String lineS = elements[3];
335
+ String posS = elements[4];
336
+ Class excClass = null;
337
+ try {
338
+ excClass = Class.forName(excName);
339
+ RecognitionException e =
340
+ (RecognitionException)excClass.newInstance();
341
+ e.index = Integer.parseInt(indexS);
342
+ e.line = Integer.parseInt(lineS);
343
+ e.charPositionInLine = Integer.parseInt(posS);
344
+ listener.recognitionException(e);
345
+ }
346
+ catch (ClassNotFoundException cnfe) {
347
+ //System.err.println("can't find class "+cnfe);
348
+ android.util.Log.e(TAG, "can't find class "+cnfe);
349
+ //cnfe.printStackTrace(System.err);
350
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(cnfe));
351
+ }
352
+ catch (InstantiationException ie) {
353
+ //System.err.println("can't instantiate class "+ie);
354
+ //ie.printStackTrace(System.err);
355
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(ie));
356
+ }
357
+ catch (IllegalAccessException iae) {
358
+ //System.err.println("can't access class "+iae);
359
+ //iae.printStackTrace(System.err);
360
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(iae));
361
+ }
362
+ }
363
+ else if ( elements[0].equals("beginResync") ) {
364
+ listener.beginResync();
365
+ }
366
+ else if ( elements[0].equals("endResync") ) {
367
+ listener.endResync();
368
+ }
369
+ else if ( elements[0].equals("terminate") ) {
370
+ listener.terminate();
371
+ }
372
+ else if ( elements[0].equals("semanticPredicate") ) {
373
+ Boolean result = Boolean.valueOf(elements[1]);
374
+ String predicateText = elements[2];
375
+ predicateText = unEscapeNewlines(predicateText);
376
+ listener.semanticPredicate(result.booleanValue(),
377
+ predicateText);
378
+ }
379
+ else if ( elements[0].equals("consumeNode") ) {
380
+ ProxyTree node = deserializeNode(elements, 1);
381
+ listener.consumeNode(node);
382
+ }
383
+ else if ( elements[0].equals("LN") ) {
384
+ int i = Integer.parseInt(elements[1]);
385
+ ProxyTree node = deserializeNode(elements, 2);
386
+ listener.LT(i, node);
387
+ }
388
+ else if ( elements[0].equals("createNodeFromTokenElements") ) {
389
+ int ID = Integer.parseInt(elements[1]);
390
+ int type = Integer.parseInt(elements[2]);
391
+ String text = elements[3];
392
+ text = unEscapeNewlines(text);
393
+ ProxyTree node = new ProxyTree(ID, type, -1, -1, -1, text);
394
+ listener.createNode(node);
395
+ }
396
+ else if ( elements[0].equals("createNode") ) {
397
+ int ID = Integer.parseInt(elements[1]);
398
+ int tokenIndex = Integer.parseInt(elements[2]);
399
+ // create dummy node/token filled with ID, tokenIndex
400
+ ProxyTree node = new ProxyTree(ID);
401
+ ProxyToken token = new ProxyToken(tokenIndex);
402
+ listener.createNode(node, token);
403
+ }
404
+ else if ( elements[0].equals("nilNode") ) {
405
+ int ID = Integer.parseInt(elements[1]);
406
+ ProxyTree node = new ProxyTree(ID);
407
+ listener.nilNode(node);
408
+ }
409
+ else if ( elements[0].equals("errorNode") ) {
410
+ // TODO: do we need a special tree here?
411
+ int ID = Integer.parseInt(elements[1]);
412
+ int type = Integer.parseInt(elements[2]);
413
+ String text = elements[3];
414
+ text = unEscapeNewlines(text);
415
+ ProxyTree node = new ProxyTree(ID, type, -1, -1, -1, text);
416
+ listener.errorNode(node);
417
+ }
418
+ else if ( elements[0].equals("becomeRoot") ) {
419
+ int newRootID = Integer.parseInt(elements[1]);
420
+ int oldRootID = Integer.parseInt(elements[2]);
421
+ ProxyTree newRoot = new ProxyTree(newRootID);
422
+ ProxyTree oldRoot = new ProxyTree(oldRootID);
423
+ listener.becomeRoot(newRoot, oldRoot);
424
+ }
425
+ else if ( elements[0].equals("addChild") ) {
426
+ int rootID = Integer.parseInt(elements[1]);
427
+ int childID = Integer.parseInt(elements[2]);
428
+ ProxyTree root = new ProxyTree(rootID);
429
+ ProxyTree child = new ProxyTree(childID);
430
+ listener.addChild(root, child);
431
+ }
432
+ else if ( elements[0].equals("setTokenBoundaries") ) {
433
+ int ID = Integer.parseInt(elements[1]);
434
+ ProxyTree node = new ProxyTree(ID);
435
+ listener.setTokenBoundaries(
436
+ node,
437
+ Integer.parseInt(elements[2]),
438
+ Integer.parseInt(elements[3]));
439
+ }
440
+ else {
441
+ //System.err.println("unknown debug event: "+line);
442
+ android.util.Log.e(TAG, "unknown debug event: "+line);
443
+ }
444
+ }
445
+
446
+ protected ProxyTree deserializeNode(String[] elements, int offset) {
447
+ int ID = Integer.parseInt(elements[offset+0]);
448
+ int type = Integer.parseInt(elements[offset+1]);
449
+ int tokenLine = Integer.parseInt(elements[offset+2]);
450
+ int charPositionInLine = Integer.parseInt(elements[offset+3]);
451
+ int tokenIndex = Integer.parseInt(elements[offset+4]);
452
+ String text = elements[offset+5];
453
+ text = unEscapeNewlines(text);
454
+ return new ProxyTree(ID, type, tokenLine, charPositionInLine, tokenIndex, text);
455
+ }
456
+
457
+ protected ProxyToken deserializeToken(String[] elements,
458
+ int offset)
459
+ {
460
+ String indexS = elements[offset+0];
461
+ String typeS = elements[offset+1];
462
+ String channelS = elements[offset+2];
463
+ String lineS = elements[offset+3];
464
+ String posS = elements[offset+4];
465
+ String text = elements[offset+5];
466
+ text = unEscapeNewlines(text);
467
+ int index = Integer.parseInt(indexS);
468
+ ProxyToken t =
469
+ new ProxyToken(index,
470
+ Integer.parseInt(typeS),
471
+ Integer.parseInt(channelS),
472
+ Integer.parseInt(lineS),
473
+ Integer.parseInt(posS),
474
+ text);
475
+ return t;
476
+ }
477
+
478
+ /** Create a thread to listen to the remote running recognizer */
479
+ public void start() {
480
+ Thread t = new Thread(this);
481
+ t.start();
482
+ }
483
+
484
+ public void run() {
485
+ eventHandler();
486
+ }
487
+
488
+ // M i s c
489
+
490
+ public String[] getEventElements(String event) {
491
+ if ( event==null ) {
492
+ return null;
493
+ }
494
+ String[] elements = new String[MAX_EVENT_ELEMENTS];
495
+ String str = null; // a string element if present (must be last)
496
+ try {
497
+ int firstQuoteIndex = event.indexOf('"');
498
+ if ( firstQuoteIndex>=0 ) {
499
+ // treat specially; has a string argument like "a comment\n
500
+ // Note that the string is terminated by \n not end quote.
501
+ // Easier to parse that way.
502
+ String eventWithoutString = event.substring(0,firstQuoteIndex);
503
+ str = event.substring(firstQuoteIndex+1,event.length());
504
+ event = eventWithoutString;
505
+ }
506
+ StringTokenizer st = new StringTokenizer(event, "\t", false);
507
+ int i = 0;
508
+ while ( st.hasMoreTokens() ) {
509
+ if ( i>=MAX_EVENT_ELEMENTS ) {
510
+ // ErrorManager.internalError("event has more than "+MAX_EVENT_ELEMENTS+" args: "+event);
511
+ return elements;
512
+ }
513
+ elements[i] = st.nextToken();
514
+ i++;
515
+ }
516
+ if ( str!=null ) {
517
+ elements[i] = str;
518
+ }
519
+ }
520
+ catch (Exception e) {
521
+ //e.printStackTrace(System.err);
522
+ android.util.Log.e(TAG, android.util.Log.getStackTraceString(e));
523
+ }
524
+ return elements;
525
+ }
526
+
527
+ protected String unEscapeNewlines(String txt) {
528
+ // this unescape is slow but easy to understand
529
+ txt = txt.replaceAll("%0A","\n"); // unescape \n
530
+ txt = txt.replaceAll("%0D","\r"); // unescape \r
531
+ txt = txt.replaceAll("%25","%"); // undo escaped escape chars
532
+ return txt;
533
+ }
534
+
535
+ public boolean tokenIndexesAreInvalid() {
536
+ return false;
537
+ //return tokenIndexesInvalid;
538
+ }
539
+
540
+ }
541
+