Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macroproto / txt / MultiLineSearchReplaceInDirectoryMacro.groovy @ 499

History | View | Annotate | Download (3.3 kB)

1
package org.txm.macroproto.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
// STANDARD DECLARATIONS
26

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

    
32
// PARAMETERS
33

    
34
// **change this parameter**
35
@Field @Option(name="inputDirectory", usage="Dossier contenant les fichiers à modifier", widget="Folder", required=true, def='/home')
36
                inputDirectory = new File(System.getProperty("user.home"), "Bureau/voeux")
37

    
38
// **change this parameter**
39
@Field @Option(name="extension", usage="extension des fichiers à modifier (expression régulière)", widget="String", required=true, def='\\.txt')
40
                extension = "\\.txt"
41

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

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

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

    
54
// PARAMETERS DIALOG
55

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

    
58
// SANITY CHECK
59

    
60
if (inputDirectory==null || find==null || replaceWith==null) { println "** MultiLineSearchReplaceInDirectoryMacro: the input directory, the regular expression to search and replacement must be specified."; return}
61

    
62
if (!inputDirectory.exists()) { println "** MultiLineSearchReplaceInDirectoryMacro: impossible to access the '$inputDirectory' input directory."; return}
63

    
64
// MAIN BODY
65

    
66
println "-- working in $inputDirectory directory with files of '$extension' extension"
67
println "-- replacing '$find' with '$replaceWith'"
68

    
69
// find = /date="([0-9]+)-([0-9]+-[0-9]+)"/
70
// **change this parameter**
71
// replaceWith = 'date="$1-$2" year="$1"'
72
// **change this parameter** (warning: '$1', '$2'... can be interpreted by Groovy in "..." strings)
73
//
74
// RECETTE
75
// <tei:pb [^>]+>\n   <tei:p>[0-9]+</tei:p>\n   <tei:p>[^ ]+ 18[5-6][0-9]\.</tei:p>\n
76

    
77
def p = /$find/
78

    
79
inputDirectory.eachFileMatch(~/.*$extension/) { file ->               // for each file matching extension
80
        println "\n-- processing: "+file.getName()
81
        def tmp = File.createTempFile("SearchReplaceInDirectoryTemp", ".tmp", file.getParentFile()) // create temporary file
82
        tmp.withWriter(encoding) { writer ->
83
                writer.print(file.getText(encoding).replaceAll(p, replaceWith))
84
        }
85
        file.delete()
86
        tmp.renameTo(file)
87
}