Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / importer / xtz / XTZPager.groovy @ 1688

History | View | Annotate | Download (15.4 kB)

1 986 mdecorde
package org.txm.scripts.importer.xtz
2 321 mdecorde
3 321 mdecorde
import java.io.File;
4 321 mdecorde
import java.util.ArrayList;
5 321 mdecorde
6 321 mdecorde
import org.txm.objects.BaseParameters
7 321 mdecorde
import org.w3c.dom.Element
8 1217 mdecorde
9 986 mdecorde
import org.txm.scripts.importer.*
10 1613 mdecorde
import org.txm.utils.ConsoleProgressBar
11 321 mdecorde
import org.txm.utils.BundleUtils;
12 479 mdecorde
import org.txm.utils.io.FileCopy;
13 321 mdecorde
import org.txm.utils.i18n.*
14 1000 mdecorde
import org.txm.importer.xtz.*
15 321 mdecorde
import javax.xml.stream.*
16 1137 mdecorde
import org.txm.objects.*
17 1196 mdecorde
import org.txm.importer.ApplyXsl2
18 321 mdecorde
19 321 mdecorde
class XTZPager extends Pager {
20 321 mdecorde
21 1137 mdecorde
        Project project;
22 321 mdecorde
23 321 mdecorde
        Element corpusElem;
24 321 mdecorde
        String lang;
25 321 mdecorde
        String page_element;
26 321 mdecorde
        String wordTag;
27 321 mdecorde
        int wordsPerPage;
28 321 mdecorde
29 321 mdecorde
        File cssDirectory, jsDirectory, imagesDirectory;
30 321 mdecorde
31 321 mdecorde
        public XTZPager(ImportModule module) {
32 321 mdecorde
                super(module, "default");
33 321 mdecorde
34 1137 mdecorde
                project = module.getProject()
35 321 mdecorde
36 1137 mdecorde
                lang = project.getLang();
37 1137 mdecorde
                wordsPerPage = project.getEditionDefinition("default").getWordsPerPage()
38 1137 mdecorde
                page_element = project.getEditionDefinition("default").getPageElement()
39 1137 mdecorde
                wordTag = project.getTokenizerWordElement()
40 321 mdecorde
41 321 mdecorde
                cssDirectory = new File(module.getSourceDirectory(), "css")
42 321 mdecorde
                jsDirectory = new File(module.getSourceDirectory(), "js")
43 321 mdecorde
                imagesDirectory = new File(module.getSourceDirectory(), "images")
44 321 mdecorde
        }
45 321 mdecorde
46 321 mdecorde
        public void process(ArrayList<File> files) {
47 321 mdecorde
                super.process(files);
48 1063 mdecorde
49 321 mdecorde
                if (files == null) {
50 321 mdecorde
                        files = inputDirectory.listFiles();
51 321 mdecorde
                        if (files != null) Collections.sort(files);
52 321 mdecorde
                }
53 1063 mdecorde
54 321 mdecorde
                if (!doDefaultEditionStep()) return;
55 321 mdecorde
                if (!doFacsEditionStep()) return;
56 321 mdecorde
                if (!doPostEditionXSLStep()) return;
57 321 mdecorde
58 321 mdecorde
                isSuccessFul = true;
59 321 mdecorde
                println ""
60 321 mdecorde
        }
61 321 mdecorde
62 321 mdecorde
        public boolean doDefaultEditionStep() {
63 1063 mdecorde
64 1137 mdecorde
                boolean build_edition = project.getEditionDefinition("default").getBuildEdition()
65 321 mdecorde
                if (!build_edition) {
66 321 mdecorde
                        return true;
67 321 mdecorde
                }
68 1063 mdecorde
69 321 mdecorde
                def second = 0
70 321 mdecorde
71 321 mdecorde
                println "-- Building 'default' edition of  ${files.size()} texts..."
72 321 mdecorde
73 321 mdecorde
                def css = ["css/txm.css", "css/${corpusname}.css"] // default CSS inclusion
74 321 mdecorde
75 321 mdecorde
                // scan existing css files that must be declared in each HTML page
76 321 mdecorde
                if (cssDirectory.exists()) {
77 321 mdecorde
                        def cssFiles = cssDirectory.listFiles();
78 321 mdecorde
                        if (cssFiles != null)
79 321 mdecorde
                                for (File cssFile : cssFiles) {
80 321 mdecorde
                                        if (cssFile.isFile() && !cssFile.isHidden() && cssFile.getName().endsWith(".css"))
81 321 mdecorde
                                                css << "css/"+cssFile.getName();
82 321 mdecorde
                                }
83 321 mdecorde
                }
84 1063 mdecorde
85 321 mdecorde
                ConsoleProgressBar cpb = new ConsoleProgressBar(files.size())
86 321 mdecorde
                for (File txmFile : files) {
87 1063 mdecorde
                        try {
88 1063 mdecorde
                                cpb.tick()
89 1063 mdecorde
                                String textname = txmFile.getName();
90 1063 mdecorde
                                int i = textname.lastIndexOf(".");
91 1063 mdecorde
                                if (i > 0) textname = textname.substring(0, i);
92 321 mdecorde
93 1063 mdecorde
                                File firstHTMLPageFile = new File(outputDirectory, textname+"_1.html");
94 1063 mdecorde
                                if (firstHTMLPageFile.exists() && firstHTMLPageFile.lastModified() >= txmFile.lastModified()) continue;
95 321 mdecorde
96 1063 mdecorde
                                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
97 1063 mdecorde
                                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
98 321 mdecorde
99 1137 mdecorde
                                Text t = project.getText(textname);
100 1137 mdecorde
                                if (t == null) {
101 1137 mdecorde
                                        t = new Text(project);
102 321 mdecorde
                                }
103 1137 mdecorde
                                t.setName(textname);
104 1137 mdecorde
                                t.setSourceFile(txmFile)
105 1137 mdecorde
                                t.setTXMFile(txmFile)
106 1137 mdecorde
107 1063 mdecorde
                                def ed = new XTZDefaultPagerStep(this, txmFile, textname, NoSpaceBefore, NoSpaceAfter, css);
108 1063 mdecorde
                                if (!ed.process()) {
109 1063 mdecorde
                                        println "Fail to build 'default' edition for text: $txmFile"
110 1063 mdecorde
                                        continue;
111 1063 mdecorde
                                }
112 1137 mdecorde
                                Edition edition = t.getEdition("default")
113 1063 mdecorde
                                if (edition == null) {
114 1137 mdecorde
                                        edition = new Edition(t);
115 1063 mdecorde
                                }
116 1137 mdecorde
                                edition.setName("default");
117 1137 mdecorde
                                edition.setIndex(outputDirectory.getAbsolutePath());
118 321 mdecorde
119 1063 mdecorde
                                for (i = 0 ; i < ed.getPageFiles().size();) {
120 1063 mdecorde
                                        File f = ed.getPageFiles().get(i);
121 1193 mdecorde
                                        String wordid = "w_0";
122 1193 mdecorde
                                if (i < ed.getIdx().size()) wordid = ed.getIdx().get(i);
123 1137 mdecorde
                                        edition.addPage(""+(++i), wordid);
124 1063 mdecorde
                                }
125 1063 mdecorde
                        } catch(Exception e) {
126 1063 mdecorde
                                println "Error: could not create $txmFile 'default' edition: "+e
127 1619 mdecorde
                                e.printStackTrace()
128 321 mdecorde
                        }
129 321 mdecorde
                }
130 1063 mdecorde
131 321 mdecorde
                // copy default TXM css file in the "facs" edition directory
132 321 mdecorde
                File csshtmlDirectory = new File(outputDirectory, "css")
133 321 mdecorde
                csshtmlDirectory.mkdirs()
134 812 mdecorde
                BundleUtils.copyFiles("org.txm.core", "res", "org/txm/css", "txm.css", csshtmlDirectory);
135 1063 mdecorde
136 321 mdecorde
                // copy CSS files in the "default" edition directory
137 321 mdecorde
                if (cssDirectory.exists()) {
138 321 mdecorde
                        FileCopy.copyFiles(cssDirectory, csshtmlDirectory)
139 321 mdecorde
                }
140 321 mdecorde
                if (jsDirectory.exists()) {
141 321 mdecorde
                        File jshtmlDirectory = new File(outputDirectory, "js")
142 321 mdecorde
                        FileCopy.copyFiles(jsDirectory, jshtmlDirectory)
143 321 mdecorde
                }
144 321 mdecorde
                if (imagesDirectory.exists()) {
145 321 mdecorde
                        File imageshtmlDirectory = new File(outputDirectory, "images")
146 321 mdecorde
                        FileCopy.copyFiles(imagesDirectory, imageshtmlDirectory)
147 321 mdecorde
                }
148 321 mdecorde
149 321 mdecorde
                // save changes
150 321 mdecorde
                println ""
151 321 mdecorde
                return true;
152 321 mdecorde
        }
153 321 mdecorde
154 321 mdecorde
        public boolean doFacsEditionStep() {
155 321 mdecorde
156 1137 mdecorde
                boolean mustBuildFacsEdition = project.getEditionDefinition("facs").getBuildEdition()
157 321 mdecorde
                if (!mustBuildFacsEdition) return true;
158 1063 mdecorde
159 1137 mdecorde
                String imageDirectoryPath = project.getEditionDefinition("facs").getImagesDirectory().trim();
160 321 mdecorde
                File imageDirectory = new File(imageDirectoryPath);
161 321 mdecorde
                if (!imageDirectoryPath.startsWith("http") && imageDirectoryPath.length()== 0 && !imageDirectory.exists() && !imageDirectory.isDirectory()) {
162 321 mdecorde
                        imageDirectory = null;
163 321 mdecorde
                }
164 321 mdecorde
165 321 mdecorde
                def second = 0
166 321 mdecorde
167 321 mdecorde
                println "-- Building 'facs' edition of ${files.size()} texts..."
168 321 mdecorde
                File newEditionDirectory = new File(htmlDirectory, "facs");
169 321 mdecorde
                newEditionDirectory.mkdir();
170 321 mdecorde
171 321 mdecorde
                ConsoleProgressBar cpb = new ConsoleProgressBar(files.size())
172 321 mdecorde
                for (File txmFile : files) {
173 321 mdecorde
                        cpb.tick()
174 321 mdecorde
                        String txtname = txmFile.getName();
175 321 mdecorde
                        int i = txtname.lastIndexOf(".");
176 321 mdecorde
                        if (i > 0) txtname = txtname.substring(0, i);
177 321 mdecorde
178 321 mdecorde
                        File firstHTMLPageFile = new File(newEditionDirectory, txtname+"_1.html");
179 321 mdecorde
                        if (firstHTMLPageFile.exists() && firstHTMLPageFile.lastModified() >= txmFile.lastModified()) continue;
180 321 mdecorde
181 321 mdecorde
                        List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
182 321 mdecorde
                        List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
183 321 mdecorde
184 1137 mdecorde
                        Text t = project.getText(txtname);
185 1137 mdecorde
                        if (t == null) {
186 1137 mdecorde
                                t = new Text(project);
187 321 mdecorde
                        }
188 1137 mdecorde
                        t.setName(txtname);
189 1137 mdecorde
                        t.setSourceFile(txmFile)
190 1137 mdecorde
                        t.setTXMFile(txmFile)
191 1137 mdecorde
192 1137 mdecorde
                        Edition edition = t.getEdition("facs")
193 321 mdecorde
                        if (edition == null) {
194 1137 mdecorde
                                edition = new Edition(t);
195 321 mdecorde
                        }
196 1137 mdecorde
                        edition.setName("facs");
197 1137 mdecorde
                        edition.setIndex(outputDirectory.getAbsolutePath());
198 321 mdecorde
199 321 mdecorde
                        try {
200 321 mdecorde
                                def ed = new XTZFacsPagerStep(txmFile, newEditionDirectory, imageDirectory, txtname, corpusname, "pb", "facs", wordTag, debug);
201 321 mdecorde
                                if (!ed.process()) {
202 1063 mdecorde
                                        println "Fail to build 'facs' edition for text: $txmFile"
203 321 mdecorde
                                        continue;
204 321 mdecorde
                                }
205 321 mdecorde
206 1063 mdecorde
207 321 mdecorde
                                def pages = ed.getPageFiles()
208 321 mdecorde
                                for (i = 0 ; i < pages.size();) {
209 321 mdecorde
                                        File f = pages[i][0];
210 321 mdecorde
                                        String wordid = pages[i][1]
211 698 mdecorde
                                        //TODO replace '""+(++i)' with something that fetch/findout the page 'name'
212 1063 mdecorde
                                        // TODO or move the Edition and Page corpus declaration in the XTZDefaultPagerStep
213 1137 mdecorde
                                        edition.addPage(""+(++i), wordid);
214 321 mdecorde
                                        //println "add facs page: $f $wordid"
215 321 mdecorde
                                }
216 321 mdecorde
                        } catch (Exception e) {
217 321 mdecorde
                                println "Error while processing $txmFile text: "+e
218 321 mdecorde
                                e.printStackTrace();
219 321 mdecorde
                                return false;
220 321 mdecorde
                        }
221 321 mdecorde
                }
222 321 mdecorde
223 321 mdecorde
                if (!imageDirectoryPath.startsWith("http") && imageDirectory != null) { // copy files only if local
224 321 mdecorde
                        File editionImagesDirectory = new File(newEditionDirectory, "res/images/"+corpusname+"/facs");
225 321 mdecorde
                        editionImagesDirectory.mkdirs();
226 321 mdecorde
                        FileCopy.copyFiles(imageDirectory, editionImagesDirectory);
227 321 mdecorde
                }
228 1063 mdecorde
229 321 mdecorde
                // copy SimpleViewer files in the "facs" edition directory
230 321 mdecorde
                File jshtmlDirectory = new File(newEditionDirectory, "js")
231 321 mdecorde
                jshtmlDirectory.mkdirs()
232 812 mdecorde
                BundleUtils.copyFiles("org.txm.core", "res", "org/txm/js", "viewer", jshtmlDirectory);
233 812 mdecorde
                BundleUtils.copyFiles("org.txm.core", "res", "org/txm/", "images", newEditionDirectory);
234 1063 mdecorde
235 321 mdecorde
                // copy default TXM css file in the "facs" edition directory
236 321 mdecorde
                File csshtmlDirectory = new File(newEditionDirectory, "css")
237 321 mdecorde
                csshtmlDirectory.mkdirs()
238 812 mdecorde
                BundleUtils.copyFiles("org.txm.core", "res", "org/txm/css", "txm.css", csshtmlDirectory);
239 1063 mdecorde
240 321 mdecorde
                // copy CSS/JS/Images sources files in the "facs" edition directory
241 321 mdecorde
                if (cssDirectory.exists()) {
242 321 mdecorde
                        FileCopy.copyFiles(cssDirectory, csshtmlDirectory)
243 321 mdecorde
                }
244 321 mdecorde
                if (jsDirectory.exists()) {
245 321 mdecorde
                        FileCopy.copyFiles(jsDirectory, jshtmlDirectory)
246 321 mdecorde
                }
247 321 mdecorde
                if (imagesDirectory.exists()) {
248 321 mdecorde
                        File imageshtmlDirectory = new File(newEditionDirectory, "images")
249 321 mdecorde
                        FileCopy.copyFiles(imagesDirectory, imageshtmlDirectory)
250 321 mdecorde
                }
251 1063 mdecorde
252 1137 mdecorde
                project.setDefaultEditionName("default,facs");
253 321 mdecorde
254 321 mdecorde
                println ""
255 321 mdecorde
                return true;
256 321 mdecorde
        }
257 321 mdecorde
258 321 mdecorde
        /**
259 321 mdecorde
         * read from $bindir/txm and write the result in $bindir/txm
260 321 mdecorde
         *
261 321 mdecorde
         */
262 321 mdecorde
        public boolean doPostEditionXSLStep() {
263 321 mdecorde
264 321 mdecorde
                File xslDirectory = new File(module.getSourceDirectory(), "xsl/4-edition")
265 321 mdecorde
                if (xslDirectory.exists()) {
266 321 mdecorde
267 321 mdecorde
                        // prepare XSL parameters
268 1137 mdecorde
                        def xslParams = module.getProject().getXsltParameters();
269 1137 mdecorde
                        String s = module.getProject().getEditionDefinition("default").getWordsPerPage()
270 321 mdecorde
                        if (s != null && s.length() > 0)
271 321 mdecorde
272 1217 mdecorde
                        // shared XSL parameters
273 1217 mdecorde
                        xslParams["number-words-per-page"] = Integer.parseInt(s);
274 1137 mdecorde
                        xslParams["pagination-element"] = module.getProject().getEditionDefinition("default").getPageElement()
275 1137 mdecorde
                        xslParams["import-xml-path"] = module.getProject().getProjectDirectory()
276 321 mdecorde
                        //println "XSL PARAMS: "+xslParams
277 321 mdecorde
278 321 mdecorde
                        def xslFiles = xslDirectory.listFiles()
279 1217 mdecorde
                        xslFiles = xslFiles.sort() { f ->
280 1217 mdecorde
                                try {
281 1217 mdecorde
                                return Integer.parseInt(f.getName().substring(0, f.getName().indexOf("-")))
282 1217 mdecorde
                                } catch(Exception e) {}
283 1217 mdecorde
                                return -1;
284 1217 mdecorde
                        }
285 321 mdecorde
                        def editionsCreated = [:]
286 321 mdecorde
                        for (File xslFile : xslFiles) {
287 321 mdecorde
                                if (xslFile.isDirectory() || xslFile.isHidden() || !xslFile.getName().endsWith(".xsl")) continue;
288 1466 mdecorde
                                if (!xslFile.getName().matches("[1-9]{1,3}-.+")) continue;
289 1063 mdecorde
290 321 mdecorde
                                String xslName = xslFile.getName().substring(2); // remove the "1-", "2-", etc.
291 321 mdecorde
                                int idx2 = xslName.indexOf(".")
292 321 mdecorde
                                if (idx2 > 0) xslName = xslName.substring(0, idx2)
293 321 mdecorde
                                else {
294 321 mdecorde
                                        println "$xslFile is not a '.xsl' file"
295 321 mdecorde
                                        continue;
296 321 mdecorde
                                }
297 321 mdecorde
                                int idx3 = xslName.indexOf("-")
298 321 mdecorde
                                if (idx3 < 0) {
299 321 mdecorde
                                        println "$xslFile file does not follow the '{Number}-{editionName}-{step}.xsl' name pattern"
300 321 mdecorde
                                        continue;
301 321 mdecorde
                                }
302 321 mdecorde
                                String pagerStep = xslName.substring(idx3 + 1);
303 321 mdecorde
                                String editionName = xslName.substring(0, idx3);
304 321 mdecorde
305 321 mdecorde
                                int idx = editionName.indexOf(".")
306 321 mdecorde
                                if (idx > 0) editionName = editionName.substring(0, idx);
307 321 mdecorde
                                println "-- Building '$editionName' XSL edition with step '$pagerStep'..."
308 321 mdecorde
309 321 mdecorde
                                File newEditionDirectory = new File(htmlDirectory, editionName);
310 321 mdecorde
                                xslParams["output-directory"] = newEditionDirectory.toURI().toString()
311 321 mdecorde
312 321 mdecorde
                                if (editionsCreated[editionName] == null) { // first XSL, replace an edition
313 321 mdecorde
                                        editionsCreated[editionName] = xslFile
314 321 mdecorde
                                        //if (!importModule.isUpdatingCorpus()) {
315 321 mdecorde
                                        //TODO: optimisation if update is enable,
316 321 mdecorde
                                        newEditionDirectory.deleteDir(); // delete previous edition if any
317 321 mdecorde
                                        //}
318 321 mdecorde
                                        newEditionDirectory.mkdir()
319 321 mdecorde
320 321 mdecorde
                                        boolean deleteOutputFiles = "pager" == pagerStep;
321 321 mdecorde
                                        if (ApplyXsl2.processImportSources(xslFile, inputDirectory, newEditionDirectory, xslParams, deleteOutputFiles)) {
322 321 mdecorde
                                                println ""
323 321 mdecorde
                                        } else {
324 321 mdecorde
                                                reason = "Fail to apply edition XSL: $xslFile"
325 321 mdecorde
                                                return false;
326 321 mdecorde
                                        }
327 321 mdecorde
328 321 mdecorde
                                        // copy CSS files in the newEditionDirector edition directory
329 321 mdecorde
                                        if (cssDirectory.exists()) {
330 321 mdecorde
                                                File csshtmlDirectory = new File(newEditionDirectory, "css")
331 321 mdecorde
                                                FileCopy.copyFiles(cssDirectory, csshtmlDirectory)
332 321 mdecorde
                                        }
333 321 mdecorde
                                        if (jsDirectory.exists()) {
334 321 mdecorde
                                                File jshtmlDirectory = new File(newEditionDirectory, "js")
335 321 mdecorde
                                                FileCopy.copyFiles(jsDirectory, jshtmlDirectory)
336 321 mdecorde
                                        }
337 321 mdecorde
                                        if (imagesDirectory.exists()) {
338 321 mdecorde
                                                File imageshtmlDirectory = new File(newEditionDirectory, "images")
339 321 mdecorde
                                                FileCopy.copyFiles(imagesDirectory, imageshtmlDirectory)
340 321 mdecorde
                                        }
341 321 mdecorde
                                } else { // N+1 XSL working with HTML files
342 321 mdecorde
                                        def htmlFiles = newEditionDirectory.listFiles()
343 321 mdecorde
                                        htmlFiles.sort()
344 321 mdecorde
345 321 mdecorde
                                        if (ApplyXsl2.processImportSources(xslFile, htmlFiles, xslParams)) {
346 321 mdecorde
                                                if ("pager".equals(pagerStep)) {
347 321 mdecorde
                                                        // delete the one page HTML files only if the XSL step is "pager"
348 321 mdecorde
                                                        for (File f : htmlFiles) f.delete();
349 321 mdecorde
                                                }
350 321 mdecorde
                                                //        println ""
351 321 mdecorde
                                        } else {
352 321 mdecorde
                                                reason = "Fail to apply edition XSL: $xslFile"
353 321 mdecorde
                                                return false;
354 321 mdecorde
                                        }
355 321 mdecorde
                                }
356 321 mdecorde
                        }
357 321 mdecorde
358 321 mdecorde
                        // UPDATE import.xml: for each XML-TXM file, we must retrieve the first word ID from the XSL output files
359 321 mdecorde
                        //println "retrieve word ids from $inputDirectory"
360 321 mdecorde
                        println "-- Fetching page word IDs..."
361 321 mdecorde
                        ConsoleProgressBar cpb = new ConsoleProgressBar(editionsCreated.keySet().size())
362 321 mdecorde
                        for (String editionName : editionsCreated.keySet()) {
363 321 mdecorde
                                cpb.tick()
364 321 mdecorde
365 321 mdecorde
                                File newEditionDirectory = new File(htmlDirectory, editionName);
366 321 mdecorde
                                File xslFile = editionsCreated[editionName]
367 321 mdecorde
                                for (File txmFile : inputDirectory.listFiles()) {
368 321 mdecorde
                                        if (txmFile.isDirectory()) continue;
369 321 mdecorde
                                        String textName = txmFile.getName()
370 321 mdecorde
                                        int idx4 = textName.indexOf(".")
371 321 mdecorde
                                        if (idx4 > 0) textName = textName.substring(0, idx4);
372 321 mdecorde
373 1137 mdecorde
                                        getFirstWordIDs(textName, editionName, newEditionDirectory, xslFile, txmFile);
374 321 mdecorde
                                }
375 321 mdecorde
                        }
376 321 mdecorde
                        println ""
377 321 mdecorde
                }
378 321 mdecorde
                return true;
379 321 mdecorde
        }
380 321 mdecorde
381 1137 mdecorde
        private void getFirstWordIDs(String textName, String editionName, File newEditionDirectory, File xslFile, File txmFile) {
382 321 mdecorde
                //                println "call getFirstWordIDs textName=$textName editionName=$editionName dir=$newEditionDirectory xsl=$xslFile"
383 1137 mdecorde
                Text t = project.getText(textName);
384 1137 mdecorde
                if (t == null) {
385 1137 mdecorde
                        t = new Text(project);
386 321 mdecorde
                }
387 1137 mdecorde
                t.setName(textName);
388 1137 mdecorde
                t.setSourceFile(txmFile)
389 1137 mdecorde
                t.setTXMFile(txmFile)
390 321 mdecorde
391 1137 mdecorde
                Edition edition = t.getEdition(editionName)
392 1137 mdecorde
                if (edition == null) {
393 1137 mdecorde
                        edition = new Edition(t);
394 321 mdecorde
                }
395 1137 mdecorde
                edition.setName(editionName);
396 1137 mdecorde
                edition.setIndex(outputDirectory.getAbsolutePath());
397 321 mdecorde
398 321 mdecorde
                LinkedHashMap<File, String> words = new LinkedHashMap<File, String>()
399 321 mdecorde
                def files = []
400 321 mdecorde
                newEditionDirectory.eachFile() {it -> if (it.isFile()) files << it}
401 321 mdecorde
402 321 mdecorde
                files.sort() { f1, f2 ->
403 321 mdecorde
                        String s1 = f1.getName()
404 321 mdecorde
                        String s2 = f2.getName()
405 321 mdecorde
                        int n1 = Integer.parseInt(s1.substring(s1.lastIndexOf("_")+1, s1.lastIndexOf(".")))
406 321 mdecorde
                        int n2 = Integer.parseInt(s2.substring(s2.lastIndexOf("_")+1, s2.lastIndexOf(".")))
407 321 mdecorde
                        return n1 - n2;
408 321 mdecorde
                }
409 321 mdecorde
410 321 mdecorde
                for (File f : files) {
411 321 mdecorde
                        String pagename = f.getName();
412 321 mdecorde
                        if (pagename.startsWith(textName+"_")) { // this is a page
413 321 mdecorde
                                String firstWordID = getMetaContent(f);
414 321 mdecorde
                                pagename = pagename.substring((textName+"_").length(), pagename.indexOf(".html")) // !!!!
415 1137 mdecorde
                                edition.addPage(pagename, firstWordID)
416 321 mdecorde
                        }
417 321 mdecorde
                }
418 321 mdecorde
        }
419 321 mdecorde
420 321 mdecorde
        public static String getMetaContent(File f) {
421 321 mdecorde
                def inputData = f.toURI().toURL().openStream();
422 321 mdecorde
                def factory = XMLInputFactory.newInstance();
423 321 mdecorde
                factory.setProperty("javax.xml.stream.supportDTD", false); // ignore the DTD declared in doctype
424 321 mdecorde
425 321 mdecorde
                def parser = factory.createXMLStreamReader(inputData);
426 321 mdecorde
                String META = "meta"
427 321 mdecorde
                String BODY = "body"
428 321 mdecorde
                String NAME = "name"
429 321 mdecorde
                String DESCRIPTION = "txm:first-word-id"
430 321 mdecorde
                String CONTENT = "content"
431 321 mdecorde
432 321 mdecorde
                String content = "";
433 321 mdecorde
                String desc = "";
434 321 mdecorde
435 321 mdecorde
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
436 321 mdecorde
                        if (event == XMLStreamConstants.START_ELEMENT) { // start elem
437 321 mdecorde
438 321 mdecorde
                                if (META.equals(parser.getLocalName())) { // ana elem
439 321 mdecorde
                                        desc = "";
440 321 mdecorde
                                        // fetch attribute values
441 321 mdecorde
                                        for (int i = 0 ; i < parser.getAttributeCount(); i++) { // scan attributes
442 321 mdecorde
                                                if (NAME.equals(parser.getAttributeLocalName(i))) { // found @name
443 321 mdecorde
                                                        desc = parser.getAttributeValue(i)
444 321 mdecorde
                                                } else if (CONTENT.equals(parser.getAttributeLocalName(i))) { // found @content
445 321 mdecorde
                                                        content = parser.getAttributeValue(i)
446 321 mdecorde
                                                }
447 321 mdecorde
                                        }
448 321 mdecorde
                                        if (DESCRIPTION.equals(desc)) { // stop now
449 321 mdecorde
                                                break;
450 321 mdecorde
                                        }
451 321 mdecorde
                                } else if (BODY.equals(parser.getLocalName())) { // no need to go further, meta@name="description" not found :(
452 321 mdecorde
                                        content = "";
453 321 mdecorde
                                        break;
454 321 mdecorde
                                }
455 321 mdecorde
                        }
456 321 mdecorde
                }
457 1688 mdecorde
                if (parser != null) parser.close();
458 1688 mdecorde
                if (inputData != null) inputData.close();
459 321 mdecorde
460 321 mdecorde
                return content;
461 321 mdecorde
        }
462 321 mdecorde
463 321 mdecorde
        public static void main(def args) {
464 321 mdecorde
                println "RESULT: "+getMetaContent(new File("/home/mdecorde/TXM/corpora/QGRAALXTZ/HTML/QGRAALXTZ/default", "qgraal_cm_test201510_page_160_2.html"))
465 321 mdecorde
        }
466 321 mdecorde
}