-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGttClient.java
More file actions
278 lines (236 loc) · 7.93 KB
/
GttClient.java
File metadata and controls
278 lines (236 loc) · 7.93 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.gtt;
import com.google.common.collect.ImmutableMap;
import com.google.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.util.ServiceException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
/**
* Contains utility methods to connect to the Google Translator Toolkit (Gtt)
* service and list/update/download/delete/share
* documents/translation-memories/glossaries using the Google Data API's
* java client library as the interface.
*
*/
public class GttClient {
private static final String USER_PROMPT
= "\nPlease enter a command or Type 'help' for list of commands."
+ "\nGoogle Translator Toolkit >";
private static final Map<String, GttCommand> NAME_TO_COMMAND_MAP;
static {
ImmutableMap.Builder<String, GttCommand> builder = ImmutableMap.builder();
for (GttCommand command : GttCommand.values()) {
builder.put(command.name().toLowerCase(), command);
}
NAME_TO_COMMAND_MAP = builder.build();
}
/**
* Prevent the creation of utility class object.
*/
private GttClient() {
}
/**
* Uses the command line arguments to authenticate the GoogleService and build
* the basic feed URI, then invokes all the other methods to demonstrate how
* to interface with the Translator toolkit service.
*
* @param args See the usage method.
*/
public static void main(String[] args) {
try {
// Connect to the Google translator toolkit service
GttService service
= new GttService("sample.gtt.GttClient");
// Login if there is a command line argument for it.
if (args.length >= 1
&& NAME_TO_COMMAND_MAP.get(args[0]) == GttCommand.LOGIN) {
GttCommand.LOGIN.execute(service, args);
}
// Input stream to get user input
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Do some actions based on user input
while (true) {
// Get user input
System.out.print(USER_PROMPT);
System.out.flush();
String userInput = in.readLine();
System.out.println();
// Do action corresponding to user input
String[] commandArgs = userInput.split("\\s+");
GttCommand command = NAME_TO_COMMAND_MAP.get(commandArgs[0]);
if (command != null) {
command.execute(service, commandArgs);
} else {
System.out.println("Sorry I did not understand that.");
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* List of commands show-cased in this sample application talking to Google
* Translator toolkit service.
*/
public enum GttCommand implements Command {
/**
* Command to login to Google Translator toolkit.
*/
LOGIN(new Command() {
public void execute(GttService gttService, String[] args)
throws ServiceException {
// Get username, password and feed URI from command-line arguments.
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String userName = parser.getValue("username", "user", "u");
String userPassword = parser.getValue("password", "pass", "p");
if (parser.containsKey("baseuri")) {
FeedUris.setBaseUrl(parser.getValue("baseuri"));
}
if (userName == null || userPassword == null) {
System.out.println(helpString());
} else {
// Authenticate using ClientLogin
gttService.setUserCredentials(userName, userPassword);
System.out.println("\nYou're now logged in as " + userName + "!");
}
}
public String helpString() {
return "Logs in to Google Translator toolkit with new credentials."
+ "\n\t--username <username>\t; Required parameter"
+ "\n\t--password <password>\t; Required parameter"
+ "\n\t--baseuri <uri>\t; "
+ "Optional, default is http://translate.google.com/toolkit/feeds";
}
}),
/**
* Command to list document in Google Translator toolkit inbox.
*/
LIST_DOCS(ListDocumentsCommand.INSTANCE),
/**
* Command to list translation memories.
*/
LIST_TMS(ListTranslationMemoriesCommand.INSTANCE),
/**
* Command to list glossaries.
*/
LIST_GLOSSARIES(ListGlossariesCommand.INSTANCE),
/**
* Command to create documents for translation.
*/
UPLOAD_DOC(UploadDocumentCommand.INSTANCE),
/**
* Command to create translation memories.
*/
ADD_TM(AddTranslationMemoryCommand.INSTANCE),
/**
* Command to create glossaries.
*/
ADD_GLOSSARY(AddGlossaryCommand.INSTANCE),
/**
* Command to delete documents.
*/
DELETE_DOC(DeleteDocumentCommand.INSTANCE),
/**
* Command to delete translation memories.
*/
DELETE_TM(DeleteTranslationMemoryCommand.INSTANCE),
/**
* Command to delete glossaries.
*/
DELETE_GLOSSARY(DeleteGlossaryCommand.INSTANCE),
/**
* Updates documents.
*/
UPDATE_DOC(UpdateDocumentCommand.INSTANCE),
/**
* Updates translation memory.
*/
UPDATE_TM(UpdateTranslationMemoryCommand.INSTANCE),
/**
* Updates glossary.
*/
UPDATE_GLOSSARY(UpdateGlossaryCommand.INSTANCE),
/**
* Updates document sharing.
*/
DOC_SHARING(ShareCommand.DOCUMENTS_INSTANCE),
/**
* Updates translation memory sharing.
*/
TM_SHARING(ShareCommand.TMS_INSTANCE),
/**
* Updates glossary sharing.
*/
GLOSSARY_SHARING(ShareCommand.GLOSSARIES_INSTANCE),
/**
* Downloads a translated document.
*/
DOWNLOAD_DOC(DownloadDocumentCommand.INSTANCE),
/**
* Command to exit the command line interface.
*/
EXIT(new Command() {
public void execute(GttService gttService, String[] args) {
System.out.println(".....thanks for using the Google translator "
+ "toolkit sample gdata app. See you again.");
System.exit(0);
}
public String helpString() {
return "Exits the program.";
}
}),
/**
* Command to print out commands list or help text of other commands.
*/
HELP(new Command() {
public void execute(GttService gttService, String[] args) {
if (args.length > 1 && NAME_TO_COMMAND_MAP.containsKey(args[1])) {
System.out.println(NAME_TO_COMMAND_MAP.get(args[1]).helpString());
} else {
// Print out the list of commands
for (Map.Entry<String, GttCommand> command
: NAME_TO_COMMAND_MAP.entrySet()) {
System.out.println(command.getKey() + "\t; "
+ command.getValue().helpString());
}
}
}
public String helpString() {
return "Type 'help <command>' for information about a specific "
+ "command";
}
});
private final Command delegate;
GttCommand(Command delegate) {
this.delegate = delegate;
}
public void execute(GttService gttService, String[] args) {
try {
delegate.execute(gttService, args);
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nOops!!! There was some problem executing your "
+ "request.");
}
}
public String helpString() {
return delegate.helpString();
}
}
}