Statistics
| Revision:

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

History | View | Annotate | Download (2.5 kB)

1 187 sjacqu01
// Copyright © 2010-2013 ENS de Lyon.
2 187 sjacqu01
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 187 sjacqu01
// Lyon 2, University of Franche-Comté, University of Nice
4 187 sjacqu01
// Sophia Antipolis, University of Paris 3.
5 187 sjacqu01
//
6 187 sjacqu01
// The TXM platform is free software: you can redistribute it
7 187 sjacqu01
// and/or modify it under the terms of the GNU General Public
8 187 sjacqu01
// License as published by the Free Software Foundation,
9 187 sjacqu01
// either version 2 of the License, or (at your option) any
10 187 sjacqu01
// later version.
11 187 sjacqu01
//
12 187 sjacqu01
// The TXM platform is distributed in the hope that it will be
13 187 sjacqu01
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 187 sjacqu01
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 187 sjacqu01
// PURPOSE. See the GNU General Public License for more
16 187 sjacqu01
// details.
17 187 sjacqu01
//
18 187 sjacqu01
// You should have received a copy of the GNU General
19 187 sjacqu01
// Public License along with the TXM platform. If not, see
20 187 sjacqu01
// http://www.gnu.org/licenses.
21 187 sjacqu01
22 187 sjacqu01
package org.txm.macro.txt
23 187 sjacqu01
24 187 sjacqu01
import org.kohsuke.args4j.*
25 187 sjacqu01
import groovy.transform.Field
26 187 sjacqu01
import org.txm.rcpapplication.swt.widget.parameters.*
27 187 sjacqu01
import org.txm.utils.CharsetDetector
28 187 sjacqu01
29 187 sjacqu01
@Field @Option(name="inputDirectory", usage="Dossier qui contient les fichiers à traiter", widget="Folder", required=true, def="")
30 187 sjacqu01
File inputDirectory
31 187 sjacqu01
32 187 sjacqu01
@Field @Option(name="inputEncoding", usage="Input encoding", widget="String", required=false, def="")
33 187 sjacqu01
String inputEncoding
34 187 sjacqu01
35 187 sjacqu01
@Field @Option(name="outputEncoding", usage="Output encoding", widget="String", required=true, def="UTF-8")
36 187 sjacqu01
String outputEncoding
37 187 sjacqu01
38 187 sjacqu01
if (!ParametersDialog.open(this)) return;
39 187 sjacqu01
40 187 sjacqu01
File outDir = new File(inputDirectory, "out") // write result in "out" folder
41 187 sjacqu01
if (outDir.exists()) {
42 187 sjacqu01
        if (!outDir.deleteDir()) {
43 187 sjacqu01
                println "Could not delete directory: $outDir"
44 187 sjacqu01
                return;
45 187 sjacqu01
        }
46 187 sjacqu01
}
47 187 sjacqu01
if (!outDir.mkdir()) {
48 187 sjacqu01
        println "Could not create directory: $outDir"
49 187 sjacqu01
        return;
50 187 sjacqu01
}
51 187 sjacqu01
println "Writing result in : $outDir"
52 187 sjacqu01
53 187 sjacqu01
def processFile(File file, File outfile, String fromEncoding, String toutputEncoding) {
54 187 sjacqu01
        String text = file.getText(fromEncoding)
55 187 sjacqu01
        outfile.withWriter(toutputEncoding) {writer -> // read with the "fromEncoding"
56 187 sjacqu01
                writer.write(text) // write with the "toutputEncoding"
57 187 sjacqu01
        }
58 187 sjacqu01
}
59 187 sjacqu01
60 187 sjacqu01
try {
61 187 sjacqu01
inputDirectory.eachFile { file ->
62 187 sjacqu01
        if (!file.isHidden() && !file.isDirectory()) {
63 187 sjacqu01
                String e = inputEncoding
64 187 sjacqu01
                if (inputEncoding == null || inputEncoding.length() == 0) {
65 187 sjacqu01
                        CharsetDetector detector = new CharsetDetector(file)
66 187 sjacqu01
                        e = detector.getEncoding()
67 187 sjacqu01
                }
68 187 sjacqu01
69 187 sjacqu01
                File outfile = new File(outDir, file.getName())
70 187 sjacqu01
                println "$e -> $outputEncoding : "+outfile.getName()
71 187 sjacqu01
                processFile(file, outfile, e, outputEncoding)
72 187 sjacqu01
        }
73 187 sjacqu01
}
74 187 sjacqu01
} catch (UnsupportedEncodingException e) {
75 187 sjacqu01
        println "ERROR: Unsupported encoding: "+e
76 187 sjacqu01
}