Statistics
| Revision:

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

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.utils.io.*
10
import org.txm.scripts.importer.*
11
import org.txm.utils.logger.*
12
import org.txm.utils.zip.Zip;
13

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

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

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

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

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

    
62
projectDirectory.mkdir()
63

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

    
69
println "Oriflamms project directory: $projectDirectory"
70

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

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

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

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

    
108
try {
109

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

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

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

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

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

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

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

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

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

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

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