Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / txt / ChangeEncodingMacro.groovy @ 479

History | View | Annotate | Download (2.5 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21

    
22
package org.txm.macro.txt
23

    
24
import org.kohsuke.args4j.*
25
import groovy.transform.Field
26
import org.txm.rcpapplication.swt.widget.parameters.*
27
import org.txm.utils.CharsetDetector
28

    
29
@Field @Option(name="inputDirectory", usage="Dossier qui contient les fichiers à traiter", widget="Folder", required=true, def="")
30
File inputDirectory
31

    
32
@Field @Option(name="inputEncoding", usage="Input encoding", widget="String", required=false, def="")
33
String inputEncoding
34

    
35
@Field @Option(name="outputEncoding", usage="Output encoding", widget="String", required=true, def="UTF-8")
36
String outputEncoding
37

    
38
if (!ParametersDialog.open(this)) return;
39

    
40
File outDir = new File(inputDirectory, "out") // write result in "out" folder
41
if (outDir.exists()) {
42
        if (!outDir.deleteDir()) {
43
                println "Could not delete directory: $outDir"
44
                return;
45
        }
46
}
47
if (!outDir.mkdir()) {
48
        println "Could not create directory: $outDir"
49
        return;
50
}
51
println "Writing result in : $outDir"
52

    
53
def processFile(File file, File outfile, String fromEncoding, String toutputEncoding) {
54
        String text = file.getText(fromEncoding)
55
        outfile.withWriter(toutputEncoding) {writer -> // read with the "fromEncoding"
56
                writer.write(text) // write with the "toutputEncoding"
57
        }
58
}
59

    
60
try {
61
inputDirectory.eachFile { file ->
62
        if (!file.isHidden() && !file.isDirectory()) {
63
                String e = inputEncoding
64
                if (inputEncoding == null || inputEncoding.length() == 0) {
65
                        CharsetDetector detector = new CharsetDetector(file)
66
                        e = detector.getEncoding()
67
                }
68
                
69
                File outfile = new File(outDir, file.getName())
70
                println "$e -> $outputEncoding : "+outfile.getName()
71
                processFile(file, outfile, e, outputEncoding)
72
        }
73
}
74
} catch (UnsupportedEncodingException e) {
75
        println "ERROR: Unsupported encoding: "+e
76
}