ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
(Generate patch)

Comparing COMP/SCRAM/src/URL/URLhandler.pm (file contents):
Revision 1.4 by williamc, Wed Aug 25 15:43:18 1999 UTC vs.
Revision 1.8 by williamc, Thu Nov 4 13:46:16 1999 UTC

# Line 1 | Line 1
1   # url handler -> returns the location of the file
2 #
2   # Interface
3   # ---------
4 < # new() :
5 < # new(cachedir)       : A new urlhandler with a defined default cahce directory
6 < # get(url)            : download from the specified url to the default cache
8 < # get(url,dirlocation)   : download to the specified directory
4 > # new(cache)       : A new urlhandler with a defined default cahce directory
5 > # get(url)         : download from the specified url to the default cache
6 > # getto(url,dirlocation): download to the specified directory - no cache registry
7   # setbase(type,variablehash) : set the url defaults for a specific url type
8   #                              arguments are dependent on type
9   # unsetbase(type)  : deactivate a previously set base
12 # setcache(dir)    : set the default cache location
10   #
11   # ----------------------------------------------------------------------
12   # returns file location - or crashes out
13   # can pass a file name for the item to be stored as
14   # if not then stores in a default cache.
15  
16 < package URLhandler;
16 > package URL::URLhandler;
17   require 5.004;
18   use Utilities::AddDir;
19 + use URL::URLUtilities;
20   use URL::URL_base;
21   use Carp;
22  
# Line 32 | Line 30 | sub new {
30   }
31  
32   sub init {
33 +        use URL::URLcache;
34          use Utilities::AddDir;
35          my $self=shift;
36          my $cache=shift;
38        if (! defined $cache  ) { $cache="/tmp/SCRAMcache" }; # default cache
39        AddDir::adddir($cache);
37          $self->{cache}=$cache;
38 +        $self->{cachestore}=$self->{cache}->filestore();
39          use URL::URL_cvs;
40 +        use URL::URL_cvsfile;
41          use URL::URL_file;
42 +        use URL::URL_filed;
43          $self->{urlmodules}={
44 <                        'cvs' => 'URL_cvs',
45 <                        'file' => 'URL_file'
44 >                        'cvsfile' => 'URL::URL_cvsfile',
45 >                        'cvs' => 'URL::URL_cvs',
46 >                        'file' => 'URL::URL_file',
47 >                        'filed' => 'URL::URL_filed'
48                  };
49          $self->{filebase}="";
50          $self->setbase("file", {}); # Base file as default
51 +        $self->setbase("filed", {}); # Base file as default
52   }
53  
54 < sub setcache {
54 > #
55 > sub get {
56          my $self=shift;
57 <        my $cache=shift;
57 >        my $url=shift;
58  
59 <        $self->{cache}=$cache;
59 >
60 >        my ($urlstring, $type, $urltypehandler, $base, $version)
61 >                       =$self->_urlexpand($url); # get a unique url string
62 >        my $rv="";
63 >        $rv=$self->getto($url,$self->{cache}->filename($urlstring));
64 >        
65 >        # now register it in the cache if successful
66 >        if ( $rv ne "" ) {
67 >          $self->{cache}->store($urlstring, $rv);
68 >        }
69 >        return $rv;
70   }
71  
72 < sub get ($@) {
72 > sub getto ($@) {
73          my $self=shift;
74          my $origurl=shift;
75          my $dirname=shift;
76 <        my $rest;
63 <        my $type;
64 <        my $url;
65 <        my $version;
76 >
77          my $rv="";
78  
79 <        if ( ! defined $dirname ) {
80 <          $dirname=$self->{cache};
81 <        }
82 <        print "dirname = $dirname \n";
83 <        chdir $dirname or carp "Unable to Enter Directory $dirname $!\n";
84 <        chomp $origurl;
85 <        # 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;
79 >        # Process the URL string
80 >        my ($type,$rest,$version)=URLUtilities::parseURL($origurl);
81 >        my ($urltypehandler, $base)=$self->_typecheck($type);
82 >
83 >        if ( defined $urltypehandler ) {
84 >             # Call the download module
85 >             $rv=eval{$urltypehandler->get($rest.(($version ne "")?"\?".$version:""), $dirname); };
86          }
87          return $rv;
88   }
# Line 143 | Line 130 | sub unsetbase {
130          }
131   }
132  
133 < # ------------------------ General Support Routines ----------------------------
133 > # -------------------- Support Routines (private Methods) -------------
134 >
135 > #
136 > # Process the URL string into its component parts
137 > #
138 > sub _typecheck {
139 >        my $self=shift;
140 >        my $type=shift;
141 >
142 >        my $base="";
143  
144 < sub cachefilename {
145 <         my $self=shift;
146 <             use File::Basename;
147 <             use Utilities::AddDir;
148 <             my $filebase=dirname($rest);
149 <             $cache="/tmp/williamc/urlhandler$$";
150 <             adddir($cache);
151 <             $filename=$cache."/".$filebase;
144 >        # check type are supported
145 >        if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
146 >           print "URLhandler: Unsupported type $type\n";
147 >           die;
148 >        }
149 >        else {
150 >           if ( $#{$self->{urlostack}{$type}} < 0 ) {
151 >             print "URLhandler : base not set for type $type \n";
152 >             die;
153 >           }
154 >           else {
155 >             # ---- type is supported
156 >             $urltypehandler=
157 >                ${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}];
158 >             $base=$urltypehandler->baseurl();
159 >           }
160 >        }
161 >
162 >        return $urltypehandler, $base;
163   }
164  
165 + sub _urlexpand {
166 +        my $self=shift;
167 +        my $url=shift;
168 +
169 +        my ($type,$rest,$cmds)=URLUtilities::parseURL($url);
170 +        my ($urltypehandler, $base)=$self->_typecheck($type);
171 +        
172 +        my $expurl=$type.":".($base ne ""?$base."/":"").$rest.
173 +                        ($cmds ne ""?"\?".$cmds:"");
174 +        return $expurl;
175 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines