Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / filters / RegexFilter / RegexFilter.groovy @ 187

History | View | Annotate | Download (2.1 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package filters.RegexFilter;
29

    
30
import org.txm.importer.filters.*;
31
import java.util.regex.Matcher;
32
import java.util.regex.Pattern;
33

    
34
import org.txm.importer.filters.Filter;
35

    
36
// TODO: Auto-generated Javadoc
37
/** Filters out lines not matching a regexp. @author jmague */
38
public class RegexFilter extends Filter {
39
        private Pattern pattern;
40

    
41
        /* (non-Javadoc)
42
         * @see org.txm.importer.filters.Filter#SetUsedParam(java.lang.Object)
43
         */
44
        void SetUsedParam(Object args) {
45
                if (args.size() == 1) {
46
                        pattern = Pattern.compile(args[0]);
47
                } else {
48
                        System.out
49
                                        .println("Regexfilter needs 1 args :\nString:patternRegex");
50
                }
51
        }
52

    
53
        /* (non-Javadoc)
54
         * @see org.txm.importer.filters.Filter#after()
55
         */
56
        void after() {
57
        }
58

    
59
        /* (non-Javadoc)
60
         * @see org.txm.importer.filters.Filter#before()
61
         */
62
        boolean before() {
63
                if (pattern == null) {
64
                        System.out
65
                                        .println("Regex filter needs 1 args :\nString:patternRegex");
66
                        return false;
67
                }
68
                return true;
69
        }
70

    
71
        /* (non-Javadoc)
72
         * @see org.txm.importer.filters.Filter#filter()
73
         */
74
        void filter() {
75
                Matcher matcher = pattern.matcher(line);
76
                if (matcher.matches())
77
                        output.write(line + lineSeparator);
78
        }
79
}