Statistics
| Revision:

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

History | View | Annotate | Download (6.5 kB)

1
package org.txm.macro.edition
2

    
3
// STANDARD DECLARATIONS
4

    
5
import groovy.transform.Field
6

    
7
import javax.xml.parsers.*
8
import javax.xml.transform.*
9
import javax.xml.transform.dom.DOMSource
10
import javax.xml.transform.stream.StreamResult
11

    
12
import org.kohsuke.args4j.*
13
import org.txm.Toolbox
14
import org.txm.objects.*
15
import org.txm.rcpapplication.commands.*
16
import org.txm.rcpapplication.swt.widget.parameters.*
17
import org.txm.searchengine.cqp.corpus.*
18
import org.txm.utils.xml.DomUtils
19
import org.w3c.dom.*
20

    
21

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

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

    
36
// BEGINNING OF PARAMETERS
37
@Field @Option(name="editionName", usage="the edition name to create", widget="String", required=true, def="facs")
38
def editionName
39

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

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

    
46
@Field @Option(name="debug", usage="Debug mode", widget="Boolean", required=false, def="true")
47
def debug = false
48

    
49
// END OF params
50
if (args.size() == 0) {
51
        if (!ParametersDialog.open(this)) return;
52
} else {
53
        monitor = args["monitor"];
54
        attribute = args["attribute"]
55
        tag = args["tag"]
56
        editionName = args["editionName"]
57
}
58

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

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

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

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

    
97
println "Working directory=$workDirectory"
98

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

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

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

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

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

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

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

    
164

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

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

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

    
180

    
181

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