Statistics
| Revision:

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

History | View | Annotate | Download (1.2 kB)

1
package org.txm.macro.txt
2

    
3
import org.kohsuke.args4j.*
4

    
5
import groovy.transform.Field
6

    
7
import java.nio.charset.Charset
8

    
9
import org.txm.rcpapplication.swt.widget.parameters.*
10
import org.txm.utils.*
11

    
12
import javax.xml.stream.*
13

    
14
import java.net.URL
15
import java.util.regex.Matcher;
16
import java.util.regex.Pattern;
17

    
18
@Field @Option(name="inputDirectory", usage="TXT directory", widget="Folder", required=false, def="txt")
19
File inputDirectory;
20
@Field @Option(name="regexp", usage="Regular expression to match", widget="String", required=false, def="p")
21
String regexp;
22
@Field @Option(name="encoding", usage="File encoding", widget="String", required=false, def="UTF-8")
23
String encoding;
24

    
25
if (!ParametersDialog.open(this)) return;
26

    
27
def p = /$regexp/
28

    
29
println "processing: "+inputDirectory.listFiles()
30
for (File inputfile : inputDirectory.listFiles()) {
31
        if (inputfile.isDirectory()) continue // ignore
32
        
33
        def lines = []
34
        inputfile.eachLine("UTF-8") { line, n ->
35
                def m = line =~ p
36
                if ( m.size() > 0) {
37
                        lines << "  line $n: $line"
38
                }
39
        }
40
        
41
        println inputfile.getName() + " "+lines.size() + " match" +((lines.size() > 1)?"s":"")
42
        if (lines.size() > 0) {
43
                for (String s : lines ) println s
44
        }
45
}