lib/NewsClipper/Interpreter.pm untested

filter: Statementstmt 0.0% 0 / 195 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 340 CC 46 SCAR 38.5
CC
46
Cov
0%
RunHandler
110.0
_DataOK
72.0
_FilterData
56.0
CRAP 47.2
1 # -*- mode: Perl; -*-
2  
3 # This package implements the interpreter engine that executes a series of
4 # News Clipper commands.
5  
6 package NewsClipper::Interpreter;
7  
8 use strict;
9 use Exporter;
10 use NewsClipper::Types qw(ValidateTypeSignature GetTypeSignature
11 ConvertTypeToEnglish TypesMatch);
12  
13 use vars qw( $VERSION @ISA @EXPORT_OK );
14  
15 @ISA = qw( Exporter );
16  
17 @EXPORT_OK = qw(RunHandler);
18  
19 $VERSION = 0.33;
20  
21 use NewsClipper::Globals;
22  
23 # ------------------------------------------------------------------------------
24  
25 # @update_times stores the update times for a handler before the handler's Get
26 # function is called. The Get function will then call a function in
27 # AcquisitionFunctions::GetURL later, which will access this value.
28  
29 my @update_times;
30  
31 # ------------------------------------------------------------------------------
32  
33 sub new
34 {
35 my $proto = shift;
36  
37 # We take the ref if "new" was called on an object, and the class ref
38 # otherwise.
39 my $class = ref($proto) || $proto;
40  
41 # Create an "object"
42 my $self = {};
43  
44 # Make the object a member of the class
45 bless ($self, $class);
46  
47 return $self;
48 }
49  
50 # ------------------------------------------------------------------------------
51  
52 # Executes a set of News Clipper commands.
53  
54 sub Execute
55 {
56 my $self = shift;
57 my @commands = @_;
58  
59 # Fill in any defaults
60 @commands = _GetDefaultCommands(@commands);
61  
62 dprint "Executing ",$#commands+1," commands.";
63  
64 return unless $#commands != -1;
65  
66 my $data = undef;
67  
68 return unless _CommandsValid(@commands);
69  
70 # For each command...
71 foreach my $command (@commands)
72 {
73 my ($handlerType,$attributeList) = @$command;
74  
75 my $handlerName = $attributeList->{name};
76 delete $attributeList->{name};
77  
78 $data = RunHandler($handlerName,$handlerType,$data,$attributeList);
79 }
80  
81 }
82  
83 # ------------------------------------------------------------------------------
84  
85 sub _GetDefaultCommands
86 {
87 my @commands = @_;
88  
89 # We only try to fill in defaults if the user only specified an input
90 # command.
91 return @commands if $#commands > 0 || $commands[0][0] ne 'input';
92  
93 my ($type,$attributeList) = @{$commands[0]};
94  
95 my $handlerName = $attributeList->{name};
96  
97 # Ask the HandlerFactory to create a handler for us, based on the name.
98 my $handler = $NewsClipper::Globals::handlerFactory->Create($handlerName);
99  
100 if (defined $handler)
101 {
102 # Set up default values
103 $attributeList = $handler->ProcessAttributes($attributeList,'input');
104 unless (defined $attributeList)
105 {
106 $errors{'interpreter'} .= dequote <<" EOF";
107 The handler "$handlerName" was loaded, but the attributes were not
108 correctly specified.
109 EOF
110  
111 return ();
112 }
113  
114 my $defaultHandlerText = $handler->GetDefaultHandlers($attributeList);
115  
116 # Save the default handler info for later, in case there is an error and
117 # we need to output diagnostic info.
118 $errors{'expanded commands'} = $defaultHandlerText;
119  
120 require NewsClipper::TagParser;
121 my $tagParser = new NewsClipper::TagParser;
122  
123 my @extraCommands = $tagParser->parse($defaultHandlerText);
124  
125 if ($#extraCommands != -1)
126 {
127 dprint "Adding " . ($#extraCommands+1) .
128 " default filter and output handlers:\n$defaultHandlerText";
129  
130 push @commands, @extraCommands;
131 }
132 else
133 {
134 $errors{'interpreter'} .= dequote <<" EOF";
135 The handler "$handlerName" was loaded, but the default handlers could
136 not be parsed.
137 EOF
138 return ();
139 }
140 }
141 else
142 {
143 $errors{'interpreter'} .= dequote <<" EOF";
144 The handler "$handlerName" could not be loaded, so this series of News
145 Clipper commands could not be executed.
146 EOF
147 return ();
148 }
149  
150 return @commands;
151 }
152  
153 # ------------------------------------------------------------------------------
154  
155 sub _CommandsValid(@)
156 {
157 my @commands = @_;
158  
159 if ($commands[0][0] ne 'input' || $commands[-1][0] ne 'output')
160 {
161 $errors{'interpreter'} .= dequote<<' EOF';
162 Your sequence of News Clipper commands should begin with an "input"
163 command and end with an "output" command.
164 EOF
165 return 0;
166 }
167  
168 # Count the number of "input" commands
169 {
170 my @count = grep { $_->[0] eq 'input' } @commands;
171  
172 if ($#count != 0)
173 {
174 $errors{'interpreter'} .= dequote<<' EOF';
175 Your sequence of News Clipper commands should have only one "input"
176 command.
177 EOF
178 return 0;
179 }
180 }
181  
182 # Count the number of "output" commands
183 {
184 my @count = grep { $_->[0] eq 'output' } @commands;
185  
186 if ($#count != 0)
187 {
188 $errors{'interpreter'} .= dequote<<' EOF';
189 Your sequence of News Clipper commands should have only one "output"
190 command.
191 EOF
192 return 0;
193 }
194 }
195  
196 return 1;
197 }
198  
199 # ------------------------------------------------------------------------------
200  
201 # Executes a single News Clipper command, invoking _GetData, _FilterData, or
202 # _OutputData depending on the type of command.
203  
204 sub RunHandler
205 {
206 my $handlerName = shift;
207 my $handlerType = shift;
208 my $data = shift;
209 my $attributeList = shift;
210  
211 die qq(Unknown handler type "$handlerType". It should be one of "input",\n) .
212 qq("filter", or "output".\n")
213 unless $handlerType =~ /^(input|filter|output)$/;
214  
215 dprint "Executing handler $handlerName\n";
216  
217 # Ask the HandlerFactory to create a handler for us, based on the name.
218 my $handler = $NewsClipper::Globals::handlerFactory->Create($handlerName);
219  
220 # Now have the handler handle it!
221 if (defined $handler)
222 {
223 if ($handlerType eq 'input')
224 {
225 $data = _GetData($handlerName,$handler,$attributeList);
226  
227 # If the get function failed, or everything was filtered out, quit
228 unless (defined $data)
229 {
230 dprint "Aborting execution for this News Clipper tag.";
231 $errors{'interpreter'} .=
232 "Get function for handler $handlerName failed.\n";
233 return undef;
234 }
235 }
236 elsif (defined $data && $handlerType eq 'filter')
237 {
238 # Typically this happens if a handler calls RunHandler with invalid data
239 return undef unless _DataOK($data,$handlerName,'input');
240  
241 $data = _FilterData($handlerName,$handler,$attributeList,$data);
242  
243 # If the get function failed, or everything was filtered out, quit
244 unless (defined $data)
245 {
246 dprint "Aborting execution for this News Clipper tag.";
247 $errors{'interpreter'} .=
248 "Filter function for handler $handlerName failed.\n";
249 return undef;
250 }
251 }
252 elsif (defined $data && $handlerType eq 'output')
253 {
254 # Typically this happens if a handler calls RunHandler with invalid data
255 return undef unless _DataOK($data,$handlerName,'input');
256  
257 _OutputData($handlerName,$handler,$attributeList,$data);
258 return undef;
259 }
260 }
261  
262 return $data;
263 }
264  
265 # ------------------------------------------------------------------------------
266  
267 # Checks that the internal data of a complex structure is a ref. (Not a ref to
268 # a ref or a plain scalar.)
269  
270 sub _DataOK($$$);
271  
272 sub _DataOK($$$)
273 {
274 my $data = shift;
275 my $handlerName = shift;
276 my $handlerType = shift;
277  
278 unless (defined $data)
279 {
280 $errors{'interpreter'} .=<<" EOF";
281 News Clipper executed the $handlerType handler "$handlerName", but the handler
282 returned an undefined data element, which is not allowed. Please notify the
283 handler author of the problem.
284 EOF
285 return 0;
286 }
287  
288 unless (ref $data)
289 {
290 $errors{'interpreter'} .=<<" EOF";
291 News Clipper executed the $handlerType handler "$handlerName", but the handler
292 returned a data element that was not a reference
293 ("$data"), which is not allowed. Please notify the handler
294 author of the problem.
295 EOF
296 return 0;
297 }
298  
299 if (UNIVERSAL::isa($data,'REF'))
300 {
301 $errors{'interpreter'} .=<<" EOF";
302 News Clipper executed the $handlerType handler $handlerName, but the handler
303 returned a data element that was a reference to a reference, which is not
304 allowed. Please notify the handler author of the problem.
305 EOF
306 return 0;
307 }
308  
309 if (UNIVERSAL::isa($data,'ARRAY'))
310 {
311 foreach my $temp (@$data)
312 {
313 return 0 unless _DataOK($temp,$handlerName,$handlerType);
314 }
315 }
316  
317 if (UNIVERSAL::isa($data,'HASH'))
318 {
319 foreach my $temp (keys %$data)
320 {
321 return 0 unless _DataOK($$data{$temp},$handlerName,$handlerType);
322 }
323 }
324  
325 return 1;
326 }
327  
328 # ------------------------------------------------------------------------------
329  
330 # Checks that the type of the data matches the expected type of the filter or
331 # output handler. This used to be strictly "name equivalence" (types match if
332 # names match, but now its "structural equivalence" (types match if structure
333 # matches).
334  
335 sub _TypesMatch($$$$$)
336 {
337 my $data = shift;
338 my $attributeList = shift;
339 my $handler = shift;
340 my $handlerName = shift;
341 my $type = shift;
342  
343 my $expectedTypes;
344  
345 # Set up default values
346 $attributeList = $handler->ProcessAttributes($attributeList,$type);
347 return undef unless defined $attributeList;
348
349 $expectedTypes = $handler->FilterType($attributeList,$data)
350 if $type eq 'filter';
351 $expectedTypes = $handler->OutputType($attributeList,$data)
352 if $type eq 'output';
353  
354 if ($expectedTypes eq 'NOT SUPPORTED')
355 {
356 $errors{'interpreter'} .= dequote<<" EOF";
357 "$handlerName" can not be used as a $type handler. This
358 normally means that your sequence of News Clipper commands is broken.
359 Try changing "$handlerName" to a more suitable handler.
360 EOF
361 return 0;
362 }
363  
364 dprint "Comparing data type \"",GetTypeSignature($data),"\" to expected ",
365 "types \"$expectedTypes\".";
366  
367 if (TypesMatch($data,$expectedTypes))
368 {
369 dprint "\"",GetTypeSignature($data),"\" is a subtype of ",
370 "\"$expectedTypes\".";
371 return 1;
372 }
373 else
374 {
375 my $translatedExpected = ConvertTypeToEnglish($expectedTypes);
376 my $translatedActual = ConvertTypeToEnglish(GetTypeSignature($data));
377 $errors{'interpreter'} .= dequote<<" EOF";
378 The data expected by "$handlerName" is supposed to be of type
379 "$translatedExpected", but it's actually of type "$translatedActual".
380 This normally means that your sequence of News Clipper commands is
381 broken. Try changing "$handlerName" to a more suitable handler, or use a
382 filter to convert the data from "$translatedActual" to
383 "$translatedExpected".
384 EOF
385 return 0;
386 }
387 }
388  
389 # ------------------------------------------------------------------------------
390  
391 # Calls the Get function of the handler and checks the result for errors.
392  
393 sub _GetData($$$)
394 {
395 my $handlerName = shift;
396 my $handler = shift;
397 my $attributeList = shift;
398  
399 dprint "Calling Get function for handler $handlerName.";
400  
401 # Set up default values
402 $attributeList = $handler->ProcessAttributes($attributeList,'input');
403 return undef unless defined $attributeList;
404  
405 # Get the update times for the handler, and store it in the global variable
406 # @update_times. This value is accessed by AcquisitionFunctions::GetURL
407 # later.
408 @NewsClipper::Interpreter::update_times =
409 _GetUpdateTimes($handlerName,$handler,$attributeList);
410  
411 # Get the data
412 my $data = $handler->Get($attributeList);
413 return undef unless defined $data;
414  
415 return undef unless _DataOK($data,$handlerName,'input');
416  
417 dprint $#{$data}+1," lines acquired" if ref($data) eq "ARRAY";
418 dprint length $$data," characters acquired." if ref($data) eq "SCALAR";
419  
420 return $data;
421 }
422  
423 # ------------------------------------------------------------------------------
424  
425 # This function calls GetUpdateTimes on the handler in order to get the update
426 # times, and checks that the times are reasonable. Returns undef if there is a
427 # problem, or a list of update times.
428  
429 sub _GetUpdateTimes
430 {
431 my $handlerName = shift;
432 my $handler = shift;
433 my $attributeList = shift;
434  
435 my $updateTimesRef = $handler->GetUpdateTimes($attributeList);
436  
437 my @updateTimes = @$updateTimesRef;
438  
439 if (DEBUG)
440 {
441 local $" = ',';
442 dprint "Update times are: @updateTimes";
443 }
444  
445 # Make sure all the time specifications look okay.
446 for my $timeSpec (@updateTimes)
447 {
448 unless ($timeSpec =~ /^[a-z]*\D*[\d ,]*\s*[a-z]{0,5}$/i)
449 {
450 $errors{'interpreter'} .= dequote <<" EOF";
451 There is a problem with your update times for handler $handlerName --
452 "$timeSpec" is an invalid time specification. Please contact the handler
453 author to have the problem fixed.
454 EOF
455 return undef;
456 }
457 }
458  
459 return @updateTimes;
460 }
461  
462 # ------------------------------------------------------------------------------
463  
464 # Calls the Filter function of the handler and checks the result for errors.
465  
466 sub _FilterData($$$$)
467 {
468 my $handlerName = shift;
469 my $handler = shift;
470 my $attributeList = shift;
471 my $data = shift;
472  
473 dprint "Calling Filter function for handler $handlerName.";
474  
475 # Set up default values
476 $attributeList = $handler->ProcessAttributes($attributeList,'filter');
477 return undef unless defined $attributeList;
478  
479 return undef unless _TypesMatch($data,$attributeList,$handler,
480 $handlerName,'filter');
481  
482 # Filter the data
483 $data = $handler->Filter($attributeList,$data);
484 return undef unless defined $data;
485  
486 return undef unless _DataOK($data,$handlerName,'filter');
487  
488 dprint $#{$data}+1," lines filtered." if ref($data) eq "ARRAY";
489 dprint length $$data," characters filtered." if ref($data) eq "SCALAR";
490  
491 return $data;
492 }
493  
494 # ------------------------------------------------------------------------------
495  
496 # Calls the Output function of the handler.
497  
498 sub _OutputData($$$$)
499 {
500 my $handlerName = shift;
501 my $handler = shift;
502 my $attributeList = shift;
503 my $data = shift;
504  
505 dprint "Calling Output function for handler $handlerName.";
506  
507 # Set up default values
508 $attributeList = $handler->ProcessAttributes($attributeList,'output');
509 return undef unless defined $attributeList;
510  
511 my $expectedTypes = $handler->OutputType($attributeList,$data);
512  
513 return undef unless _TypesMatch($data,$attributeList,$handler,
514 $handlerName,'output');
515  
516 $handler->Output($attributeList,$data);
517 }
518  
519 1;