Statistics
| Revision:

root / tmp / org.txm.oriflamms.rcp / groovy / org / txm / macro / oriflamms / prepare / Project2XTZMacro.groovy @ 1094

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.utils.io.*
12
import org.txm.scripts.importer.*
13
import org.txm.utils.logger.*
14
import org.txm.utils.xml.UpdateXSLParameters
15
import org.txm.utils.zip.Zip;
16

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
168
// Create XSL directories
169

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

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

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

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

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

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

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

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

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

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

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