Statistics
| Revision:

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

History | View | Annotate | Download (2.4 kB)

1
package org.txm.macroproto
2
// Copyright © 2015 - 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
import org.kohsuke.args4j.*
28

    
29
import groovy.transform.Field
30

    
31
import java.nio.charset.Charset
32

    
33
import org.txm.rcp.swt.widget.parameters.*
34
import org.txm.utils.*
35

    
36
import javax.xml.stream.*
37

    
38
import java.net.URL
39
import java.util.regex.Matcher;
40
import java.util.regex.Pattern;
41

    
42
// PARAMETERS
43

    
44
@Field @Option(name="inputDirectory", usage="TXT directory", widget="Folder", required=false, def="")
45
File inputDirectory;
46
@Field @Option(name="regexp", usage="Regular expression to match", widget="String", required=false, def="")
47
String regexp;
48
@Field @Option(name="encoding", usage="File encoding", widget="String", required=false, def="UTF-8")
49
String encoding;
50

    
51
// PARAMETERS
52

    
53
if (inputDirectory == null) {
54
        if (!ParametersDialog.open(this)) return;
55
}
56

    
57
// SANITY CHECK
58

    
59
if (inputDirectory==null || regexp==null) { println "** FindMultiLineRegExpMacro: the input directory and the regular expression must be specified."; return}
60

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

    
63
// MAIN BODY
64

    
65
def p = /$regexp/
66

    
67
println "-- looking for: $regexp"
68

    
69
for (File inputfile : inputDirectory.listFiles()) {
70

    
71
        if (inputfile.isDirectory()) continue // ignore
72

    
73
        def matcher = inputfile.getText() =~ p
74
        
75
        println "\n-- file:"+inputfile.getName() + " "+matcher.size() + " match" +((matcher.size() > 1)?"s":"")
76

    
77
        if (matcher.size() > 0) {
78
                matcher.each { println it }
79
        }
80
}