Statistics
| Revision:

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

History | View | Annotate | Download (8 kB)

1 321 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
2 321 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 321 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
4 321 mdecorde
// Sophia Antipolis, University of Paris 3.
5 321 mdecorde
//
6 321 mdecorde
// The TXM platform is free software: you can redistribute it
7 321 mdecorde
// and/or modify it under the terms of the GNU General Public
8 321 mdecorde
// License as published by the Free Software Foundation,
9 321 mdecorde
// either version 2 of the License, or (at your option) any
10 321 mdecorde
// later version.
11 321 mdecorde
//
12 321 mdecorde
// The TXM platform is distributed in the hope that it will be
13 321 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 321 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 321 mdecorde
// PURPOSE. See the GNU General Public License for more
16 321 mdecorde
// details.
17 321 mdecorde
//
18 321 mdecorde
// You should have received a copy of the GNU General
19 321 mdecorde
// Public License along with the TXM platform. If not, see
20 321 mdecorde
// http://www.gnu.org/licenses.
21 321 mdecorde
//
22 479 mdecorde
// $LastChangedDate: 2016-05-11 08:38:47 +0200 (mer. 11 mai 2016) $
23 321 mdecorde
// $LastChangedRevision: 3211 $
24 321 mdecorde
// $LastChangedBy: mdecorde $
25 321 mdecorde
//
26 321 mdecorde
package org.txm.importer.xmltxm
27 321 mdecorde
28 321 mdecorde
import javax.xml.stream.*;
29 321 mdecorde
import java.io.BufferedWriter;
30 321 mdecorde
import java.io.File;
31 321 mdecorde
import java.io.FileOutputStream;
32 321 mdecorde
import java.io.OutputStreamWriter;
33 321 mdecorde
import java.io.PrintStream;
34 321 mdecorde
import java.net.URL;
35 321 mdecorde
import java.text.DateFormat;
36 321 mdecorde
import java.text.SimpleDateFormat;
37 321 mdecorde
38 479 mdecorde
import java.io.File;
39 321 mdecorde
import org.txm.importer.*;
40 321 mdecorde
import org.txm.importer.xmltxm.compiler;
41 321 mdecorde
import org.txm.importer.xml.pager;
42 321 mdecorde
import org.txm.objects.*;
43 927 mdecorde
import org.txm.utils.*
44 928 mdecorde
import org.txm.utils.io.*;
45 321 mdecorde
import org.txm.*;
46 927 mdecorde
import org.txm.core.engines.*;
47 927 mdecorde
import org.txm.importer.xmltxm.*;
48 321 mdecorde
import org.txm.metadatas.*;
49 321 mdecorde
import org.txm.utils.i18n.*;
50 321 mdecorde
import org.w3c.dom.Element;
51 321 mdecorde
52 321 mdecorde
String userDir = System.getProperty("user.home");
53 321 mdecorde
boolean debug = false
54 321 mdecorde
def MONITOR;
55 321 mdecorde
BaseParameters params;
56 321 mdecorde
try {params = paramsBinding;MONITOR=monitor} catch (Exception)
57 321 mdecorde
{        println "DEV MODE";//exception means we debug
58 321 mdecorde
        debug = true
59 321 mdecorde
        params = new BaseParameters(new File(userDir, "xml/anapovoas/FOLLOWING TEST OF MATTHIEU/OUTPUT 2/import.xml"))
60 321 mdecorde
        params.load()
61 321 mdecorde
        if (!org.txm.Toolbox.isInitialized()) {
62 321 mdecorde
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("/usr/lib/TXM"));
63 321 mdecorde
                Toolbox.setParam(Toolbox.METADATA_ENCODING, "UTF-8");
64 321 mdecorde
                Toolbox.setParam(Toolbox.METADATA_COLSEPARATOR, ",");
65 321 mdecorde
                Toolbox.setParam(Toolbox.METADATA_TXTSEPARATOR, "\"");
66 321 mdecorde
                Toolbox.setParam(Toolbox.USER_TXM_HOME, new File(System.getProperty("user.home"), "TXM"));
67 321 mdecorde
        }
