PageRenderTime 98ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/okapi/steps/xliffkit/src/main/java/net/sf/okapi/steps/xliffkit/writer/XLIFFKitWriterStep.java

http://okapi.googlecode.com/
Java | 385 lines | 287 code | 58 blank | 40 comment | 18 complexity | ea741284f79220e798f61c9c2565b4ed MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. /*===========================================================================
  2. Copyright (C) 2010-2011 by the Okapi Framework contributors
  3. -----------------------------------------------------------------------------
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or (at
  7. your option) any later version.
  8. This library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  11. General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this library; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. See also the full LGPL text here: http://www.gnu.org/copyleft/lesser.html
  16. ===========================================================================*/
  17. package net.sf.okapi.steps.xliffkit.writer;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileNotFoundException;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.PrintWriter;
  26. import java.net.URI;
  27. import java.net.URISyntaxException;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import net.sf.okapi.common.Event;
  31. import net.sf.okapi.common.EventType;
  32. import net.sf.okapi.common.IParameters;
  33. import net.sf.okapi.common.LocaleId;
  34. import net.sf.okapi.common.MimeTypeMapper;
  35. import net.sf.okapi.common.UsingParameters;
  36. import net.sf.okapi.common.Util;
  37. import net.sf.okapi.common.XMLWriter;
  38. import net.sf.okapi.common.encoder.EncoderManager;
  39. import net.sf.okapi.common.filterwriter.XLIFFContent;
  40. import net.sf.okapi.common.filterwriter.XLIFFWriter;
  41. import net.sf.okapi.common.pipeline.BasePipelineStep;
  42. import net.sf.okapi.common.pipeline.annotations.StepParameterMapping;
  43. import net.sf.okapi.common.pipeline.annotations.StepParameterType;
  44. import net.sf.okapi.common.resource.DocumentPart;
  45. import net.sf.okapi.common.resource.Ending;
  46. import net.sf.okapi.common.resource.ITextUnit;
  47. import net.sf.okapi.common.resource.Property;
  48. import net.sf.okapi.common.resource.StartDocument;
  49. import net.sf.okapi.common.resource.StartGroup;
  50. import net.sf.okapi.common.resource.StartSubDocument;
  51. import net.sf.okapi.common.resource.TextContainer;
  52. import net.sf.okapi.common.resource.TextFragment;
  53. import net.sf.okapi.lib.persistence.IPersistenceSession;
  54. import net.sf.okapi.lib.persistence.PersistenceSession;
  55. import net.sf.okapi.lib.beans.v1.OkapiBeans;
  56. import net.sf.okapi.persistence.json.jackson.JSONPersistenceSession;
  57. import net.sf.okapi.lib.beans.sessions.OkapiJsonSession;
  58. import net.sf.okapi.steps.xliffkit.opc.TKitRelationshipTypes;
  59. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  60. import org.apache.poi.openxml4j.opc.OPCPackage;
  61. import org.apache.poi.openxml4j.opc.PackagePart;
  62. import org.apache.poi.openxml4j.opc.PackagePartName;
  63. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  64. import org.apache.poi.openxml4j.opc.StreamHelper;
  65. import org.apache.poi.openxml4j.opc.TargetMode;
  66. @SuppressWarnings("unused")
  67. @UsingParameters(Parameters.class)
  68. public class XLIFFKitWriterStep extends BasePipelineStep {
  69. private XLIFFWriter writer;
  70. private LocaleId srcLoc;
  71. private LocaleId trgLoc;
  72. //private boolean inFile;
  73. private String docMimeType;
  74. private String docName;
  75. private String inputEncoding;
  76. private String configId;
  77. private Parameters params;
  78. private URI outputURI;
  79. private String resourcesFileExt = ".json";
  80. private String originalFileName;
  81. private String sourceFileName;
  82. private String xliffFileName;
  83. private String skeletonFileName;
  84. private String resourcesFileName;
  85. private String originalPartName;
  86. private String sourcePartName;
  87. private String xliffPartName;
  88. private String skeletonPartName;
  89. private String resourcesPartName;
  90. private String filterWriterClassName;
  91. private OPCPackage pack;
  92. private File tempXliff;
  93. private File tempResources;
  94. private PersistenceSession session; // resource session
  95. private List<String> sources = new ArrayList<String> ();
  96. private List<String> originals = new ArrayList<String> ();
  97. public XLIFFKitWriterStep() {
  98. super();
  99. params = new Parameters();
  100. session = new OkapiJsonSession();
  101. writer = new XLIFFWriter();
  102. }
  103. public String getDescription () {
  104. return "Generate an XLIFF translation kit. Expects: filter events. Sends back: filter events.";
  105. }
  106. public String getName () {
  107. return "XLIFF Kit Writer";
  108. }
  109. public void close() {
  110. if ( writer != null ) {
  111. writer.close();
  112. }
  113. }
  114. public EncoderManager getEncoderManager() {
  115. return null;
  116. }
  117. @StepParameterMapping(parameterType = StepParameterType.TARGET_LOCALE)
  118. public void setTargetLocale (LocaleId targetLocale) {
  119. this.trgLoc = targetLocale;
  120. }
  121. @StepParameterMapping(parameterType = StepParameterType.OUTPUT_URI)
  122. public void setOutputURI (URI outputURI) {
  123. this.outputURI = outputURI;
  124. }
  125. public Event handleEvent (Event event) {
  126. //System.out.println(event.getEventType());
  127. switch ( event.getEventType() ) {
  128. case NO_OP:
  129. return event;
  130. case START_BATCH:
  131. processStartBatch();
  132. break;
  133. case END_BATCH:
  134. processEndBatch();
  135. break;
  136. case START_DOCUMENT:
  137. processStartDocument((StartDocument)event.getResource());
  138. break;
  139. case END_DOCUMENT:
  140. session.serialize(event);
  141. processEndDocument(); // Closes persistence session
  142. close();
  143. break;
  144. case START_SUBDOCUMENT:
  145. processStartSubDocument((StartSubDocument)event.getResource());
  146. break;
  147. case END_SUBDOCUMENT:
  148. processEndSubDocument((Ending)event.getResource());
  149. break;
  150. case START_GROUP:
  151. processStartGroup((StartGroup)event.getResource());
  152. break;
  153. case END_GROUP:
  154. processEndGroup((Ending)event.getResource());
  155. break;
  156. case TEXT_UNIT:
  157. ITextUnit tu = event.getTextUnit();
  158. processTextUnit(tu); // XLIFF
  159. Event ev = new Event(EventType.TEXT_UNIT, tu.clone());
  160. // TextContainer srcCont = tu.getSource();
  161. // if ( !tu.getSource().contentIsOneSegment() ) {
  162. // srcCont.getSegments().joinAll();
  163. // }
  164. // TextFragment tf = srcCont.getFirstContent();
  165. // tf.setCodedText("", false);
  166. tu.removeTarget(trgLoc); // Also removes AltTranslationsAnnotations
  167. session.serialize(event); // JSON
  168. return ev;
  169. }
  170. session.serialize(event); // won't serialize END_DOCUMENT
  171. return event;
  172. }
  173. private void processStartBatch() {
  174. try {
  175. // If outputURI is defined explicitly in parameters, get it from there, otherwise use the one from the batch item
  176. if (params != null && !Util.isEmpty(params.getOutputURI())) {
  177. outputURI = new URI(params.getOutputURI());
  178. }
  179. } catch (URISyntaxException e) {
  180. // TODO Handle exception
  181. e.printStackTrace();
  182. }
  183. File outFile = new File(outputURI);
  184. if (outFile.exists())
  185. outFile.delete();
  186. Util.createDirectories(outFile.getAbsolutePath());
  187. try {
  188. pack = OPCPackage.openOrCreate(outFile);
  189. } catch (InvalidFormatException e1) {
  190. // TODO Handle exception
  191. }
  192. writer.setCopySource(params.isCopySource());
  193. writer.setPlaceholderMode(params.isPlaceholderMode());
  194. session.setDescription(params.getMessage());
  195. }
  196. private void processEndBatch() {
  197. sources.clear();
  198. originals.clear();
  199. try {
  200. pack.close();
  201. } catch (IOException e) {
  202. // TODO Handle exception
  203. }
  204. }
  205. private void processStartDocument (StartDocument resource) {
  206. close();
  207. srcLoc = resource.getLocale();
  208. docMimeType = resource.getMimeType();
  209. docName = resource.getName();
  210. inputEncoding = resource.getEncoding();
  211. IParameters fparams = resource.getFilterParameters();
  212. if ( fparams == null ) configId = null;
  213. else configId = fparams.getPath();
  214. originalFileName = Util.getFilename(docName, true);
  215. sourceFileName = Util.getFilename(docName, true);
  216. xliffFileName = originalFileName + ".xlf";
  217. resourcesFileName = originalFileName + resourcesFileExt;
  218. skeletonFileName = String.format("resources/%s/%s", sourceFileName, resourcesFileName);
  219. filterWriterClassName = resource.getFilterWriter().getClass().getName();
  220. try {
  221. tempXliff = File.createTempFile(xliffFileName, null);
  222. tempXliff.deleteOnExit();
  223. tempResources = File.createTempFile(resourcesFileName, null);
  224. tempResources.deleteOnExit();
  225. writer.create(tempXliff.getAbsolutePath(), skeletonFileName, resource.getLocale(), trgLoc,
  226. resource.getMimeType(), sourceFileName, params.getMessage());
  227. } catch (IOException e) {
  228. // TODO Handle exception
  229. e.printStackTrace();
  230. }
  231. // Skeleton
  232. try {
  233. session.start(new FileOutputStream(tempResources));
  234. } catch (FileNotFoundException e) {
  235. // TODO Handle exception
  236. }
  237. }
  238. private PackagePart createPart(OPCPackage pack, PackagePart corePart, String name, File file, String contentType, String relationshipType) {
  239. PackagePart part = null;
  240. try {
  241. PackagePartName partName = PackagingURIHelper.createPartName("/" + name);
  242. if (pack.containPart(partName)) return null;
  243. part = pack.createPart(partName, contentType);
  244. if (corePart != null)
  245. corePart.addRelationship(partName, TargetMode.INTERNAL, relationshipType);
  246. else
  247. pack.addRelationship(partName, TargetMode.INTERNAL, relationshipType);
  248. try {
  249. InputStream is = new FileInputStream(file);
  250. OutputStream os = part.getOutputStream();
  251. StreamHelper.copyStream(is, os);
  252. try {
  253. is.close();
  254. os.close();
  255. } catch (IOException e) {
  256. // TODO Handle exception
  257. e.printStackTrace();
  258. }
  259. } catch (FileNotFoundException e) {
  260. // TODO Handle exception
  261. e.printStackTrace();
  262. }
  263. } catch (InvalidFormatException e) {
  264. // TODO Handle exception
  265. e.printStackTrace();
  266. }
  267. return part;
  268. }
  269. private void processEndDocument () {
  270. // Skeleton
  271. session.end();
  272. // XLIFF
  273. writer.close();
  274. // Save to package
  275. originalPartName = String.format("content/original/%s/%s", srcLoc.toString(), originalFileName);
  276. sourcePartName = String.format("content/source/%s/%s", srcLoc.toString(), sourceFileName);
  277. xliffPartName = String.format("content/target/%s.%s/%s", srcLoc.toString(), trgLoc.toString(), xliffFileName);
  278. resourcesPartName = String.format("content/target/%s.%s/resources/%s/%s", srcLoc.toString(), trgLoc.toString(), sourceFileName, resourcesFileName);
  279. PackagePart corePart =
  280. createPart(pack, null, xliffPartName, tempXliff, MimeTypeMapper.XLIFF_MIME_TYPE, TKitRelationshipTypes.CORE_DOCUMENT);
  281. createPart(pack, corePart, resourcesPartName, tempResources, session.getMimeType(), TKitRelationshipTypes.RESOURCES);
  282. if (params.isIncludeSource())
  283. if (!sources.contains(docName)) {
  284. createPart(pack, corePart, sourcePartName, new File(docName), docMimeType, TKitRelationshipTypes.SOURCE);
  285. sources.add(docName);
  286. }
  287. if (params.isIncludeOriginal())
  288. if (!originals.contains(docName)) {
  289. createPart(pack, corePart, originalPartName, new File(docName), docMimeType, TKitRelationshipTypes.ORIGINAL);
  290. originals.add(docName);
  291. }
  292. }
  293. private void processStartSubDocument (StartSubDocument resource) {
  294. writer.writeStartFile(resource.getName(), resource.getMimeType(), skeletonFileName);
  295. }
  296. private void processEndSubDocument (Ending resource) {
  297. writer.writeEndFile();
  298. }
  299. private void processStartGroup (StartGroup resource) {
  300. writer.writeStartGroup(resource.getId(), resource.getName(), resource.getType());
  301. }
  302. private void processEndGroup (Ending resource) {
  303. writer.writeEndGroup();
  304. }
  305. private void processTextUnit (ITextUnit tu) {
  306. writer.writeTextUnit(tu);
  307. }
  308. @Override
  309. public IParameters getParameters() {
  310. return params;
  311. }
  312. public PersistenceSession getSession() {
  313. return session;
  314. }
  315. public void setSession(PersistenceSession session) {
  316. this.session = session;
  317. }
  318. public String getResourcesFileExt() {
  319. return resourcesFileExt;
  320. }
  321. public void setResourcesFileExt(String resourcesFileExt) {
  322. this.resourcesFileExt = resourcesFileExt;
  323. }
  324. }