ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.12
Committed: Wed Mar 1 11:28:53 2000 UTC (25 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.11: +5 -9 lines
Log Message:
Remove need to store location specific info

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.7 use URL::URL_filed;
44 williamc 1.2 $self->{urlmodules}={
45 williamc 1.5 'cvsfile' => 'URL::URL_cvsfile',
46     'cvs' => 'URL::URL_cvs',
47 williamc 1.7 'file' => 'URL::URL_file',
48     'filed' => 'URL::URL_filed'
49 williamc 1.2 };
50 williamc 1.1 }
51    
52 williamc 1.7 sub get {
53 williamc 1.4 my $self=shift;
54 williamc 1.9 my $origurl=shift;
55 williamc 1.11 my $file="";
56 williamc 1.9
57 williamc 1.11 my $url=URL::URLclass->new($origurl);
58     my $type=$url->type();
59     $url->expandurl($self->currentbase($type));
60     my $fullurl=$url->url();
61    
62 williamc 1.12 $file=$self->{cache}->file($fullurl);
63     if ( $file eq "" ) {
64 williamc 1.11 ($fullurl,$file)=$self->download($origurl, @_);
65     }
66     return ($fullurl, $file);
67     }
68    
69     sub download {
70     my $self=shift;
71     my $origurl=shift;
72    
73     # Process the URL string
74     my $url=URL::URLclass->new($origurl);
75 williamc 1.9 my $type=$url->type();
76     $urltypehandler=$self->_typehandler($type);
77     $url->expandurl($self->currentbase($type));
78    
79     # Generate a location name if not provided
80 williamc 1.12 $nocache=1;
81 williamc 1.9 if ( @_ ) {
82     $location=shift;
83 williamc 1.12 $nocache=0; # dont cache if downloaded to an external location
84 williamc 1.9 }
85     else {
86     $location=$self->{cache}->filename($url->url());
87     }
88 williamc 1.11 # -- get the file from the appropriate handler
89 williamc 1.9 if ( defined $urltypehandler ) {
90     # Call the download module
91     $file=eval{$urltypehandler->get($url, $location)};
92     }
93 williamc 1.4
94 williamc 1.9 # now register it in the cache if successful
95 williamc 1.12 if ( $file && $nocache) {
96 williamc 1.11 $self->{cache}->store($url->url(), $location);
97 williamc 1.9 }
98 williamc 1.10 return ($url->url(), $file);
99 williamc 1.4 }
100    
101 williamc 1.9 sub setbase {
102 williamc 1.1 my $self=shift;
103 williamc 1.9 my $type=shift;
104     my @args=@_;
105     my $oref;
106 williamc 1.7
107 williamc 1.9 $self->checktype($type);
108     # make a new base object
109     my $base=URL::URLbase->new(@_);
110 williamc 1.10 push @{$self->{"basestack"}{$type}}, $base;
111 williamc 1.9 }
112 williamc 1.1
113 williamc 1.9 sub unsetbase {
114     my $self=shift;
115     my $type=shift;
116     my $oref;
117 williamc 1.7
118 williamc 1.9 $self->checktype($type);
119     # pop off the stack and call the unset base method
120     if ( $#{$self->{basestack}{$type}} >=0 ) {
121     my $base=pop @{$self->{basestack}{$type}};
122     undef $base;
123     }
124     else {
125     die "URLhandler error: Unable to unset type $type\n";
126 williamc 1.4 }
127 williamc 1.10 # remove the stack if its empty
128     if ( $#{$self->{basestack}{$type}} == -1 ) {
129     delete $self->{basestack}{$type};
130     }
131 williamc 1.1 }
132    
133 williamc 1.9 sub currentbase {
134 williamc 1.1 my $self=shift;
135     my $type=shift;
136 williamc 1.9 my $rv;
137 williamc 1.1
138 williamc 1.9 if ( exists $self->{basestack}{$type} ) {
139 williamc 1.10 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
140 williamc 1.1 }
141     else {
142 williamc 1.9 $rv=$self->{dummybase};
143 williamc 1.1 }
144 williamc 1.9 return $rv;
145 williamc 1.1 }
146    
147 williamc 1.9 sub checktype($type) {
148 williamc 1.1 my $self=shift;
149 williamc 1.9 my $type=shift;
150 williamc 1.1
151 williamc 1.9 # Check type is supported
152 williamc 1.2 if ( ! exists $self->{urlmodules}{$type} ) {
153 williamc 1.9 die "URLhandler error: Unsupported type $type\n";
154 williamc 1.1 }
155     }
156    
157 williamc 1.9 sub _typehandler {
158 williamc 1.7 my $self=shift;
159 williamc 1.8 my $type=shift;
160 williamc 1.7
161 williamc 1.9 $self->checktype($type);
162 williamc 1.7
163 williamc 1.9 # instantiate only if it dosnt already exist;
164 williamc 1.10 if ( exists $self->{'urlobjs'}{$type} ) {
165     $self->{'urlobjs'}{$type};
166     }
167     else {
168     $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
169     }
170 williamc 1.1 }