1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package name.angoca.zemucan.ui.impl.jline;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
34
35 import jline.Completor;
36 import name.angoca.zemucan.AbstractZemucanException;
37 import name.angoca.zemucan.interfaze.InterfaceCore;
38 import name.angoca.zemucan.interfaze.model.ReturnOptions;
39 import name.angoca.zemucan.tools.messages.jline.Messages;
40 import name.angoca.zemucan.ui.api.InputReader;
41
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class ZemucanCompletor implements Completor {
69
70
71
72 private static final Logger LOGGER = LoggerFactory
73 .getLogger(ZemucanCompletor.class);
74
75
76
77
78 private boolean assertsEnabled;
79
80
81
82
83 ZemucanCompletor() {
84 this.assertsEnabled = false;
85
86 assert this.assertsEnabled = true;
87 }
88
89
90
91
92
93
94
95 @Override
96 @SuppressWarnings("unchecked")
97 public final int complete(final String
98 @SuppressWarnings("rawtypes") final List
99 assert buffer != null;
100 assert candidateRaw != null;
101 if (this.assertsEnabled) {
102 for (final Object object : candidateRaw) {
103 assert object != null;
104 }
105 }
106
107 final List<String> candidates = candidateRaw;
108
109 ZemucanCompletor.LOGGER.debug(
110 "Completor before: buffer '{}', cursor: {}",
111 buffer, Integer.valueOf(cursor));
112
113 final String phrase = buffer.substring(0, cursor);
114 try {
115 final ReturnOptions answer = InterfaceCore.analyzePhrase(phrase);
116
117
118 final String complete = answer.getPhrase().toLowerCase();
119
120
121 final String trim = phrase.trim().toLowerCase();
122
123
124 if (complete.startsWith(trim)) {
125
126 String diff = complete.substring(trim.length());
127 if (diff.startsWith(" ") && phrase.endsWith(" ")) {
128 diff = diff.substring(1, diff.length());
129 }
130 candidates.add(diff);
131 ZemucanCompletor.LOGGER.debug("Diff '" + diff + "'");
132 } else {
133 candidates.add("");
134 }
135
136
137
138 candidates.addAll(this.fromArrayToColletion(answer.getPhrases()));
139 candidates.addAll(this.fromArrayToColletion(answer.getOptions()));
140
141
142 if ((candidates.size() == 2) && (answer.getOptions().length == 1)
143 && (answer.getPhrases().length == 0)) {
144 candidates.add("");
145 }
146 } catch (final AbstractZemucanException e) {
147 String cause = "";
148 if (e.getCause() != null) {
149 cause = e.getCause().toString();
150 }
151 ZemucanCompletor.LOGGER.error(
152 Messages.getString("ZemucanCompletor.JLINE1-Exception"),
153 new String[] { e.getMessage(), cause });
154 if (e.getCause() != null) {
155 final Throwable ex = e.getCause();
156 ZemucanCompletor.LOGGER.error(Messages.getString("ZemucanCompletor"
157 + ".JLINE2-SpecificProblem"),
158 new String[] { e.getMessage(), cause, ex.getMessage(),
159 ex.getCause().toString() });
160 }
161 System.exit(InputReader.ASSISTING_ERROR);
162 }
163
164 ZemucanCompletor.LOGGER.debug("Completor after: buffer '{}', cursor: {}",
165 buffer, Integer.valueOf(cursor));
166 return cursor;
167 }
168
169
170
171
172
173
174
175
176 private Collection<String>
177 final String[]
178 assert array != null;
179 if (this.assertsEnabled) {
180 for (final String string : array) {
181 assert string != null;
182 }
183 }
184
185 final List<String> collection = new ArrayList<String>();
186 for (final String string : array) {
187 collection.add(string);
188 }
189
190 assert collection != null;
191 if (this.assertsEnabled) {
192 for (final String string : collection) {
193 assert string != null;
194 }
195 }
196 return collection;
197 }
198 }