ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.17
Committed: Thu Jun 13 14:46:22 2002 UTC (22 years, 11 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_0, V1_pre0, SCRAM_V1, SCRAMV1_IMPORT, V0_19_7, V0_19_6, V0_19_6p1, SFATEST
Branch point for: V1_pre1, SCRAM_V1_BRANCH
Changes since 1.16: +1 -3 lines
Log Message:
Finished download page CGI script.

File Contents

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