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