Statistics
| Revision:

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

History | View | Annotate | Download (8 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.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.importer.*;
40
import org.txm.importer.xmltxm.compiler;
41
import org.txm.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
import org.txm.importer.xmltxm.*;
48
import org.txm.metadatas.*;
49
import org.txm.utils.i18n.*;
50
import org.w3c.dom.Element;
51

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

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

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

    
94
File txmDir = new File(binDir,"txm/$corpusname");
95
txmDir.deleteDir();
96
txmDir.mkdirs();
97

    
98
File allmetadatasfile = Metadatas.findMetadataFile(srcDir);
99

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

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

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

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

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

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

    
178
//sort
179
if (useDates) {
180
        files.sort() { f-> dates[f] }
181
} else {
182
        files.sort()
183
}
184

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

    
196
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
197

    
198
new File(binDir,"HTML/$corpusname").deleteDir();
199
new File(binDir,"HTML/$corpusname").mkdirs();
200
if (build_edition) {
201

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

    
208
        def second = 0
209

    
210
        println "Paginating text: "
211
        for (File srcfile : files) {
212

    
213
                String txtname = srcfile.getName();
214
                int i = txtname.lastIndexOf(".");
215
                if(i > 0) txtname = txtname.substring(0, i);
216

    
217
                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
218
                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
219

    
220
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
221

    
222
                if (second) { print(", ") }
223
                if (second > 0 && (second++ % 5) == 0) println ""
224
                print(".");
225

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

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