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 479 mdecorde
package org.txm.macro.txt
2 479 mdecorde
// Copyright © - ENS de Lyon - http://textometrie.ens-lyon.fr
3 479 mdecorde
// @author sheiden
4 479 mdecorde
//
5 479 mdecorde
// This file is part of the TXM platform.
6 479 mdecorde
//
7 479 mdecorde
// The TXM platform is free software: you can redistribute it and/or modify
8 479 mdecorde
// it under the terms of the GNU General Public License as published by
9 479 mdecorde
// the Free Software Foundation, either version 3 of the License, or
10 479 mdecorde
// (at your option) any later version.
11 479 mdecorde
//
12 479 mdecorde
// The TXM platform is distributed in the hope that it will be useful,
13 479 mdecorde
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14 479 mdecorde
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 479 mdecorde
// GNU General Public License for more details.
16 479 mdecorde
//
17 479 mdecorde
// You should have received a copy of the GNU General Public License
18 479 mdecorde
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
19 479 mdecorde
//
20 479 mdecorde
// $LastChangedDate: 2013-05-02 11:28:42 +0200 (jeu., 02 mai 2013) $
21 479 mdecorde
// $LastChangedRevision: 2378 $
22 479 mdecorde
// $LastChangedBy: sheiden $
23 479 mdecorde
//
24 479 mdecorde
25 479 mdecorde
// imports
26 479 mdecorde
import org.kohsuke.args4j.*
27 479 mdecorde
import groovy.transform.Field
28 499 mdecorde
import org.txm.rcp.swt.widget.parameters.*
29 479 mdecorde
30 479 mdecorde
// parameters
31 479 mdecorde
32 479 mdecorde
// **change this parameter**
33 479 mdecorde
@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 479 mdecorde
                file = new File(System.getProperty("user.home"), "Bureau/voeux")
35 479 mdecorde
36 479 mdecorde
// **change this parameter**
37 479 mdecorde
@Field @Option(name="extension",usage="Regexp de l'extension des fichiers à modifier", widget="String", required=true, def='\\.txt')
38 479 mdecorde
                extension = "\\.txt"
39 479 mdecorde
40 479 mdecorde
// **change this parameter**
41 479 mdecorde
@Field @Option(name="find",usage="Expression régulière", widget="String", required=true, def='')
42 479 mdecorde
                find = ""
43 479 mdecorde
44 479 mdecorde
// **change this parameter**
45 479 mdecorde
@Field @Option(name="replaceWith",usage="Chaîne de remplacement", widget="String", required=false, def='\'')
46 479 mdecorde
                replaceWith = "'"
47 479 mdecorde
48 479 mdecorde
// **change this parameter**
49 479 mdecorde
@Field @Option(name="encoding",usage="Encodage des fichiers", widget="String", required=true, def='UTF-8')
50 479 mdecorde
                encoding = "utf-8"
51 479 mdecorde
52 479 mdecorde
if (!ParametersDialog.open(this)) return;
53 479 mdecorde
54 479 mdecorde
println "SearchAndReplace in $file"
55 479 mdecorde
56 479 mdecorde
        def tmp = File.createTempFile("SearchReplaceInDirectoryTemp", ".tmp", file.getParentFile()) // create temporary file
57 479 mdecorde
        tmp.write('')                                // create empty file
58 479 mdecorde
        tmp.withWriter(encoding) { writer ->
59 479 mdecorde
                String txt = file.getText(encoding);
60 479 mdecorde
                writer.println txt.replaceAll(find, replaceWith)
61 479 mdecorde
                writer.close()
62 479 mdecorde
        }
63 479 mdecorde
        file.delete()
64 479 mdecorde
        tmp.renameTo(file)                           // save results