WebSource/Cache.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 102 CC 5 SCAR 23.0
CC
5
Cov
0%
handle
20.0
_init_
6.0
findext
2.0
CRAP 10.0
1 package WebSource::Cache;
2  
3 use strict;
4 use WebSource::Module;
5 #use File::Temp qw/tempfile/;
6 use Carp;
7 #use DB_File;
8  
9 our @ISA = ('WebSource::Module');
10  
11 =head1 NAME
12  
13 WebSource::Cache : saves a copy of the data into a file and forwards it
14  
15 =head1 DESCRIPTION
16  
17 A B<Cache> operator allows to save the data objects which pass thru it
18 into seperate files. Such an operator is described by a DOM Node having the following
19 form :
20  
21 <ws:cache name="opname"
22 directory="dir"
23 template="temp-XXXXX"
24 forward-to="ops" />
25  
26 The C<forward-to> and C<name> attributes have there usual signification.
27  
28 The C<directory> attribut allows to determine which directory is to be used
29 to store the cached files. The C<template> gives the naming template for the
30 cached files (A substring of fives X's is replaced by a number).
31  
32 =head1 SYNOPSIS
33  
34 $cache = WebSource::Cache->new(wsnode => $node);
35  
36 # for the rest it works as a WebSource::Module
37  
38 =head1 METHODS
39  
40 =over 2
41  
42 =item B<< $source = WebSource->new(desc => $node); >>
43  
44 Create a new Cache module;
45  
46 =cut
47  
48 sub _init_ {
49 my $self = shift;
50 $self->SUPER::_init_;
51  
52 my $wsd = $self->{wsdnode};
53 if($wsd) {
54 $self->{cachetemp} = $wsd->getAttribute("template");
55 $self->{cachedir} = $wsd->getAttribute("directory");
56 }
57
58 $self->{cachedir} or $self->{cachedir} = "/tmp";
59 $self->{cachecount} = 0;
60  
61 my %index;
62 # tie %index, "DB_File", $self->{cachedir} . "/index.db";
63 $self->{cacheindex} = \%index;
64  
65 $self->log(1,"Saving fetched files to directory ",$self->{cachedir},
66 " with template ", $self->{cachetemp});
67 }
68  
69 =item B<< $cache->handle($env); >>
70  
71 Saves the envelopes data into a file
72  
73 =cut
74  
75 sub handle {
76 my $self = shift;
77 my $env = shift;
78  
79 my ($fh, $filename);
80 if($self->{cachetemp}) {
81 $filename = $self->{cachetemp};
82 my $strcnt = sprintf("%08d",$self->{cachecount});
83 $filename =~ s/XXXXX/$strcnt/;
84 $self->{cachecount} += 1;
85 } else {
86 $filename = $env->{baseuri};
87 $filename =~ s{[/\.\:\+\&]+}{_}g;
88 }
89  
90 if(! $filename =~ m/\./) {
91 my $ext = findext($env->{type});
92 $filename .= ".$ext";
93 }
94
95 open($fh,'>',$self->{cachedir}.'/'.$filename);
96 if($env->type eq "object/dom-node") {
97 print $fh $env->data->toString(1);
98 } else {
99 print $fh $env->data;
100 }
101 close($fh);
102  
103 $self->{cacheindex}->{$env->{baseuri}} = $filename;
104  
105 $self->log(1,"Cached result for ",$env->{baseuri}," in $filename");
106 return $env;
107 }
108  
109 sub findext {
110 my $t = shift;
111 $t eq "text/string" and return "txt";
112 $t eq "object/dom-node" and return "xml";
113 $t eq "text/html" and return "html";
114 $t eq "text/xml" and return "xml";
115 $t eq "application/pdf" and return "pdf";
116 $t eq "application/postscript" and return "ps";
117 $t eq "application/ms-word" and return "doc";
118 $t eq "application/rtf" and return "rtf";
119  
120 # set txt a default for text
121 $t =~ "^text/" and return "txt";
122 return undef;
123 }
124  
125 =back
126  
127 =head1 SEE ALSO
128  
129 WebSource::Module
130  
131 =cut
132  
133 1;