Révision 3939
TXM/trunk/bundles/org.txm.groovy.core/src/groovy/org/txm/macro/txt/SplitFileByPatternMacro.groovy (revision 3939) | ||
---|---|---|
1 |
package org.txm.sw |
|
2 |
|
|
3 |
import org.kohsuke.args4j.* |
|
4 |
import groovy.transform.Field |
|
5 |
import org.txm.rcpapplication.swt.widget.parameters.* |
|
6 |
|
|
7 |
// PARAMETERS // |
|
8 |
|
|
9 |
@Field @Option(name="infile",usage="input file", widget="File", required=true, def="") |
|
10 |
File infile // the file to process |
|
11 |
|
|
12 |
@Field @Option(name="outdir",usage="output folder", widget="Folder", required=false, def="") |
|
13 |
File outdir // the directory which received result files |
|
14 |
|
|
15 |
@Field @Option(name="regex",usage="search pattern", widget="String", required=false, def="(<\\?(.+))") |
|
16 |
def regex // matching line test |
|
17 |
|
|
18 |
@Field @Option(name="prefix",usage="result files prefix", widget="String", required=false, def="") |
|
19 |
String prefix // result files prefix |
|
20 |
|
|
21 |
@Field @Option(name="replace",usage="replace with, use the matching line to build the filename", widget="String", required=false, def="\$2") |
|
22 |
String replace // use the matching line to build the filename |
|
23 |
|
|
24 |
@Field @Option(name="ext",usage=" result files extension", widget="String", required=false, def=".xml") |
|
25 |
String ext // result files extension |
|
26 |
|
|
27 |
@Field @Option(name="includeSeparatorLine",usage="should include matching line in result files", widget="Boolean", required=false, def="true") |
|
28 |
boolean includeSeparatorLine // should include matching line in result files |
|
29 |
|
|
30 |
@Field @Option(name="writeAtStartup",usage="should create files only if a matching line is found ", widget="Boolean", required=false, def="true") |
|
31 |
boolean writeAtStartup // should create files only if a matching line is found |
|
32 |
|
|
33 |
@Field @Option(name="encoding",usage="file encoding", widget="String", required=false, def="UTF-8") |
|
34 |
String encoding |
|
35 |
|
|
36 |
// Open the parameters input dialog box |
|
37 |
if (!ParametersDialog.open(this)) return; |
|
38 |
|
|
39 |
regex = /$regex/ // create a pattern object |
|
40 |
|
|
41 |
// CALL // |
|
42 |
splitByRegExp(infile, outdir, |
|
43 |
regex, prefix, replace, ext, |
|
44 |
// regex, prefix, null, ext, |
|
45 |
includeSeparatorLine, writeAtStartup) |
|
46 |
|
|
47 |
|
|
48 |
// PUBLIC METHODS // |
|
49 |
def splitByRegExp(def infile, def outdir, def regex, def prefix, def replace, def ext, def includeSeparatorLine, def writeAtStartup) { |
|
50 |
int count = 1; |
|
51 |
outdir.deleteDir() |
|
52 |
outdir.mkdirs() |
|
53 |
if (!outdir.exists()) return false |
|
54 |
if (!infile.isFile()) return false |
|
55 |
|
|
56 |
File outfile = null; |
|
57 |
def writer = null |
|
58 |
|
|
59 |
if (writeAtStartup) { |
|
60 |
outfile = new File(outdir, prefix+(count++)+ext) |
|
61 |
writer = outfile.newWriter(encoding) |
|
62 |
} |
|
63 |
infile.eachLine(encoding) { line -> |
|
64 |
if (line ==~ regex) { // new file |
|
65 |
if (replace == null || replace == "") { |
|
66 |
outfile = new File(outdir, prefix+(count++)+ext) |
|
67 |
} else { |
|
68 |
String name = prefix+line.replaceAll(regex, replace)+ext |
|
69 |
outfile = new File(outdir, name) |
|
70 |
} |
|
71 |
if (writer != null) writer.close() // close previous writer |
|
72 |
writer = outfile.newWriter(encoding) |
|
73 |
println "new file $outfile" |
|
74 |
if (includeSeparatorLine) { |
|
75 |
writer.println line |
|
76 |
} |
|
77 |
} else { |
|
78 |
if (writer != null) |
|
79 |
writer.println line |
|
80 |
} |
|
81 |
} |
|
82 |
if (writer != null) writer.close() |
|
83 |
} |
Formats disponibles : Unified diff