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.9 by williamc, Fri Nov 12 17:38:03 1999 UTC vs.
Revision 1.13 by williamc, Wed Mar 29 09:48:33 2000 UTC

# Line 2 | Line 2
2   # Interface
3   # ---------
4   # new(cache)       : A new urlhandler with a defined default cahce directory
5 + # download(url,[location]) : as get but always download
6   # get(url,[location]) : download from the specified url to cache or location
7   #                       return the full url path name incl. any base expansion
8   #                       and the filename downloaded to
# Line 11 | Line 12
12   # currentbase(type) : return the current base for the given type
13   #
14   # ----------------------------------------------------------------------
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.
15  
16   package URL::URLhandler;
17   require 5.004;
# Line 42 | Line 40 | sub init {
40          use URL::URL_cvs;
41          use URL::URL_cvsfile;
42          use URL::URL_file;
43 +        use URL::URL_test;
44          use URL::URL_filed;
45          $self->{urlmodules}={
46                          'cvsfile' => 'URL::URL_cvsfile',
47                          'cvs' => 'URL::URL_cvs',
48                          'file' => 'URL::URL_file',
49 <                        'filed' => 'URL::URL_filed'
49 >                        'filed' => 'URL::URL_filed',
50 >                        'test' => 'URL::URL_test'
51                  };
52        $self->{filebase}="";
53        $self->setbase("file", {}); # Base file as default
54        $self->setbase("filed", {}); # Base file as default
52   }
53  
54   sub get {
55          my $self=shift;
56          my $origurl=shift;
57 +        my $file="";
58  
59 <        # Process the URL string
60 <        $url=URL::URLclass->new($origurl);
59 >        my $url=URL::URLclass->new($origurl);
60 >        my $type=$url->type();
61 >        $url->expandurl($self->currentbase($type));
62 >        my $fullurl=$url->url();
63 >
64 >        $file=$self->{cache}->file($fullurl);
65 >        if ( $file eq "" ) {
66 >          ($fullurl,$file)=$self->download($origurl, @_);
67 >        }
68 >        return ($fullurl, $file);
69 > }
70 >
71 > sub download {
72 >        my $self=shift;
73 >        my $origurl=shift;
74 >
75 >        # Process the URL string
76 >        my $url=URL::URLclass->new($origurl);
77          my $type=$url->type();
78          $urltypehandler=$self->_typehandler($type);
79          $url->expandurl($self->currentbase($type));
80  
81          # Generate a location name if not provided
82 +        $nocache=1;
83          if ( @_ ) {
84             $location=shift;
85 +           $nocache=0; # dont cache if downloaded to an external location
86          }
87          else {
88             $location=$self->{cache}->filename($url->url());
89          }
90 <
75 <        # -- get the file form the appropriate handler
90 >        # -- get the file from the appropriate handler
91          if ( defined $urltypehandler ) {
92               # Call the download module
93               $file=eval{$urltypehandler->get($url, $location)};
94          }
95  
96          # now register it in the cache if successful
97 <        if ( $file ) {
98 <          $self->{cache}->store($fullurl, $rv);
97 >        if ( $file && $nocache) {
98 >          $self->{cache}->store($url->url(), $location);
99          }
100 <        return ($fullurl, $file);
100 >        return ($url->url(), $file);
101   }
102  
103   sub setbase {
# Line 94 | Line 109 | sub setbase {
109          $self->checktype($type);
110          # make a new base object
111          my $base=URL::URLbase->new(@_);
112 <        push @{$self->{basestack}{$type}}, $base;
112 >        push @{$self->{"basestack"}{$type}}, $base;
113   }
114  
115   sub unsetbase {
# Line 111 | Line 126 | sub unsetbase {
126          else {
127             die "URLhandler error: Unable to unset type $type\n";
128          }
129 +        # remove the stack if its empty
130 +        if ( $#{$self->{basestack}{$type}} == -1 ) {
131 +          delete $self->{basestack}{$type};
132 +        }
133   }
134  
135   sub currentbase {
# Line 119 | Line 138 | sub currentbase {
138          my $rv;
139  
140          if ( exists $self->{basestack}{$type} ) {
141 <          $rv=$self->{basestack}{$type}[$#$self->{basestack}{$type}];
141 >          $rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
142          }
143          else {
144            $rv=$self->{dummybase};
# Line 144 | Line 163 | sub _typehandler {
163          $self->checktype($type);
164  
165          # instantiate only if it dosnt already exist;
166 <        (exists $self->{urlobjs}{$type})?$self->{urlobjs}{$type}
167 <                :$self->{urlobjs}{$type}=$self->{urlmodules}{$type}->new();
166 >        if ( exists $self->{'urlobjs'}{$type} ) {
167 >                $self->{'urlobjs'}{$type};
168 >        }      
169 >        else {
170 >                $self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
171 >        }
172   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines