Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / doc / DocumentToTei.groovy @ 479

History | View | Annotate | Download (5.4 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate: 2016-03-14 17:07:32 +0100 (lun. 14 mars 2016) $
25
// $LastChangedRevision: 3165 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.doc
29

    
30
import java.io.File;
31
import org.txm.importer.ApplyXsl2;
32
import org.txm.importer.ConvertDocument;
33

    
34
/**
35
 * The Class DocumentToTei.
36
 */
37
class DocumentToTei {
38

    
39
        static boolean DEBUG = true;
40

    
41
        public static boolean processFiles(def files, File outdir, File xsldir) {
42
                boolean ret = true;
43

    
44
                File xslOdtTei = new File(xsldir, "tei/odt/odttotei.xsl");
45

    
46
                if (!xsldir.exists()) { println "XslDir does not exists: "+xsldir; return false;}
47
                if (!xslOdtTei.exists()) { println "xslOdtTei file does not exists: "+xslOdtTei; return false;}
48

    
49
                ConvertDocument converter;
50
                for (File file : files) {
51
                        print "."
52
                        String name = file.getName();
53
                        if (file.getName().endsWith(".odt")) {
54
                                name = name.substring(0, name.indexOf(".odt"));
55
                                File teifile = new File(outdir, name+".xml");
56
                                try {
57
                                        if (!new DocumentToTei().run(file, xslOdtTei, teifile)) {
58
                                                println "ODT to TEI failed: "+file
59
                                                //                                                ret = false;
60
                                                //                                                break;
61
                                        }
62
                                } catch(Exception e) { println "ODT to TEI failed: $file: $e"; }
63
                        } else if (file.getName().endsWith(".docx")) {
64
                                name = name.substring(0, name.indexOf(".docx"));
65
                                File teifile = new File(outdir, name+".xml");
66
                                File odtFile = File.createTempFile("workflowdocx", "sfsdf.odt", outdir);
67
                                
68
                                try {
69
                                        converter = new ConvertDocument();
70
                                        converter.setDebug(DEBUG)
71
                                        converter.autoFile(file, odtFile, "odt")
72
                                        if (!new DocumentToTei().run(odtFile, xslOdtTei, teifile)) {
73
                                                println "Docx to Odt to Tei failed: "+file
74
                                                odtFile.delete();
75
                                        }
76
                                } catch(Exception e) { println "DOCX to ODT to TEI failed: $file: $e"; }
77
                                finally { 
78
                                        odtFile.delete();
79
                                        if (converter != null) converter.stop();
80
                                }
81
                                
82
                        } else if (file.getName().endsWith(".doc")) {
83
                                name = name.substring(0, name.indexOf(".doc"));
84
                                File teifile = new File(outdir, name+".xml");
85
                                File odtFile = File.createTempFile("workflowdoc", "sfsdf.odt", outdir);
86
                                
87
                                try {
88
                                        converter = new ConvertDocument();
89
                                        converter.setDebug(DEBUG)
90
                                        converter.autoFile(file, odtFile, "odt")
91
                                        if (!new DocumentToTei().run(odtFile, xslOdtTei, teifile)) {
92
                                                println "Doc to ODT to TEI failed: "+file
93
                                                odtFile.delete();
94
                                        }
95
                                } catch(Exception e) { println "DOC to ODT to TEI failed: $file: $e"; }
96
                                finally {
97
                                        odtFile.delete();
98
                                        if (converter != null) converter.stop();
99
                                }
100
                                
101
                        }
102
                }
103

    
104
                return ret;
105
        }
106

    
107
        /**
108
         * Run.
109
         *
110
         * @param odtfile the odtfile
111
         * @param xslfile the xslfile
112
         * @param teifile the teifile
113
         * @return true, if successful
114
         */
115
        public boolean run(File odtfile, File xslfile, File teifile)
116
        {
117
                println "** ODT TO TEI $odtfile $teifile"
118
                File unzipdir = new File(teifile.getParent(), "files-"+teifile.getName());
119
                unzipdir.deleteDir()
120
                Zip.decompress(odtfile.getAbsolutePath(), unzipdir.getAbsolutePath(), false);
121

    
122
                //println "get content.xml"
123
                File content = new File(unzipdir, "content.xml");
124
                if (!content.exists()) {
125
                        println "OdtToTei: failed to get 'content.xml' file from "+unzipdir
126
                        return false;
127
                }
128

    
129
                //println "apply xsl oo-to-tei"
130
                ApplyXsl2 a = new ApplyXsl2(xslfile.getAbsolutePath());
131
                a.process(content.getAbsolutePath(), teifile.getAbsolutePath());
132

    
133
                //unzipdir.deleteDir()
134
                return true;
135
        }
136

    
137
        /**
138
         * Run.
139
         *
140
         * @param odtfile the odtfile
141
         * @param xslfile the xslfile
142
         * @param teifile the teifile
143
         * @return true, if successful
144
         */
145
        public boolean rundocx(File docxfile, File xslfile, File teifile)
146
        {
147
                //println "** ODT TO TEI $docxfile $teifile"
148
                File unzipdir = new File(docxfile.getParent(), "files-"+teifile.getName());
149
                unzipdir.deleteDir()
150
                Zip.decompress(docxfile.getAbsolutePath(), unzipdir.getAbsolutePath(), false);
151

    
152
                //println "get content.xml"
153
                File content = new File(unzipdir, "word/document.xml");
154
                if (!content.exists()) {
155
                        println "failed to get $content"
156
                        return false;
157
                }
158

    
159
                //println "apply xsl oo-to-tei"
160
                ApplyXsl2 a = new ApplyXsl2(xslfile.getAbsolutePath());
161
                a.setParam("word-directory", unzipdir.getAbsolutePath())
162
                a.process(content.getAbsolutePath(), teifile.getAbsolutePath());
163

    
164
                return true;
165
        }
166
        
167
        public static void main(String[] args) {
168
                File odtfile = new File("/home/mdecorde/xml/manual/Manuel de TXM 0.7 FR.odt")
169
                File xslfile = new File("/home/mdecorde/TXM/xsl/tei/odt/odttotei.xsl")
170
                File teifile = new File("/home/mdecorde/xml/manual/Manuel de TXM 0.7 FR.odt.xml")
171
                new DocumentToTei().run(odtfile, xslfile, teifile)
172
        }
173
}