1 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
2 |
joko |
1.2 |
## $Id: Group.pm,v 1.1 2002/12/22 14:16:39 joko Exp $ |
3 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
4 |
joko |
1.2 |
## $Log: Group.pm,v $ |
5 |
|
|
## Revision 1.1 2002/12/22 14:16:39 joko |
6 |
|
|
## + initial check-in |
7 |
|
|
## |
8 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
9 |
|
|
|
10 |
|
|
|
11 |
|
|
package Regexp::Group; |
12 |
|
|
|
13 |
|
|
use strict; |
14 |
|
|
use warnings; |
15 |
|
|
|
16 |
|
|
use base 'DesignPattern::Object::Logger'; |
17 |
|
|
|
18 |
|
|
|
19 |
|
|
use Data::Dumper; |
20 |
|
|
|
21 |
|
|
use Data::Compare::Struct qw( isEmpty ); |
22 |
|
|
|
23 |
|
|
my @patterns; |
24 |
|
|
|
25 |
|
|
# ------------ common perl object constructor ------------ |
26 |
|
|
sub new { |
27 |
|
|
my $invocant = shift; |
28 |
|
|
my $class = ref($invocant) || $invocant; |
29 |
|
|
my @args = (); |
30 |
|
|
@_ && (@args = @_); |
31 |
|
|
#$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! |
32 |
|
|
my $self = { @_ }; |
33 |
|
|
bless $self, $class; |
34 |
|
|
$self->{caller} = caller; |
35 |
|
|
|
36 |
|
|
#print Dumper(caller(2)); |
37 |
|
|
#exit; |
38 |
|
|
|
39 |
|
|
$self->_init(); |
40 |
|
|
return $self; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
sub _init { |
44 |
|
|
my $self = shift; |
45 |
|
|
|
46 |
|
|
# TODO/REVIEW: compile patterns? |
47 |
|
|
#@patterns = map{ qr/$_/ } @patterns; |
48 |
|
|
|
49 |
|
|
# dereference main/raw data (payload) |
50 |
|
|
$self->{data} = ${$self->{data}}; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
sub scan { |
54 |
|
|
my $self = shift; |
55 |
|
|
my $stepkeys = shift; |
56 |
|
|
my $coderef = shift; |
57 |
|
|
|
58 |
|
|
#my $nodecount_default = shift; |
59 |
|
|
my $nodecount_default = 19; |
60 |
|
|
|
61 |
|
|
#print "SCAN", "\n"; |
62 |
|
|
#print $self->{pattern}, "\n"; |
63 |
|
|
$self->{result} = []; |
64 |
|
|
$self->{result_raw} = []; |
65 |
|
|
|
66 |
|
|
# configure tracing |
67 |
|
|
# respects additional trace-options passed to _trace-method |
68 |
|
|
$self->{TRACE_OPTIONS} = 1; |
69 |
|
|
# disables _any_ tracing - even if sub-conditions evaluate to true values |
70 |
|
|
$self->{TRACE_DISABLED} = 0; |
71 |
|
|
|
72 |
|
|
$self->log('starting to scan'); |
73 |
|
|
|
74 |
|
|
#for (1..20) { |
75 |
|
|
|
76 |
|
|
my $patterncount = 0; |
77 |
|
|
my $match_in_loop; |
78 |
|
|
|
79 |
|
|
my $steps; |
80 |
|
|
if ($stepkeys) { |
81 |
|
|
$steps = $stepkeys; |
82 |
|
|
$patterncount++; |
83 |
|
|
} else { |
84 |
|
|
$steps = $self->{metadata}->{steps}; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
#print "cb-2: $coderef", "\n"; |
88 |
|
|
|
89 |
|
|
foreach my $stepkey_current (@{$steps}) { |
90 |
|
|
$self->_scan_step($stepkey_current, $coderef); |
91 |
|
|
$patterncount++; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
#} |
95 |
|
|
|
96 |
|
|
# ... propagate them to processing |
97 |
|
|
|
98 |
|
|
# assign result to property of object's current instance |
99 |
|
|
#push @{$self->{result}}, $result; |
100 |
|
|
|
101 |
|
|
#$self->{code}->($self->{caller}, $self); |
102 |
|
|
|
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
sub _scan_step { |
106 |
|
|
|
107 |
|
|
my $self = shift; |
108 |
|
|
my $stepkey = shift; |
109 |
|
|
my $callback = shift; |
110 |
|
|
|
111 |
|
|
#print "cb-3: $callback", "\n"; |
112 |
|
|
|
113 |
|
|
#$self->log("running step '$stepkey'", 'debug'); |
114 |
|
|
$self->log("running step '$stepkey'"); |
115 |
|
|
|
116 |
|
|
my $dataref = $self->{data}; |
117 |
|
|
my $metadata = $self->{metadata}->{$stepkey}; |
118 |
|
|
my $pattern = $self->{patterns}->{$stepkey}; |
119 |
|
|
my $coderef = $self->{coderefs}->{$stepkey}; |
120 |
|
|
|
121 |
|
|
#print Dumper($coderef); |
122 |
|
|
#print Dumper($self->{data}); |
123 |
|
|
#exit; |
124 |
|
|
|
125 |
|
|
$self->trace("data used", $dataref, 0, undef, { tag => '', exit => 0 }); |
126 |
|
|
$self->trace("metadata used", $metadata, 0, undef, { tag => '', exit => 0 }); |
127 |
|
|
$self->trace("pattern used", $pattern, 0, undef, { tag => '', exit => 0 }); |
128 |
|
|
$self->trace("coderef used", $coderef, 0, undef, { tag => '', exit => 0 }); |
129 |
|
|
|
130 |
|
|
#print "pattern: $pattern", "\n"; |
131 |
|
|
#exit; |
132 |
|
|
|
133 |
|
|
# if metadata variable "noscan" is supplied, don't kick off the regex-engine, just call the coderef |
134 |
|
|
# TODO: log this event |
135 |
|
|
if ($metadata->{noscan} && $metadata->{noscan} == 1) { |
136 |
|
|
$coderef->($self, undef, undef, $callback); |
137 |
|
|
return; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
# dereference data if its still referenced |
141 |
|
|
my $data = ${$self->{data}}; |
142 |
|
|
|
143 |
joko |
1.2 |
if (!$data) { |
144 |
|
|
$self->log("data is empty: stepkey=$stepkey", 'warning'); |
145 |
|
|
return; |
146 |
|
|
} |
147 |
|
|
|
148 |
joko |
1.1 |
#$self->{data} =~ s{ |
149 |
|
|
$data =~ s{ |
150 |
|
|
|
151 |
|
|
$pattern # the pattern itself - as contained in a string - actually gets interpolated here |
152 |
|
|
|
153 |
|
|
}{ |
154 |
|
|
|
155 |
|
|
print "." if $self->{verbose}; |
156 |
|
|
|
157 |
|
|
$self->log("match in step '$stepkey'"); |
158 |
|
|
|
159 |
|
|
# have a clean item to fill in slots |
160 |
|
|
my $result_item = []; |
161 |
|
|
|
162 |
|
|
# 1. build matched structure from slots - grouping functionality for slots |
163 |
|
|
|
164 |
|
|
# V1 - hand-rolled |
165 |
|
|
#my $result = [$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20]; |
166 |
|
|
#print Dumper($result); |
167 |
|
|
|
168 |
|
|
my $count = $metadata->{nodecount}; |
169 |
|
|
|
170 |
|
|
# TODO: re-renable this! |
171 |
|
|
#$count ||= $nodecount_default; |
172 |
|
|
|
173 |
|
|
# V2 - iterate! |
174 |
|
|
# push dummy item to slot '0' to mimic known behaviour of accessing $1, $2, $3, .... |
175 |
|
|
push @{$result_item}, 'n/a'; |
176 |
|
|
for my $i (1..$count) { |
177 |
|
|
my $slot = '$' . $i; |
178 |
|
|
push @{$result_item}, eval($slot); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
# 2. remember built result (raw) in object - grouping functionality for items |
183 |
|
|
$self->log("pushing raw result"); |
184 |
|
|
push @{$self->{result_raw}}, $result_item; |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
# 4. check/modify further metadata |
188 |
|
|
# e.g. 'column_positions' means: "this step is a ColumnProvider" |
189 |
|
|
#print Dumper($metadata); |
190 |
|
|
if (my $positions = $metadata->{column_positions}) { |
191 |
|
|
$self->trace('positions', $positions, 0); |
192 |
|
|
|
193 |
|
|
#print Dumper($result_item); |
194 |
|
|
|
195 |
|
|
# build column-names array from position-metadata |
196 |
|
|
my @column_names; |
197 |
|
|
map { |
198 |
|
|
push @column_names, $result_item->[$_]; |
199 |
|
|
} @$positions; |
200 |
|
|
|
201 |
|
|
# store additional metadata |
202 |
|
|
$metadata->{column_names} = \@column_names; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
$self->trace('column-names', $metadata->{column_names}, 0); |
206 |
|
|
|
207 |
|
|
|
208 |
|
|
#return ''; |
209 |
|
|
#$&; |
210 |
|
|
push(@{$self->{matches}->{parsed_chunks}}, $&); |
211 |
|
|
#undef; |
212 |
|
|
|
213 |
|
|
my $matchpos = pos($data); |
214 |
|
|
$self->{matches}->{position} = $matchpos; |
215 |
|
|
push @{$self->{matches}->{positions}}, $matchpos; |
216 |
|
|
|
217 |
|
|
|
218 |
|
|
# 3. call coderef to get _processed_ result if given (callback) |
219 |
|
|
$self->log("calling coderef '$coderef'"); |
220 |
|
|
if ($coderef) { |
221 |
|
|
my $result_processed = {}; |
222 |
|
|
if ($coderef->($self, $result_item, $result_processed, $callback)) { |
223 |
|
|
$self->trace("pushing processed result", $result_processed, 0); |
224 |
|
|
push @{$self->{result}}, $result_processed if $result_processed; |
225 |
|
|
} else { |
226 |
|
|
$self->log('processing of container failed', 'warning'); |
227 |
|
|
return; |
228 |
|
|
} |
229 |
|
|
} else { |
230 |
|
|
# TODO: croak 'no coderef'; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
# pass back matched content if we are not the ones to delete something (pass-back-if-not-remover) |
234 |
|
|
# with this mechanism, we can iterate over the same content again and again and don't loose data while doing this |
235 |
|
|
# without this, multiple scan stepping would not be possible since each step would either remove its |
236 |
|
|
# matched content - or not: no flexibility. |
237 |
|
|
# as it seems we need flexibility at any point ;-( in order not to break our necks with code-maintenance ;-) |
238 |
|
|
# this also can get configured here (this flag is contained in the metadata-part of your filter-declaration) |
239 |
|
|
$& if !$metadata->{remover}; |
240 |
|
|
|
241 |
|
|
}xge; |
242 |
|
|
|
243 |
|
|
|
244 |
|
|
#$self->{data_rest} = \$data; |
245 |
|
|
$self->{data} = \$data; |
246 |
|
|
|
247 |
|
|
#$self->trace('matches', $self->getMatches(), 1); |
248 |
|
|
#$self->trace('matches-positions', $self->getMatchPositions(), 1); |
249 |
joko |
1.2 |
|
250 |
|
|
#print "\r" if $self->{verbose}; |
251 |
|
|
print " " x 50 if $self->{verbose}; |
252 |
|
|
print "\r" if $self->{verbose}; |
253 |
joko |
1.1 |
|
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
sub continue { |
257 |
|
|
my $self = shift; |
258 |
|
|
my $scankey = shift; |
259 |
|
|
#print "CONTINUE!", "\n"; |
260 |
|
|
#$self->{data} = $self->{data_rest}; |
261 |
|
|
return $self->scan($scankey); |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
sub getMatches { |
265 |
|
|
my $self = shift; |
266 |
|
|
return $self->{result_raw}; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
sub getResults { |
270 |
|
|
my $self = shift; |
271 |
|
|
return $self->{result}; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
sub getDataRest { |
275 |
|
|
my $self = shift; |
276 |
|
|
return $self->{data_rest}; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
sub getMatchCount { |
280 |
|
|
my $self = shift; |
281 |
|
|
|
282 |
|
|
my $matches = $self->getMatches(); |
283 |
|
|
return if isEmpty($matches); |
284 |
|
|
|
285 |
|
|
my $count = $#{$matches}; |
286 |
|
|
$count++; |
287 |
|
|
return $count; |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
sub getMatchPositions { |
291 |
|
|
my $self = shift; |
292 |
|
|
if (!$self->getMatchCount()) { |
293 |
|
|
$self->{matches}->{positions} = []; |
294 |
|
|
} |
295 |
|
|
return $self->{matches}->{positions}; |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
sub getMatchPosition { |
299 |
|
|
my $self = shift; |
300 |
|
|
return $self->{matches}->{position}; |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
1; |