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 479 mdecorde
package org.txm.macro.oriflamms.prepare;
2 479 mdecorde
3 479 mdecorde
import org.kohsuke.args4j.*
4 479 mdecorde
5 479 mdecorde
import groovy.transform.Field
6 479 mdecorde
7 479 mdecorde
import org.txm.objects.BaseParameters;
8 499 mdecorde
import org.txm.rcp.swt.widget.parameters.*
9 479 mdecorde
import org.txm.*
10 479 mdecorde
import org.txm.utils.*
11 479 mdecorde
import org.txm.importer.*
12 479 mdecorde
import org.txm.utils.logger.*
13 479 mdecorde
import org.txm.utils.xml.UpdateXSLParameters
14 479 mdecorde
import org.txm.utils.zip.Zip;
15 479 mdecorde
16 479 mdecorde
File oriflammsMacroDirectory = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME), "scripts/macro/org/txm/macro/oriflamms/prepare")
17 479 mdecorde
if (!oriflammsMacroDirectory.exists()) {
18 479 mdecorde
        println "Oriflamms macro directory not found: $oriflammsMacroDirectory. Aborting"
19 479 mdecorde
        return;
20 479 mdecorde
}
21 479 mdecorde
File wFrontXSLFile = new File(oriflammsMacroDirectory, "txm-front-teioriflammsw-xtz.xsl")
22 479 mdecorde
if (!wFrontXSLFile.exists()) {
23 479 mdecorde
        println "Oriflamms to XML-XTZ front XSL file is missing: $wFrontXSLFile. Aborting"
24 479 mdecorde
        return;
25 479 mdecorde
}
26 479 mdecorde
File cFrontXSLFile = new File(oriflammsMacroDirectory, "txm-front-teioriflammsc-xtz.xsl")
27 479 mdecorde
if (!cFrontXSLFile.exists()) {
28 479 mdecorde
        println "Oriflamms to XML-XTZ front XSL file is missing: $cFrontXSLFile. Aborting"
29 479 mdecorde
        return;
30 479 mdecorde
}
31 479 mdecorde
32 479 mdecorde
File cSplitXSLFile = new File(oriflammsMacroDirectory, "1-oriflamms-split-surfaces.xsl")
33 479 mdecorde
if (!cSplitXSLFile.exists()) {
34 479 mdecorde
        println "Oriflamms to XML-XTZ split XSL file is missing: $cSplitXSLFile. Aborting"
35 479 mdecorde
        return;
36 479 mdecorde
}
37 479 mdecorde
38 479 mdecorde
File editionXSLFile1 = new File(oriflammsMacroDirectory, "1-default-html.xsl")
39 479 mdecorde
if (!editionXSLFile1.exists()) {
40 479 mdecorde
        println "Oriflamms to XML-XTZ edition XSL file is missing: $editionXSLFile1."
41 479 mdecorde
        return;
42 479 mdecorde
}
43 479 mdecorde
File editionXSLFile2 = new File(oriflammsMacroDirectory, "2-default-pager.xsl")
44 479 mdecorde
if (!editionXSLFile2.exists()) {
45 479 mdecorde
        println "Oriflamms to XML-XTZ edition XSL file is missing: $editionXSLFile2."
46 479 mdecorde
        return;
47 479 mdecorde
}
48 479 mdecorde
File editionXSLFile3 = new File(oriflammsMacroDirectory, "3-facsimile-pager.xsl")
49 479 mdecorde
if (!editionXSLFile3.exists()) {
50 479 mdecorde
        println "Oriflamms to XML-XTZ edition XSL file is missing: $editionXSLFile3."
51 479 mdecorde
        return;
52 479 mdecorde
}
53 479 mdecorde
File cssDirectory = new File(oriflammsMacroDirectory, "css")
54 479 mdecorde
if (!cssDirectory.exists()) {
55 479 mdecorde
        println "Oriflamms css directory is missing: $cssDirectory."
56 479 mdecorde
        return;
57 479 mdecorde
}
58 479 mdecorde
File jsDirectory = new File(oriflammsMacroDirectory, "js")
59 479 mdecorde
if (!jsDirectory.exists()) {
60 479 mdecorde
        println "Oriflamms js directory is missing: $jsDirectory."
61 479 mdecorde
        return;
62 479 mdecorde
}
63 479 mdecorde
File imagesDirectory = new File(oriflammsMacroDirectory, "images")
64 479 mdecorde
if (!imagesDirectory.exists()) {
65 479 mdecorde
        println "Oriflamms images directory is missing: $imagesDirectory."
66 479 mdecorde
        return;
67 479 mdecorde
}
68 479 mdecorde
69 479 mdecorde
@Field @Option(name="projectDirectory", usage="Oriflamms' project directory", widget="Folder", required=true, def="project")
70 479 mdecorde
File projectDirectory
71 479 mdecorde
72 479 mdecorde
if (!ParametersDialog.open(this)) return;
73 479 mdecorde
74 479 mdecorde
File textDirectory = new File(projectDirectory, "texts")
75 479 mdecorde
76 479 mdecorde
File txmDirectory = new File(projectDirectory, "txm");
77 479 mdecorde
if (txmDirectory.exists()) txmDirectory.deleteDir();
78 479 mdecorde
txmDirectory.mkdir()
79 479 mdecorde
if (!txmDirectory.exists()) {
80 479 mdecorde
        println "Error: the 'txm' directory could not be created: $txmDirectory. Aborting."
81 479 mdecorde
        return
82 479 mdecorde
}
83 479 mdecorde
84 479 mdecorde
File wDirectory = null
85 479 mdecorde
File cDirectory = null
86 479 mdecorde
File wFile = null
87 479 mdecorde
File cFile = null
88 479 mdecorde
89 479 mdecorde
def xmlFiles = textDirectory.listFiles()
90 479 mdecorde
if (xmlFiles == null) return;
91 479 mdecorde
92 479 mdecorde
for (File xmlFile : xmlFiles) {
93 479 mdecorde
        if (xmlFile.getName().endsWith("-w.xml")) {
94 479 mdecorde
                String name = xmlFile.getName().substring(0, xmlFile.getName().indexOf(".xml"))
95 479 mdecorde
                wDirectory = new File(txmDirectory, name)
96 479 mdecorde
                wFile = xmlFile
97 479 mdecorde
        } else if (xmlFile.getName().endsWith("-c.xml")) {
98 479 mdecorde
                String name = xmlFile.getName().substring(0, xmlFile.getName().indexOf(".xml"))
99 479 mdecorde
                cDirectory = new File(txmDirectory, name)
100 479 mdecorde
                cFile = xmlFile
101 479 mdecorde
        }
102 479 mdecorde
}
103 479 mdecorde
104 479 mdecorde
if (wDirectory == null) {
105 479 mdecorde
        println "The Word corpus XML file was not found in $textDirectory. Aborting."
106 479 mdecorde
        return
107 479 mdecorde
}
108 479 mdecorde
if (cDirectory == null) {
109 479 mdecorde
        println "The Letter corpus XML file was not found in $textDirectory. Aborting."
110 479 mdecorde
        return
111 479 mdecorde
}
112 479 mdecorde
113 479 mdecorde
//Create XML-XTZ source directories
114 479 mdecorde
wDirectory.mkdirs()
115 479 mdecorde
cDirectory.mkdirs()
116 479 mdecorde
117 479 mdecorde
// Copy XML files and split character XML file
118 479 mdecorde
FileCopy.copy(wFile, new File(wDirectory, wFile.getName()));
119 479 mdecorde
120 479 mdecorde
ApplyXsl2 builder = new ApplyXsl2(cSplitXSLFile);
121 479 mdecorde
def xslParams = ["output-directory":cDirectory.getAbsoluteFile().toURI().toString()];
122 479 mdecorde
for (String name : xslParams.keySet()) builder.setParam(name, xslParams.get(name));
123 479 mdecorde
if (!builder.process(cFile, null)) {
124 479 mdecorde
        println "Error: fail to split $cFile"
125 479 mdecorde
        return false
126 479 mdecorde
}
127 479 mdecorde
if (!ApplyXsl2.processImportSources(cFrontXSLFile, cDirectory.listFiles(), [:])) {
128 479 mdecorde
        println "Error: fail to apply front XSL with $cDirectory files"
129 479 mdecorde
        return false
130 479 mdecorde
}
131 479 mdecorde
// INJECT ontologies
132 479 mdecorde
133 479 mdecorde
println "Injecting ontologies..."
134 479 mdecorde
for (File f : cDirectory.listFiles()) {
135 479 mdecorde
        if (f.getName().startsWith(cDirectory.getName())) {
136 479 mdecorde
                OntologiesProjection cp = new OntologiesProjection(f, projectDirectory);
137 479 mdecorde
                File outputFile = new File(cDirectory, "temp.xml")
138 479 mdecorde
                cp.process(outputFile)
139 479 mdecorde
                if (outputFile.exists() && f.delete() && outputFile.renameTo(f)) {
140 479 mdecorde
141 479 mdecorde
                } else {
142 479 mdecorde
                        println "Failed to replace XML file $f with $outputFile"
143 479 mdecorde
                        return;
144 479 mdecorde
                }
145 479 mdecorde
        }
146 479 mdecorde
}
147 479 mdecorde
148 479 mdecorde
// INJECT word's coordinates
149 479 mdecorde
println "Injecting coordinates..."
150 479 mdecorde
File xmlFile = new File(wDirectory, wFile.getName())
151 479 mdecorde
File img_links_directory = new File(projectDirectory, "img_links")
152 479 mdecorde
File zones_directory = new File(projectDirectory, "zones")
153 479 mdecorde
File outputFile = new File(wDirectory, "temp.xml")
154 479 mdecorde
CoordsProjection cp = new CoordsProjection(xmlFile, img_links_directory, zones_directory, "w");
155 479 mdecorde
if (cp.process(outputFile)) {
156 479 mdecorde
        if (outputFile.exists() && xmlFile.delete() && outputFile.renameTo(xmlFile)) {
157 479 mdecorde
158 479 mdecorde
        } else {
159 479 mdecorde
                println "Failed to replace XML file $xmlFile with $outputFile"
160 479 mdecorde
                return;
161 479 mdecorde
        }
162 479 mdecorde
} else {
163 479 mdecorde
        println "Coordinates injection failed. Aborting"
164 479 mdecorde
        return;
165 479 mdecorde
}
166 479 mdecorde
167 479 mdecorde
// Create XSL directories
168 479 mdecorde
169 479 mdecorde
File wXSLDirectory = new File(wDirectory, "xsl")
170 479 mdecorde
File cXSLDirectory = new File(cDirectory, "xsl")
171 479 mdecorde
172 479 mdecorde
//File cSplitXSLDirectory = new File(cXSLDirectory, "1-split-merge")
173 479 mdecorde
//cSplitXSLDirectory.mkdirs()
174 479 mdecorde
175 479 mdecorde
File wFrontXSLDirectory = new File(wXSLDirectory, "2-front")
176 479 mdecorde
//File cFrontXSLDirectory = new File(cXSLDirectory, "2-front")
177 479 mdecorde
wFrontXSLDirectory.mkdirs()
178 479 mdecorde
//cFrontXSLDirectory.mkdirs()
179 479 mdecorde
180 479 mdecorde
// Copy Split XSL file
181 479 mdecorde
//File newCSplitXSLFile = new File(cSplitXSLDirectory, cSplitXSLFile.getName())
182 479 mdecorde
//FileCopy.copy(cSplitXSLFile, newCSplitXSLFile);
183 479 mdecorde
184 479 mdecorde
// Copy Front XSL file
185 479 mdecorde
File newWFrontXSLFile = new File(wFrontXSLDirectory, wFrontXSLFile.getName())
186 479 mdecorde
//File newCFrontXSLFile = new File(cFrontXSLDirectory, cFrontXSLFile.getName())
187 479 mdecorde
FileCopy.copy(wFrontXSLFile, newWFrontXSLFile);
188 479 mdecorde
//FileCopy.copy(cFrontXSLFile, newCFrontXSLFile);
189 479 mdecorde
190 479 mdecorde
// Copy edition XSL file
191 479 mdecorde
File wEditionXSLDirectory = new File(wXSLDirectory, "4-edition")
192 479 mdecorde
File cEditionXSLDirectory = new File(cXSLDirectory, "4-edition")
193 479 mdecorde
wEditionXSLDirectory.mkdirs()
194 479 mdecorde
cEditionXSLDirectory.mkdirs()
195 479 mdecorde
File newWEditionXSLFile1 = new File(wEditionXSLDirectory, editionXSLFile1.getName())
196 479 mdecorde
File newCEditionXSLFile1 = new File(cEditionXSLDirectory, editionXSLFile1.getName())
197 479 mdecorde
FileCopy.copy(editionXSLFile1, newWEditionXSLFile1);
198 479 mdecorde
FileCopy.copy(editionXSLFile1, newCEditionXSLFile1);
199 479 mdecorde
File newWEditionXSLFile2 = new File(wEditionXSLDirectory, editionXSLFile2.getName())
200 479 mdecorde
File newCEditionXSLFile2 = new File(cEditionXSLDirectory, editionXSLFile2.getName())
201 479 mdecorde
FileCopy.copy(editionXSLFile2, newWEditionXSLFile2);
202 479 mdecorde
FileCopy.copy(editionXSLFile2, newCEditionXSLFile2);
203 479 mdecorde
File newWEditionXSLFile3 = new File(wEditionXSLDirectory, editionXSLFile3.getName())
204 479 mdecorde
File newCEditionXSLFile3 = new File(cEditionXSLDirectory, editionXSLFile3.getName())
205 479 mdecorde
FileCopy.copy(editionXSLFile3, newWEditionXSLFile3);
206 479 mdecorde
FileCopy.copy(editionXSLFile3, newCEditionXSLFile3);
207 479 mdecorde
208 479 mdecorde
//patch XSL files with image directory path and set the 'word-element' xsl param
209 479 mdecorde
File projectImgDirectory = new File(projectDirectory, "img")
210 479 mdecorde
def parameters = ["image-directory":projectImgDirectory.getAbsolutePath(), "word-element":"w"]
211 479 mdecorde
println "update $newWEditionXSLFile3 with $parameters"
212 479 mdecorde
UpdateXSLParameters p = new UpdateXSLParameters(newWEditionXSLFile3);
213 479 mdecorde
if (!p.process(parameters)) {
214 479 mdecorde
        println "Fail to patch $newWEditionXSLFile3"
215 479 mdecorde
        return
216 479 mdecorde
}
217 479 mdecorde
parameters = ["image-directory":projectImgDirectory.getAbsolutePath(), "word-element":"c"]
218 479 mdecorde
println "update $newCEditionXSLFile3 with $parameters"
219 479 mdecorde
UpdateXSLParameters p2 = new UpdateXSLParameters(newCEditionXSLFile3);
220 479 mdecorde
if (!p2.process(parameters)) {
221 479 mdecorde
        println "Fail to patch $newCEditionXSLFile3"
222 479 mdecorde
        return
223 479 mdecorde
}
224 479 mdecorde
225 479 mdecorde
// Copy js and images directories
226 479 mdecorde
File wCSSDirectory =  new File(wDirectory, cssDirectory.getName())
227 479 mdecorde
wCSSDirectory.mkdir()
228 479 mdecorde
File wJsDirectory =  new File(wDirectory, jsDirectory.getName())
229 479 mdecorde
wJsDirectory.mkdir()
230 479 mdecorde
File wImagesDirectory =  new File(wDirectory, imagesDirectory.getName())
231 479 mdecorde
wImagesDirectory.mkdir()
232 479 mdecorde
File cCSSDirectory =  new File(cDirectory, cssDirectory.getName())
233 479 mdecorde
cCSSDirectory.mkdir()
234 479 mdecorde
File cJsDirectory =  new File(cDirectory, jsDirectory.getName())
235 479 mdecorde
cJsDirectory.mkdir()
236 479 mdecorde
File cImagesDirectory =  new File(cDirectory, imagesDirectory.getName())
237 479 mdecorde
cImagesDirectory.mkdir()
238 479 mdecorde
FileCopy.copyFiles(cssDirectory, wCSSDirectory);
239 479 mdecorde
FileCopy.copyFiles(jsDirectory, wJsDirectory);
240 479 mdecorde
FileCopy.copyFiles(imagesDirectory, wImagesDirectory);
241 479 mdecorde
FileCopy.copyFiles(cssDirectory, cCSSDirectory);
242 479 mdecorde
FileCopy.copyFiles(jsDirectory, cJsDirectory);
243 479 mdecorde
FileCopy.copyFiles(imagesDirectory, cImagesDirectory);
244 479 mdecorde
245 479 mdecorde
// Prepare import.xml files
246 479 mdecorde
File wImportXMLFile = new File(wDirectory, "import.xml")
247 479 mdecorde
File cImportXMLFile = new File(cDirectory, "import.xml")
248 479 mdecorde
249 479 mdecorde
BaseParameters.createEmptyParams(wImportXMLFile, AsciiUtils.buildId(wDirectory.getName()).toUpperCase())
250 479 mdecorde
BaseParameters wParams = new BaseParameters(wImportXMLFile)
251 479 mdecorde
wParams.load()
252 479 mdecorde
wParams.setSkipTokenization(true);
253 479 mdecorde
wParams.setWordElement("w")
254 479 mdecorde
wParams.setDoAnnotation(false)
255 479 mdecorde
wParams.setAnnotationLang("fr")
256 479 mdecorde
wParams.setWordsPerPage(9999999)
257 479 mdecorde
wParams.setTextualPlans("", "note", "teiHeader,facsimile","pb,cb,lb")
258 479 mdecorde
wParams.getCorpusElement().setAttribute("font", "Junicode");
259 479 mdecorde
wParams.getEditionsElement(wParams.getCorpusElement()).setAttribute("default", "default,facsimile")
260 479 mdecorde
wParams.getCorpusElement().setAttribute("name", AsciiUtils.buildId(wDirectory.getName()).toUpperCase());
261 479 mdecorde
wParams.save();
262 479 mdecorde
263 479 mdecorde
BaseParameters.createEmptyParams(cImportXMLFile, AsciiUtils.buildId(cDirectory.getName()).toUpperCase())
264 479 mdecorde
BaseParameters cParams = new BaseParameters(cImportXMLFile)
265 479 mdecorde
cParams.load()
266 479 mdecorde
cParams.setSkipTokenization(true);
267 479 mdecorde
cParams.setWordElement("c")
268 479 mdecorde
cParams.setDoAnnotation(false)
269 479 mdecorde
cParams.setAnnotationLang("fr")
270 479 mdecorde
cParams.setWordsPerPage(9999999)
271 479 mdecorde
cParams.setTextualPlans("", "note", "teiHeader,facsimile","pb,cb,lb")
272 479 mdecorde
cParams.getCorpusElement().setAttribute("font", "Junicode");
273 479 mdecorde
cParams.getEditionsElement(cParams.getCorpusElement()).setAttribute("default", "default,facsimile")
274 479 mdecorde
cParams.getCorpusElement().setAttribute("name", AsciiUtils.buildId(cDirectory.getName()).toUpperCase());
275 479 mdecorde
cParams.save();