ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.9
Committed: Fri Nov 12 17:38:03 1999 UTC (25 years, 6 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.8: +69 -95 lines
Log Message:
just saving

File Contents

# Content
1 # url handler -> returns the location of the file
2 # Interface
3 # ---------
4 # new(cache) : A new urlhandler with a defined default cahce directory
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(type,variablehash) : set the url defaults for a specific url type
9 # arguments are dependent on type
10 # unsetbase(type) : deactivate a previously set base
11 # currentbase(type) : return the current base for the given type
12 #
13 # ----------------------------------------------------------------------
14 # returns file location - or crashes out
15 # can pass a file name for the item to be stored as
16 # if not then stores in a default cache.
17
18 package URL::URLhandler;
19 require 5.004;
20 use Utilities::AddDir;
21 use URL::URLcache;
22 use URL::URLclass;
23 use URL::URLbase;
24 use Carp;
25
26 sub new {
27 my $class=shift;
28 my $cache=shift;
29 $self={};
30 bless $self, $class;
31 $self->init($cache);
32 return $self;
33 }
34
35 sub init {
36 use Utilities::AddDir;
37 my $self=shift;
38 my $cache=shift;
39 $self->{cache}=$cache;
40 $self->{dummybase}=URL::URLbase->new({});
41 $self->{cachestore}=$self->{cache}->filestore();
42 use URL::URL_cvs;
43 use URL::URL_cvsfile;
44 use URL::URL_file;
45 use URL::URL_filed;
46 $self->{urlmodules}={
47 'cvsfile' => 'URL::URL_cvsfile',
48 'cvs' => 'URL::URL_cvs',
49 'file' => 'URL::URL_file',
50 'filed' => 'URL::URL_filed'
51 };
52 $self->{filebase}="";
53 $self->setbase("file", {}); # Base file as default
54 $self->setbase("filed", {}); # Base file as default
55 }
56
57 sub get {
58 my $self=shift;
59 my $origurl=shift;
60
61 # Process the URL string
62 $url=URL::URLclass->new($origurl);
63 my $type=$url->type();
64 $urltypehandler=$self->_typehandler($type);
65 $url->expandurl($self->currentbase($type));
66
67 # Generate a location name if not provided
68 if ( @_ ) {
69 $location=shift;
70 }
71 else {
72 $location=$self->{cache}->filename($url->url());
73 }
74
75 # -- get the file form the appropriate handler
76 if ( defined $urltypehandler ) {
77 # Call the download module
78 $file=eval{$urltypehandler->get($url, $location)};
79 }
80
81 # now register it in the cache if successful
82 if ( $file ) {
83 $self->{cache}->store($fullurl, $rv);
84 }
85 return ($fullurl, $file);
86 }
87
88 sub setbase {
89 my $self=shift;
90 my $type=shift;
91 my @args=@_;
92 my $oref;
93
94 $self->checktype($type);
95 # make a new base object
96 my $base=URL::URLbase->new(@_);
97 push @{$self->{basestack}{$type}}, $base;
98 }
99
100 sub unsetbase {
101 my $self=shift;
102 my $type=shift;
103 my $oref;
104
105 $self->checktype($type);
106 # pop off the stack and call the unset base method
107 if ( $#{$self->{basestack}{$type}} >=0 ) {
108 my $base=pop @{$self->{basestack}{$type}};
109 undef $base;
110 }
111 else {
112 die "URLhandler error: Unable to unset type $type\n";
113 }
114 }
115
116 sub currentbase {
117 my $self=shift;
118 my $type=shift;
119 my $rv;
120
121 if ( exists $self->{basestack}{$type} ) {
122 $rv=$self->{basestack}{$type}[$#$self->{basestack}{$type}];
123 }
124 else {
125 $rv=$self->{dummybase};
126 }
127 return $rv;
128 }
129
130 sub checktype($type) {
131 my $self=shift;
132 my $type=shift;
133
134 # Check type is supported
135 if ( ! exists $self->{urlmodules}{$type} ) {
136 die "URLhandler error: Unsupported type $type\n";
137 }
138 }
139
140 sub _typehandler {
141 my $self=shift;
142 my $type=shift;
143
144 $self->checktype($type);
145
146 # instantiate only if it dosnt already exist;
147 (exists $self->{urlobjs}{$type})?$self->{urlobjs}{$type}
148 :$self->{urlobjs}{$type}=$self->{urlmodules}{$type}->new();
149 }