ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.4
Committed: Wed Aug 25 15:43:18 1999 UTC (25 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.3: +17 -9 lines
Log Message:
Return filename/dir as appropriate from get calls

File Contents

# Content
1 # url handler -> returns the location of the file
2 #
3 # Interface
4 # ---------
5 # new() :
6 # new(cachedir) : A new urlhandler with a defined default cahce directory
7 # get(url) : download from the specified url to the default cache
8 # get(url,dirlocation) : download to the specified directory
9 # setbase(type,variablehash) : set the url defaults for a specific url type
10 # arguments are dependent on type
11 # unsetbase(type) : deactivate a previously set base
12 # setcache(dir) : set the default cache location
13 #
14 # ----------------------------------------------------------------------
15 # returns file location - or crashes out
16 # can pass a file name for the item to be stored as
17 # if not then stores in a default cache.
18
19 package URLhandler;
20 require 5.004;
21 use Utilities::AddDir;
22 use URL::URL_base;
23 use Carp;
24
25 sub new {
26 my $class=shift;
27 my $cache=shift;
28 $self={};
29 bless $self, $class;
30 $self->init($cache);
31 return $self;
32 }
33
34 sub init {
35 use Utilities::AddDir;
36 my $self=shift;
37 my $cache=shift;
38 if (! defined $cache ) { $cache="/tmp/SCRAMcache" }; # default cache
39 AddDir::adddir($cache);
40 $self->{cache}=$cache;
41 use URL::URL_cvs;
42 use URL::URL_file;
43 $self->{urlmodules}={
44 'cvs' => 'URL_cvs',
45 'file' => 'URL_file'
46 };
47 $self->{filebase}="";
48 $self->setbase("file", {}); # Base file as default
49 }
50
51 sub setcache {
52 my $self=shift;
53 my $cache=shift;
54
55 $self->{cache}=$cache;
56 }
57
58 sub get ($@) {
59 my $self=shift;
60 my $origurl=shift;
61 my $dirname=shift;
62 my $rest;
63 my $type;
64 my $url;
65 my $version;
66 my $rv="";
67
68 if ( ! defined $dirname ) {
69 $dirname=$self->{cache};
70 }
71 print "dirname = $dirname \n";
72 chdir $dirname or carp "Unable to Enter Directory $dirname $!\n";
73 chomp $origurl;
74 # get our version info from the url (after last ??)
75 ( $url, $version) = split /\?\?/, $origurl;
76 if ( $url=~/:/ ) {
77 ($type,$rest) = split /:/, $url;
78 }
79 else {
80 $type='label';
81 $rest=$url.":".$version;
82 }
83 if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
84 print "URLhandler: Unsupported type $type\n";
85 carp;
86 }
87 else {
88 if ( $#{$self->{urlostack}{$type}} < 0 ) {
89 print "URLhandler : base not set for type $type \n";
90 }
91 else {
92 $rv=
93 eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}->
94 get($rest);
95 }
96 }
97 if ( $rv ne "" ) {
98 $rv=$dirname."/".$rv;
99 }
100 return $rv;
101 }
102
103 sub setbase {
104 my $self=shift;
105 my $type=shift;
106 my @args=@_;
107 my $oref;
108
109 # Check type is supported
110 if ( ! exists $self->{urlmodules}{$type} ) {
111 print "URLhandler error: Unsupported type $type\n";
112 return 1;
113 }
114 else {
115 # A new URL object - pushed onto the stack
116 $oref=eval{$self->{urlmodules}{$type}}->new();
117 push @{$self->{urlostack}{$type}}, $oref;
118 $oref->setbase(@args);
119 }
120 }
121
122 sub unsetbase {
123 my $self=shift;
124 my $type=shift;
125 my $oref;
126
127 # Check type is supported
128 if ( ! exists $self->{urlmodules}{$type} ) {
129 print "URLhandler error: Unsupported type $type\n";
130 return 1;
131 }
132 else {
133 # pop off the stack and call the unset base method
134 if ( $#{$self->{urlostack}{$type}} >=0 ) {
135 $oref=pop @{$self->{urlostack}{$type}};
136 $oref->unsetbase();
137 undef $oref;
138 }
139 else {
140 print "URLhandler error: Unable to unset type $type\n";
141 return 1;
142 }
143 }
144 }
145
146 # ------------------------ General Support Routines ----------------------------
147
148 sub cachefilename {
149 my $self=shift;
150 use File::Basename;
151 use Utilities::AddDir;
152 my $filebase=dirname($rest);
153 $cache="/tmp/williamc/urlhandler$$";
154 adddir($cache);
155 $filename=$cache."/".$filebase;
156 }
157