Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / macro / edition / EditionUpdaterMacro.groovy @ 187

History | View | Annotate | Download (8 kB)

1
package org.txm.macro.edition
2
// STANDARD DECLARATIONS
3

    
4
import groovy.transform.Field
5

    
6
import javax.xml.parsers.*
7
import javax.xml.transform.*
8

    
9
import org.kohsuke.args4j.*
10
import org.txm.Toolbox
11
import org.txm.importer.*
12
import org.txm.objects.*
13
import org.txm.rcpapplication.commands.*
14
import org.txm.rcpapplication.swt.widget.parameters.*
15
import org.txm.searchengine.cqp.corpus.*
16
import org.txm.utils.FileCopy
17
import org.w3c.dom.*
18

    
19
try { println "ARGS=$args";} catch(Exception e) {args = [:]}
20

    
21
def corpus = null
22
if (args.size() == 0) {
23
        if (!(corpusViewSelection instanceof MainCorpus)) {
24
                println "This marcro works with a MainCorpus selection. Aborting"
25
                return;
26
        }
27
        println "Working on $corpusViewSelection corpus"
28
        corpus = corpusViewSelection
29
} else {
30
        monitor = args["monitor"];
31
}
32

    
33
// BEGINNING OF params
34

    
35
@Field @Option(name="xslEdition", usage="XSL to build the HTML edition - if empty uses 'txm-edition-xtz.xsl'", widget="File", required=false, def="txm-edition-xtz.xsl")
36
def xslEdition
37

    
38
@Field @Option(name="xslPages", usage="XSL to build the edition pages - if empty uses 'txm-edition-page-split.xsl'", widget="File", required=false, def="txm-edition-page-split.xsl")
39
def xslPages
40

    
41
@Field @Option(name="editionName", usage="The edition name to produce", widget="String", required=false, def="default")
42
String editionName
43

    
44
@Field @Option(name="useTokenizedDirectory", usage="Use the 'XML/w' of the 'tokenized' directory instead of the 'XML-TXM' files", widget="Boolean", required=false, def="false")
45
def useTokenizedDirectory = false
46

    
47
@Field @Option(name="debug", usage="Enable debug mode: temporary files are not deleted", widget="Boolean", required=false, def="false")
48
def debug = false
49

    
50
// END OF params
51
if (args.size() == 0) {
52
        if (!ParametersDialog.open(this)) return;
53
} else {
54
        monitor = args["monitor"];
55
        xslEdition = new File(args["xslEdition"])
56
        xslPages = new File(args["xslPages"])
57
        editionName = args["editionName"]
58
        useTokenizedDirectory = args["useTokenizedDirectory"]
59
}
60

    
61
if (args.size() == 0) { // corpus informations
62
        defaultEditionName = corpus.getDefaultEdition()
63
        corpusName = corpus.getName()
64
        binDirectory = corpus.getBase().getBaseDirectory()
65
        txmDirectory = new File(binDirectory, "txm/"+corpusName.toUpperCase())
66
        params = corpus.getBase().params
67
} else { // the "corpus" variable does not exists
68
        defaultEditionName = "default"
69
        binDirectory = args["binDirectory"]
70
        txmDirectory = args["txmDirectory"]
71
        params = args["params"]
72
        corpusName = params.getCorpusName()
73
}
74

    
75
if (editionName == null || editionName.length() == 0) {
76
        editionName = defaultEditionName
77
}
78
println "Parameters:"
79
println "        xslEdition = $xslEdition"
80
println "        xslPages = $xslPages"
81
println "        editionName = $editionName"
82
println "        useTokenizedDirectory = $useTokenizedDirectory"
83

    
84
File TXMHOME = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME))
85
File xslDirectory = new File(TXMHOME, "xsl")
86
if (xslEdition == null || xslEdition.getName() == "txm-edition-xtz.xsl")
87
        xslEdition = new File(xslDirectory, "txm-edition-xtz.xsl")
88
if (xslPages == null || xslPages.getName() == "txm-edition-page-split.xsl")
89
        xslPages = new File(xslDirectory, "txm-edition-page-split.xsl")
