ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.13
Committed: Wed Mar 29 09:48:33 2000 UTC (25 years, 1 month ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.12: +3 -1 lines
Log Message:
handlers for test type

File Contents

# User Rev Content
1 williamc 1.7 # url handler -> returns the location of the file
2     # Interface
3 williamc 1.1 # ---------
4 williamc 1.7 # new(cache) : A new urlhandler with a defined default cahce directory
5 williamc 1.11 # download(url,[location]) : as get but always download
6 williamc 1.9 # get(url,[location]) : download from the specified url to cache or location
7     # return the full url path name incl. any base expansion
8     # and the filename downloaded to
9 williamc 1.4 # setbase(type,variablehash) : set the url defaults for a specific url type
10 williamc 1.7 # arguments are dependent on type
11 williamc 1.1 # unsetbase(type) : deactivate a previously set base
12 williamc 1.9 # currentbase(type) : return the current base for the given type
13 williamc 1.1 #
14     # ----------------------------------------------------------------------
15    
16 williamc 1.5 package URL::URLhandler;
17 williamc 1.1 require 5.004;
18     use Utilities::AddDir;
19 williamc 1.9 use URL::URLcache;
20     use URL::URLclass;
21     use URL::URLbase;
22 williamc 1.1 use Carp;
23    
24     sub new {
25     my $class=shift;
26     my $cache=shift;
27     $self={};
28     bless $self, $class;
29     $self->init($cache);
30     return $self;
31     }
32    
33     sub init {
34     use Utilities::AddDir;
35     my $self=shift;
36     my $cache=shift;
37     $self->{cache}=$cache;
38 williamc 1.9 $self->{dummybase}=URL::URLbase->new({});
39 williamc 1.7 $self->{cachestore}=$self->{cache}->filestore();
40 williamc 1.2 use URL::URL_cvs;
41 williamc 1.5 use URL::URL_cvsfile;
42 williamc 1.2 use URL::URL_file;
43 williamc 1.13 use URL::URL_test;
44 williamc 1.7 use URL::URL_filed;
45 williamc 1.2 $self->{urlmodules}={
46 williamc 1.5 'cvsfile' => 'URL::URL_cvsfile',
47     'cvs' => 'URL::URL_cvs',
48 williamc 1.7 'file' => 'URL::URL_file',
49 williamc 1.13 'filed' => 'URL::URL_filed',
50     'test' => 'URL::URL_test'
51 williamc 1.2 };
52 williamc 1.1 }
53    
54 williamc 1.7 sub get {
55 williamc 1.4 my $self=shift;
56 williamc 1.9 my $origurl=shift;
57 williamc 1.11 my $file="";
58 williamc 1.9
59 williamc 1.11 my $url=URL::URLclass->new($origurl);
60     my $type=$url->type();
61     $url->expandurl($self->currentbase($type));
62     my $fullurl=$url->url();
63    
64 williamc 1.12 $file=$self->{cache}->file($fullurl);
65     if ( $file eq "" ) {
66 williamc 1.11 ($fullurl,$file)=$self->download($origurl, @_);
67     }
68     return ($fullurl, $file);
69     }
70    
71     sub download {
72     my $self=shift;
73     my $origurl=shift;
74    
75     # Process the URL string
76     my $url=URL::URLclass->new($origurl);
77 williamc 1.9 my $type=$url->type();
78     $urltypehandler=$self->_typehandler($type);
79     $url->expandurl($self->currentbase($type));
80    
81     # Generate a location name if not provided
82 williamc 1.12 $nocache=1;
83 williamc 1.9 if ( @_ ) {
84     $location=shift;
85 williamc 1.12 $nocache=0; # dont cache if downloaded to an external location
86 williamc 1.9 }
87     else {
88     $location=$self->{cache}->filename($url->url());
89     }
90 williamc 1.11 # -- get the file from the appropriate handler
91 williamc 1.9 if ( defined $urltypehandler ) {
92     # Call the download module
93     $file=eval{$urltypehandler->get($url, $location)};
94     }
95 williamc 1.4
96 williamc 1.9 # now register it in the cache if successful
97 williamc 1.12 if ( $file && $nocache) {
98 williamc 1.11 $self->{cache}->store($url->url(), $location);
99 williamc 1.9 }
100 williamc 1.10 return ($url->url(), $file);
101 williamc 1.4 }
102    
103 williamc 1.9 sub setbase {
104 williamc 1.1 my $self=shift;
105 williamc 1.9 my $type=shift;
106     my @args=@_;
107     my $oref;
108 williamc 1.7
109 williamc 1.9 $self->checktype($type);
110     # make a new base object
111     my $base=URL::URLbase->new(@_);
112 williamc 1.10 push @{$self->{"basestack"}{$type}}, $base;
113 williamc 1.9 }
114 williamc 1.1
115 williamc 1.9 sub unsetbase {
116     my $self=shift;
117     my $type=shift;
118     my $oref;
119 williamc 1.7
120 williamc 1.9 $self->checktype($type);
121     # pop off the stack and call the unset base method
122     if ( $#{$self->{basestack}{$type}} >=0 ) {
123     my $base=pop @{$self->{basestack}{$type}};
124     undef $base;
125     }
126     else {
127     die "URLhandler error: Unable to unset type $type\n";
128 williamc 1.4 }
129 williamc 1.10 # remove the stack if its empty
130     if ( $#{$self->{basestack}{$type}} == -1 ) {
131     delete $self->{basestack}{$type};
132     }
133 williamc 1.1 }
134    
135 williamc 1.9 sub currentbase {
136 williamc 1.1 my $self=shift;
137     my $type=shift;
138 williamc 1.9 my $rv;
139 williamc 1.1
140 williamc 1.9 if ( exists $self->{basestack}{$type} ) {
141 williamc 1.10 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
142 williamc 1.1 }
143     else {
144 williamc 1.9 $rv=$self->{dummybase};
145 williamc 1.1 }
146 williamc 1.9 return $rv;
147 williamc 1.1 }
148    
149 williamc 1.9 sub checktype($type) {
150 williamc 1.1 my $self=shift;
151 williamc 1.9 my $type=shift;
152 williamc 1.1
153 williamc 1.9 # Check type is supported
154 williamc 1.2 if ( ! exists $self->{urlmodules}{$type} ) {
155 williamc 1.9 die "URLhandler error: Unsupported type $type\n";
156 williamc 1.1 }
157     }
158    
159 williamc 1.9 sub _typehandler {
160 williamc 1.7 my $self=shift;
161 williamc 1.8 my $type=shift;
162 williamc 1.7
163 williamc 1.9 $self->checktype($type);
164 williamc 1.7
165 williamc 1.9 # instantiate only if it dosnt already exist;
166 williamc 1.10 if ( exists $self->{'urlobjs'}{$type} ) {
167     $self->{'urlobjs'}{$type};
168     }
169     else {
170     $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
171     }
172 williamc 1.1 }