-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadDocumentCommand.java
More file actions
217 lines (182 loc) · 7.18 KB
/
UploadDocumentCommand.java
File metadata and controls
217 lines (182 loc) · 7.18 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
/* 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";
}
}