Statistics
| Revision:

root / tmp / org.txm.tigersearch.rcp / groovy / org / txm / importer / tigersearch / TSImport.groovy @ 966

History | View | Annotate | Download (3.7 kB)

1 911 mdecorde
package org.txm.importer.tigersearch;
2 911 mdecorde
3 911 mdecorde
import java.io.File;
4 911 mdecorde
import java.util.ArrayList;
5 911 mdecorde
6 911 mdecorde
import ims.tiger.index.writer.*
7 911 mdecorde
import ims.tiger.system.*
8 911 mdecorde
9 911 mdecorde
import org.txm.Toolbox;
10 911 mdecorde
import org.txm.importer.ApplyXsl2;
11 911 mdecorde
import org.txm.importer.xtz.*
12 911 mdecorde
import org.txm.objects.BaseParameters
13 911 mdecorde
import org.txm.utils.BundleUtils;
14 911 mdecorde
import org.txm.utils.io.FileCopy;
15 911 mdecorde
import org.apache.log4j.BasicConfigurator;
16 911 mdecorde
17 911 mdecorde
class TSImport extends XTZImport {
18 911 mdecorde
19 911 mdecorde
        public TSImport(BaseParameters params) {
20 911 mdecorde
                super(params);
21 911 mdecorde
        }
22 911 mdecorde
23 911 mdecorde
        @Override
24 911 mdecorde
        public void init(BaseParameters p) {
25 911 mdecorde
                super.init(p);
26 911 mdecorde
27 911 mdecorde
                importer = new TSImporter(this); // only to build metadata
28 911 mdecorde
                compiler = new XTZCompiler(this)
29 911 mdecorde
                annotater = null; // no annotater step to do
30 911 mdecorde
                pager = new XTZPager(this)
31 911 mdecorde
        }
32 911 mdecorde
33 911 mdecorde
        /**
34 911 mdecorde
         * Do a XTZ Import then build the TIGERSearch indexes in the binary corpus "tiger" directory
35 911 mdecorde
         */
36 911 mdecorde
        @Override
37 911 mdecorde
        public void start() throws InterruptedException {
38 911 mdecorde
                File tigerSrcDir = sourceDirectory
39 911 mdecorde
40 911 mdecorde
                def xmlFiles = []
41 911 mdecorde
                sourceDirectory.listFiles(new FileFilter() {
42 911 mdecorde
                        boolean accept(File file) {
43 911 mdecorde
                                if (file.isDirectory()) return false;
44 911 mdecorde
                                if (file.isHidden()) return false;
45 911 mdecorde
                                if (file.getName().equals("import.xml")) return false;
46 911 mdecorde
                                if (!file.getName().endsWith(".xml")) return false;
47 911 mdecorde
48 911 mdecorde
                                xmlFiles << file
49 911 mdecorde
                        }
50 911 mdecorde
                });
51 911 mdecorde
52 911 mdecorde
                xmlFiles.remove(new File(sourceDirectory, "import.xml"))
53 911 mdecorde
54 911 mdecorde
                if (xmlFiles.size() == 0) {
55 911 mdecorde
                        println "Error no XML file found in $sourceDirectory"
56 911 mdecorde
                        isSuccessful = false;
57 911 mdecorde
                        return;
58 911 mdecorde
                }
59 911 mdecorde
60 911 mdecorde
                File master = xmlFiles[0];
61 911 mdecorde
                println "Main TIGER XML file found: $master"
62 911 mdecorde
63 911 mdecorde
                File tsXSLFile = new File(Toolbox.getTXMHOMEPATH(), "xsl/ts.xsl");
64 911 mdecorde
                BundleUtils.copyFiles("TIGERSearchRCP", "src", "org/txm/importer/tigersearch", "ts.xsl", tsXSLFile.getParentFile());
65 911 mdecorde
66 911 mdecorde
                File xmltxmSrcDir = new File(binaryDirectory, "src"); // output directory of the TS XSL transformation
67 911 mdecorde
                xmltxmSrcDir.mkdirs();
68 911 mdecorde
                FileCopy.copy(master, new File(xmltxmSrcDir, master.getName()));
69 911 mdecorde
70 911 mdecorde
                if (!ApplyXsl2.processImportSources(tsXSLFile, xmltxmSrcDir, xmltxmSrcDir)) {
71 911 mdecorde
                        println "Error while applying TS XSL file to $tigerSrcDir"
72 911 mdecorde
                        isSuccessful = false;
73 911 mdecorde
                        return;
74 911 mdecorde
                }
75 911 mdecorde
76 911 mdecorde
                File[] files = xmltxmSrcDir.listFiles();
77 911 mdecorde
                if (files == null || files.length == 0) {
78 911 mdecorde
                        println "Error while applying TS XSL file to $xmltxmSrcDir is empty"
79 911 mdecorde
                        isSuccessful = false;
80 911 mdecorde
                        return;
81 911 mdecorde
                }
82 911 mdecorde
83 911 mdecorde
                sourceDirectory = xmltxmSrcDir; // hop
84 911 mdecorde
                File txmDir = new File(binaryDirectory, "txm/"+corpusName);
85 911 mdecorde
                txmDir.mkdirs();
86 911 mdecorde
                FileCopy.copyFiles(sourceDirectory, txmDir) // the compiler step will use these files
87 911 mdecorde
88 911 mdecorde
                super.start(); // call the usual XTZ import
89 911 mdecorde
90 911 mdecorde
                if (isSuccessful) {
91 911 mdecorde
92 911 mdecorde
                        File tigerDir = new File(binaryDirectory, "tiger");
93 911 mdecorde
                        tigerDir.mkdir();
94 911 mdecorde
95 911 mdecorde
96 911 mdecorde
                                File logprop = new File(tigerDir, "tigersearch.logprop");
97 911 mdecorde
98 911 mdecorde
                                logprop.withWriter("UTF-8") { writer ->
99 911 mdecorde
                                        writer.write("""# Default log configuration of the TIGERSearch suite
100 911 mdecorde
log4j.rootLogger=WARN,Logfile
101 911 mdecorde
log4j.logger.ims.tiger.gui.tigersearch.TIGERSearch=WARNING
102 911 mdecorde
log4j.appender.Logfile=org.apache.log4j.RollingFileAppender
103 911 mdecorde
log4j.appender.Logfile.File=\${user.home}/tigersearch/tigersearch.log
104 911 mdecorde
log4j.appender.Logfile.MaxFileSize=500KB
105 911 mdecorde
log4j.appender.Logfile.MaxBackupIndex=1
106 911 mdecorde
log4j.appender.Logfile.layout=org.apache.log4j.PatternLayout
107 911 mdecorde
log4j.appender.Logfile.layout.ConversionPattern=%5r %-5p [%t] %c{2} - %m%n""")
108 911 mdecorde
                                }
109 911 mdecorde
110 911 mdecorde
                                BasicConfigurator.configure();
111 911 mdecorde
                                String uri = master.getAbsolutePath();
112 911 mdecorde
                                File tigerBinDir = new File(tigerDir, corpusName)
113 911 mdecorde
                                tigerBinDir.mkdir()
114 911 mdecorde
                                try {
115 911 mdecorde
                                        IndexBuilderErrorHandler handler = new SimpleErrorHandler(tigerBinDir.getAbsolutePath());
116 911 mdecorde
                                        XMLIndexing indexing = new XMLIndexing(corpusName, uri, tigerBinDir.getAbsolutePath(), handler,false);
117 911 mdecorde
                                        indexing.startIndexing();
118 911 mdecorde
                                }
119 911 mdecorde
                                catch (Exception e) { System.out.println(e.getMessage()); }
120 911 mdecorde
                        }
121 911 mdecorde
        }
122 911 mdecorde
}