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