68 321 mdecorde
}
69 321 mdecorde
if (params == null) { println "no parameters. Aborting"; return; }
70 321 mdecorde
71 321 mdecorde
String corpusname = params.getCorpusName();
72 321 mdecorde
Element corpusElem = params.corpora.get(corpusname);
73 321 mdecorde
String basename = params.name;
74 321 mdecorde
String rootDir = params.rootDir;
75 321 mdecorde
String lang = corpusElem.getAttribute("lang");
76 321 mdecorde
String model = lang
77 321 mdecorde
String encoding = corpusElem.getAttribute("encoding");
78 321 mdecorde
boolean annotate = "true" == corpusElem.getAttribute("annotate");
79 321 mdecorde
String xsl = params.getXsltElement(corpusElem).getAttribute("xsl")
80 321 mdecorde
def xslParams = params.getXsltParams(corpusElem);
81 321 mdecorde
int wordsPerPage = params.getWordsPerPage("default")
82 321 mdecorde
String page_element = params.getPageElement("default")
83 321 mdecorde
boolean build_edition = params.getDoEdition("default")
84 321 mdecorde
85 321 mdecorde
File srcDir = new File(rootDir);
86 878 sjacqu01
File binDir = new File(Toolbox.getTxmHomePath(),"corpora/"+basename);
87 321 mdecorde
binDir.deleteDir();
88 321 mdecorde
binDir.mkdirs();
89 321 mdecorde
if (!binDir.exists()) {
90 321 mdecorde
        println "Could not create binDir "+binDir
91 321 mdecorde
        return;
92 321 mdecorde
}
93 321 mdecorde
94 321 mdecorde
File txmDir = new File(binDir,"txm/$corpusname");
95 321 mdecorde
txmDir.deleteDir();
96 321 mdecorde
txmDir.mkdirs();
97 321 mdecorde
98 965 mdecorde
File allmetadatasfile = Metadatas.findMetadataFile(srcDir);
99 321 mdecorde
100 321 mdecorde
// Apply XSL
101 321 mdecorde
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
102 321 mdecorde
if (MONITOR != null) MONITOR.worked(1, "APPLYING XSL")
103 321 mdecorde
if (xsl != null && xslParams != null && xsl.trim().length() > 0) {
104 321 mdecorde
        if (ApplyXsl2.processImportSources(new File(xsl), srcDir, new File(binDir, "src"), xslParams))
105 321 mdecorde
        // return; // error during process
106 321 mdecorde
        srcDir = new File(binDir, "src");
107 321 mdecorde
        println ""
108 321 mdecorde
}
109 321 mdecorde
110 321 mdecorde
// copy txm files
111 321 mdecorde
println "Copying XML-TXM files..."
112 321 mdecorde
List<File> srcfiles = srcDir.listFiles();
113 321 mdecorde
for (File f : srcfiles) {// check XML format, and copy file into binDir
114 966 mdecorde
        if (f.isHidden() || f.getName().equals("import.xml") || f.getName().matches("metadata\.....?") || f.getName().endsWith(".properties"))
115 321 mdecorde
                continue;
116 321 mdecorde
        if (ValidateXml.test(f)) {
117 321 mdecorde
                FileCopy.copy(f, new File(txmDir, f.getName()));
118 321 mdecorde
        } else {
119 321 mdecorde
                println "Won't process file "+f;
120 321 mdecorde
        }
121 321 mdecorde
}
122 321 mdecorde
123 321 mdecorde
def files = txmDir.listFiles();
124 321 mdecorde
if (files == null || files.size() == 0) {
125 321 mdecorde
        println "No txm file to process"
126 321 mdecorde
        return;
127 321 mdecorde
}
128 321 mdecorde
129 321 mdecorde
////get metadata values from CSV
130 321 mdecorde
//Metadatas metadatas; // text metadata
131 321 mdecorde
//if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
132 321 mdecorde
//if (allmetadatasfile.exists()) {
133 321 mdecorde
//        println "Trying to read metadata from: "+allmetadatasfile
134 321 mdecorde
//        File copy = new File(binDir, "metadata.csv")
135 321 mdecorde
//        if (!FileCopy.copy(allmetadatasfile, copy)) {
136 321 mdecorde
//                println "Error: could not create a copy of metadata file "+allmetadatasfile.getAbsoluteFile();
137 321 mdecorde
//                return;
138 321 mdecorde
//        }
139 788 mdecorde
//        metadatas = new Metadatas(copy, Toolbox.getMetadataEncoding(), Toolbox.getMetadataColumnSeparator(), Toolbox.getMetadataTextSeparator(), 1)
140 321 mdecorde
//} else {
141 321 mdecorde
//        println "no metadata file: "+allmetadatasfile
142 321 mdecorde
//}
143 321 mdecorde
//
144 321 mdecorde
//if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
145 321 mdecorde
//if (MONITOR != null) MONITOR.worked(20, "INJECTING METADATA - from csv file: "+allmetadatasfile)
146 321 mdecorde
//if (metadatas != null) {
147 321 mdecorde
//        println("Injecting metadata: "+metadatas.getHeadersList()+" in texts of directory "+txmDir)
148 321 mdecorde
//        for (File infile : files) {
149 321 mdecorde
//                print "."
150 321 mdecorde
//                File outfile = File.createTempFile("temp", ".xml", infile.getParentFile());
151 321 mdecorde
//
152 321 mdecorde
//                if (!metadatas.injectMetadatasInXml(infile, outfile, "text", null)) {
153 321 mdecorde
//                        outfile.delete();
154 321 mdecorde
//                } else {
155 321 mdecorde
//                        if (!(infile.delete() && outfile.renameTo(infile))) println "Warning can't rename file "+outfile+" to "+infile
156 321 mdecorde
//                        if (!infile.exists()) {
157 321 mdecorde
//                                println "Error: could not replace $infile by $outfile"
158 321 mdecorde
//                                return false;
159 321 mdecorde
//                        }
160 321 mdecorde
//                }
161 321 mdecorde
//        }
162 321 mdecorde
//        println ""
163 321 mdecorde
//}
164 321 mdecorde
165 321 mdecorde
//sorting files per metadata "date" with format
166 321 mdecorde
def dates = [:]
167 321 mdecorde
def useDates = false
168 321 mdecorde
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
169 321 mdecorde
for (File infile : files) { // get dates
170 321 mdecorde
        String str = GetAttributeValue.process(infile, "/TEI/text", "date")
171 321 mdecorde
172 321 mdecorde
        if (str != "N/A") {
173 321 mdecorde
                dates[infile] = simpleDateFormat.parse(str)
174 321 mdecorde
                useDates = true
175 321 mdecorde
        }
176 321 mdecorde
}
177 321 mdecorde
178 321 mdecorde
//sort
179 321 mdecorde
if (useDates) {
180 321 mdecorde
        files.sort() { f-> dates[f] }
181 321 mdecorde
} else {
182 321 mdecorde
        files.sort()
183 321 mdecorde
}
184 321 mdecorde
185 321 mdecorde
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
186 321 mdecorde
if (MONITOR != null) MONITOR.worked(25, "COMPILING")
187 321 mdecorde
println "-- COMPILING - Building Search Engine indexes"
188 321 mdecorde
def c = new compiler();
189 321 mdecorde
if (debug) c.setDebug();
190 321 mdecorde
c.setLang(lang);
191 321 mdecorde
if (!c.run(binDir, txmDir, basename, corpusname, Arrays.asList(files))) {
192 321 mdecorde
        println "import process stopped";
193 321 mdecorde
        return;
194 321 mdecorde
}
195 321 mdecorde
196 321 mdecorde
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
197 321 mdecorde
198 321 mdecorde
new File(binDir,"HTML/$corpusname").deleteDir();
199 321 mdecorde
new File(binDir,"HTML/$corpusname").mkdirs();
200 321 mdecorde
if (build_edition) {
201 321 mdecorde
202 321 mdecorde
        if (MONITOR != null) MONITOR.worked(20, "EDITION")
203 321 mdecorde
        println "-- EDITION - Building edition"
204 321 mdecorde
205 321 mdecorde
        File outdir = new File(binDir,"/HTML/$corpusname/default/");
206 321 mdecorde
        outdir.mkdirs();
207 321 mdecorde
208 321 mdecorde
        def second = 0
209 321 mdecorde
210 321 mdecorde
        println "Paginating text: "
211 321 mdecorde
        for (File srcfile : files) {
212 321 mdecorde
213 321 mdecorde
                String txtname = srcfile.getName();
214 321 mdecorde
                int i = txtname.lastIndexOf(".");
215 321 mdecorde
                if(i > 0) txtname = txtname.substring(0, i);
216 321 mdecorde
217 321 mdecorde
                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
218 321 mdecorde
                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
219 321 mdecorde
220 321 mdecorde
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
221 321 mdecorde
222 321 mdecorde
                if (second) { print(", ") }
223 321 mdecorde
                if (second > 0 && (second++ % 5) == 0) println ""
224 321 mdecorde
                print(".");
225 321 mdecorde
226 321 mdecorde
                def ed = new pager(srcfile, outdir, txtname, NoSpaceBefore, NoSpaceAfter, wordsPerPage, basename, page_element);
227 321 mdecorde
                Element edition = params.addEdition(text, "default", outdir.getAbsolutePath(), "html");
228 321 mdecorde
229 321 mdecorde
                for (i = 0 ; i < ed.getPageFiles().size();) {
230 321 mdecorde
                        File f = ed.getPageFiles().get(i);
231 321 mdecorde
                        String wordid = ed.getIdx().get(i);
232 321 mdecorde
                        params.addPage(edition, ""+(++i), wordid);
233 321 mdecorde
                }
234 321 mdecorde
        }
235 321 mdecorde
}
236 321 mdecorde
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
237 321 mdecorde
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
238 321 mdecorde
File paramFile = new File(binDir, "import.xml");
239 927 mdecorde
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;