lib/NewsClipper/Handler.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran n/a Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 121 CC 1 SCAR 6.9
CC
1
Cov
0%
error
2.0
new
2.0
ProcessAttributes
2.0
CRAP 2.0
1 # -*- mode: Perl; -*-
2 package NewsClipper::Handler;
3  
4 # This package contains the Handler class, from which all handlers derive. To
5 # use it, subclass it and redefine the Get, Filter, and Output methods.
6  
7 use strict;
8 use Carp;
9 use Exporter;
10 use File::Cache;
11  
12 use vars qw( $VERSION @ISA @EXPORT );
13  
14 @ISA = qw( Exporter );
15  
16 # We'll import all these APIs so we can pass them down to handlers that derive
17 # from this class.
18 use NewsClipper::AcquisitionFunctions;
19 use NewsClipper::HTMLTools;
20 use NewsClipper::Globals;
21 use NewsClipper::Interpreter qw(RunHandler);
22 use NewsClipper::Types;
23  
24 # We'll export our own function, as well as all the functions in the HTMLTools
25 # API, and all the Globals stuff. This will save us from having to do all that
26 # in the handler.
27 @EXPORT = ("error","RunHandler",@NewsClipper::HTMLTools::EXPORT,
28 @NewsClipper::Globals::EXPORT, @NewsClipper::Types::EXPORT,
29 @NewsClipper::AcquisitionFunctions::EXPORT,
30 'NewsClipper::Interpreter::RunHandler');
31  
32 $VERSION = 0.44;
33  
34 # ------------------------------------------------------------------------------
35  
36 sub error
37 {
38 my $message = join '',@_;
39  
40 # Get the caller's name
41 my $caller = (caller(0))[0];
42 $caller =~ s/.*://s;
43  
44 $message =~ s/\n*$//s;
45  
46 $errors{"handler#$caller"} .= "$message\n";
47  
48 return 1;
49 }
50  
51 # ------------------------------------------------------------------------------
52  
53 sub new
54 {
55 my $proto = shift;
56  
57 # We take the ref if "new" was called on an object, and the class ref
58 # otherwise.
59 my $class = ref($proto) || $proto;
60  
61 # Create an "object"
62 my $self = {};
63  
64 # Make the object a member of the class
65 bless ($self, $class);
66  
67 my $handlerType = $class;
68 $handlerType =~ s/.*::(\w+?)::.*?$/$1/;
69 my $namespace = $class;
70 $namespace =~ s/.*:://;
71  
72 my $cache_key =
73 "$NewsClipper::Globals::home/.NewsClipper/state/$handlerType";
74  
75 # Set up the handler's state
76 $self->{'state'} = new File::Cache (
77 { cache_key => $cache_key,
78 namespace => $namespace,
79 username => '',
80 filemode => 0666,
81 auto_remove_stale => 0,
82 persistence_mechanism => 'Data::Dumper',
83 } );
84  
85 return $self;
86 }
87  
88 # ------------------------------------------------------------------------------
89  
90 # This should be overridden by handlers that have default attribute values.
91  
92 sub ProcessAttributes
93 {
94 my $self = shift;
95 my $attributes = shift;
96 my $handlerRole = shift;
97  
98 return $attributes;
99 }
100  
101 # ------------------------------------------------------------------------------
102  
103 # Overriding this method is optional, but recommended.
104  
105 sub GetDefaultHandlers
106 {
107 my $self = shift;
108 my $attributes = shift;
109  
110 # Sometimes we have to know how the input is called in order to choose a
111 # handler.
112 my $inputAttributes = shift;
113  
114 # The format should be a string that looks like a series of News Clipper
115 # filter and output commands. The last item should be an output handler
116 # description, and the others are filter descriptions.
117 #
118 # my $returnVal =<<EOF;
119 # <filter name='highlight' words='linux,wine,mitnick'>
120 # <output name = 'string'>
121 # EOF
122  
123 return '';
124 }
125  
126 # ------------------------------------------------------------------------------
127  
128 # This should be overridden by data acquisition handlers. (Filter and output
129 # handlers can safely ignore it.)
130  
131 sub ComputeURL
132 {
133 my $self = shift;
134 my $attributes = shift;
135  
136 return 'NO URL PROVIDED';
137 }
138  
139 # ------------------------------------------------------------------------------
140  
141 # This should be overridden by data acquisition handlers. (Filter and output
142 # handlers can safely ignore it.)
143  
144 sub Get
145 {
146 my $self = shift;
147 my $attributes = shift;
148  
149 my $type = ref($self);
150 croak ("$type does not have the ability to do data acquisition.\n");
151 }
152  
153 # ------------------------------------------------------------------------------
154  
155 # Declares what type of data a filter handler can handle. Subclasses should
156 # define this function if they are handlers that can be used as filters.
157  
158 sub FilterType
159 {
160 my $self = shift;
161 my $attributes = shift;
162  
163 return 'NOT SUPPORTED';
164 }
165  
166 # ------------------------------------------------------------------------------
167  
168 # This function is used to filter out some of the data acquired using Get.
169 # Currently it does nothing, but subclasses can override this behavior.
170  
171 sub Filter
172 {
173 my $self = shift;
174 my $attributes = shift;
175 my $grabbedData = shift;
176  
177 # Return a reference to the data.
178 return $grabbedData;
179 }
180  
181 # ------------------------------------------------------------------------------
182  
183 # Declares what type of data a output handler can handle. Subclasses should
184 # define this function if they are handlers that can be used to output data.
185  
186 sub OutputType
187 {
188 my $self = shift;
189 my $attributes = shift;
190 my $data = shift;
191  
192 return 'NOT SUPPORTED';
193 }
194  
195 # ------------------------------------------------------------------------------
196  
197 # This should be overridden by data acquisition handlers. (Filter and output
198 # handlers can safely ignore it.)
199  
200 sub Output
201 {
202 my $self = shift;
203 my $attributes = shift;
204 my $grabbedData = shift;
205 }
206  
207 # ------------------------------------------------------------------------------
208  
209 # Overriding this method is optional.
210  
211 sub GetUpdateTimes
212 {
213 my $self = shift;
214 my $attributes = shift;
215  
216 return ['2,5,8,11,14,17,20,23'];
217 }
218  
219 1;