Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / prototypes / txt / AdvancedSearchInDirectoryMacro.groovy @ 2219

History | View | Annotate | Download (2.3 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.rcp.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
@Field @Option(name="reverse", usage="Show the files not matching", widget="Boolean", required=false, def="false")
25
def reverse;
26
@Field @Option(name="recursive", usage="recurse in directories", widget="Boolean", required=false, def="false")
27
def recursive;
28
@Field @Option(name="filePathFilterRegExp", usage="Show the files not matching", widget="String", required=false, def=".*/MANIFEST.MF")
29
def filePathFilterRegExp;
30
@Field @Option(name="directoryPathFilterRegExp", usage="Show the files not matching", widget="String", required=false, def="")
31
def directoryPathFilterRegExp;
32

    
33
if (!ParametersDialog.open(this)) return;
34

    
35
p = /$regexp/
36

    
37
println "processing: "+inputDirectory
38

    
39
def processDir(def directory) {
40
        
41
for (File inputfile : directory.listFiles()) {
42

    
43
        if (inputfile.isDirectory()) {
44
                if (recursive) {
45
                        if (directoryPathFilterRegExp != null && directoryPathFilterRegExp.length() > 0) {
46
                                if (!inputfile.getAbsolutePath().matches(directoryPathFilterRegExp)) {
47
                                        continue // ignore since does not match
48
                                }
49
                        }
50
                
51
                        processDir(inputfile)
52
                }
53
                continue // ignore
54
        }
55
        
56
        if (filePathFilterRegExp != null && filePathFilterRegExp.length() > 0) {
57
                if (!inputfile.getAbsolutePath().matches(filePathFilterRegExp)) {
58
                        continue // ignore since does not match
59
                }
60
        }
61
        
62
        def lines = []
63
        inputfile.eachLine("UTF-8") { line, n ->
64
                def m = line =~ p
65
                if ( m.size() > 0) {
66
                        lines << "  line $n: $line"
67
                }
68
        }
69
        
70
        if (reverse) {
71
                if (lines.size() == 0) {
72
                        println inputfile.getAbsolutePath()
73
                }
74
        } else {
75
                if (lines.size() > 0) {
76
                        println inputfile.getAbsolutePath() + " "+lines.size() + " match" +((lines.size() > 1)?"s":"")
77
                        for (String s : lines ) println s
78
                }
79
        }
80
}
81
}
82

    
83
processDir(inputDirectory)