Statistics
| Revision:

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

History | View | Annotate | Download (5.2 kB)

1
package org.txm.macroproto.edition
2

    
3
// STANDARD DECLARATIONS
4

    
5
import org.kohsuke.args4j.*
6

    
7
import groovy.transform.Field
8

    
9
import org.txm.rcp.swt.widget.parameters.*
10
import org.txm.macro.edition.BuildFacsEditions;
11
import org.txm.macro.edition.RemoveTag;
12
import org.txm.objects.*
13
import org.txm.searchengine.cqp.corpus.*;
14
import org.w3c.dom.*
15
import org.txm.utils.xml.DomUtils
16
import org.txm.Toolbox
17

    
18
import org.w3c.dom.Document;
19
import org.xml.sax.SAXException;
20
import org.txm.rcp.commands.*
21

    
22
import javax.xml.parsers.*;
23
import javax.xml.transform.*;
24
import javax.xml.transform.dom.DOMSource;
25
import javax.xml.transform.stream.StreamResult;
26

    
27
println "Corpus view selection is: "+corpusViewSelection
28
if (!(corpusViewSelection instanceof MainCorpus)) {
29
        println "This marcro works with a MainCorpus selection. Aborting"
30
        return;
31
}
32
def corpus = corpusViewSelection
33
debug = false
34
def editionName = corpus.getDefaultEdition()
35

    
36
// PARAMETERS
37
@Field @Option(name="wordsPerPage", usage="Number of words per page", widget="Integer", required=true, def="500")
38
int wordsPerPage
39
if (!ParametersDialog.open(this)) return;
40
// END OF PARAMETERS
41

    
42
File binDirectory = corpus.getBase().getBaseDirectory()
43
File txmDirectory = new File(binDirectory, "txm/"+corpus.getName().toUpperCase())
44

    
45
File HTMLDirectory = new File(binDirectory, "HTML")
46
File newEditionDirectory = new File(HTMLDirectory, corpus.getName().toUpperCase()+"/"+editionName)
47

    
48
BaseParameters parameters = corpus.getBase().params
49

    
50
if (!HTMLDirectory.exists()) {
51
        println "ERROR: can't find this corpus 'HTML' directory: $HTMLDirectory. Aborting"
52
        return false;
53
}
54

    
55
File workDirectory = txmDirectory
56
if (!workDirectory.exists()) {
57
        println "XML-TXM directory ($txmDirectory) not found. Using XML tokenized directory instead: "+tokenizedDirectory
58
        workDirectory = tokenizedDirectory
59
}
60
if (!workDirectory.exists()) {
61
        println "XML tokenized directory not found: "+tokenizedDirectory
62
        println "Aborting."
63
        return false
64
}
65

    
66
println "Work directory=$workDirectory"
67

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

    
84
//2- fix import.xml
85
println "** Updating corpus configuration..."
86
// for edition list
87
def corpusElem = parameters.getCorpusElement()
88
parameters.addEditionDefinition(corpusElem, editionName, "groovy", "FacsEditionBuilderMacro");
89

    
90
//1- create HTML files
91
println "** Building new edition HTML files..."
92

    
93
println " Creating edition $editionName directory: $newEditionDirectory"
94
newEditionDirectory.mkdir()
95
for (def xmlFile : workDirectory.listFiles()) {
96
        if (xmlFile.isHidden() || xmlFile.isDirectory()) continue // ignore
97
        String txtname = xmlFile.getName()
98
        int idx = txtname.lastIndexOf(".")
99
        if (idx > 0) txtname = txtname.substring(0,idx)
100

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

    
118
        printDOM(parameters.root.getOwnerDocument())
119

    
120
//3- Save and reload the corpus
121
println " Saving corpus configuration..."
122
File paramFile = new File(binDirectory, "import.xml");
123
DomUtils.save(parameters.root.getOwnerDocument(), paramFile);
124

    
125

    
126
//4- Reload Corpora
127
Toolbox.restart();
128
monitor.syncExec(new Runnable() {
129
                        public void run() {
130
                                println "RELOAD VIEWS"
131
                                RestartTXM.reloadViews();
132
                                println "RELOAD VIEWS2"
133
                        }
134
                });
135

    
136
//5- Done
137
println "New edition created. Be sure to copy images files in $newEditionDirectory"
138

    
139
printDOM(parameters.root.getOwnerDocument())
140

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