source: other-projects/the-macronizer/trunk/src/java/monogram/plugin/PluginODT.java@ 29855

Last change on this file since 29855 was 29855, checked in by davidb, 9 years ago

John's code after refactoring by Tom over the summer of 2014/2015

File size: 2.2 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package monogram.plugin;
7
8import util.Utils4JJ;
9import java.io.File;
10import java.io.IOException;
11import java.util.UUID;
12import monogram.restorer.XMLRestorer;
13import util.FileUtil;
14
15/**
16 *
17 * @author OEM
18 */
19public class PluginODT implements Plugin {
20
21 private File tmpdir;
22
23 public PluginODT(File tmpdir) {
24 this.tmpdir = tmpdir;
25 }
26
27 public File run(PluginConfiguration configuration) throws Exception {
28 //Create a directory to store the unzipped file.
29 File unzipDir = createTmpDirectoryInTmp();
30 //Unzip the file in the directory.
31 Utils4JJ.unzip(configuration.getFile(), unzipDir);
32
33 //Restore the document.xml file.
34 File documentIn = new File(unzipDir, "content.xml");
35 File documentOut = new File(unzipDir, "mi-tmp-content.xml");
36 XMLRestorer restorer = new XMLRestorer();
37 restorer.restore(documentIn, "utf-8", documentOut, configuration.getPreserveExistingMacrons());
38 //Delete the old document.xml and rename the restored document.
39 documentIn.delete();
40 documentOut.renameTo(new File(unzipDir.getCanonicalPath() + File.separator + "content.xml"));
41
42 //Create a file to store the zipped file.
43 File zipFile = File.createTempFile("mi-tmp-", configuration.getFileType(), tmpdir);
44 //Zip the file.
45 Utils4JJ.zipDir(unzipDir, null, zipFile);
46 //Delete zip directory.
47 if (unzipDir.getName().startsWith("mi-tmp-")) {
48 FileUtil.deleteFile(unzipDir);
49 }
50 return zipFile;
51 }
52
53 //TODO move this to fileutil preferably with methods from Utils4JJ.
54 private File createTmpDirectoryInTmp() throws IOException {
55 final int maxAttempts = 10;
56 int attempt = 0;
57 do {
58 String random = UUID.randomUUID().toString();
59 File dir = new File(tmpdir + File.separator + "mi-tmp-" + random);
60 if (! dir.exists() && dir.mkdir()) {
61 System.out.println(dir.getCanonicalPath());
62 return dir;
63 }
64 } while (++attempt < maxAttempts);
65 throw new IOException("Cound not create temp directory.");
66 }
67}
Note: See TracBrowser for help on using the repository browser.