Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / importer / xtz / XTZImport.groovy @ 2126

History | View | Annotate | Download (3.4 kB)

1 986 mdecorde
package org.txm.scripts.importer.xtz;
2 321 mdecorde
3 479 mdecorde
import java.io.File;
4 479 mdecorde
import java.io.FileFilter;
5 479 mdecorde
import java.util.ArrayList;
6 479 mdecorde
import java.util.Arrays;
7 479 mdecorde
import java.util.Collections;
8 479 mdecorde
9 490 mdecorde
import org.eclipse.core.runtime.IProgressMonitor;
10 479 mdecorde
import org.txm.utils.xml.DomUtils;
11 321 mdecorde
import org.txm.metadatas.Metadatas
12 321 mdecorde
import org.txm.utils.io.FileCopy;
13 479 mdecorde
import org.txm.*
14 1135 mdecorde
import org.txm.objects.*
15 479 mdecorde
import org.w3c.dom.Element
16 1000 mdecorde
import org.txm.importer.xtz.*
17 321 mdecorde
18 321 mdecorde
public class XTZImport extends ImportModule {
19 321 mdecorde
20 321 mdecorde
        public XTZImport(File importParametersFile) {
21 321 mdecorde
                super(importParametersFile);
22 321 mdecorde
        }
23 321 mdecorde
24 1135 mdecorde
        public XTZImport(Project p) {
25 1135 mdecorde
                super(p);
26 321 mdecorde
        }
27 321 mdecorde
28 321 mdecorde
        @Override
29 1135 mdecorde
        public void init(Project p) {
30 321 mdecorde
                super.init(p);
31 321 mdecorde
32 321 mdecorde
                importer = new XTZImporter(this)
33 321 mdecorde
                compiler = new XTZCompiler(this)
34 321 mdecorde
                annotater = new TTAnnotater(this);
35 321 mdecorde
                pager = new XTZPager(this)
36 321 mdecorde
        }
37 321 mdecorde
38 321 mdecorde
        @Override
39 321 mdecorde
        protected ArrayList<File> getTXMFilesOrder() {
40 321 mdecorde
                //System.out.println("XTZ FILES ORDER");
41 321 mdecorde
                if (importer == null) {
42 321 mdecorde
                        println "no importer step, using default text order"
43 321 mdecorde
                        return super.getTXMFilesOrder();
44 321 mdecorde
                }
45 321 mdecorde
                Metadatas metadata = importer.getMetadata();
46 321 mdecorde
                if (metadata == null) {
47 321 mdecorde
                        println "no metadata, using default text order"
48 321 mdecorde
                        return super.getTXMFilesOrder();
49 321 mdecorde
                }
50 321 mdecorde
                File txmDirectory = new File(binaryDirectory, "txm/"+corpusName);
51 321 mdecorde
                ArrayList<File> files = new ArrayList<File>(Arrays.asList(txmDirectory.listFiles(new FileFilter() {
52 321 mdecorde
                        @Override
53 321 mdecorde
                        public boolean accept(File file) {
54 321 mdecorde
                                return file.isFile() && file.getName().endsWith(".xml");
55 321 mdecorde
                        }
56 321 mdecorde
                })));
57 321 mdecorde
58 321 mdecorde
                final HashMap<File, String> textorder = new HashMap<File, String>();
59 321 mdecorde
                for (File f : files) {
60 321 mdecorde
                        HashMap<String, String> m = metadata.getTextMetadata(f);
61 321 mdecorde
                        if (m != null && m.containsKey("textorder")) textorder[f] = m["textorder"];
62 321 mdecorde
                }
63 321 mdecorde
                println "Sorting texts using 'textorder' metadata values: "+textorder
64 321 mdecorde
                Collections.sort(files, new Comparator<File>() {
65 321 mdecorde
                        public int compare(File f1, File f2) {
66 321 mdecorde
                                String o1 = textorder[f1];
67 321 mdecorde
                                String o2 = textorder[f2];
68 321 mdecorde
                                if (o1 == null && o2 == null) {
69 321 mdecorde
                                        return f1.getName().compareTo(f2.getName());
70 321 mdecorde
                                } else if (o1 == null) {
71 321 mdecorde
                                        return 1
72 321 mdecorde
                                } else if (o2 == null) {
73 321 mdecorde
                                        return -1
74 321 mdecorde
                                } else {
75 321 mdecorde
                                        int c = o1.compareTo(o2);
76 321 mdecorde
                                        if (c == 0) return f1.getName().compareTo(f2.getName());
77 321 mdecorde
                                        else return c;
78 321 mdecorde
                                }
79 321 mdecorde
                        }
80 321 mdecorde
                });
81 321 mdecorde
                //println files
82 321 mdecorde
                return files;
83 321 mdecorde
        }
84 321 mdecorde
85 321 mdecorde
        public void start() throws InterruptedException {
86 321 mdecorde
                super.start();
87 321 mdecorde
88 321 mdecorde
                if (isSuccessful) {
89 321 mdecorde
90 321 mdecorde
                        //declare a local KR
91 715 mdecorde
                        //TODO find out how the annotation plugin may hook the import steps
92 715 mdecorde
//                        List<String> krnames = importParameters.getKnowledgeRepositoryNames();
93 715 mdecorde
//                        if (krnames.size() == 0) {
94 715 mdecorde
//                                importParameters.createKnowledgeRepositoryElement("DEFAULT"); // set a default KR shared by all XTZ corpus
95 715 mdecorde
//                        } else if (krnames.size() == 1 && krnames.get(0).equals("DEFAULT")) {
96 715 mdecorde
//                                // nothing to do
97 715 mdecorde
//                        } else {
98 715 mdecorde
//                                println("Corpus is using custom Knowledge repositories: "+importParameters.getKnowledgeRepositoryNames());
99 715 mdecorde
//                        }
100 321 mdecorde
101 321 mdecorde
                        //copy sub directories
102 321 mdecorde
                        if (isUpdatingCorpus()) {
103 321 mdecorde
104 321 mdecorde
                        } else {
105 321 mdecorde
                                def dirToCopy = ["xsl", "css", "dtd"]
106 321 mdecorde
                                println "--- Copying subdirectories $dirToCopy"
107 321 mdecorde
                                for (String dir : dirToCopy) {
108 321 mdecorde
                                        File origDirectory = new File(this.sourceDirectory, dir)
109 321 mdecorde
                                        if (origDirectory.exists()) {
110 321 mdecorde
                                                print "."
111 321 mdecorde
                                                File copyDirectory = new File(this.binaryDirectory, dir)
112 1971 mdecorde
                                                copyDirectory.deleteDir(); // clean before copying
113 321 mdecorde
                                                FileCopy.copyFiles(origDirectory, copyDirectory)
114 321 mdecorde
                                        }
115 321 mdecorde
                                }
116 321 mdecorde
                                println ""
117 321 mdecorde
                        }
118 321 mdecorde
                }
119 321 mdecorde
        }
120 321 mdecorde
}