Statistics
| Revision:

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

History | View | Annotate | Download (3.9 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.core.preferences.TBXPreferences;
12
import org.txm.metadatas.Metadatas
13
import org.txm.utils.io.FileCopy;
14
import org.txm.*
15
import org.txm.objects.*
16
import org.w3c.dom.Element
17
import org.txm.importer.xtz.*
18

    
19
public class XTZImport extends ImportModule {
20

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

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

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

    
107
                if (isSuccessful) {
108
                        
109
                        //declare a local KR
110
                        //TODO find out how the annotation plugin may hook the import steps
111
//                        List<String> krnames = importParameters.getKnowledgeRepositoryNames();
112
//                        if (krnames.size() == 0) {
113
//                                importParameters.createKnowledgeRepositoryElement("DEFAULT"); // set a default KR shared by all XTZ corpus
114
//                        } else if (krnames.size() == 1 && krnames.get(0).equals("DEFAULT")) {
115
//                                // nothing to do
116
//                        } else {
117
//                                println("Corpus is using custom Knowledge repositories: "+importParameters.getKnowledgeRepositoryNames());
118
//                        }
119
                        
120
                        //copy sub directories
121
                        if (isUpdatingCorpus()) {
122

    
123
                        } else {
124
                                def dirToCopy = ["xsl", "css", "dtd"]
125
                                println "--- Copying subdirectories $dirToCopy"
126
                                for (String dir : dirToCopy) {
127
                                        File origDirectory = new File(this.sourceDirectory, dir)
128
                                        if (origDirectory.exists()) {
129
                                                print "."
130
                                                File copyDirectory = new File(this.binaryDirectory, dir)
131
                                                copyDirectory.deleteDir(); // clean before copying
132
                                                FileCopy.copyFiles(origDirectory, copyDirectory)
133
                                        }
134
                                }
135
                                println ""
136
                        }
137
                }
138
        }
139
}