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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines