Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / txt / SearchReplaceInFileMacro.groovy @ 499

History | View | Annotate | Download (2.4 kB)

1
package org.txm.macro.txt
2
// Copyright © - ENS de Lyon - http://textometrie.ens-lyon.fr
3
// @author sheiden
4
//
5
// This file is part of the TXM platform.
6
//
7
// The TXM platform is free software: you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation, either version 3 of the License, or
10
// (at your option) any later version.
11
//
12
// The TXM platform is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
19
//
20
// $LastChangedDate: 2013-05-02 11:28:42 +0200 (jeu., 02 mai 2013) $
21
// $LastChangedRevision: 2378 $
22
// $LastChangedBy: sheiden $
23
//
24

    
25
// imports
26
import org.kohsuke.args4j.*
27
import groovy.transform.Field
28
import org.txm.rcp.swt.widget.parameters.*
29

    
30
// parameters
31

    
32
// **change this parameter**
33
@Field @Option(name="file",usage="Dossier qui contient les fichiers à modifier", widget="File", required=true, def='/home/mdecorde/xml/qgraalc/qgraal_cm-c.xml')
34
                file = new File(System.getProperty("user.home"), "Bureau/voeux")
35

    
36
// **change this parameter**
37
@Field @Option(name="extension",usage="Regexp de l'extension des fichiers à modifier", widget="String", required=true, def='\\.txt')
38
                extension = "\\.txt"
39

    
40
// **change this parameter**
41
@Field @Option(name="find",usage="Expression régulière", widget="String", required=true, def='')
42
                find = ""
43

    
44
// **change this parameter**
45
@Field @Option(name="replaceWith",usage="Chaîne de remplacement", widget="String", required=false, def='\'')
46
                replaceWith = "'"
47

    
48
// **change this parameter**
49
@Field @Option(name="encoding",usage="Encodage des fichiers", widget="String", required=true, def='UTF-8')
50
                encoding = "utf-8"
51

    
52
if (!ParametersDialog.open(this)) return;
53

    
54
println "SearchAndReplace in $file"
55

    
56
        def tmp = File.createTempFile("SearchReplaceInDirectoryTemp", ".tmp", file.getParentFile()) // create temporary file
57
        tmp.write('')                                // create empty file
58
        tmp.withWriter(encoding) { writer ->
59
                String txt = file.getText(encoding);
60
                writer.println txt.replaceAll(find, replaceWith)
61
                writer.close()
62
        }
63
        file.delete()
64
        tmp.renameTo(file)                           // save results