Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / oriflamms / prepare / Project2XTZMacro.groovy @ 499

History | View | Annotate | Download (10.3 kB)

1
package org.txm.macro.oriflamms.prepare;
2

    
3
import org.kohsuke.args4j.*
4

    
5
import groovy.transform.Field
6

    
7
import org.txm.objects.BaseParameters;
8
import org.txm.rcp.swt.widget.parameters.*
9
import org.txm.*
10
import org.txm.utils.*
11
import org.txm.importer.*
12
import org.txm.utils.logger.*
13
import org.txm.utils.xml.UpdateXSLParameters
14
import org.txm.utils.zip.Zip;
15

    
16
File oriflammsMacroDirectory = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME), "scripts/macro/org/txm/macro/oriflamms/prepare")
17
if (!oriflammsMacroDirectory.exists()) {
18
        println "Oriflamms macro directory not found: $oriflammsMacroDirectory. Aborting"
19
        return;
20
}
21
File wFrontXSLFile = new File(oriflammsMacroDirectory, "txm-front-teioriflammsw-xtz.xsl")
22
if (!wFrontXSLFile.exists()) {
23
        println "Oriflamms to XML-XTZ front XSL file is missing: $wFrontXSLFile. Aborting"
24
        return;
25
}
26
File cFrontXSLFile = new File(oriflammsMacroDirectory, "txm-front-teioriflammsc-xtz.xsl")
27
if (!cFrontXSLFile.exists()) {
28
        println "Oriflamms to XML-XTZ front XSL file is missing: $cFrontXSLFile. Aborting"
29
        return;
30
}
31

    
32
File cSplitXSLFile = new File(oriflammsMacroDirectory, "1-oriflamms-split-surfaces.xsl")
33
if (!cSplitXSLFile.exists()) {
34
        println "Oriflamms to XML-XTZ split XSL file is missing: $cSplitXSLFile. Aborting"
35
        return;
36
}
37

    
38
File editionXSLFile1 = new File(oriflammsMacroDirectory, "1-default-html.xsl")
39
if (!editionXSLFile1.exists()) {
40
        println "Oriflamms to XML-XTZ edition XSL file is missing: $editionXSLFile1."
41
        return;
42
}
43
File editionXSLFile2 = new File(oriflammsMacroDirectory, "2-default-pager.xsl")
44
if (!editionXSLFile2.exists()) {
45
        println "Oriflamms to XML-XTZ edition XSL file is missing: $editionXSLFile2."
46
        return;
47
}
48
File editionXSLFile3 = new File(oriflammsMacroDirectory, "3-facsimile-pager.xsl")
49
if (!editionXSLFile3.exists()) {
50
        println "Oriflamms to XML-XTZ edition XSL file is missing: $editionXSLFile3."
51
        return;
52
}
53
File cssDirectory = new File(oriflammsMacroDirectory, "css")
54
if (!cssDirectory.exists()) {
55
        println "Oriflamms css directory is missing: $cssDirectory."
56
        return;
57
}
58
File jsDirectory = new File(oriflammsMacroDirectory, "js")
59
if (!jsDirectory.exists()) {
60
        println "Oriflamms js directory is missing: $jsDirectory."
61
        return;
62
}
63
File imagesDirectory = new File(oriflammsMacroDirectory, "images")
64
if (!imagesDirectory.exists()) {
65
        println "Oriflamms images directory is missing: $imagesDirectory."
66
        return;
67
}
68

    
69
@Field @Option(name="projectDirectory", usage="Oriflamms' project directory", widget="Folder", required=true, def="project")
70
File projectDirectory
71

    
72
if (!ParametersDialog.open(this)) return;
73

    
74
File textDirectory = new File(projectDirectory, "texts")
75

    
76
File txmDirectory = new File(projectDirectory, "txm");
77
if (txmDirectory.exists()) txmDirectory.deleteDir();
78
txmDirectory.mkdir()
79
if (!txmDirectory.exists()) {
80
        println "Error: the 'txm' directory could not be created: $txmDirectory. Aborting."
81
        return
82
}
83

    
84
File wDirectory = null
85
File cDirectory = null
86
File wFile = null
87
File cFile = null
88

    
89
def xmlFiles = textDirectory.listFiles()
90
if (xmlFiles == null) return;
91

    
92
for (File xmlFile : xmlFiles) {
93
        if (xmlFile.getName().endsWith("-w.xml")) {
94
                String name = xmlFile.getName().substring(0, xmlFile.getName().indexOf(".xml"))
95
                wDirectory = new File(txmDirectory, name)
96
                wFile = xmlFile
97
        } else if (xmlFile.getName().endsWith("-c.xml")) {
98
                String name = xmlFile.getName().substring(0, xmlFile.getName().indexOf(".xml"))
99
                cDirectory = new File(txmDirectory, name)
100
                cFile = xmlFile
101
        }
102
}
103

    
104
if (wDirectory == null) {
105
        println "The Word corpus XML file was not found in $textDirectory. Aborting."
106
        return
107
}
108
if (cDirectory == null) {
109
        println "The Letter corpus XML file was not found in $textDirectory. Aborting."
110
        return
111
}
112

    
113
//Create XML-XTZ source directories
114
wDirectory.mkdirs()
115
cDirectory.mkdirs()
116

    
117
// Copy XML files and split character XML file
118
FileCopy.copy(wFile, new File(wDirectory, wFile.getName()));
119

    
120
ApplyXsl2 builder = new ApplyXsl2(cSplitXSLFile);
121
def xslParams = ["output-directory":cDirectory.getAbsoluteFile().toURI().toString()];
122
for (String name : xslParams.keySet()) builder.setParam(name, xslParams.get(name));
123
if (!builder.process(cFile, null)) {
124
        println "Error: fail to split $cFile"
125
        return false
126
}
127
if (!ApplyXsl2.processImportSources(cFrontXSLFile, cDirectory.listFiles(), [:])) {
128
        println "Error: fail to apply front XSL with $cDirectory files"
129
        return false
130
}
131
// INJECT ontologies 
132

    
133
println "Injecting ontologies..."
134
for (File f : cDirectory.listFiles()) {
135
        if (f.getName().startsWith(cDirectory.getName())) {
136
                OntologiesProjection cp = new OntologiesProjection(f, projectDirectory);
137
                File outputFile = new File(cDirectory, "temp.xml")
138
                cp.process(outputFile)
139
                if (outputFile.exists() && f.delete() && outputFile.renameTo(f)) {
140
                
141
                } else {
142
                        println "Failed to replace XML file $f with $outputFile"
143
                        return;
144
                }
145
        }
146
}
147

    
148
// INJECT word's coordinates
149
println "Injecting coordinates..."
150
File xmlFile = new File(wDirectory, wFile.getName())
151
File img_links_directory = new File(projectDirectory, "img_links")
152
File zones_directory = new File(projectDirectory, "zones")
153
File outputFile = new File(wDirectory, "temp.xml")
154
CoordsProjection cp = new CoordsProjection(xmlFile, img_links_directory, zones_directory, "w");
155
if (cp.process(outputFile)) {
156
        if (outputFile.exists() && xmlFile.delete() && outputFile.renameTo(xmlFile)) {
157
                
158
        } else {
159
                println "Failed to replace XML file $xmlFile with $outputFile"
160
                return;
161
        }
162
} else {
163
        println "Coordinates injection failed. Aborting"
164
        return;
165
}
166

    
167
// Create XSL directories
168

    
169
File wXSLDirectory = new File(wDirectory, "xsl")
170
File cXSLDirectory = new File(cDirectory, "xsl")
171

    
172
//File cSplitXSLDirectory = new File(cXSLDirectory, "1-split-merge")
173
//cSplitXSLDirectory.mkdirs()
174

    
175
File wFrontXSLDirectory = new File(wXSLDirectory, "2-front")
176
//File cFrontXSLDirectory = new File(cXSLDirectory, "2-front")
177
wFrontXSLDirectory.mkdirs()
178
//cFrontXSLDirectory.mkdirs()
179

    
180
// Copy Split XSL file
181
//File newCSplitXSLFile = new File(cSplitXSLDirectory, cSplitXSLFile.getName())
182
//FileCopy.copy(cSplitXSLFile, newCSplitXSLFile);
183

    
184
// Copy Front XSL file
185
File newWFrontXSLFile = new File(wFrontXSLDirectory, wFrontXSLFile.getName())
186
//File newCFrontXSLFile = new File(cFrontXSLDirectory, cFrontXSLFile.getName())
187
FileCopy.copy(wFrontXSLFile, newWFrontXSLFile);
188
//FileCopy.copy(cFrontXSLFile, newCFrontXSLFile);
189

    
190
// Copy edition XSL file
191
File wEditionXSLDirectory = new File(wXSLDirectory, "4-edition")
192
File cEditionXSLDirectory = new File(cXSLDirectory, "4-edition")
193
wEditionXSLDirectory.mkdirs()
194
cEditionXSLDirectory.mkdirs()
195
File newWEditionXSLFile1 = new File(wEditionXSLDirectory, editionXSLFile1.getName())
196
File newCEditionXSLFile1 = new File(cEditionXSLDirectory, editionXSLFile1.getName())
197
FileCopy.copy(editionXSLFile1, newWEditionXSLFile1);
198
FileCopy.copy(editionXSLFile1, newCEditionXSLFile1);
199
File newWEditionXSLFile2 = new File(wEditionXSLDirectory, editionXSLFile2.getName())
200
File newCEditionXSLFile2 = new File(cEditionXSLDirectory, editionXSLFile2.getName())
201
FileCopy.copy(editionXSLFile2, newWEditionXSLFile2);
202
FileCopy.copy(editionXSLFile2, newCEditionXSLFile2);
203
File newWEditionXSLFile3 = new File(wEditionXSLDirectory, editionXSLFile3.getName())
204
File newCEditionXSLFile3 = new File(cEditionXSLDirectory, editionXSLFile3.getName())
205
FileCopy.copy(editionXSLFile3, newWEditionXSLFile3);
206
FileCopy.copy(editionXSLFile3, newCEditionXSLFile3);
207

    
208
//patch XSL files with image directory path and set the 'word-element' xsl param
209
File projectImgDirectory = new File(projectDirectory, "img")
210
def parameters = ["image-directory":projectImgDirectory.getAbsolutePath(), "word-element":"w"]
211
println "update $newWEditionXSLFile3 with $parameters"
212
UpdateXSLParameters p = new UpdateXSLParameters(newWEditionXSLFile3);
213
if (!p.process(parameters)) {
214
        println "Fail to patch $newWEditionXSLFile3"
215
        return
216
}
217
parameters = ["image-directory":projectImgDirectory.getAbsolutePath(), "word-element":"c"]
218
println "update $newCEditionXSLFile3 with $parameters"
219
UpdateXSLParameters p2 = new UpdateXSLParameters(newCEditionXSLFile3);
220
if (!p2.process(parameters)) {
221
        println "Fail to patch $newCEditionXSLFile3"
222
        return
223
}
224

    
225
// Copy js and images directories
226
File wCSSDirectory =  new File(wDirectory, cssDirectory.getName())
227
wCSSDirectory.mkdir()
228
File wJsDirectory =  new File(wDirectory, jsDirectory.getName())
229
wJsDirectory.mkdir()
230
File wImagesDirectory =  new File(wDirectory, imagesDirectory.getName())
231
wImagesDirectory.mkdir()
232
File cCSSDirectory =  new File(cDirectory, cssDirectory.getName())
233
cCSSDirectory.mkdir()
234
File cJsDirectory =  new File(cDirectory, jsDirectory.getName())
235
cJsDirectory.mkdir()
236
File cImagesDirectory =  new File(cDirectory, imagesDirectory.getName())
237
cImagesDirectory.mkdir()
238
FileCopy.copyFiles(cssDirectory, wCSSDirectory);
239
FileCopy.copyFiles(jsDirectory, wJsDirectory);
240
FileCopy.copyFiles(imagesDirectory, wImagesDirectory);
241
FileCopy.copyFiles(cssDirectory, cCSSDirectory);
242
FileCopy.copyFiles(jsDirectory, cJsDirectory);
243
FileCopy.copyFiles(imagesDirectory, cImagesDirectory);
244

    
245
// Prepare import.xml files
246
File wImportXMLFile = new File(wDirectory, "import.xml")
247
File cImportXMLFile = new File(cDirectory, "import.xml")
248

    
249
BaseParameters.createEmptyParams(wImportXMLFile, AsciiUtils.buildId(wDirectory.getName()).toUpperCase())
250
BaseParameters wParams = new BaseParameters(wImportXMLFile)
251
wParams.load()
252
wParams.setSkipTokenization(true);
253
wParams.setWordElement("w")
254
wParams.setDoAnnotation(false)
255
wParams.setAnnotationLang("fr")
256
wParams.setWordsPerPage(9999999)
257
wParams.setTextualPlans("", "note", "teiHeader,facsimile","pb,cb,lb")
258
wParams.getCorpusElement().setAttribute("font", "Junicode");
259
wParams.getEditionsElement(wParams.getCorpusElement()).setAttribute("default", "default,facsimile")
260
wParams.getCorpusElement().setAttribute("name", AsciiUtils.buildId(wDirectory.getName()).toUpperCase());
261
wParams.save();
262

    
263
BaseParameters.createEmptyParams(cImportXMLFile, AsciiUtils.buildId(cDirectory.getName()).toUpperCase())
264
BaseParameters cParams = new BaseParameters(cImportXMLFile)
265
cParams.load()
266
cParams.setSkipTokenization(true);
267
cParams.setWordElement("c")
268
cParams.setDoAnnotation(false)
269
cParams.setAnnotationLang("fr")
270
cParams.setWordsPerPage(9999999)
271
cParams.setTextualPlans("", "note", "teiHeader,facsimile","pb,cb,lb")
272
cParams.getCorpusElement().setAttribute("font", "Junicode");
273
cParams.getEditionsElement(cParams.getCorpusElement()).setAttribute("default", "default,facsimile")
274
cParams.getCorpusElement().setAttribute("name", AsciiUtils.buildId(cDirectory.getName()).toUpperCase());
275
cParams.save();