ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.15
Committed: Fri Aug 4 07:47:43 2000 UTC (24 years, 9 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: ProtoEnd
Branch point for: HPWbranch
Changes since 1.14: +8 -13 lines
Log Message:
working versions

File Contents

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