This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
PoWriter2.java
282 lines (262 loc) · 8.98 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
package org.zanata.adapter.po;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.fedorahosted.tennera.jgettext.HeaderFields;
import org.fedorahosted.tennera.jgettext.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.common.ContentState;
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;
public class PoWriter2
{
private static final Logger log = LoggerFactory.getLogger(PoWriter2.class);
private final org.fedorahosted.tennera.jgettext.PoWriter poWriter = new org.fedorahosted.tennera.jgettext.PoWriter();
public PoWriter2()
{
}
private void mkdirs(File dir) throws IOException
{
if (!dir.exists())
{
if (!dir.mkdirs())
throw new IOException("unable to create output directory: " + dir);
}
}
/**
* Generates a pot file from Resource (document), using the publican
* directory layout.
*
* @param baseDir
* @param doc
* @throws IOException
*/
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
*/
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");
PathUtil.makeParents(potFile);
FileWriter fWriter = new FileWriter(potFile);
write(fWriter, "UTF-8", doc, null);
}
/**
* 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
*/
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");
mkdirs(poFile.getParentFile());
FileWriter fWriter = new FileWriter(poFile);
write(fWriter, "UTF-8", doc, targetDoc);
}
/**
* 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);
}
/**
* Generates a pot or po file from a Resource and/or TranslationsResource,
* using the publican directory layout. If targetDoc is non-null, a po file
* will be generated from Resource+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();
if (poHeader == null)
{
log.warn("No PO header in document named " + document.getName());
}
else
{
copyToHeaderFields(hf, poHeader.getEntries());
}
setEncodingHeaderFields(hf, charset);
Message headerMessage = null;
if (targetDoc != null)
{
PoTargetHeader poTargetHeader = targetDoc.getExtensions(true).findByType(PoTargetHeader.class);
if (poTargetHeader != null)
{
copyToHeaderFields(hf, poTargetHeader.getEntries());
headerMessage = hf.unwrap();
headerMessage.setFuzzy(false); // By default, header message unwraps as fuzzy, so avoid it
copyTargetHeaderComments(headerMessage, poTargetHeader);
}
}
if (headerMessage == null)
{
headerMessage = hf.unwrap();
}
poWriter.write(headerMessage, writer);
writer.write("\n");
Map<String, TextFlowTarget> targets = null;
if (targetDoc != null)
{
targets = new HashMap<String, TextFlowTarget>();
for (TextFlowTarget target : targetDoc.getTextFlowTargets())
{
targets.put(target.getResId(), target);
}
}
// 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();
message.setMsgid(textFlow.getContent());
message.setMsgstr("");
if (targetDoc != null)
{
TextFlowTarget contentData = targets.get(textFlow.getId());
if (contentData != null)
{
if (entryData == null)
{
log.warn("Missing POT entry for text-flow ID " + textFlow.getId());
}
else if (!contentData.getResId().equals(textFlow.getId()))
{
throw new RuntimeException("ID from target doesn't match text-flow ID");
}
message.setMsgstr(contentData.getContent());
SimpleComment poComment = contentData.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);
}
}
}
if (contentData.getState() == ContentState.NeedReview)
{
message.setFuzzy(true);
}
}
}
if (entryData != null)
copyToMessage(entryData, srcComment, message);
poWriter.write(message, writer);
writer.write("\n");
}
}
private static void copyTargetHeaderComments(Message headerMessage, PoTargetHeader poTargetHeader)
{
for (String s : poTargetHeader.getComment().split("\n"))
{
headerMessage.addComment(s);
}
}
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 copyToMessage(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);
}
}
}
}
}