Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / importer / xmltxm / xmltxmLoader.groovy @ 1000

History | View | Annotate | Download (8.1 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
// $LastChangedDate: 2016-05-11 08:38:47 +0200 (mer. 11 mai 2016) $
23
// $LastChangedRevision: 3211 $
24
// $LastChangedBy: mdecorde $
25
//
26
package org.txm.scripts.importer.xmltxm
27

    
28
import javax.xml.stream.*;
29
import java.io.BufferedWriter;
30
import java.io.File;
31
import java.io.FileOutputStream;
32
import java.io.OutputStreamWriter;
33
import java.io.PrintStream;
34
import java.net.URL;
35
import java.text.DateFormat;
36
import java.text.SimpleDateFormat;
37

    
38
import java.io.File;
39
import org.txm.scripts.importer.*;
40
import org.txm.scripts.importer.xmltxm.compiler;
41
import org.txm.scripts.importer.xml.pager;
42
import org.txm.objects.*;
43
import org.txm.utils.*
44
import org.txm.utils.io.*;
45
import org.txm.*;
46
import org.txm.core.engines.*;
47

    
48
import org.txm.importer.scripts.xmltxm.*;
49
import org.txm.metadatas.*;
50
import org.txm.utils.i18n.*;
51
import org.txm.utils.xml.*
52
import org.w3c.dom.Element;
53
import org.txm.importer.*
54

    
55
String userDir = System.getProperty("user.home");
56
boolean debug = false
57
def MONITOR;
58
BaseParameters params;
59
try {params = paramsBinding;MONITOR=monitor} catch (Exception)
60
{        println "DEV MODE";//exception means we debug
61
        debug = true
62
        params = new BaseParameters(new File(userDir, "xml/anapovoas/FOLLOWING TEST OF MATTHIEU/OUTPUT 2/import.xml"))
63
        params.load()
64
        if (!org.txm.Toolbox.isInitialized()) {
65
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("/usr/lib/TXM"));
66
                Toolbox.setParam(Toolbox.METADATA_ENCODING, "UTF-8");
67
                Toolbox.setParam(Toolbox.METADATA_COLSEPARATOR, ",");
68
                Toolbox.setParam(Toolbox.METADATA_TXTSEPARATOR, "\"");
69
                Toolbox.setParam(Toolbox.USER_TXM_HOME, new File(System.getProperty("user.home"), "TXM"));
70
        }
71
}
72
if (params == null) { println "no parameters. Aborting"; return; }
73

    
74
String corpusname = params.getCorpusName();
75
Element corpusElem = params.corpora.get(corpusname);
76
String basename = params.name;
77
String rootDir = params.rootDir;
78
String lang = corpusElem.getAttribute("lang");
79
String model = lang
80
String encoding = corpusElem.getAttribute("encoding");
81
boolean annotate = "true" == corpusElem.getAttribute("annotate");
82
String xsl = params.getXsltElement(corpusElem).getAttribute("xsl")
83
def xslParams = params.getXsltParams(corpusElem);
84
int wordsPerPage = params.getWordsPerPage("default")
85
String page_element = params.getPageElement("default")
86
boolean build_edition = params.getDoEdition("default")
87

    
88
File srcDir = new File(rootDir);
89
File binDir = new File(Toolbox.getTxmHomePath(),"corpora/"+basename);
90
binDir.deleteDir();
91
binDir.mkdirs();
92
if (!binDir.exists()) {
93
        println "Could not create binDir "+binDir
94
        return;
95
}
96

    
97
File txmDir = new File(binDir,"txm/$corpusname");
98
txmDir.deleteDir();
99
txmDir.mkdirs();
100

    
101
File allMetadataFile = Metadatas.findMetadataFile(srcDir);
102

    
103
// Apply XSL
104
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
105
if (MONITOR != null) MONITOR.worked(1, "APPLYING XSL")
106
if (xsl != null && xslParams != null && xsl.trim().length() > 0) {
107
        if (ApplyXsl2.processImportSources(new File(xsl), srcDir, new File(binDir, "src"), xslParams))
108
        // return; // error during process
109
        srcDir = new File(binDir, "src");
110
        println ""
111
}
112

    
113
// copy txm files
114
println "Copying XML-TXM files..."
115
List<File> srcfiles = srcDir.listFiles();
116
for (File f : srcfiles) {// check XML format, and copy file into binDir
117
        if (f.isHidden() || f.getName().equals("import.xml") || f.getName().matches("metadata\\.....?") || f.getName().endsWith(".properties"))
118
                continue;
119
        if (ValidateXml.test(f)) {
120
                FileCopy.copy(f, new File(txmDir, f.getName()));
121
        } else {
122
                println "Won't process file "+f;
123
        }
124
}
125

    
126
def files = txmDir.listFiles();
127
if (files == null || files.size() == 0) {
128
        println "No txm file to process"
129
        return;
130
}
131

    
132
////get metadata values from CSV
133
//Metadatas metadatas; // text metadata
134
//if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
135
//if (allMetadataFile.exists()) {
136
//        println "Trying to read metadata from: "+allMetadataFile
137
//        File copy = new File(binDir, "metadata.csv")
138
//        if (!FileCopy.copy(allMetadataFile, copy)) {
139
//                println "Error: could not create a copy of metadata file "+allMetadataFile.getAbsoluteFile();
140
//                return;
141
//        }
142
//        metadatas = new Metadatas(copy, Toolbox.getMetadataEncoding(), Toolbox.getMetadataColumnSeparator(), Toolbox.getMetadataTextSeparator(), 1)
143
//} else {
144
//        println "no metadata file: "+allMetadataFile
145
//}
146
//
147
//if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
148
//if (MONITOR != null) MONITOR.worked(20, "INJECTING METADATA - from csv file: "+allMetadataFile)
149
//if (metadatas != null) {
150
//        println("Injecting metadata: "+metadatas.getHeadersList()+" in texts of directory "+txmDir)
151
//        for (File infile : files) {
152
//                print "."
153
//                File outfile = File.createTempFile("temp", ".xml", infile.getParentFile());
154
//
155
//                if (!metadatas.injectMetadatasInXml(infile, outfile, "text", null)) {
156
//                        outfile.delete();
157
//                } else {
158
//                        if (!(infile.delete() && outfile.renameTo(infile))) println "Warning can't rename file "+outfile+" to "+infile
159
//                        if (!infile.exists()) {
160
//                                println "Error: could not replace $infile by $outfile"
161
//                                return false;
162
//                        }
163
//                }
164
//        }
165
//        println ""
166
//}
167

    
168
//sorting files per metadata "date" with format
169
def dates = [:]
170
def useDates = false
171
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
172
for (File infile : files) { // get dates
173
        String str = GetAttributeValue.process(infile, "/TEI/text", "date")
174

    
175
        if (str != "N/A") {
176
                dates[infile] = simpleDateFormat.parse(str)
177
                useDates = true
178
        }
179
}
180

    
181
//sort
182
if (useDates) {
183
        files.sort() { f-> dates[f] }
184
} else {
185
        files.sort()
186
}
187

    
188
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
189
if (MONITOR != null) MONITOR.worked(25, "COMPILING")
190
println "-- COMPILING - Building Search Engine indexes"
191
def c = new compiler();
192
if (debug) c.setDebug();
193
c.setLang(lang);
194
if (!c.run(binDir, txmDir, basename, corpusname, Arrays.asList(files))) {
195
        println "import process stopped";
196
        return;
197
}
198

    
199
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
200

    
201
new File(binDir,"HTML/$corpusname").deleteDir();
202
new File(binDir,"HTML/$corpusname").mkdirs();
203
if (build_edition) {
204

    
205
        if (MONITOR != null) MONITOR.worked(20, "EDITION")
206
        println "-- EDITION - Building edition"
207
        
208
        File outdir = new File(binDir,"/HTML/$corpusname/default/");
209
        outdir.mkdirs();
210

    
211
        def second = 0
212

    
213
        println "Paginating text: "
214
        for (File srcfile : files) {
215

    
216
                String txtname = srcfile.getName();
217
                int i = txtname.lastIndexOf(".");
218
                if(i > 0) txtname = txtname.substring(0, i);
219

    
220
                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
221
                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
222

    
223
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
224

    
225
                if (second) { print(", ") }
226
                if (second > 0 && (second++ % 5) == 0) println ""
227
                print(".");
228

    
229
                def ed = new pager(srcfile, outdir, txtname, NoSpaceBefore, NoSpaceAfter, wordsPerPage, basename, page_element);
230
                Element edition = params.addEdition(text, "default", outdir.getAbsolutePath(), "html");
231

    
232
                for (i = 0 ; i < ed.getPageFiles().size();) {
233
                        File f = ed.getPageFiles().get(i);
234
                        String wordid = ed.getIdx().get(i);
235
                        params.addPage(edition, ""+(++i), wordid);
236
                }
237
        }
238
}
239
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
240
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
241
File paramFile = new File(binDir, "import.xml");
242
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;