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

# 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.2.2 # setbase(urlstring) : set a base url type - return the url object
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.15.2.2 return $base;
105 williamc 1.9 }
106 williamc 1.1
107 williamc 1.9 sub unsetbase {
108     my $self=shift;
109     my $type=shift;
110     my $oref;
111 williamc 1.7
112 williamc 1.9 $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 williamc 1.4 }
121 williamc 1.10 # remove the stack if its empty
122     if ( $#{$self->{basestack}{$type}} == -1 ) {
123     delete $self->{basestack}{$type};
124     }
125 williamc 1.1 }
126    
127 williamc 1.9 sub currentbase {
128 williamc 1.1 my $self=shift;
129     my $type=shift;
130 williamc 1.9 my $rv;
131 williamc 1.1
132 williamc 1.9 if ( exists $self->{basestack}{$type} ) {
133 williamc 1.10 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
134 williamc 1.1 }
135     else {
136 williamc 1.15 $rv=undef;
137 williamc 1.1 }
138 williamc 1.9 return $rv;
139 williamc 1.1 }
140    
141 williamc 1.9 sub checktype($type) {
142 williamc 1.1 my $self=shift;
143 williamc 1.9 my $type=shift;
144 williamc 1.1
145 williamc 1.9 # Check type is supported
146 williamc 1.2 if ( ! exists $self->{urlmodules}{$type} ) {
147 williamc 1.9 die "URLhandler error: Unsupported type $type\n";
148 williamc 1.1 }
149     }
150    
151 williamc 1.9 sub _typehandler {
152 williamc 1.7 my $self=shift;
153 williamc 1.8 my $type=shift;
154 williamc 1.7
155 williamc 1.9 $self->checktype($type);
156 williamc 1.7
157 williamc 1.9 # instantiate only if it dosnt already exist;
158 williamc 1.10 if ( exists $self->{'urlobjs'}{$type} ) {
159     $self->{'urlobjs'}{$type};
160     }
161     else {
162     $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
163     }
164 williamc 1.1 }