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.7 by williamc, Thu Nov 4 10:08:33 1999 UTC vs.
Revision 1.15.2.3 by williamc, Fri Aug 4 18:39:25 2000 UTC

# Line 1 | Line 1
1 # url handler -> returns the location of the file
1   # Interface
2   # ---------
3   # new(cache)       : A new urlhandler with a defined default cahce directory
4 < # get(url)         : download from the specified url to the default cache
5 < # getto(url,dirlocation): download to the specified directory - no cache registry
6 < # setbase(type,variablehash) : set the url defaults for a specific url type
7 < #                              arguments are dependent on type
4 > # download(url,[location]) : as get but always download
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(urlstring) : set a base url type - return the url object
9   # unsetbase(type)  : deactivate a previously set base
10 + # currentbase(type) : return the current base for the given type
11   #
12   # ----------------------------------------------------------------------
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.
13  
14   package URL::URLhandler;
15   require 5.004;
16   use Utilities::AddDir;
17 < use URL::URLUtilities;
18 < use URL::URL_base;
17 > use URL::URLcache;
18 > use URL::URLclass;
19   use Carp;
20  
21   sub new {
# Line 30 | Line 28 | sub new {
28   }
29  
30   sub init {
33        use URL::URLcache;
31          use Utilities::AddDir;
32          my $self=shift;
33          my $cache=shift;
34          $self->{cache}=$cache;
35          $self->{cachestore}=$self->{cache}->filestore();
36          use URL::URL_cvs;
40        use URL::URL_cvsfile;
37          use URL::URL_file;
38 <        use URL::URL_filed;
38 >        use URL::URL_test;
39          $self->{urlmodules}={
44                        'cvsfile' => 'URL::URL_cvsfile',
40                          'cvs' => 'URL::URL_cvs',
41                          'file' => 'URL::URL_file',
42 <                        'filed' => 'URL::URL_filed'
42 >                        'test' => 'URL::URL_test'
43                  };
49        $self->{filebase}="";
50        $self->setbase("file", {}); # Base file as default
51        $self->setbase("filed", {}); # Base file as default
44   }
45  
54 #
46   sub get {
47          my $self=shift;
48 <        my $url=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 <        my ($urlstring, $type, $urltypehandler, $base, $version)
57 <                       =$self->_parseurl($url); # get a unique url string
58 <        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 <          print "Storing ".$urlstring.",".$rv."\n";
68 <          $self->{cache}->store($urlstring, $rv);
56 >        $file=$self->{cache}->file($fullurl);
57 >        if ( $file eq "" ) {
58 >          ($fullurl,$file)=$self->download($origurl, @_);
59          }
60 <        return $rv;
60 >        return ($fullurl, $file);
61   }
62  
63 < sub getto ($@) {
63 > sub download {
64          my $self=shift;
65 <        my $origurl=shift;
76 <        my $dirname=shift;
65 >        my $origurl=shift;
66  
67 <        my $rest;
68 <        my $type;
69 <        my $version;
70 <        my $rv="";
71 <
72 <        # Process the URL string
84 <        my ($urlstring, $type, $urltypehandler, $base, $version)
85 <                                        =$self->_parseurl($origurl);
67 >        # Process the URL string
68 >        my $url=URL::URLclass->new($origurl);
69 >        my $type=$url->type();
70 >        $urltypehandler=$self->_typehandler($type);
71 >        $url->merge($self->currentbase($type));
72 >        print "Attempting download of ".$url->url()."\n";
73  
74 +        # Generate a location name if not provided
75 +        $nocache=1;
76 +        if ( @_ ) {
77 +           $location=shift;
78 +           $nocache=0; # dont cache if downloaded to an external location
79 +        }
80 +        else {
81 +           $location=$self->{cache}->filename($url->url());
82 +        }
83 +        # -- get the file from the appropriate handler
84          if ( defined $urltypehandler ) {
85               # Call the download module
86 <             $rv=eval{$urltypehandler->get($base.(($version ne "")?"\?".$version:""), $dirname); };
87 <        }
86 >             $file=eval{$urltypehandler->get($url, $location)};
87 >        }
88  
89 <        # Check the return type
90 <        if ( defined $rv ) {
91 <        #  if ( $rv!~/^\// ) {
92 <        #       $rv=$dirname."/".$rv;
93 <        #  }
97 <        }
98 <        return $rv;
89 >        # now register it in the cache if successful
90 >        if ( $file && $nocache) {
91 >          $self->{cache}->store($url->url(), $location);
92 >        }
93 >        return ($url->url(), $file);
94   }
95  
96   sub setbase {
97          my $self=shift;
98 <        my $type=shift;
99 <        my @args=@_;
98 >        my $partialurl=shift;
99 >
100 >        my $base=URL::URLclass->new($partialurl);
101 >        my $type=$base->type();
102 >        $self->checktype($type);
103 >        # make a new base-url object
104 >        push @{$self->{"basestack"}{$type}}, $base;
105 >        return $base;
106 > }
107 >
108 > sub unsetbase {
109 >        my $self=shift;
110 >        my $type=shift;
111          my $oref;
112  
113 <        # Check type is supported
114 <        if ( ! exists $self->{urlmodules}{$type} ) {
115 <          print "URLhandler error: Unsupported type $type\n";
116 <          return 1;
113 >        $self->checktype($type);
114 >        # pop off the stack and call the unset base method
115 >        if ( $#{$self->{basestack}{$type}} >=0 ) {
116 >           my $base=pop @{$self->{basestack}{$type}};
117 >           undef $base;
118          }
119          else {
120 <          # A new URL object - pushed onto the stack
121 <          $oref=eval{$self->{urlmodules}{$type}}->new();
122 <          push @{$self->{urlostack}{$type}}, $oref;
123 <          $oref->setbase(@args);
120 >           die "URLhandler error: Unable to unset type $type\n";
121 >        }
122 >        # remove the stack if its empty
123 >        if ( $#{$self->{basestack}{$type}} == -1 ) {
124 >          delete $self->{basestack}{$type};
125          }
126   }
127  
128 < sub unsetbase {
128 > sub currentbase {
129          my $self=shift;
130 <        my $type=shift;
131 <        my $oref;
130 >        my $type=shift;
131 >        my $rv;
132 >
133 >        if ( exists $self->{basestack}{$type} ) {
134 >          $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
135 >        }
136 >        else {
137 >          $rv=undef;
138 >        }
139 >        return $rv;
140 > }
141 >
142 > sub checktype($type) {
143 >        my $self=shift;
144 >        my $type=shift;
145  
146 <        # Check type is supported
146 >        # Check type is supported
147          if ( ! exists $self->{urlmodules}{$type} ) {
148 <          print "URLhandler error: Unsupported type $type\n";
128 <          return 1;
129 <        }
130 <        else {
131 <          # pop off the stack and call the unset base method
132 <          if ( $#{$self->{urlostack}{$type}} >=0 ) {
133 <            $oref=pop @{$self->{urlostack}{$type}};
134 <            $oref->unsetbase();
135 <            undef $oref;
136 <          }
137 <          else {
138 <           print "URLhandler error: Unable to unset type $type\n";
139 <           return 1;
140 <          }
148 >          die "URLhandler error: Unsupported type $type\n";
149          }
150   }
151  
152 < # -------------------- Support Routines (private Methods) -------------
145 <
146 < #
147 < # Process the URL string into its component parts
148 < #
149 < sub _parseurl {
152 > sub _typehandler {
153          my $self=shift;
154 <        my $origurl=shift;
152 <        chomp $origurl;
154 >        my $type=shift;
155  
156 <        my ($type,$rest,$cmdstring)=URLUtilities::parseURL($origurl);
157 <        my $urlstring="";
158 <        my $url;
159 <        
160 <        $base="";
161 <
162 <        # check type are supported
163 <        if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
164 <           print "URLhandler: Unsupported type $type\n";
163 <           die;
164 <        }
165 <        else {
166 <           if ( $#{$self->{urlostack}{$type}} < 0 ) {
167 <             print "URLhandler : base not set for type $type \n";
168 <             die;
169 <           }
170 <           else {
171 <             # ---- type is supported
172 <             $urltypehandler=
173 <                ${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}];
174 <             $base=$urltypehandler->baseurl();
175 <           }
176 <        }
177 <
178 <        my $urlbase=($base ne ""?$base."/":"").$rest;
179 <        $urlstring=$type.":".$urlbase.($cmdstring ne ""?"?".$cmdstring:"");
180 <        return $urlstring, $type, $urltypehandler, $urlbase, $cmdstring;
156 >        $self->checktype($type);
157 >
158 >        # instantiate only if it dosnt already exist;
159 >        if ( exists $self->{'urlobjs'}{$type} ) {
160 >                $self->{'urlobjs'}{$type};
161 >        }      
162 >        else {
163 >                $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
164 >        }
165   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines