Statistics
| Revision:

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

History | View | Annotate | Download (10.4 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.importer.ApplyXsl2
11
import org.txm.utils.*
12
import org.txm.utils.io.*
13
import org.txm.scripts.importer.*
14
import org.txm.utils.logger.*
15
import org.txm.utils.xml.UpdateXSLParameters
16
import org.txm.utils.zip.Zip;
17

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

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

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

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

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

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

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

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

    
91
def xmlFiles = textDirectory.listFiles(IOUtils.HIDDENFILE_FILTER)
92
if (xmlFiles == null) return;
93

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

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

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

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

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

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

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

    
169
// Create XSL directories
170

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

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

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

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

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

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

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

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

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

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

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