Statistics
| Revision:

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

History | View | Annotate | Download (3.9 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.rcpapplication.swt.widget.parameters.*
29

    
30
// parameters
31

    
32
// **change this parameter**
33
@Field @Option(name="inputDirectory", usage="Dossier qui contient les fichiers à modifier", widget="Folder", required=true, def='voeux')
34
                inputDirectory = 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
// **change this parameter**
53
// 'true' = only display matching lines, 'false' = replace in matching files
54
//@Field @Option(name="showMatchingFilesOnly", usage="Montrer seulement les matchs", widget="Boolean", required=false, def='false')
55
                showMatchingFilesOnly = false;
56

    
57
if (!ParametersDialog.open(this)) return;
58

    
59
println "Working with $inputDirectory files with extension="+/.*$extension/
60
if (showMatchingFilesOnly) {
61
        println "Search '$find'"
62
} else {
63
        println "Replace '$find' with '$replaceWith'"
64
}
65

    
66
//find = /date="([0-9]+)-([0-9]+-[0-9]+)"/
67
// **change this parameter**
68
//replaceWith = 'date="$1-$2" year="$1"'
69
// **change this parameter** (warning: '$1', '$2'... can be interpreted by Groovy in "..." strings)
70
process(inputDirectory, extension, encoding, find, replaceWith, showMatchingFilesOnly)
71

    
72
// main body
73
def process(File inputDirectory, extension, encoding, find, replaceWith, showMatchingFilesOnly) {
74
        
75
        inputDirectory.eachFileMatch(~/.*$extension/) { file ->               // for each file matching extension
76
                println "Processing: "+file.getName()
77
//                if (showMatchingFilesOnly) {
78
//                        printMatches(file, encoding, find)
79
//                } else {
80
                        replaceInFile(file, encoding, find,replaceWith)
81
//                }
82
        }
83
}
84

    
85
//def printMatches(File file, encoding, find) {
86
//        file.eachLine(encoding) { line ->                  // for each line
87
//                if (line.matches("$find")) { println " *match* : "+line }
88
//        }
89
//}
90

    
91
def replaceInFile(File file, encoding,  find, replaceWith) {
92
        def tmp = File.createTempFile("SearchReplaceInDirectoryTemp", ".tmp", file.getParentFile()) // create temporary file
93
        tmp.write('')                                // create empty file
94
        tmp.withWriter(encoding) { writer ->
95
                String txt = file.getText(encoding);
96
                writer.println txt.replaceAll(find, replaceWith)
97
//                file.eachLine(encoding) { line ->                  // for each line
98
//                        writer.println line.replaceAll(find, replaceWith) // find&replace and print
99
//                }
100
                writer.close()
101
        }
102
        file.delete()
103
        tmp.renameTo(file)                           // save results
104
}