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

# Content
1 # url handler -> returns the location of the file
2 # Interface
3 # ---------
4 # new(cache) : A new urlhandler with a defined default cahce directory
5 # download(url,[location]) : as get but always download
6 # 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 # setbase(type,variablehash) : set the url defaults for a specific url type
10 # arguments are dependent on type
11 # unsetbase(type) : deactivate a previously set base
12 # currentbase(type) : return the current base for the given type
13 #
14 # ----------------------------------------------------------------------
15
16 package URL::URLhandler;
17 require 5.004;
18 use Utilities::AddDir;
19 use URL::URLcache;
20 use URL::URLclass;
21 use URL::URLbase;
22 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 $self->{dummybase}=URL::URLbase->new({});
39 $self->{cachestore}=$self->{cache}->filestore();
40 use URL::URL_cvs;
41 use URL::URL_cvsfile;
42 use URL::URL_file;
43 use URL::URL_filed;
44 $self->{urlmodules}={
45 'cvsfile' => 'URL::URL_cvsfile',
46 'cvs' => 'URL::URL_cvs',
47 'file' => 'URL::URL_file',
48 'filed' => 'URL::URL_filed'
49 };
50 }
51
52 sub get {
53 my $self=shift;
54 my $origurl=shift;
55 my $file="";
56
57 my $url=URL::URLclass->new($origurl);
58 my $type=$url->type();
59 $url->expandurl($self->currentbase($type));
60 my $fullurl=$url->url();
61
62 $file=$self->{cache}->file($fullurl);
63 if ( $file eq "" ) {
64 ($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 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 $nocache=1;
81 if ( @_ ) {
82 $location=shift;
83 $nocache=0; # dont cache if downloaded to an external location
84 }
85 else {
86 $location=$self->{cache}->filename($url->url());
87 }
88 # -- get the file from the appropriate handler
89 if ( defined $urltypehandler ) {
90 # Call the download module
91 $file=eval{$urltypehandler->get($url, $location)};
92 }
93
94 # now register it in the cache if successful
95 if ( $file && $nocache) {
96 $self->{cache}->store($url->url(), $location);
97 }
98 return ($url->url(), $file);
99 }
100
101 sub setbase {
102 my $self=shift;
103 my $type=shift;
104 my @args=@_;
105 my $oref;
106
107 $self->checktype($type);
108 # make a new base object
109 my $base=URL::URLbase->new(@_);
110 push @{$self->{"basestack"}{$type}}, $base;
111 }
112
113 sub unsetbase {
114 my $self=shift;
115 my $type=shift;
116 my $oref;
117
118 $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 }
127 # remove the stack if its empty
128 if ( $#{$self->{basestack}{$type}} == -1 ) {
129 delete $self->{basestack}{$type};
130 }
131 }
132
133 sub currentbase {
134 my $self=shift;
135 my $type=shift;
136 my $rv;
137
138 if ( exists $self->{basestack}{$type} ) {
139 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
140 }
141 else {
142 $rv=$self->{dummybase};
143 }
144 return $rv;
145 }
146
147 sub checktype($type) {
148 my $self=shift;
149 my $type=shift;
150
151 # Check type is supported
152 if ( ! exists $self->{urlmodules}{$type} ) {
153 die "URLhandler error: Unsupported type $type\n";
154 }
155 }
156
157 sub _typehandler {
158 my $self=shift;
159 my $type=shift;
160
161 $self->checktype($type);
162
163 # instantiate only if it dosnt already exist;
164 if ( exists $self->{'urlobjs'}{$type} ) {
165 $self->{'urlobjs'}{$type};
166 }
167 else {
168 $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
169 }
170 }