Statistics
| Revision:

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

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 (Mon, 14 Mar 2016) $
25
// $LastChangedRevision: 3165 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.doc
29

    
30
import java.io.File;
31

    
32
import org.txm.importer.ApplyXsl2;
33
import org.txm.importer.ConvertDocument;
34
import org.txm.utils.zip.Zip;
35

    
36
/**
37
 * The Class DocumentToTei.
38
 */
39
class DocumentToTei {
40

    
41
        static boolean DEBUG = true;
42

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

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

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

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

    
106
                return ret;
107
        }
108

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

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

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

    
135
                //unzipdir.deleteDir()
136
                return true;
137
        }
138

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

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

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

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