Statistics
| Revision:

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

History | View | Annotate | Download (3.5 kB)

1
package org.txm.scripts.importer.xtz;
2

    
3
import java.io.File;
4
import java.io.FileFilter;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.Collections;
8

    
9
import org.eclipse.core.runtime.IProgressMonitor;
10
import org.txm.utils.xml.DomUtils;
11
import org.txm.metadatas.Metadatas
12
import org.txm.utils.io.FileCopy;
13
import org.txm.*
14
import org.txm.objects.*
15
import org.w3c.dom.Element
16
import org.txm.importer.xtz.*
17

    
18
public class XTZImport extends ImportModule {
19

    
20
        public XTZImport(File importParametersFile) {
21
                super(importParametersFile);
22
        }
23

    
24
        public XTZImport(Project p) {
25
                super(p);
26
        }
27

    
28
        @Override
29
        public void init(Project p) {
30
                super.init(p);
31
                
32
                importer = new XTZImporter(this)
33
                compiler = new XTZCompiler(this)
34
                annotater = new TTAnnotater(this);
35
                pager = new XTZPager(this)
36
        }
37
        
38
        @Override
39
        protected ArrayList<String> getTXMFilesOrder() {
40
                //System.out.println("XTZ FILES ORDER");
41
                if (importer == null) {
42
                        println "no importer step, using default text order"
43
                        return super.getTXMFilesOrder();
44
                }
45
                Metadatas metadata = importer.getMetadata();
46
                if (metadata == null) {
47
                        println "no metadata, using default text order"
48
                        return super.getTXMFilesOrder();
49
                }
50
                File txmDirectory = new File(binaryDirectory, "txm/"+corpusName);
51
                ArrayList<File> files = new ArrayList<File>(Arrays.asList(txmDirectory.listFiles(new FileFilter() {
52
                        @Override
53
                        public boolean accept(File file) {
54
                                return file.isFile() && file.getName().endsWith(".xml");
55
                        }
56
                })));
57
        
58
                // fill sort data map
59
                final HashMap<String, String> textorder = new HashMap<String, String>();
60
                for (String t : metadata.keySet()) {
61
                        def ti = metadata.get(t)
62
                        for (org.txm.metadatas.Entry e : ti) {
63
                                if ("textorder".equals(e.getId())) {
64
                                        textorder[t] = ti.get("textorder")
65
                                }
66
                        }
67
                }
68
                
69
                println "Sorting texts using 'textorder' metadata values: "+textorder
70
                def texts = project.getTextsID();
71
                Collections.sort(texts, new Comparator<String>() {
72
                        public int compare(String f1, String f2) {
73
                                String o1 = textorder[f1];
74
                                String o2 = textorder[f2];
75
                                if (o1 == null && o2 == null) {
76
                                        return f1.compareTo(f2);
77
                                } else if (o1 == null) {
78
                                        return 1
79
                                } else if (o2 == null) {
80
                                        return -1
81
                                } else {
82
                                        int c = o1.compareTo(o2);
83
                                        if (c == 0) return f1.compareTo(f2);
84
                                        else return c;
85
                                }
86
                        }
87
                });
88
                //println files
89
                return files;
90
        }
91
        
92
        public void start() throws InterruptedException {
93
                super.start();
94

    
95
                if (isSuccessful) {
96
                        
97
                        //declare a local KR
98
                        //TODO find out how the annotation plugin may hook the import steps
99
//                        List<String> krnames = importParameters.getKnowledgeRepositoryNames();
100
//                        if (krnames.size() == 0) {
101
//                                importParameters.createKnowledgeRepositoryElement("DEFAULT"); // set a default KR shared by all XTZ corpus
102
//                        } else if (krnames.size() == 1 && krnames.get(0).equals("DEFAULT")) {
103
//                                // nothing to do
104
//                        } else {
105
//                                println("Corpus is using custom Knowledge repositories: "+importParameters.getKnowledgeRepositoryNames());
106
//                        }
107
                        
108
                        //copy sub directories
109
                        if (isUpdatingCorpus()) {
110

    
111
                        } else {
112
                                def dirToCopy = ["xsl", "css", "dtd"]
113
                                println "--- Copying subdirectories $dirToCopy"
114
                                for (String dir : dirToCopy) {
115
                                        File origDirectory = new File(this.sourceDirectory, dir)
116
                                        if (origDirectory.exists()) {
117
                                                print "."
118
                                                File copyDirectory = new File(this.binaryDirectory, dir)
119
                                                copyDirectory.deleteDir(); // clean before copying
120
                                                FileCopy.copyFiles(origDirectory, copyDirectory)
121
                                        }
122
                                }
123
                                println ""
124
                        }
125
                }
126
        }
127
}