Statistics
| Revision:

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

History | View | Annotate | Download (8 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macro.oriflamms.prepare
3

    
4
import org.kohsuke.args4j.*
5
import groovy.transform.Field
6
import org.txm.rcp.swt.widget.parameters.*
7
import org.txm.*
8
import org.txm.utils.*
9
import org.txm.importer.*
10
import org.txm.utils.logger.*
11
import org.txm.utils.zip.Zip;
12

    
13
@Field @Option(name="xmlFile", usage="Raw XML-TEI Oriflamm", widget="File", required=true, def="file.xml")
14
File xmlFile
15
@Field @Option(name="xslFile", usage="Optional XSL file applied", widget="File", required=false, def="file.xsl")
16
File xslFile
17
@Field @Option(name="imagesDirectory", usage="Optional images directory", widget="Folder", required=false, def="images")
18
File imagesDirectory
19
@Field @Option(name="createArchive", usage="Optional zip the project", widget="Boolean", required=false, def="false")
20
boolean createArchive
21
if (!ParametersDialog.open(this)) return;
22

    
23
if (!xmlFile.exists()) {
24
        println "Could not read input XML input file: $xmlFile"
25
        return;
26
}
27

    
28
File oriflammsMacroDirectory = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME), "scripts/macro/org/txm/macro/oriflamms/prepare")
29
if (!oriflammsMacroDirectory.exists()) {
30
        println "Oriflamms macro directory not found: "+oriflammsMacroDirectory
31
        return;
32
}
33
File xslTokenizer = new File(oriflammsMacroDirectory, "oriflamms-tokenize-words.xsl");
34
File xslPatchLbInWords = new File(oriflammsMacroDirectory,"oriflamms-patch-words-with-lb.xsl");
35
File xslMissingMilestones = new File(oriflammsMacroDirectory, "oriflamms-patch-milestones.xsl");
36
File xslCharactersTokenizer = new File(oriflammsMacroDirectory, "oriflamms-tokenize-chars-1-tag.xsl");
37
File xslCharactersIdentifier = new File(oriflammsMacroDirectory, "oriflamms-tokenize-chars-2-identify.xsl");
38
File xslZones = new File(oriflammsMacroDirectory, "oriflamms-convert-transcriptions-orizones.xsl");
39

    
40
if (!xslTokenizer.exists()  || !xslPatchLbInWords.exists() || !xslMissingMilestones.exists() ||        
41
!xslCharactersTokenizer.exists() || !xslCharactersIdentifier.exists() || !xslZones.exists()) {
42
        println "Could not find one of TXM's XSL file : "+
43
                [xslTokenizer, xslPatchLbInWords, xslMissingMilestones, 
44
                xslCharactersTokenizer, xslCharactersIdentifier, xslZones];
45
                
46
        println ""+[xslTokenizer.exists(), xslPatchLbInWords.exists(), xslMissingMilestones.exists()
47
        , xslCharactersTokenizer.exists(), xslCharactersIdentifier.exists(), xslZones.exists()]
48
        return;
49
}
50

    
51
File xmlFileParentDirectory = xmlFile.getParentFile()
52
String projectName = xmlFile.getName()
53
if (projectName.indexOf(".") > 0) projectName = projectName.substring(0, projectName.indexOf("."))
54
File projectDirectory = new File(xmlFileParentDirectory, projectName)
55
projectDirectory.deleteDir()
56
if (projectDirectory.exists()) {
57
        println "Could not delete previous project directory: $projectDirectory"
58
        return;
59
}
60

    
61
projectDirectory.mkdir()
62

    
63
if (!projectDirectory.exists()) {
64
        println "Could not create project directory: $projectDirectory"
65
        return;
66
}
67

    
68
println "Oriflamms project directory: $projectDirectory"
69

    
70
File xmlFileCopy = new File(projectDirectory, xmlFile.getName())
71
println "Copying XML files: $xmlFile to $projectDirectory"
72
CopyXMLFiles cdf = new CopyXMLFiles(xmlFile)
73
projectDirectory.mkdir()
74
println "Files copied: "+cdf.copy(projectDirectory)
75
if (!xmlFileCopy.exists()) {
76
        println "Could not copy input XML input file: $xmlFile to $xmlFileCopy"
77
        return;
78
}
79

    
80
if (xslFile != null) {
81
        if (xslFile.exists()) {
82
                println "Applying $xslFile to $xmlFileCopy..."
83
                ApplyXsl2 builder = new ApplyXsl2(xslFile);
84
                if (!builder.process(xmlFileCopy, xmlFileCopy)) {
85
                        System.out.println("Failed to process "+xmlFileCopy+" with "+xslFile);
86
                        return;
87
                }
88
        }
89
}
90

    
91
File textsDirectory = new File(projectDirectory, "texts")
92
File imgDirectory = new File(projectDirectory, "img")
93
File img_linksDirectory = new File(projectDirectory, "img_links")
94
File ontologiesDirectory = new File(projectDirectory, "ontologies")
95
File ontologies_linksDirectory = new File(projectDirectory, "ontologies_links")
96
File zonesDirectory = new File(projectDirectory, "zones")
97
textsDirectory.mkdir()
98
imgDirectory.mkdir()
99
img_linksDirectory.mkdir()
100
ontologiesDirectory.mkdir()
101
ontologies_linksDirectory.mkdir()
102
zonesDirectory.mkdir()
103

    
104
File xmlWFile = new File(textsDirectory, projectName+"-w.xml")
105
File xmlWCFile = new File(textsDirectory, projectName+"-c.xml")
106

    
107
try {
108

    
109
println "Applying $xslMissingMilestones to $xmlWFile..."
110
if (monitor != null) monitor.worked(1, "Applying $xslMissingMilestones to $xmlWFile...")
111
builder = new ApplyXsl2(xslMissingMilestones);
112
if (!builder.process(xmlFileCopy, xmlWFile)) {
113
        System.out.println("Failed to process "+xmlWFile+" with "+xslMissingMilestones);
114
        return;
115
}
116

    
117
println "Applying $xslTokenizer to $xmlWFile..."
118
if (monitor != null) monitor.worked(15, "Applying $xslTokenizer to $xmlWFile...")
119
builder = new ApplyXsl2(xslTokenizer);
120
if (!builder.process(xmlWFile, xmlWFile)) {
121
        System.out.println("Failed to process "+xmlFileCopy+" with "+xslTokenizer);
122
        return;
123
}
124

    
125
println "Merging words <w/><w>"
126
if (monitor != null) monitor.worked(15, "Merging words <w/><w>")
127
String content = xmlWFile.getText("UTF-8")
128
content = content.replaceAll("</w><w[^>]*>", "")
129
content = content.replaceAll("</w>\\s*(<milestone[^>]*>)?\\s*(<pb[^>]*>)?\\s*(<cb[^>]*>)?\\s*(<lb[^>]*break=\"no\"[^>]*>)\\s*<w[^>]*>", '$1$2$3$4')
130
try {
131
        def writer = xmlWFile.newWriter("UTF-8")
132
        writer.print(content)
133
        writer.close()
134
} catch (Exception e2) {
135
        println "Error while fixing words: $e2"
136
        return;
137
}
138

    
139
println "Applying $xslPatchLbInWords to $xmlWFile..."
140
if (monitor != null) monitor.worked(15, "Applying $xslPatchLbInWords to $xmlWFile...")
141
builder = new ApplyXsl2(xslPatchLbInWords);
142
if (!builder.process(xmlWFile, xmlWFile)) {
143
        System.out.println("Failed to process "+xmlFileCopy+" with "+xslPatchLbInWords);
144
        return;
145
}
146

    
147
println "Fixing 'id' and 'n' attributes in $xmlWFile..."
148
if (monitor != null) monitor.worked(15, "Fixing 'id' and 'n' attributes in $xmlWFile...")
149
WriteIdAndNAttributes wiana = new WriteIdAndNAttributes(xmlWFile, projectName)
150
File tmp = new File(xmlWFile.getParentFile(), "tmp_"+xmlWFile.getName())
151
if (!wiana.process(tmp)) {
152
        System.out.println("Failed to fix id and n attributes with of $xmlWFile file")
153
        return;
154
} else {
155
        xmlWFile.delete()
156
        tmp.renameTo(xmlWFile)
157
        if (tmp.exists()) {
158
                System.out.println("Fail to replace $xmlWFile with result file $tmp")
159
                return;
160
        }
161
}
162

    
163
println "Applying $xslCharactersTokenizer to $xmlWFile..."
164
if (monitor != null) monitor.worked(15, "Applying $xslCharactersTokenizer to $xmlWFile...")
165
builder = new ApplyXsl2(xslCharactersTokenizer);
166
if (!builder.process(xmlWFile, xmlWCFile)) {
167
        System.out.println("Failed to process "+xmlWFile+" with "+xslCharactersTokenizer);
168
        return;
169
} 
170

    
171
println "Applying $xslCharactersIdentifier to $xmlWCFile..."
172
if (monitor != null) monitor.worked(1, "Applying $xslCharactersIdentifier to $xmlWCFile...")
173
builder = new ApplyXsl2(xslCharactersIdentifier);
174
if (!builder.process(xmlWCFile, xmlWCFile)) {
175
        System.out.println("Failed to process "+xmlWCFile+" with "+xslCharactersIdentifier);
176
        return;
177
}
178

    
179
println "Applying $xslZones to $xmlWFile..."
180
if (monitor != null) monitor.worked(15, "Applying $xslZones to $xmlWFile...")
181
builder = new ApplyXsl2(xslZones);
182
if (!builder.process(xmlWFile, null)) {
183
        System.out.println("Failed to process "+xmlFileCopy+" with "+xslZones);
184
        return;
185
}
186

    
187
if (imagesDirectory.exists() && imagesDirectory.listFiles().size() > 0) {
188
        println "Copying images files from $imagesDirectory to $imgDirectory..."
189
        FileCopy.copyFiles(imagesDirectory, imgDirectory);
190
        def files = imgDirectory.listFiles()
191
        if (files != null) println ""+files.size()+" images copied."
192
}
193

    
194
if (createArchive) {
195
        if (monitor != null) monitor.worked(15, "Building Oriflamms binary project... ")
196
        File zipFile = new File(xmlFileParentDirectory, projectName+".oriflamms")
197
        zipFile.delete()
198
        Zip.compress(projectDirectory, zipFile)
199
        
200
        if (zipFile.exists()) {
201
                println "Project oriflamms exported to $zipFile"
202
                projectDirectory.deleteDir()
203
        } else {
204
                println "Fail to export project $projectDirectory"
205
        }
206
}
207

    
208
} catch (Exception e) {
209
        println "Error while applying a XSL file: "+e
210
        Log.printStackTrace(e)
211
}
212
if (monitor != null) monitor.done()