90

    
91
println "XSLs: "+xslEdition.getName()+" & "+ xslPages.getName()
92
println ""
93

    
94

    
95
if (useTokenizedDirectory) {
96
        println "Using the 'tokenized' directory instead of the 'txm' directory to get XML files"
97
        txmDirectory = new File(binDirectory, "tokenized")
98
}
99
File HTMLDirectory = new File(binDirectory, "HTML")
100
File HTMLCorpusDirectory = new File(HTMLDirectory, corpusName.toUpperCase())
101
File defaultEditionDirectory = new File(HTMLCorpusDirectory, editionName)
102

    
103
File cssDirectory = new File(TXMHOME, "css")
104
File cssDefaultEditionDirectory = new File(defaultEditionDirectory, "css")
105
File cssTXM = new File(cssDirectory, "txm.css")
106
File cssTEI = new File(cssDirectory, "tei.css")
107
boolean newEdition = false;
108

    
109
if (!txmDirectory.exists()) {
110
        println "ERROR: can't find this corpus 'txm' directory: $txmDirectory. Aborting"
111
        return false;
112
}
113
if (!defaultEditionDirectory.exists()) {
114
        println "This is a new edition"
115
        newEdition = true;
116
        defaultEditionDirectory.mkdir()
117
        if (!defaultEditionDirectory.exists()) {
118
                println "HTML directory could be created: $defaultEditionDirectory. Aborting"
119
                return false
120
        }
121
}
122
if (!cssTXM.exists()) {
123
        println "WARNING: can't find the $cssTXM CSS file. Aborting"
124
        return false;
125
}
126
if (!cssTEI.exists()) {
127
        println "WARNING: can't find the $cssTEI CSS file. Aborting"
128
        return false;
129
}
130
if (!xslEdition.exists()) {
131
        println "Error: can't find $xslEdition XSL file"
132
        return false;
133
}
134
if (!xslPages.exists()) {
135
        println "Error: can't find $xslPages XSL file"
136
        return false;
137
}
138

    
139
defaultEditionDirectory.mkdir()
140
cssDefaultEditionDirectory.mkdir()
141
FileCopy.copy(cssTXM, new File(cssDefaultEditionDirectory, cssTXM.getName()))
142
FileCopy.copy(cssTEI, new File(cssDefaultEditionDirectory, cssTEI.getName()))
143

    
144
//1- Back up current "HTML" directory
145
if (!newEdition) {
146
        File backupDirectory = new File(binDirectory, "HTML-"+defaultEditionDirectory.getName()+"-back")
147
        backupDirectory.mkdir()
148
        println "Backup of $defaultEditionDirectory directory to $backupDirectory..."
149
        for (File f : defaultEditionDirectory.listFiles()) {
150
                String name = f.getName()
151
                if (f.isDirectory() || f.isHidden()) continue
152
                
153
                File rez = new File(backupDirectory, f.getName())
154
                
155
                if (debug) println " file $f >> $rez"
156
                else print "."
157
                
158
                if (!FileCopy.copy(f, rez)) {
159
                        println "Error: failed to backup $f"
160
                        return false;
161
                }
162
        }
163
        println ""
164
}
165

    
166
//2- Apply edition XSL
167
println "Applying XSL 1: $xslEdition..."
168
ApplyXsl2 applier = new ApplyXsl2(xslEdition);
169
def htmlFiles = []
170
for (File f : txmDirectory.listFiles()) {
171
        String name = f.getName()
172
        String txtname = name.substring(0, name.indexOf("."));
173
        File rez = new File(HTMLCorpusDirectory, txtname+".html")
174

    
175
        if (!f.isDirectory() && !f.isHidden() && name.endsWith(".xml") && !name.equals("import.xml")) {
176
        
177
                if (debug) println " file $f >> $rez"
178
                else print "."
179
        
180
                if (!applier.process(f, rez)) {
181
                        println "Error: failed to process $f"
182
                        return false
183
                } else {
184
                        htmlFiles << rez
185
                }
186
        }
187
}
188
println ""
189

    
190
//3- Apply pages XSL
191
println "Applying XSL 2: $xslPages..."
192
ApplyXsl2 applier2 = new ApplyXsl2(xslPages);
193
applier2.setParam("editionname", editionName)
194
applier2.setParam("cssname", corpusName)
195
for (File f : htmlFiles) {
196
        String name = f.getName()
197
        String txtname = name.substring(0, name.indexOf("."));
198
        File rez = new File(defaultEditionDirectory, txtname+"-pages.html")
199
        
200
        if (debug) println " file $f >> $rez"
201
        else print "."
202
        
203
        if (!applier2.process(f, rez)) {
204
                println "Error: failed to process $f"
205
                return false
206
        } else {
207
                if (!debug) rez.delete()
208
        }
209
}
210
println ""
211

    
212
// clean temp files
213
if (!debug) {
214
        for (File f : htmlFiles) {
215
                f.delete()
216
        }
217
}
218

    
219
//4- register new edition if any (copy edition)
220

    
221
if (editionName != defaultEditionName) {
222
        println "Update corpus configuration"
223
        
224
        // remove edition declaration if any
225
        RemoveTag rt = new RemoveTag(
226
                        params.root.getOwnerDocument(), // will be updated
227
                        null, // don't create a new import.xml
228
                        "//edition[@name='$editionName']"
229
                        )
230

    
231
        def corpusElem = params.getCorpusElement()
232
        params.addEditionDefinition(corpusElem, editionName, "xsl", "XSLEditionBuilder"); // declare the new edition
233
        
234
        for (def text : corpus.getTexts()) {
235
                Element textElem = text.getSelfElement()
236
                def defaultEdition = text.getEdition(defaultEditionName)
237
                if (defaultEdition == null) { println "Error: no default edition with name="+defaultEditionName; return false} 
238
                Element editionElem = params.addEdition(textElem, editionName, defaultEditionDirectory.getAbsolutePath(), "html");
239
                def pages = defaultEdition.getPages()
240
                for (int i = 1 ; i <= pages.size() ; i++) {
241
                        def page = pages[i-1]
242
                        params.addPage(editionElem, "$i", page.getWordId());
243
                }
244
        }
245
        
246
        File paramFile = new File(binDirectory, "import.xml");
247
        DomUtils.save(params.root.getOwnerDocument(), paramFile);
248
}
249

    
250
//5- Reload Corpora
251
if (args.size() == 0) { // the user run the macro
252
        Toolbox.restart();
253
        monitor.syncExec(new Runnable() {
254
                        public void run() {
255
                                RestartTXM.reloadViews();
256
                        }
257
                });
258
}
259
println "New edition created."