-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-sdlxliff-internal-files.groovy
60 lines (46 loc) · 1.8 KB
/
extract-sdlxliff-internal-files.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* :name=SDLXLIFF Extract :description=Extracts internal files from sdlxliff files
*
* @author Briac Pilpré
* @date 2017-06-15
* @version 0.2
*/
import static javax.swing.JOptionPane.*
def prop = project.projectProperties
if (!prop) {
final def title = 'SDLXLIFF Extract';
final def msg = 'Please try again after you open a project.';
showMessageDialog(null, msg, title, INFORMATION_MESSAGE);
return;
}
outputDir = new File(prop.projectRoot, 'internal-files');
sourceDir = new File(prop.projectRoot, 'source');
outputDir.mkdirs();
class SDLXLIFFFilter implements FilenameFilter {
public boolean accept(File f, String filename) {
return filename.endsWith("sdlxliff") || filename.endsWith("xdliff");
}
}
sourceDir.list(new SDLXLIFFFilter()).each { xliffFile ->
console.println("Processing ${xliffFile}...");
outputFileName = xliffFile.replaceAll(/\.\w+$/, '');
def xliff = new XmlSlurper().parse(new File(sourceDir, xliffFile));
def fileForm = xliff.file.header.reference['internal-file'].@form;
if (fileForm != 'base64') {
console.println("Internal file is not a base64 file (" + fileForm + ")");
return;
}
def base64 = xliff.file.header.reference['internal-file'].text();
def outputFile = new File(outputDir, outputFileName + ".zip");
console.println(" -> ${outputFile} extracted.");
outputFile.setBytes(base64.decodeBase64());
def zipFile = new java.util.zip.ZipFile(outputFile);
zipFile.entries().findAll { !it.directory }.each {
def extractedFile = outputFileName + "_" + it.toString();
console.println(" -> ${extractedFile}");
new File(outputDir, extractedFile).text = zipFile.getInputStream(it).text
}
zipFile.close();
outputFile.delete();
console.println("\n");
}
return true;