This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PoWriter2.java
518 lines (480 loc) · 18.9 KB
/
PoWriter2.java
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* Copyright 2013, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.adapter.po;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringUtils;
import org.fedorahosted.tennera.jgettext.HeaderFields;
import org.fedorahosted.tennera.jgettext.Message;
import org.fedorahosted.tennera.jgettext.PoWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.common.ContentState;
import org.zanata.common.io.DigestWriter;
import org.zanata.common.io.FileDetails;
import org.zanata.rest.dto.extensions.comment.SimpleComment;
import org.zanata.rest.dto.extensions.gettext.HeaderEntry;
import org.zanata.rest.dto.extensions.gettext.PoHeader;
import org.zanata.rest.dto.extensions.gettext.PoTargetHeader;
import org.zanata.rest.dto.extensions.gettext.PotEntryHeader;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.util.PathUtil;
import com.google.common.base.Charsets;
public class PoWriter2 {
private static final Logger log = LoggerFactory.getLogger(PoWriter2.class);
private static final int DEFAULT_NPLURALS = 1;
private static final String CONTINUE_ERROR_MESSAGE_FMT =
"%s. %s, please use --continue-after-error option.";
private final PoWriter poWriter;
private boolean mapIdToMsgctxt;
private boolean continueAfterError;
// TODO Expose and use the one in
// org.fedorahosted.tennera.jgettext.HeaderFields
// Modified version to extract the nplurals value
private static final Pattern pluralPattern = Pattern.compile(
"nplurals(\\s*?)=(\\s*?)(\\d*?)(\\s*?);(\\s*?)(.*)",
Pattern.CASE_INSENSITIVE);
/**
* @param encodeTabs
* @param mapIdToMsgctxt
* true to output zanata id as msgctxt, which can be used by
* {@link PoReader2} to correctly match the ID for text flows
* that are not originally from po documents. This should be
* false if the documents to be written were originally in po
* files.
* @param continueAfterError
* true to try to workaround an error and continue
*/
public PoWriter2(boolean encodeTabs, boolean mapIdToMsgctxt,
boolean continueAfterError) {
this.continueAfterError = continueAfterError;
this.poWriter = new PoWriter(encodeTabs);
this.mapIdToMsgctxt = mapIdToMsgctxt;
}
public PoWriter2(boolean encodeTabs, boolean mapIdToMsgctxt) {
this(encodeTabs, mapIdToMsgctxt, false);
}
public PoWriter2(boolean encodeTabs) {
this(encodeTabs, false, false);
}
public PoWriter2() {
this(false);
}
/**
* Generates a pot file from Resource (document), using the publican
* directory layout.
*
* @param baseDir
* @param doc
* @throws IOException
*/
@Deprecated
public void writePot(File baseDir, Resource doc) throws IOException {
// write the POT file to pot/$name.pot
File potDir = new File(baseDir, "pot");
writePotToDir(potDir, doc);
}
/**
* Generates a pot file from Resource (document), in the specified
* directory.
*
* @param potDir
* @param doc
* @throws IOException
*/
@Deprecated
public void writePotToDir(File potDir, Resource doc) throws IOException {
// write the POT file to $potDir/$name.pot
File potFile = new File(potDir, doc.getName() + ".pot");
writePotToFile(potFile, doc);
}
/**
* Generates a pot file from Resource (document).
*
* @param doc
* @param potFile
* file to be written
* @throws IOException
*/
public void writePotToFile(File potFile, Resource doc) throws IOException {
PathUtil.makeParents(potFile);
Writer fWriter =
new OutputStreamWriter(new FileOutputStream(potFile),
Charsets.UTF_8);
try {
write(fWriter, "UTF-8", doc, null);
} finally {
fWriter.close();
}
}
/**
* Generates a pot file from a Resource, writing it directly to an output
* stream.
*/
public void writePot(OutputStream stream, String charset, Resource doc)
throws IOException {
OutputStreamWriter osWriter = new OutputStreamWriter(stream, charset);
write(osWriter, charset, doc, null);
osWriter.flush();
}
/**
* Generates a po file from a Resource and a TranslationsResource, using the
* publican directory layout.
*
* @param baseDir
* @param doc
* @param locale
* @param targetDoc
* @throws IOException
*/
@Deprecated
public void writePo(File baseDir, Resource doc, String locale,
TranslationsResource targetDoc) throws IOException {
// write the PO file to $locale/$name.po
File localeDir = new File(baseDir, locale);
File poFile = new File(localeDir, doc.getName() + ".po");
writePoToFile(poFile, doc, targetDoc);
}
/**
* Generates a po file from a Resource and a TranslationsResource.
*
* @param poFile
* file to be written
* @param doc
* a source Resource whose translation is to be written
* @param targetDoc
* translated document to be written
* @return
* @throws IOException
*/
public FileDetails writePoToFile(File poFile, Resource doc,
TranslationsResource targetDoc) throws IOException {
PathUtil.makeDirs(poFile.getParentFile());
MessageDigest md5Digest;
try {
md5Digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
Writer fWriter =
new OutputStreamWriter(new FileOutputStream(poFile),
Charsets.UTF_8);
try {
DigestWriter dWriter = new DigestWriter(fWriter, md5Digest);
write(dWriter, "UTF-8", doc, targetDoc);
FileDetails details = new FileDetails(poFile);
details.setMd5(new String(Hex.encodeHex(md5Digest.digest())));
return details;
} finally {
fWriter.close();
}
}
/**
* Generates a po file from a Resource and a TranslationsResource, writing
* it directly to an output stream.
*
* @param stream
* @param doc
* @param targetDoc
* @throws IOException
*/
public void writePo(OutputStream stream, String charset, Resource doc,
TranslationsResource targetDoc) throws IOException {
OutputStreamWriter osWriter = new OutputStreamWriter(stream, charset);
write(osWriter, charset, doc, targetDoc);
osWriter.flush();
}
/**
* Generates a pot or po file from a Resource and/or TranslationsResource.
* If targetDoc is non-null, a po file will be generated from the Resource
* and TranslationsResource, otherwise a pot file will be generated from the
* Resource only.
*
* @param writer
* @param document
* @param targetDoc
* @throws IOException
*/
private void write(Writer writer, String charset, Resource document,
TranslationsResource targetDoc) throws IOException {
PoHeader poHeader =
document.getExtensions(true).findByType(PoHeader.class);
HeaderFields hf = new HeaderFields();
// we don't expect a pot header for mapped non-pot documents
if (poHeader == null) {
if (!mapIdToMsgctxt) {
log.warn("No PO header in document named " + document.getName());
}
} else {
copyToHeaderFields(hf, poHeader.getEntries());
}
setEncodingHeaderFields(hf, charset);
Map<String, TextFlowTarget> targets =
new HashMap<String, TextFlowTarget>();
Message headerMessage = null;
int nPlurals = DEFAULT_NPLURALS;
if (targetDoc != null) {
PoTargetHeader poTargetHeader =
targetDoc.getExtensions(true).findByType(
PoTargetHeader.class);
if (poTargetHeader != null) {
copyToHeaderFields(hf, poTargetHeader.getEntries());
headerMessage = hf.unwrap();
// By default, header message unwraps as fuzzy, so avoid it
headerMessage.setFuzzy(false);
copyCommentsToHeader(poTargetHeader, headerMessage);
nPlurals = extractNPlurals(poTargetHeader);
}
for (TextFlowTarget target : targetDoc.getTextFlowTargets()) {
targets.put(target.getResId(), target);
}
}
if (headerMessage == null) {
headerMessage = hf.unwrap();
}
poWriter.write(headerMessage, writer);
writer.write("\n");
// first write header
for (TextFlow textFlow : document.getTextFlows()) {
PotEntryHeader entryData =
textFlow.getExtensions(true).findByType(
PotEntryHeader.class);
SimpleComment srcComment =
textFlow.getExtensions().findByType(SimpleComment.class);
Message message = new Message();
copyTFContentsToMessage(textFlow, message);
List<String> tftContents = new ArrayList<String>();
TextFlowTarget tfTarget = targets.get(textFlow.getId());
if (tfTarget != null) {
if (!tfTarget.getResId().equals(textFlow.getId())) {
throw new RuntimeException(
"ID from target doesn't match text-flow ID");
}
tftContents.addAll(tfTarget.getContents());
if (tfTarget.getState() == ContentState.NeedReview) {
message.setFuzzy(true);
}
copyCommentsToMessage(tfTarget, message);
}
copyTFTContentsToMessage(document.getName(), textFlow, tftContents, nPlurals, message);
if (entryData != null) {
copyMetadataToMessage(entryData, srcComment, message);
} else {
// we don't expect a pot header for mapped non-pot documents
if (!mapIdToMsgctxt) {
log.warn("Missing POT entry for text-flow ID "
+ textFlow.getId());
}
}
if (mapIdToMsgctxt) {
mapIdToMsgctxt(message, textFlow.getId());
}
poWriter.write(message, writer);
writer.write("\n");
}
}
/**
* Populate msgctxt with text flow id.
*
* @throws RuntimeException
* if there is already a value in msgctxt
*/
private void mapIdToMsgctxt(Message message, String textFlowId) {
// safety check to avoid clobbering existing msgctxt
// (this mapping should not be used for resources from po files)
if (message.getMsgctxt() != null) {
throw new RuntimeException(
"Mapping id to msgctxt, but there is already a msgctxt for text flow id: "
+ textFlowId);
}
message.setMsgctxt(textFlowId);
}
private static void copyCommentsToHeader(PoTargetHeader poTargetHeader,
Message headerMessage) {
for (String s : poTargetHeader.getComment().split("\n")) {
headerMessage.addComment(s);
}
}
private void copyTFContentsToMessage(TextFlow textFlow, Message message) {
List<String> tfContents = textFlow.getContents();
message.setMsgid(tfContents.get(0));
if (textFlow.isPlural()) {
if (tfContents.size() < 1) {
throw new RuntimeException(
"textflow has plural flag but only has one form: resId="
+ textFlow.getId());
}
message.setMsgidPlural(tfContents.get(1));
} else {
if (tfContents.size() > 1) {
if (continueAfterError) {
log.warn(
"textflow has no plural flag but has multiple plural forms: resId={}",
textFlow.getId());
} else {
throwContinueableException(
"textflow has no plural flag but multiple plural forms: [resId="
+ textFlow.getId()
+ "]. This is likely caused by changed plural forms",
"To write content as singular form and continue");
}
}
}
if (tfContents.size() > 2) {
throw new RuntimeException(
"POT format only supports 2 plural forms: resId="
+ textFlow.getId());
}
}
/**
* @see org.zanata.adapter.po.PoWriter2#CONTINUE_ERROR_MESSAGE_FMT
* @param specificErrorMessage
* @param specificRemedy
*/
private static void throwContinueableException(String specificErrorMessage,
String specificRemedy) {
throw new RuntimeException(String.format(CONTINUE_ERROR_MESSAGE_FMT,
specificErrorMessage, specificRemedy));
}
private void
copyCommentsToMessage(TextFlowTarget tfTarget, Message message) {
SimpleComment poComment =
tfTarget.getExtensions().findByType(SimpleComment.class);
if (poComment != null) {
String[] comments = poComment.getValue().split("\n");
if (comments.length == 1 && comments[0].isEmpty()) {
// nothing
} else {
for (String comment : comments) {
message.getComments().add(comment);
}
}
}
}
private void copyTFTContentsToMessage(String docName, TextFlow textFlow,
List<String> tftContents, int nPlurals, Message message) {
if (message.isPlural()) {
while (tftContents.size() < nPlurals) {
tftContents.add("");
}
for (int i = 0; i < tftContents.size(); i++) {
message.addMsgstrPlural(tftContents.get(i), i);
}
if (tftContents.size() > nPlurals) {
log.warn("Marking as fuzzy: too many plural forms for text "
+ "flow: resId={}, doc={}", textFlow.getId(), docName);
message.setFuzzy(true);
}
} else {
if (tftContents.size() == 0) {
message.setMsgstr("");
} else {
message.setMsgstr(tftContents.get(0));
if (tftContents.size() > 1) {
log.warn("Marking as fuzzy: unexpected plural translation "
+ "found for text flow: resId={}, doc={}",
textFlow.getId(), docName);
message.setFuzzy(true);
}
}
}
}
static void setEncodingHeaderFields(HeaderFields hf, String charset) {
hf.setValue(HeaderFields.KEY_MimeVersion, "1.0");
hf.setValue(HeaderFields.KEY_ContentTransferEncoding, "8bit");
String ct, contentType = hf.getValue(HeaderFields.KEY_ContentType);
if (contentType == null) {
ct = "text/plain; charset=" + charset;
} else {
ct =
contentType.replaceFirst("charset=[^;]*", "charset="
+ charset);
}
hf.setValue(HeaderFields.KEY_ContentType, ct);
}
static void copyToHeaderFields(HeaderFields hf,
final List<HeaderEntry> entries) {
for (HeaderEntry e : entries) {
hf.setValue(e.getKey(), e.getValue());
}
}
private static void copyMetadataToMessage(PotEntryHeader data,
SimpleComment simpleComment, Message message) {
if (data != null) {
String context = data.getContext();
if (context != null)
message.setMsgctxt(context);
for (String flag : data.getFlags()) {
message.addFormat(flag);
}
for (String ref : data.getReferences()) {
message.addSourceReference(ref);
}
}
if (simpleComment != null) {
String[] comments =
StringUtils.splitPreserveAllTokens(
simpleComment.getValue(), "\n");
if (!(comments.length == 1 && comments[0].isEmpty())) {
for (String comment : comments) {
message.addExtractedComment(comment);
}
}
}
}
/**
* Determines the number of plural entries to fill for the TransResource. If
* this value can't be found, this method will provide a sensible default.
*/
/*
* TODO This method is similar to org.zanata.rest.service.ResourceUtils, so
* perhaps it should be placed in a common class.
*/
private static int extractNPlurals(PoTargetHeader header) {
for (HeaderEntry entry : header.getEntries()) {
if (entry.getKey().equals("Plural-Forms")) {
Matcher pluralMatcher = pluralPattern.matcher(entry.getValue());
if (pluralMatcher.find()) {
String pluralStr = pluralMatcher.group(3);
return Integer.parseInt(pluralStr);
}
}
}
// No suitable nplural entry found. return default
return DEFAULT_NPLURALS;
}
}