Inital commit

This commit is contained in:
Tilman
2011-10-26 09:44:02 +02:00
commit 5971f4b417
1813 changed files with 737837 additions and 0 deletions
@@ -0,0 +1,102 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.gtt.GlossaryEntry;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* Adds glossary.
*
*
*/
public class AddGlossaryCommand implements Command {
public static final AddGlossaryCommand INSTANCE
= new AddGlossaryCommand();
/**
* This is a singleton.
*/
private AddGlossaryCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
GlossaryEntry requestEntry = createEntryFromArgs(args);
System.out.print("Adding glossary....");
System.out.flush();
URL feedUrl = FeedUris.getGlossariesFeedUrl();
GlossaryEntry resultEntry = service.insert(feedUrl, requestEntry);
printResults(resultEntry);
}
private GlossaryEntry createEntryFromArgs(String[] args)
throws IOException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
System.out.println("You asked to add a glossary...");
GlossaryEntry entry = new GlossaryEntry();
String title = parser.getValue("title");
System.out.println("...with title " + title);
entry.setTitle(new PlainTextConstruct(title));
String filename = parser.getValue("file");
System.out.println("...with contents from " + filename);
File file = new File(filename);
String mimeType = "text/csv";
MediaFileSource fileSource = new MediaFileSource(file, mimeType);
MediaContent content = new MediaContent();
content.setMediaSource(fileSource);
content.setMimeType(new ContentType(mimeType));
entry.setContent(content);
return entry;
}
private void printResults(GlossaryEntry entry) {
System.out.println("...done, glossary was successfully created with "
+ "attributes.");
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println("->"
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'");
}
public String helpString() {
return "Adds glossaries."
+ "\n\t--title <title>\t; a name for this glossary"
+ "\n\t--file <filename>\t; Path of file containing glossary "
+ "entries";
}
}
@@ -0,0 +1,116 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.gtt.ScopeEntry;
import com.google.gdata.data.gtt.TranslationMemoryEntry;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* Adds translation memories.
*
*
*/
public class AddTranslationMemoryCommand implements Command {
public static final AddTranslationMemoryCommand INSTANCE
= new AddTranslationMemoryCommand();
/**
* This is a singleton.
*/
private AddTranslationMemoryCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
TranslationMemoryEntry requestEntry = createEntryFromArgs(args);
System.out.print("Adding tm....");
System.out.flush();
URL feedUrl = FeedUris.getTranslationMemoriesFeedUrl();
TranslationMemoryEntry resultEntry = service.insert(feedUrl, requestEntry);
printResults(resultEntry);
}
private TranslationMemoryEntry createEntryFromArgs(String[] args)
throws IOException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
System.out.println("You asked to add translation memory...");
TranslationMemoryEntry entry = new TranslationMemoryEntry();
String title = parser.getValue("title");
System.out.println("...with title " + title);
entry.setTitle(new PlainTextConstruct(title));
if (parser.containsKey("file")) {
String filename = parser.getValue("file");
System.out.println("...with contents from " + filename);
File file = new File(filename);
String mimeType = "text/xml";
MediaFileSource fileSource = new MediaFileSource(file, mimeType);
MediaContent content = new MediaContent();
content.setMediaSource(fileSource);
content.setMimeType(new ContentType(mimeType));
entry.setContent(content);
}
if (parser.containsKey("private")) {
System.out.println("...with private access");
entry.setScope(new ScopeEntry(ScopeEntry.Value.PRIVATE));
} else {
System.out.println("...with public access");
entry.setScope(new ScopeEntry(ScopeEntry.Value.PUBLIC));
}
return entry;
}
private void printResults(TranslationMemoryEntry entry) {
System.out.println("...done, translation memory was successfully created "
+ "with given attributes.");
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println("->"
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'"
+ ", scope = '" + entry.getScope().getValue() + "'");
}
public String helpString() {
return "Creates a new translation memory."
+ "\n\t--title <title>\t; a name for this translation memory"
+ "\n\t--file <filename>\t; Optional path of tmx file to upload"
+ "\n\t--private\t; if data in tmx shouldn't be shared with public";
}
}
+47
View File
@@ -0,0 +1,47 @@
/* 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.gdata.client.gtt.GttService;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
/**
* The interface to be implemented by any class whose instances represent a
* gdata request to the translator toolkit service.
*
*
*/
public interface Command {
/**
* Executes the actions specified by the implementation using the given
* GttService object.
*
* @param service An authenticated GttService object.
* @param args arguments for the command, will be parsed using
* {@link sample.util.SimpleCommandLineParser}
*/
public void execute(GttService service, String[] args)
throws ServiceException, IOException;
/**
* @return a help string corresponding to this command.
*/
public String helpString();
}
@@ -0,0 +1,72 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Deletes translation documents.
*
*
*/
public class DeleteDocumentCommand implements Command {
public static final DeleteDocumentCommand INSTANCE
= new DeleteDocumentCommand();
/**
* This is a singleton.
*/
private DeleteDocumentCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String docId = parser.getValue("id");
boolean deletePermanently = parser.containsKey("perm");
URL feedUrl;
if (deletePermanently) {
feedUrl = FeedUris.getDocumentPermDeleteUrl(docId);
} else {
feedUrl = FeedUris.getDocumentFeedUrl(docId);
}
if (deletePermanently) {
System.out.print("Permanently ");
}
System.out.print("Deleting document with id: " + docId + " ...");
System.out.flush();
service.delete(feedUrl);
System.out.println("...done");
}
public String helpString() {
return "Deletes specified translation document."
+ "\n\t--id <id>\t; id of the document to delete"
+ "\n\t--perm\t; if document is to be deleted permanently";
}
}
@@ -0,0 +1,61 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Deletes glossaries.
*
*
*/
public class DeleteGlossaryCommand implements Command {
public static final DeleteGlossaryCommand INSTANCE
= new DeleteGlossaryCommand();
/**
* This is a singleton.
*/
private DeleteGlossaryCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String id = parser.getValue("id");
URL feedUrl = FeedUris.getGlossaryFeedUrl(id);
System.out.print("Deleting glossary with id: " + id + " ....");
System.out.flush();
service.delete(feedUrl);
System.out.println("...done");
}
public String helpString() {
return "Deletes specified glossary."
+ "\n\t--id <id>\t; id of the glossary to delete";
}
}
@@ -0,0 +1,61 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Deletes translation memories.
*
*
*/
public class DeleteTranslationMemoryCommand implements Command {
public static final DeleteTranslationMemoryCommand INSTANCE
= new DeleteTranslationMemoryCommand();
/**
* This is a singleton.
*/
private DeleteTranslationMemoryCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String id = parser.getValue("id");
URL feedUrl = FeedUris.getTranslationMemoryFeedUrl(id);
System.out.print("Deleting translation memory with id: " + id + " ....");
System.out.flush();
service.delete(feedUrl);
System.out.println("...done");
}
public String helpString() {
return "Deletes specified translation memory."
+ "\n\t--id <id>\t; id of the tm to delete";
}
}
@@ -0,0 +1,90 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.util.ServiceException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Downloads a translated document.
*
*
*/
public class DownloadDocumentCommand implements Command {
public static final DownloadDocumentCommand INSTANCE
= new DownloadDocumentCommand();
/**
* This is a singleton.
*/
private DownloadDocumentCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String id = parser.getValue("id");
URL feedUrl = FeedUris.getDocumentDownloadFeedUrl(id);
String targetFile = parser.getValue("file");
System.out.print("Downloading document with id :" + id + " ....");
System.out.flush();
MediaContent mc = new MediaContent();
mc.setUri(feedUrl.toString());
MediaSource ms = service.getMedia(mc);
InputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = ms.getInputStream();
outStream = new FileOutputStream(targetFile);
int c;
while ((c = inStream.read()) != -1) {
outStream.write(c);
}
} finally {
if (inStream != null) {
inStream.close();
}
if (outStream != null) {
outStream.flush();
outStream.close();
}
}
System.out.print("....done. Saved translation to '" + targetFile + "' .");
}
public String helpString() {
return "Downloads a translation document."
+ "\n\t--id <id>\t; Id of the document to download"
+ "\n\t--file <filepath>\t; path where downloaded doc is to be stored";
}
}
+154
View File
@@ -0,0 +1,154 @@
/* 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 java.net.MalformedURLException;
import java.net.URL;
/**
* Contains constants for the various feed uris.
*
*
*/
public class FeedUris {
private static String baseUrl = "http://translate.google.com/toolkit/feeds";
/**
* Prevent instantiation of utility function
*/
private FeedUris() {
}
/**
* @param baseUrlArg the base url for the feeds
*/
public static void setBaseUrl(String baseUrlArg) {
baseUrl = baseUrlArg;
}
/**
* Returns the documents feed url.
*/
public static URL getDocumentsFeedUrl() throws MalformedURLException {
return new URL(baseUrl + "/documents");
}
/**
* Returns the feed url for given document.
*
* @param docId id of the document whose feed url is requried
*/
public static URL getDocumentFeedUrl(String docId)
throws MalformedURLException {
String format = baseUrl + "/documents/%s";
return createUrl(format, docId);
}
/**
* Returns the feed url to use for deleting the given document.
*
* @param docId id of the document whose feed url is requried
*/
public static URL getDocumentPermDeleteUrl(String docId)
throws MalformedURLException {
String format = baseUrl + "/documents/%s?delete=true";
return createUrl(format, docId);
}
/**
* Returns the feed url to use for downloading the given document.
*
* @param docId id of the document whose feed url is requried
*/
public static URL getDocumentDownloadFeedUrl(String docId)
throws MalformedURLException {
String format = baseUrl + "/documents/export/%s";
return createUrl(format, docId);
}
/**
* Returns the translation memories feed url.
*/
public static URL getTranslationMemoriesFeedUrl()
throws MalformedURLException {
return new URL(baseUrl + "/tm");
}
/**
* Returns the feed url for given translation memory.
*
* @param docId id of the translation memory whose feed url is requried
*/
public static URL getTranslationMemoryFeedUrl(String tmId)
throws MalformedURLException {
String format = baseUrl + "/tm/%s";
return createUrl(format, tmId);
}
/**
* Returns the glossaries feed url.
*/
public static URL getGlossariesFeedUrl() throws MalformedURLException {
return new URL(baseUrl + "/glossary");
}
/**
* Returns the feed url for given glossary.
*
* @param docId id of the glossary whose feed url is requried
*/
public static URL getGlossaryFeedUrl(String glossaryId)
throws MalformedURLException {
String format = baseUrl + "/glossary/%s";
return createUrl(format, glossaryId);
}
/**
* Returns the acl feeds url for given feed and entryId.
*
* @param feedName the name of the feed, one of {'documents, 'tm, 'glossary'}
* @param entryId id of the document/translation memory/glossary
* whose feed url is requried
*/
public static URL getAclFeedUrl(String feedName, String entryId)
throws MalformedURLException {
String format = baseUrl + "/acl/%s/%s";
return createUrl(format, feedName, entryId);
}
/**
* Returns the acl feeds url for given feed, entryId and email id.
*
* @param feedName the name of the feed, one of {'documents, 'tm, 'glossary'}
* @param entryId id of the document/translation memory/glossary
* whose feed url is requried
* @param emailId email id of the person relative to whom the feed url is
* required.
*/
public static URL getAclFeedUrl(String feedName, String entryId,
String emailId) throws MalformedURLException {
String format = baseUrl + "/acl/%s/%s/%s";
return createUrl(format, feedName, entryId, emailId);
}
private static URL createUrl(String format, Object... args)
throws MalformedURLException {
String feedUrl = String.format(format, args);
return new URL(feedUrl);
}
}
+278
View File
@@ -0,0 +1,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();
}
}
}
@@ -0,0 +1,163 @@
/* 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.gdata.client.Query;
import com.google.gdata.client.gtt.DocumentQuery;
import com.google.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.Category;
import com.google.gdata.data.ICategory;
import com.google.gdata.data.gtt.DocumentEntry;
import com.google.gdata.data.gtt.DocumentFeed;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Lists translation documents in the user's inbox.
*
*
*/
public class ListDocumentsCommand implements Command {
public static final ListDocumentsCommand INSTANCE
= new ListDocumentsCommand();
/**
* This is a singleton.
*/
private ListDocumentsCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
DocumentQuery query = createQueryFromArgs(args);
System.out.print("Fetching documents....");
System.out.flush();
DocumentFeed resultFeed = service.getFeed(query, DocumentFeed.class);
printResults(resultFeed);
}
private DocumentQuery createQueryFromArgs(String[] args) throws IOException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
System.out.println("You asked to list documents....");
URL feedUrl = FeedUris.getDocumentsFeedUrl();
DocumentQuery query = new DocumentQuery(feedUrl);
if (parser.containsKey("onlydeleted")) {
System.out.println("...that are deleted");
query.setOnlydeleted(true);
}
if (parser.containsKey("onlyhidden")) {
System.out.println("...that are hidden");
Query.CategoryFilter filter = new Query.CategoryFilter();
filter.addCategory(new HiddenCategory());
query.addCategoryFilter(filter);
}
if (parser.containsKey("excludehidden")) {
System.out.println("...that are not hidden");
Query.CategoryFilter filter = new Query.CategoryFilter();
filter.addExcludeCategory(new HiddenCategory());
query.addCategoryFilter(filter);
}
String sharedWithEmail = parser.getValue("sharedwith");
if (sharedWithEmail != null) {
System.out.println("...that are shared with " + sharedWithEmail);
query.setSharedWithEmailId(sharedWithEmail);
}
String startIndex = parser.getValue("start-index");
if (startIndex != null) {
System.out.println("...that start from position " + startIndex);
query.setStartIndex(Integer.parseInt(startIndex));
}
String maxResults = parser.getValue("max-results");
if (maxResults != null) {
System.out.println("...and you don't want more than " + maxResults
+ " results");
query.setMaxResults(Integer.parseInt(maxResults));
}
return query;
}
private void printResults(DocumentFeed resultFeed) {
System.out.println("...done, there are " + resultFeed.getEntries().size()
+ " documents matching the query in your inbox.\n");
int i = 1;
for (DocumentEntry entry : resultFeed.getEntries()) {
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
StringBuilder categories = new StringBuilder();
for (Category category : entry.getCategories()) {
categories.append(category.getLabel())
.append(';');
}
System.out.println(String.valueOf(i++) + ") "
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'"
+ ", % complete = '" + entry.getPercentComplete().getValue() + "'"
+ ", num source words = '" + entry.getNumberOfSourceWords()
.getValue() + "'"
+ ", categories = '" + categories.toString() + "'");
}
System.out.println();
}
/**
* Utility class for hidden category filter.
*/
public static class HiddenCategory implements ICategory {
public String getLabel() {
return com.google.gdata.data.gtt.HiddenCategory.Label.HIDDEN;
}
public String getScheme() {
return com.google.gdata.data.gtt.HiddenCategory.Scheme.LABELS;
}
public String getTerm() {
return com.google.gdata.data.gtt.HiddenCategory.Term.HIDDEN;
}
};
public String helpString() {
return "Lists translation documents in user's inbox."
+ "\n\t--onlydeleted\t; Optional param to show only deleted documents"
+ "\n\t--sharedwith <email-id>\t; Optional param to show only "
+ "documents shared with given user"
+ "\n\t--start-index <number>\t; Optional param to show only "
+ "documents starting from given index"
+ "\n\t--max-results <number>\t; Optional param to show only "
+ "given number of documents"
+ "\n\t--onlyhidden\t; Optional param to show only hidden documents"
+ "\n\t--excludehidden\t; Optional param to not show hidden documents";
}
}
@@ -0,0 +1,85 @@
/* 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.gdata.client.Query;
import com.google.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.gtt.GlossaryEntry;
import com.google.gdata.data.gtt.GlossaryFeed;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Lists glossaries.
*
*
*/
public class ListGlossariesCommand implements Command {
public static final ListGlossariesCommand INSTANCE
= new ListGlossariesCommand();
/**
* This is a singleton.
*/
private ListGlossariesCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
Query query = createQueryFromArgs(args);
System.out.print("Fetching glossaries....");
System.out.flush();
GlossaryFeed resultFeed
= service.getFeed(query, GlossaryFeed.class);
printResults(resultFeed);
}
private Query createQueryFromArgs(String[] args)
throws IOException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
URL feedUrl = FeedUris.getGlossariesFeedUrl();
Query query = new Query(feedUrl);
return query;
}
private void printResults(GlossaryFeed resultFeed) {
System.out.println("...done, there are " + resultFeed.getEntries().size()
+ " glossaries matching the query.\n");
int i = 1;
for (GlossaryEntry entry : resultFeed.getEntries()) {
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println(String.valueOf(i++) + ") "
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'");
}
System.out.println();
}
public String helpString() {
return "Lists glossaries accessible for given user.";
}
}
@@ -0,0 +1,104 @@
/* 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.gdata.client.gtt.GttService;
import com.google.gdata.client.gtt.TranslationMemoryQuery;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.gtt.ScopeEntry;
import com.google.gdata.data.gtt.TranslationMemoryEntry;
import com.google.gdata.data.gtt.TranslationMemoryFeed;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Lists translation memories.
*
*
*/
public class ListTranslationMemoriesCommand implements Command {
public static final ListTranslationMemoriesCommand INSTANCE
= new ListTranslationMemoriesCommand();
/**
* This is a singleton.
*/
private ListTranslationMemoriesCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
TranslationMemoryQuery query = createQueryFromArgs(args);
System.out.print("Fetching translation memories....");
System.out.flush();
TranslationMemoryFeed resultFeed
= service.getFeed(query, TranslationMemoryFeed.class);
printResults(resultFeed);
}
private TranslationMemoryQuery createQueryFromArgs(String[] args)
throws IOException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
URL feedUrl = FeedUris.getTranslationMemoriesFeedUrl();
TranslationMemoryQuery query = new TranslationMemoryQuery(feedUrl);
if (parser.containsKey("onlyprivate")) {
System.out.println("You asked to list all private translation "
+ "memories...");
query.setScope(ScopeEntry.Value.PRIVATE.toString());
} else if (parser.containsKey("onlypublic")) {
System.out.println("You asked to list all public translation "
+ "memories...");
query.setScope(ScopeEntry.Value.PUBLIC.toString());
} else {
System.out.println("You asked to list all translation "
+ "memories...");
}
return query;
}
private void printResults(TranslationMemoryFeed resultFeed) {
System.out.println("...done, there are " + resultFeed.getEntries().size()
+ " translation memories matching the query.\n");
int i = 1;
for (TranslationMemoryEntry entry : resultFeed.getEntries()) {
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println(String.valueOf(i++) + ") "
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'"
+ ", scope = '" + entry.getScope().getValue() + "'");
}
System.out.println();
}
public String helpString() {
return "Lists translation memories accessible for given user."
+ "\n\t--onlypublic\t; Optional param to show only "
+ "public translation memories"
+ "\n\t--onlyprivate\t; Optional param to show only "
+ "translation memories private to the given user";
}
}
+137
View File
@@ -0,0 +1,137 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.acl.AclEntry;
import com.google.gdata.data.acl.AclFeed;
import com.google.gdata.data.acl.AclRole;
import com.google.gdata.data.acl.AclScope;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Update entry sharing.
*
*
*/
public class ShareCommand implements Command {
public static final ShareCommand DOCUMENTS_INSTANCE
= new ShareCommand("documents");
public static final ShareCommand TMS_INSTANCE
= new ShareCommand("tm");
public static final ShareCommand GLOSSARIES_INSTANCE
= new ShareCommand("glossary");
protected final String feedName;
public ShareCommand(String feedName) {
this.feedName = feedName;
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String entryId = parser.getValue("id");
if (parser.containsKey("list")) {
URL feedUrl = FeedUris.getAclFeedUrl(feedName, entryId);
System.out.println("Listing all accessors for " + feedName
+ " with id" + entryId + " ...");
// Get the list of accessors for this entry
AclFeed aclFeed = service.getFeed(feedUrl, AclFeed.class);
printAclInfo(aclFeed);
} else if (parser.containsKey("changetype")) {
String changeType = parser.getValue("changetype");
String emailId = parser.getValue("email");
if ("add".equals(changeType)) {
AclScope scope = new AclScope(AclScope.Type.USER, emailId);
AclRole role = new AclRole(parser.getValue("role"));
// Add a new accessor for this entry
AclEntry entry = new AclEntry();
entry.setRole(role);
entry.setScope(scope);
System.out.println("Adding user " + emailId + " as " + role.getValue()
+ " to " + feedName + " with id " + entryId + " ...");
URL feedUrl = FeedUris.getAclFeedUrl(feedName, entryId);
service.insert(feedUrl, entry);
System.out.println("...done");
} else if ("change".equals(changeType)) {
AclScope scope = new AclScope(AclScope.Type.USER, emailId);
AclRole role = new AclRole(parser.getValue("role"));
// Change the role of an accessor for this entry
AclEntry entry = new AclEntry();
entry.setRole(role);
entry.setScope(scope);
System.out.println("Changing user " + emailId + "'s access to "
+ role.getValue() + " for " + feedName + " with id "
+ entryId + " ...");
URL feedUrl = FeedUris.getAclFeedUrl(feedName, entryId, emailId);
service.update(feedUrl, entry);
System.out.println("...done");
} else if ("remove".equals(changeType)) {
System.out.println("Removing user " + emailId + "'s access to "
+ feedName + " with id " + entryId + " ...");
URL feedUrl = FeedUris.getAclFeedUrl(feedName, entryId, emailId);
// Remove an accessor for this entry
service.delete(feedUrl);
System.out.println("...done");
}
}
}
private void printAclInfo(AclFeed aclFeed)
throws IOException, ServiceException {
System.out.println("...done, currently their are "
+ aclFeed.getEntries().size() + " accessors for this entry.\n");
int i = 1;
for (AclEntry entry : aclFeed.getEntries()) {
System.out.println(String.valueOf(i++) + ") "
+ " scope = '" + entry.getScope().getValue() + "'"
+ ", role = '" + entry.getRole().getValue() + "'");
}
}
public String helpString() {
return "Updates sharing info."
+ "\n\t--id <id>\t; the id of the entry whose acl needs updation"
+ "\n\t--list\t; just list the current collaborators, no "
+ "updation"
+ "\n\t--changetype <type>\t; one of 'add', 'change', 'remove'"
+ "\n\t--email <emailid>\t; email id of user who acl is to be "
+ "updated"
+ "\n\t--role <role>\t; one of 'owner', 'reader', 'writer', "
+ "'commenter'";
}
}
@@ -0,0 +1,122 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.Link;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.gtt.DocumentEntry;
import com.google.gdata.data.gtt.GlossariesElement;
import com.google.gdata.data.gtt.TmsElement;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Updates translation documents.
*
*
*/
public class UpdateDocumentCommand implements Command {
public static final UpdateDocumentCommand INSTANCE
= new UpdateDocumentCommand();
/**
* This is a singleton.
*/
private UpdateDocumentCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String id = parser.getValue("id");
URL feedUrl = FeedUris.getDocumentFeedUrl(id);
DocumentEntry requestEntry = service.getEntry(feedUrl, DocumentEntry.class);
requestEntry.setLastModifiedBy(null);
System.out.println("You want to update document with id:" + id + "...");
if (parser.containsKey("title")) {
String title = parser.getValue("title");
System.out.println("...by changing title to " + title);
requestEntry.setTitle(new PlainTextConstruct(title));
}
if (parser.containsKey("tmids")) {
String tmIds = parser.getValue("tmids");
System.out.println("...by adding translation memories with ids: "
+ tmIds);
TmsElement tm = new TmsElement();
for (String tmId : tmIds.split(",")) {
String tmHref = FeedUris.getTranslationMemoryFeedUrl(tmId).toString();
Link tmLink = new Link();
tmLink.setHref(tmHref);
tm.addLink(tmLink);
}
requestEntry.setTranslationMemory(tm);
}
if (parser.containsKey("glids")) {
String glIds = parser.getValue("glids");
System.out.println("...by adding glossaries with ids: "
+ glIds);
GlossariesElement gl = new GlossariesElement();
for (String glId : glIds.split(",")) {
String glHref = FeedUris.getGlossaryFeedUrl(glId).toString();
Link glLink = new Link();
glLink.setHref(glHref);
gl.addLink(glLink);
}
requestEntry.setGlossary(gl);
}
System.out.print("Updating document....");
System.out.flush();
DocumentEntry resultEntry = service.update(feedUrl, requestEntry);
printResults(resultEntry);
}
private void printResults(DocumentEntry entry) {
System.out.println("...done, document was successfully updated with "
+ "attributes.");
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println("->"
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'");
}
public String helpString() {
return "Updates translation documents."
+ "\n\t--id <id>\t; the id of the document to update"
+ "\n\t--title <title>\t; a name for the document"
+ "\n\t--tmids <id, id, ...>\t; Optional param to attach translation "
+ "memories to document, value must be comma separated tm ids"
+ "\n\t--glids <id, id, ...>\t; Optional param to attach glossaries "
+ "to document, value must be comma separated glossary ids";
}
}
@@ -0,0 +1,108 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.gtt.GlossaryEntry;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* Updates glossaries.
*
*
*/
public class UpdateGlossaryCommand implements Command {
public static final UpdateGlossaryCommand INSTANCE
= new UpdateGlossaryCommand();
/**
* This is a singleton.
*/
private UpdateGlossaryCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String id = parser.getValue("id");
URL feedUrl = FeedUris.getGlossaryFeedUrl(id);
GlossaryEntry requestEntry = service.getEntry(feedUrl, GlossaryEntry.class);
System.out.println("You want to update glossary with id:" + id + " ...");
if (parser.containsKey("title")) {
String title = parser.getValue("title");
System.out.println("...by changing title to " + title);
requestEntry.setTitle(new PlainTextConstruct(title));
}
if (parser.containsKey("file")) {
String filename = parser.getValue("file");
System.out.println("...by appending contents from file " + filename);
File file = new File(filename);
String mimeType = "text/csv";
MediaFileSource fileSource = new MediaFileSource(file, mimeType);
MediaContent content = new MediaContent();
content.setMediaSource(fileSource);
content.setMimeType(new ContentType(mimeType));
requestEntry.setContent(content);
}
System.out.print("Updating glossaries....");
System.out.flush();
GlossaryEntry resultEntry = null;
if (requestEntry.getContent() == null) {
resultEntry = service.update(feedUrl, requestEntry);
} else {
resultEntry = service.updateMedia(feedUrl, requestEntry);
}
printResults(resultEntry);
}
private void printResults(GlossaryEntry entry) {
System.out.println("...done, glossary was successfully updated with "
+ " given attributes.");
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println("->"
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'");
}
public String helpString() {
return "Updates glossary."
+ "\n\t--id <id>\t; id of glossary to update"
+ "\n\t--title <title>\t; a name for this glossary"
+ "\n\t--file <filename>\t; Path of file containing glossary "
+ "entries";
}
}
@@ -0,0 +1,111 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.gtt.TranslationMemoryEntry;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* Update translation memory.
*
*
*/
public class UpdateTranslationMemoryCommand implements Command {
public static final UpdateTranslationMemoryCommand INSTANCE
= new UpdateTranslationMemoryCommand();
/**
* This is a singleton.
*/
private UpdateTranslationMemoryCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
String id = parser.getValue("id");
URL feedUrl = FeedUris.getTranslationMemoryFeedUrl(id);
TranslationMemoryEntry requestEntry = service.getEntry(feedUrl,
TranslationMemoryEntry.class);
System.out.println("You want to update translation memory with id:"
+ id + " ...");
if (parser.containsKey("title")) {
String title = parser.getValue("title");
System.out.println("...by changing title to " + title);
requestEntry.setTitle(new PlainTextConstruct(title));
}
if (parser.containsKey("file")) {
String filename = parser.getValue("file");
System.out.println("...by appending contents from file " + filename);
File file = new File(filename);
String mimeType = "text/xml";
MediaFileSource fileSource = new MediaFileSource(file, mimeType);
MediaContent content = new MediaContent();
content.setMediaSource(fileSource);
content.setMimeType(new ContentType(mimeType));
requestEntry.setContent(content);
}
System.out.print("Updating translation memory....");
System.out.flush();
TranslationMemoryEntry resultEntry = null;
if (requestEntry.getContent() == null) {
resultEntry = service.update(feedUrl, requestEntry);
} else {
resultEntry = service.updateMedia(feedUrl, requestEntry);
}
printResults(resultEntry);
}
private void printResults(TranslationMemoryEntry entry) {
System.out.println("...done, translation memory was successfully updated "
+ "with attributes.");
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println("->"
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'"
+ ", scope = '" + entry.getScope().getValue() + "'");
}
public String helpString() {
return "Updates translation documents."
+ "\n\t--id <id>\t; id of the translation memory to update"
+ "\n\t--title <title>\t; a name for this translation memory"
+ "\n\t--file <filename>\t; Optional path of tmx file to upload";
}
}
@@ -0,0 +1,217 @@
/* 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.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.Link;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.gtt.DocumentEntry;
import com.google.gdata.data.gtt.DocumentSource;
import com.google.gdata.data.gtt.GlossariesElement;
import com.google.gdata.data.gtt.SourceLanguage;
import com.google.gdata.data.gtt.TargetLanguage;
import com.google.gdata.data.gtt.TmsElement;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* Creates translation documents.
*
*
*/
public class UploadDocumentCommand implements Command {
/**
* Represents the MIME types supported by the Translator toolkit GData feed
*/
public enum MediaType {
CSV("text/csv"),
DOC("application/msword"),
HTML("text/html"),
HTM("text/html"),
ODT("application/vnd.oasis.opendocument.text"),
RTF("application/rtf"),
TXT("text/plain"),
AEA("application/octet-stream"),
AES("application/octet-stream"),
SRT("text/plain"),
SUB("text/plain")
;
private String mimeType;
private MediaType(String mimeType) {
this.mimeType = mimeType;
}
public String getMimeType() {
return mimeType;
}
public static MediaType fromFileName(String fileName) {
int index = fileName.lastIndexOf('.');
if (index > 0) {
return valueOf(fileName.substring(index + 1).toUpperCase());
} else {
return valueOf(fileName);
}
}
}
public static final UploadDocumentCommand INSTANCE
= new UploadDocumentCommand();
/**
* This is a singleton.
*/
private UploadDocumentCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
DocumentEntry requestEntry = createEntryFromArgs(args);
System.out.print("Creating document....");
System.out.flush();
URL feedUrl = FeedUris.getDocumentsFeedUrl();
DocumentEntry resultEntry = service.insert(feedUrl, requestEntry);
printResults(resultEntry);
}
private DocumentEntry createEntryFromArgs(String[] args) throws IOException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
DocumentEntry entry = new DocumentEntry();
System.out.println("You want to create a new document...");
String srcLang = parser.getValue("srclang");
System.out.println("... with source language " + srcLang);
entry.setSourceLanguage(new SourceLanguage(srcLang));
String targetLang = parser.getValue("targetlang");
System.out.println("... with target language " + targetLang);
entry.setTargetLanguage(new TargetLanguage(targetLang));
String title = parser.getValue("title");
System.out.println("... with title " + title);
entry.setTitle(new PlainTextConstruct(title));
if (parser.containsKey("weburl")) {
String url = parser.getValue("weburl");
System.out.println("... with html contents from " + url);
DocumentSource docSource = new DocumentSource(DocumentSource.Type.HTML,
url);
entry.setDocumentSource(docSource);
} else if (parser.containsKey("wikipediaurl")) {
String url = parser.getValue("wikipediaurl");
System.out.println("... with mediawiki contents from " + url);
DocumentSource docSource = new DocumentSource(DocumentSource.Type.WIKI,
url);
entry.setDocumentSource(docSource);
} else if (parser.containsKey("knolurl")) {
String url = parser.getValue("knolurl");
System.out.println("... with knol contents from " + url);
DocumentSource docSource = new DocumentSource(DocumentSource.Type.KNOL,
url);
entry.setDocumentSource(docSource);
} else if (parser.containsKey("file")) {
String filename = parser.getValue("file");
System.out.println("... with contents from file at " + filename);
File file = new File(filename);
String mimeType = MediaType.fromFileName(filename).getMimeType();
MediaFileSource fileSource = new MediaFileSource(file, mimeType);
MediaContent content = new MediaContent();
content.setMediaSource(fileSource);
content.setMimeType(new ContentType(mimeType));
entry.setContent(content);
}
if (parser.containsKey("tmids")) {
String tmIds = parser.getValue("tmids");
System.out.println("...by adding translation memories with ids: "
+ tmIds);
TmsElement tm = new TmsElement();
for (String id : tmIds.split(",")) {
String tmHref = FeedUris.getTranslationMemoryFeedUrl(id).toString();
Link tmLink = new Link();
tmLink.setHref(tmHref);
tm.addLink(tmLink);
}
entry.setTranslationMemory(tm);
}
if (parser.containsKey("glids")) {
String glIds = parser.getValue("glids");
System.out.println("...by adding glossaries with ids: "
+ glIds);
GlossariesElement gl = new GlossariesElement();
for (String id : glIds.split(",")) {
String glHref = FeedUris.getGlossaryFeedUrl(id).toString();
Link glLink = new Link();
glLink.setHref(glHref);
gl.addLink(glLink);
}
entry.setGlossary(gl);
}
return entry;
}
private void printResults(DocumentEntry entry) {
System.out.println("...done, document was successfully created with "
+ "attributes.");
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
System.out.println("->"
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'");
}
public String helpString() {
return "Creates translation documents."
+ "\n\t--srclang <lang id>\t; id of source language"
+ "\n\t--targetlang <lang id>\t; id of target language"
+ "\n\t--title <title>\t; a name for the document"
+ "\n\t--file <filename>\t; if content for translation is "
+ "in a local file"
+ "\n\t--weburl <url>\t; if content for translation is a web page"
+ "\n\t--wikipediaurl <url>\t; if content for translation is a "
+ "wikipedia article"
+ "\n\t--knolurl <url>\t; if content for translation is a "
+ "knol article"
+ "\n\t--tmids <id, id, ...>\t; Optional param to attach translation "
+ "memories to document, value must be comma separated tm ids"
+ "\n\t--glids <id, id, ...>\t; Optional param to attach glossaries "
+ "to document, value must be comma separated glossary ids";
}
}
Binary file not shown.