Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / macroproto / edition / XTZEditionBuilderMacro.groovy @ 148

History | View | Annotate | Download (5.1 kB)

1
package org.txm.macroproto.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.macro.edition.BuildFacsEditions
15
import org.txm.macro.edition.RemoveTag
16
import org.txm.objects.*
17
import org.txm.rcpapplication.commands.*
18
import org.txm.rcpapplication.swt.widget.parameters.*
19
import org.txm.searchengine.cqp.corpus.*
20
import org.w3c.dom.*
21

    
22
println "Corpus view selection is: "+corpusViewSelection
23
if (!(corpusViewSelection instanceof MainCorpus)) {
24
        println "This marcro works with a MainCorpus selection. Aborting"
25
        return;
26
}
27
def corpus = corpusViewSelection
28
debug = false
29
def editionName = corpus.getDefaultEdition()
30

    
31
// PARAMETERS
32
@Field @Option(name="wordsPerPage", usage="Number of words per page", widget="Integer", required=true, def="500")
33
int wordsPerPage
34
if (!ParametersDialog.open(this)) return;
35
// END OF PARAMETERS
36

    
37
File binDirectory = corpus.getBase().getBaseDirectory()
38
File txmDirectory = new File(binDirectory, "txm/"+corpus.getName().toUpperCase())
39

    
40
File HTMLDirectory = new File(binDirectory, "HTML")
41
File newEditionDirectory = new File(HTMLDirectory, corpus.getName().toUpperCase()+"/"+editionName)
42

    
43
BaseParameters parameters = corpus.getBase().params
44

    
45
if (!HTMLDirectory.exists()) {
46
        println "ERROR: can't find this corpus 'HTML' directory: $HTMLDirectory. Aborting"
47
        return false;
48
}
49

    
50
File workDirectory = txmDirectory
51
if (!workDirectory.exists()) {
52
        println "XML-TXM directory ($txmDirectory) not found. Using XML tokenized directory instead: "+tokenizedDirectory
53
        workDirectory = tokenizedDirectory
54
}
55
if (!workDirectory.exists()) {
56
        println "XML tokenized directory not found: "+tokenizedDirectory
57
        println "Aborting."
58
        return false
59
}
60

    
61
println "Work directory=$workDirectory"
62

    
63
//0- clean previous edition if any : html files, import.xml
64
if (newEditionDirectory.exists()) {
65
        println "** Old version of $editionName edition found."
66
        println " removing the 'edition' reference from the corpus configuration."
67
        File tempParam = new File(binDirectory, "import.xml.cpy")
68
        RemoveTag rt = new RemoveTag(
69
                        parameters.root.getOwnerDocument(), // will be updated
70
                        null, // don't create a new import.xml
71
                        "//edition[@name='$editionName']"
72
                        )
73
        println " delete $newEditionDirectory"
74
        newEditionDirectory.deleteDir()
75
        
76
        printDOM(parameters.root.getOwnerDocument())
77
}
78

    
79
//2- fix import.xml
80
println "** Updating corpus configuration..."
81
// for edition list
82
def corpusElem = parameters.getCorpusElement()
83
parameters.addEditionDefinition(corpusElem, editionName, "groovy", "FacsEditionBuilderMacro");
84

    
85
//1- create HTML files
86
println "** Building new edition HTML files..."
87

    
88
println " Creating edition $editionName directory: $newEditionDirectory"
89
newEditionDirectory.mkdir()
90
for (def xmlFile : workDirectory.listFiles()) {
91
        if (xmlFile.isHidden() || xmlFile.isDirectory()) continue // ignore
92
        String txtname = xmlFile.getName()
93
        int idx = txtname.indexOf(".")
94
        if (idx > 0) txtname = txtname.substring(0,idx)
95

    
96
        println " Build HTML pages of text=$txtname"
97
        BuildFacsEditions builder = new BuildFacsEditions(xmlFile, newEditionDirectory, parameters.name, txtname, wordsPerPage);
98
        def newPages = builder.process()
99
        if (newPages == null || newPages.size() == 0) {
100
                println "WARNING: no edition files created with $xmlFile"
101
        } 
102
                
103
        println " Build edition references in corpus configuration"
104
        
105
        Element textElem = corpus.getText(txtname).getSelfElement()
106
        Element editionElem = parameters.addEdition(textElem, editionName, newEditionDirectory.getAbsolutePath(), "html");
107
        //println "$textElem $editionElem"
108
        for (def pagedef : newPages) {
109
                parameters.addPage(editionElem, pagedef[0], pagedef[1]);
110
        }
111
}
112

    
113
        printDOM(parameters.root.getOwnerDocument())
114

    
115
//3- Save and reload the corpus
116
println " Saving corpus configuration..."
117
File paramFile = new File(binDirectory, "import.xml");
118
DomUtils.save(parameters.root.getOwnerDocument(), paramFile);
119

    
120

    
121
//4- Reload Corpora
122
Toolbox.restart();
123
monitor.syncExec(new Runnable() {
124
                        public void run() {
125
                                println "RELOAD VIEWS"
126
                                RestartTXM.reloadViews();
127
                                println "RELOAD VIEWS2"
128
                        }
129
                });
130

    
131
//5- Done
132
println "New edition created. Be sure to copy images files in $newEditionDirectory"
133

    
134
printDOM(parameters.root.getOwnerDocument())
135

    
136
def printDOM(def doc) {
137
        if (!debug) return;
138
        try {
139
                // Création de la source DOM
140
                Source source = new DOMSource(doc);
141
                
142
                // Création du fichier de sortie
143
                StreamResult resultat = new StreamResult(new PrintWriter(System.out));
144
                
145
                // Configuration du transformer
146
                TransformerFactory fabrique = new net.sf.saxon.TransformerFactoryImpl();
147
                Transformer transformer = fabrique.newTransformer();
148
                transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
149
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");  //$NON-NLS-1$
150
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");  //$NON-NLS-1$
151
                
152
                // Transformation
153
                transformer.transform(source, resultat);
154
                // writer.close();
155
                return true;
156
        } catch (Exception e) {
157
                e.printStackTrace();
158
                return false;
159
        }
160
}