Statistics
| Revision:

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

History | View | Annotate | Download (8.1 kB)

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

    
4
import org.kohsuke.args4j.*
5
import groovy.transform.Field
6
import org.txm.rcpapplication.swt.widget.parameters.*
7
import org.txm.objects.*
8
import org.txm.searchengine.cqp.corpus.*;
9
import org.w3c.dom.*
10
import org.txm.importer.*
11
import org.txm.Toolbox
12
import java.io.*
13

    
14
import org.w3c.dom.Document;
15
import org.xml.sax.SAXException;
16
import org.txm.rcpapplication.commands.*
17
import javax.xml.parsers.*;
18
import javax.xml.transform.*;
19
import javax.xml.transform.dom.DOMSource;
20
import javax.xml.transform.stream.StreamResult;
21
import org.txm.utils.io.FileCopy
22

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

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

    
37
// BEGINNING OF params
38

    
39
@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")
40
def xslEdition
41

    
42
@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")
43
def xslPages
44

    
45
@Field @Option(name="editionName", usage="The edition name to produce", widget="String", required=false, def="default")
46
String editionName
47

    
48
@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")
49
def useTokenizedDirectory = false
50

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

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

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

    
79
if (editionName == null || editionName.length() == 0) {
80
        editionName = defaultEditionName
81
}
82
println "Parameters:"
83
println "        xslEdition = $xslEdition"
84
println "        xslPages = $xslPages"
85
println "        editionName = $editionName"
86
println "        useTokenizedDirectory = $useTokenizedDirectory"
87

    
88
File TXMHOME = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME))
89
File xslDirectory = new File(TXMHOME, "xsl")
90
if (xslEdition == null || xslEdition.getName() == "txm-edition-xtz.xsl")
91
        xslEdition = new File(xslDirectory, "txm-edition-xtz.xsl")
92
if (xslPages == null || xslPages.getName() == "txm-edition-page-split.xsl")
93
        xslPages = new File(xslDirectory, "txm-edition-page-split.xsl")
94

    
95
println "XSLs: "+xslEdition.getName()+" & "+ xslPages.getName()
96
println ""
97

    
98

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

    
107
File cssDirectory = new File(TXMHOME, "css")
108
File cssDefaultEditionDirectory = new File(defaultEditionDirectory, "css")
109
File cssTXM = new File(cssDirectory, "txm.css")
110
File cssTEI = new File(cssDirectory, "tei.css")
111
boolean newEdition = false;
112

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

    
143
defaultEditionDirectory.mkdir()
144
cssDefaultEditionDirectory.mkdir()
145
FileCopy.copy(cssTXM, new File(cssDefaultEditionDirectory, cssTXM.getName()))
146
FileCopy.copy(cssTEI, new File(cssDefaultEditionDirectory, cssTEI.getName()))
147

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

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

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

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

    
216
// clean temp files
217
if (!debug) {
218
        for (File f : htmlFiles) {
219
                f.delete()
220
        }
221
}
222

    
223
//4- register new edition if any (copy edition)
224

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

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

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