ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.15.2.2
Committed: Fri Aug 4 12:57:47 2000 UTC (24 years, 9 months ago) by williamc
Content type: text/plain
Branch: HPWbranch
Changes since 1.15.2.1: +2 -1 lines
Log Message:
add return value for setbase

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 #
12 # ----------------------------------------------------------------------
13
14 package URL::URLhandler;
15 require 5.004;
16 use Utilities::AddDir;
17 use URL::URLcache;
18 use URL::URLclass;
19 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 $self->{cachestore}=$self->{cache}->filestore();
36 use URL::URL_cvs;
37 use URL::URL_file;
38 use URL::URL_test;
39 $self->{urlmodules}={
40 'cvs' => 'URL::URL_cvs',
41 'file' => 'URL::URL_file',
42 'test' => 'URL::URL_test'
43 };
44 }
45
46 sub get {
47 my $self=shift;
48 my $origurl=shift;
49 my $file="";
50
51 my $url=URL::URLclass->new($origurl);
52 my $type=$url->type();
53 $url->merge($self->currentbase($type));
54 my $fullurl=$url->url();
55
56 $file=$self->{cache}->file($fullurl);
57 if ( $file eq "" ) {
58 ($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 my $type=$url->type();
70 $urltypehandler=$self->_typehandler($type);
71 $url->merge($self->currentbase($type));
72
73 # Generate a location name if not provided
74 $nocache=1;
75 if ( @_ ) {
76 $location=shift;
77 $nocache=0; # dont cache if downloaded to an external location
78 }
79 else {
80 $location=$self->{cache}->filename($url->url());
81 }
82 # -- get the file from the appropriate handler
83 if ( defined $urltypehandler ) {
84 # Call the download module
85 $file=eval{$urltypehandler->get($url, $location)};
86 }
87
88 # now register it in the cache if successful
89 if ( $file && $nocache) {
90 $self->{cache}->store($url->url(), $location);
91 }
92 return ($url->url(), $file);
93 }
94
95 sub setbase {
96 my $self=shift;
97 my $partialurl=shift;
98
99 my $base=URL::URLclass->new($partialurl);
100 my $type=$base->type();
101 $self->checktype($type);
102 # make a new base-url object
103 push @{$self->{"basestack"}{$type}}, $base;
104 return $base;
105 }
106
107 sub unsetbase {
108 my $self=shift;
109 my $type=shift;
110 my $oref;
111
112 $self->checktype($type);
113 # pop off the stack and call the unset base method
114 if ( $#{$self->{basestack}{$type}} >=0 ) {
115 my $base=pop @{$self->{basestack}{$type}};
116 undef $base;
117 }
118 else {
119 die "URLhandler error: Unable to unset type $type\n";
120 }
121 # remove the stack if its empty
122 if ( $#{$self->{basestack}{$type}} == -1 ) {
123 delete $self->{basestack}{$type};
124 }
125 }
126
127 sub currentbase {
128 my $self=shift;
129 my $type=shift;
130 my $rv;
131
132 if ( exists $self->{basestack}{$type} ) {
133 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
134 }
135 else {
136 $rv=undef;
137 }
138 return $rv;
139 }
140
141 sub checktype($type) {
142 my $self=shift;
143 my $type=shift;
144
145 # Check type is supported
146 if ( ! exists $self->{urlmodules}{$type} ) {
147 die "URLhandler error: Unsupported type $type\n";
148 }
149 }
150
151 sub _typehandler {
152 my $self=shift;
153 my $type=shift;
154
155 $self->checktype($type);
156
157 # instantiate only if it dosnt already exist;
158 if ( exists $self->{'urlobjs'}{$type} ) {
159 $self->{'urlobjs'}{$type};
160 }
161 else {
162 $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
163 }
164 }