Statistics
| Revision:

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

History | View | Annotate | Download (6.6 kB)

1
package org.txm.macro.edition
2

    
3
// STANDARD DECLARATIONS
4

    
5
import org.kohsuke.args4j.*
6
import groovy.transform.Field
7
import org.txm.rcpapplication.swt.widget.parameters.*
8
import org.txm.objects.*
9
import org.txm.searchengine.cqp.corpus.*;
10
import org.w3c.dom.*
11
import org.txm.utils.xml.DomUtils
12
import org.txm.Toolbox
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

    
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 PARAMETERS
38
@Field @Option(name="editionName", usage="the edition name to create", widget="String", required=true, def="facs")
39
def editionName
40

    
41
@Field @Option(name="tag", usage="The tag to upgrade", widget="String", required=true, def="pb")
42
def tag
43

    
44
@Field @Option(name="attribute", usage="The attribute to add", widget="String", required=true, def="facs")
45
def attribute
46

    
47
@Field @Option(name="debug", usage="Debug mode", widget="Boolean", required=false, def="true")
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
        attribute = args["attribute"]
56
        tag = args["tag"]
57
        editionName = args["editionName"]
58
}
59

    
60
if (args.size() == 0) { // the "corpus" variable exists
61
        binDirectory = corpus.getBase().getBaseDirectory()
62
        txmDirectory = new File(binDirectory, "txm/"+corpus.getName().toUpperCase())
63
        tokenizedDirectory = new File(binDirectory, "tokenized")
64
        HTMLDirectory = new File(binDirectory, "HTML")
65
        newEditionDirectory = new File(HTMLDirectory, corpus.getName().toUpperCase()+"/"+editionName)
66
        params = corpus.getBase().params
67
} else { // the "corpus" variable does not exist, get informations from the "args" map
68
        binDirectory = args["binDirectory"]
69
        txmDirectory = args["txmDirectory"]
70
        params = args["params"]
71
        tokenizedDirectory = new File(binDirectory, "tokenized")
72
        HTMLDirectory = new File(binDirectory, "HTML")
73
        corpusName = params.getCorpusName()
74
        newEditionDirectory = new File(HTMLDirectory, corpusName.toUpperCase()+"/"+editionName)
75
}
76

    
77
println "Parameters:"
78
println "        attribute = $attribute"
79
println "        tag =  $tag"
80
println "        editionName =  $editionName"
81

    
82
if (!HTMLDirectory.exists()) {
83
        println "ERROR: can't find this corpus 'HTML' directory: $HTMLDirectory. Aborting"
84
        return false;
85
}
86

    
87
File workDirectory = txmDirectory
88
if (!workDirectory.exists()) {
89
        println "XML-TXM directory ($txmDirectory) not found. Using XML tokenized directory instead: "+tokenizedDirectory
90
        workDirectory = tokenizedDirectory
91
}
92
if (!workDirectory.exists()) {
93
        println "XML tokenized directory not found: "+tokenizedDirectory
94
        println "Aborting."
95
        return false
96
}
97

    
98
println "Working directory=$workDirectory"
99

    
100
//0- clean previous edition if any : html files, import.xml
101
if (newEditionDirectory.exists()) {
102
        println "** Old version of $editionName edition found."
103
        println " removing the 'edition' reference from the corpus configuration."
104
        File tempParam = new File(binDirectory, "import.xml.cpy")
105
        RemoveTag rt = new RemoveTag(
106
                        params.root.getOwnerDocument(), // will be updated
107
                        null, // don't create a new import.xml
108
                        "//edition[@name='$editionName']"
109
                        )
110
        println " delete $newEditionDirectory"
111
        newEditionDirectory.deleteDir()
112
        
113
        //printDOM(params.root.getOwnerDocument())
114
}
115

    
116
//2- fix import.xml
117
println "** Updating corpus configuration..."
118
// for edition list
119
def corpusElem = params.getCorpusElement()
120
params.addEditionDefinition(corpusElem, editionName, "groovy", "FacsEditionBuilderMacro");
121

    
122
//1- create HTML files
123
println "** Building new edition HTML files..."
124

    
125
println " Creating edition '$editionName' directory: '$newEditionDirectory'"
126
newEditionDirectory.mkdir()
127
for (def xmlFile : workDirectory.listFiles()) {
128
        if (xmlFile.isHidden() || xmlFile.isDirectory()) continue // ignore
129
        String txtname = xmlFile.getName()
130
        int idx = txtname.lastIndexOf(".")
131
        if (idx > 0) txtname = txtname.substring(0,idx)
132

    
133
        // create a page per "pb" element found in the xml-txm file
134
        println " Building HTML pages of text=$txtname"
135
        BuildFacsEditions builder = new BuildFacsEditions(xmlFile, newEditionDirectory, params.name, txtname, tag, attribute, debug);
136
        def newPages = builder.process()
137
        if (newPages == null || newPages.size() == 0) {
138
                println "WARNING: no edition files created with $xmlFile"
139
        }
140
                
141
        println " Building edition references in corpus configuration"
142
        
143
        Element corpusElement = params.getCorpusElement();
144
        //println "corpusElement $corpusElement"
145
        Element textsElem = params.getTextsElement(corpusElement);
146
        //println "textsElem $textsElem"
147
        Element textElem = params.getTextElement(textsElem, txtname);
148
        if (textElem == null) { // just in case
149
                textElem = params.addText(textsElem, txtname, xmlFile)
150
        }
151
        //println "textElem $textElem with txtname=$txtname"
152
        Element editionElem = params.addEdition(textElem, editionName, newEditionDirectory.getAbsolutePath(), "html"); // add a new edition to the "text" node
153
        for (def pagedef : newPages) {
154
                params.addPage(editionElem, pagedef[0], pagedef[1]);
155
        }
156
}
157

    
158
//printDOM(params.root.getOwnerDocument())
159

    
160
//3- Save and reload the corpus
161
println " Saving corpus configuration..."
162
File paramFile = new File(binDirectory, "import.xml");
163
DomUtils.save(params.root.getOwnerDocument(), paramFile);
164

    
165

    
166
//4- Reload Corpora
167
if (args.size() == 0) {
168
        Toolbox.restart();
169
        monitor.syncExec(new Runnable() {
170
                                public void run() {
171
                                        RestartTXM.reloadViews();
172
                                }
173
                        });
174
}
175

    
176
//5- Done
177
println "New edition created."
178

    
179
//printDOM(params.root.getOwnerDocument())
180

    
181

    
182

    
183
def printDOM(def doc) {
184
        if (!debug) return;
185
        try {
186
                // Création de la source DOM
187
                Source source = new DOMSource(doc);
188
                
189
                // Création du fichier de sortie
190
                StreamResult resultat = new StreamResult(new PrintWriter(System.out));
191
                
192
                // Configuration du transformer
193
                TransformerFactory fabrique = new net.sf.saxon.TransformerFactoryImpl();
194
                Transformer transformer = fabrique.newTransformer();
195
                transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
196
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");  //$NON-NLS-1$
197
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");  //$NON-NLS-1$
198
                
199
                // Transformation
200
                transformer.transform(source, resultat);
201
                // writer.close();
202
                return true;
203
        } catch (Exception e) {
204
                e.printStackTrace();
205
                return false;
206
        }
207
}