ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.19
Committed: Wed Aug 17 11:17:51 2005 UTC (19 years, 8 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_4p1, V1_0_2, V1_0_2_p1
Branch point for: v103_branch
Changes since 1.18: +1 -3 lines
Log Message:
*** empty log message ***

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.16 # 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.16 # expandurl(urlstring) : return the base expanded URLclass of the given string
12 williamc 1.1 #
13     # ----------------------------------------------------------------------
14    
15 williamc 1.5 package URL::URLhandler;
16 williamc 1.1 require 5.004;
17     use Utilities::AddDir;
18 williamc 1.9 use URL::URLcache;
19     use URL::URLclass;
20 williamc 1.1 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 williamc 1.7 $self->{cachestore}=$self->{cache}->filestore();
37 williamc 1.2 use URL::URL_cvs;
38     use URL::URL_file;
39     $self->{urlmodules}={
40 williamc 1.5 'cvs' => 'URL::URL_cvs',
41 sashby 1.19 'file' => 'URL::URL_file'
42 williamc 1.2 };
43 williamc 1.1 }
44    
45 sashby 1.18 sub get
46     {
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     {
59     ($fullurl,$file)=$self->download($origurl, @_);
60     if (-f $file)
61     {
62     my $filemode = 0644; chmod $filemode, $file;
63     }
64     }
65     return ($fullurl, $file);
66     }
67 williamc 1.11
68     sub download {
69     my $self=shift;
70     my $origurl=shift;
71    
72     # Process the URL string
73     my $url=URL::URLclass->new($origurl);
74 williamc 1.9 my $type=$url->type();
75     $urltypehandler=$self->_typehandler($type);
76 williamc 1.15 $url->merge($self->currentbase($type));
77 williamc 1.9
78     # Generate a location name if not provided
79 williamc 1.16 my $nocache=1;
80 williamc 1.9 if ( @_ ) {
81     $location=shift;
82 williamc 1.12 $nocache=0; # dont cache if downloaded to an external location
83 williamc 1.9 }
84     else {
85     $location=$self->{cache}->filename($url->url());
86     }
87 williamc 1.11 # -- get the file from the appropriate handler
88 williamc 1.9 if ( defined $urltypehandler ) {
89     # Call the download module
90     $file=eval{$urltypehandler->get($url, $location)};
91     }
92 williamc 1.4
93 williamc 1.9 # now register it in the cache if successful
94 williamc 1.16 if ( $file ne "" ) {
95     if ( $file && $nocache) {
96     $self->{cache}->store($url->url(), $file);
97     }
98     }
99     return ($url->url(), $file, $urltypehandler->error());
100     }
101    
102     sub expandurl {
103     my $self=shift;
104     my $urlstring=shift;
105    
106     my $url=URL::URLclass->new($urlstring);
107     my $type=$url->type();
108     $url->merge($self->currentbase($type));
109     return $url;
110 williamc 1.4 }
111    
112 williamc 1.9 sub setbase {
113 williamc 1.1 my $self=shift;
114 williamc 1.15 my $partialurl=shift;
115 williamc 1.7
116 williamc 1.15 my $base=URL::URLclass->new($partialurl);
117     my $type=$base->type();
118 williamc 1.9 $self->checktype($type);
119 williamc 1.15 # make a new base-url object
120 williamc 1.10 push @{$self->{"basestack"}{$type}}, $base;
121 williamc 1.16 return $base;
122 williamc 1.9 }
123 williamc 1.1
124 williamc 1.9 sub unsetbase {
125     my $self=shift;
126     my $type=shift;
127     my $oref;
128 williamc 1.7
129 williamc 1.9 $self->checktype($type);
130     # pop off the stack and call the unset base method
131     if ( $#{$self->{basestack}{$type}} >=0 ) {
132     my $base=pop @{$self->{basestack}{$type}};
133     undef $base;
134     }
135     else {
136     die "URLhandler error: Unable to unset type $type\n";
137 williamc 1.4 }
138 williamc 1.10 # remove the stack if its empty
139     if ( $#{$self->{basestack}{$type}} == -1 ) {
140     delete $self->{basestack}{$type};
141     }
142 williamc 1.1 }
143    
144 williamc 1.9 sub currentbase {
145 williamc 1.1 my $self=shift;
146     my $type=shift;
147 williamc 1.9 my $rv;
148 williamc 1.1
149 williamc 1.9 if ( exists $self->{basestack}{$type} ) {
150 williamc 1.10 $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
151 williamc 1.1 }
152     else {
153 williamc 1.15 $rv=undef;
154 williamc 1.1 }
155 williamc 1.9 return $rv;
156 williamc 1.1 }
157    
158 williamc 1.9 sub checktype($type) {
159 williamc 1.1 my $self=shift;
160 williamc 1.9 my $type=shift;
161 williamc 1.1
162 williamc 1.9 # Check type is supported
163 williamc 1.2 if ( ! exists $self->{urlmodules}{$type} ) {
164 williamc 1.9 die "URLhandler error: Unsupported type $type\n";
165 williamc 1.1 }
166     }
167    
168 williamc 1.9 sub _typehandler {
169 williamc 1.7 my $self=shift;
170 williamc 1.8 my $type=shift;
171 williamc 1.7
172 williamc 1.9 $self->checktype($type);
173 williamc 1.7
174 sashby 1.17 # instantiate only if it doesnt already exist;
175 williamc 1.10 if ( exists $self->{'urlobjs'}{$type} ) {
176     $self->{'urlobjs'}{$type};
177     }
178     else {
179     $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
180     }
181 williamc 1.1 }