1 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
2 |
joko |
1.2 |
## $Id: Regexp.pm,v 1.1 2002/12/23 04:22:22 joko Exp $ |
3 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
4 |
joko |
1.2 |
## $Log: Regexp.pm,v $ |
5 |
|
|
## Revision 1.1 2002/12/23 04:22:22 joko |
6 |
|
|
## + refactored from Data::Filter |
7 |
|
|
## |
8 |
joko |
1.1 |
## Revision 1.1 2002/12/22 14:19:17 joko |
9 |
|
|
## + initial check-in |
10 |
|
|
## |
11 |
|
|
## ------------------------------------------------------------------------ |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
package Data::Query::Filter::Regexp; |
15 |
|
|
|
16 |
|
|
use strict; |
17 |
|
|
use warnings; |
18 |
|
|
|
19 |
|
|
use base 'DesignPattern::Object::Logger'; |
20 |
|
|
|
21 |
|
|
|
22 |
|
|
use Data::Dumper; |
23 |
|
|
|
24 |
|
|
use Regexp::Group; |
25 |
joko |
1.2 |
use DesignPattern::Object; |
26 |
joko |
1.1 |
use Data::Compare::Struct qw( isEmpty ); |
27 |
|
|
|
28 |
|
|
# ------------ common perl object constructor ------------ |
29 |
|
|
sub new { |
30 |
|
|
my $invocant = shift; |
31 |
|
|
my $class = ref($invocant) || $invocant; |
32 |
|
|
my @args = (); |
33 |
|
|
@_ && (@args = @_); |
34 |
|
|
#$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! |
35 |
|
|
my $self = { @_ }; |
36 |
|
|
bless $self, $class; |
37 |
|
|
$self->{caller} = caller; |
38 |
|
|
|
39 |
|
|
#print Dumper(caller(2)); |
40 |
|
|
#exit; |
41 |
|
|
|
42 |
|
|
$self->_init(); |
43 |
|
|
return $self; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
sub _init { |
47 |
|
|
my $self = shift; |
48 |
|
|
|
49 |
|
|
# try to load filter-declaration from configuration scope inside perl-module (yes - it's already abstracted out there!) |
50 |
joko |
1.2 |
$self->{declaration} = DesignPattern::Object->fromPackage($self->{module}); |
51 |
joko |
1.1 |
#print Dumper($self->{declaration}); |
52 |
|
|
|
53 |
|
|
|
54 |
|
|
# the regexp-object which does the hard work for us ;-) |
55 |
|
|
$self->{regexp} = Regexp::Group->new( |
56 |
|
|
'data' => \$self->{data}, # reference ... |
57 |
|
|
#'data' => $self->{data}, # ... or not? |
58 |
|
|
'metadata' => $self->{declaration}->metadata(), |
59 |
|
|
'patterns' => $self->{declaration}->patterns(), |
60 |
|
|
'coderefs' => $self->{declaration}->coderefs(), |
61 |
|
|
'verbose' => 1, |
62 |
|
|
); |
63 |
|
|
|
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
sub run { |
67 |
|
|
my $self = shift; |
68 |
|
|
my $steps = shift; |
69 |
|
|
my $coderef = shift; |
70 |
|
|
|
71 |
|
|
#print "cb: $coderef", "\n"; |
72 |
|
|
|
73 |
|
|
my $result = $self->{regexp}->scan($steps, $coderef); |
74 |
|
|
|
75 |
|
|
# configure tracing |
76 |
|
|
# respects additional trace-options passed to _trace-method |
77 |
|
|
$self->{TRACE_OPTIONS} = 1; |
78 |
|
|
# disables _any_ tracing - even if sub-conditions evaluate to true values |
79 |
|
|
$self->{TRACE_DISABLED} = 0; |
80 |
|
|
|
81 |
|
|
# trace the result |
82 |
|
|
# cute: ;-) |
83 |
|
|
#$self->trace('matches after Regexp::Group->scan', $self->{regexp}->getMatches(), 1, undef, { tag => '', exit => 0 }); |
84 |
|
|
#$self->trace('results after Regexp::Group->scan', $self->{regexp}->getResults(), 1, undef, { tag => '', exit => 0 }); |
85 |
|
|
|
86 |
|
|
return $result; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
sub index { |
90 |
|
|
my $self = shift; |
91 |
|
|
my $steps = shift; |
92 |
|
|
|
93 |
|
|
# configure tracing |
94 |
|
|
# respects additional trace-options passed to _trace-method |
95 |
|
|
$self->{TRACE_OPTIONS} = 1; |
96 |
|
|
# disables _any_ tracing - even if sub-conditions evaluate to true values |
97 |
|
|
$self->{TRACE_DISABLED} = 0; |
98 |
|
|
|
99 |
|
|
# trace the result |
100 |
|
|
# cute: ;-) |
101 |
|
|
#$self->trace('matches after Regexp::Group->scan', $self->{regexp}->getMatches(), 1, undef, { tag => '', exit => 0 }); |
102 |
|
|
#$self->trace('results after Regexp::Group->scan', $self->{regexp}->getResults(), 1, undef, { tag => '', exit => 0 }); |
103 |
|
|
|
104 |
|
|
my $result = $self->{regexp}->scan($steps); |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
return $result; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
sub continue { |
111 |
|
|
my $self = shift; |
112 |
|
|
my $scankey = shift; |
113 |
|
|
my $result = $self->{regexp}->continue($scankey); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
sub getResults { |
117 |
|
|
my $self = shift; |
118 |
|
|
return $self->{regexp}->{result}; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
sub getResultCount { |
122 |
|
|
my $self = shift; |
123 |
|
|
#my $count = ($#{$self->{regexp}->{result}} == -1 ? 0 : $#{$self->{regexp}->{result}}); |
124 |
|
|
#$count++; |
125 |
|
|
|
126 |
|
|
|
127 |
|
|
my $matches = $self->{regexp}->getMatches(); |
128 |
|
|
return if isEmpty($matches); |
129 |
|
|
|
130 |
|
|
my $count = $#{$matches}; |
131 |
|
|
$count++; |
132 |
|
|
return $count; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
1; |