source: other-projects/the-macronizer/trunk/src/java/monogram/plugin/PluginDOCX.java@ 30062

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

Removal/Tidy-up of debug statements

File size: 2.3 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 java.io.File;
9import java.io.IOException;
10import java.util.UUID;
11import org.fuin.utils4j.Utils4J;
12import monogram.restorer.XMLRestorer;
13import util.FileUtil;
14
15/**
16 *
17 * @author OEM
18 */
19public class PluginDOCX implements Plugin {
20
21 private File tmpdir;
22
23 public PluginDOCX(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 Utils4J.unzip(configuration.getFile(), unzipDir);
32
33 //Restore the document.xml file.
34 File documentIn = new File(unzipDir, "word" + File.separator + "document.xml");
35 File documentOut = new File(unzipDir, "word" + File.separator + "mi-tmp-document.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 + "word" + File.separator + "document.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 Utils4J.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 private File createTmpDirectoryInTmp() throws IOException {
54 final int maxAttempts = 10;
55 int attempt = 0;
56 do {
57 String random = UUID.randomUUID().toString();
58 File dir = new File(tmpdir + File.separator + "mi-tmp-" + random);
59 if (! dir.exists() && dir.mkdir()) {
60 System.out.println("PluginDOCX::createTmpDirectoryInTmp(): " + dir.getCanonicalPath());
61 return dir;
62 }
63 } while (++attempt < maxAttempts);
64 throw new IOException("Cound not create temp directory.");
65 }
66}
Note: See TracBrowser for help on using the repository browser.