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.5 by williamc, Wed Sep 29 07:29:16 1999 UTC vs.
Revision 1.9 by williamc, Fri Nov 12 17:38:03 1999 UTC

# Line 1 | Line 1
1 < # url handler -> returns the location of thefilu&# Interface
1 > # url handler -> returns the location of the file
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
7 < # get(url,dirlocation)   : download to the specified directory
4 > # new(cache)       : A new urlhandler with a defined default cahce directory
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(type,variablehash) : set the url defaults for a specific url type
9 < #                              arguments are dependenton type
9 > #                              arguments are dependent on type
10   # unsetbase(type)  : deactivate a previously set base
11 < # setcache(dir)    : set the default cache location
11 > # currentbase(type) : return the current base for the given type
12   #
13   # ----------------------------------------------------------------------
14   # returns file location - or crashes out
# Line 17 | Line 18
18   package URL::URLhandler;
19   require 5.004;
20   use Utilities::AddDir;
21 < use URL::URL_base;
21 > use URL::URLcache;
22 > use URL::URLclass;
23 > use URL::URLbase;
24   use Carp;
25  
26   sub new {
# Line 33 | Line 36 | sub init {
36          use Utilities::AddDir;
37          my $self=shift;
38          my $cache=shift;
36        if (! defined $cache  ) { $cache="/tmp/SCRAMcache" }; # default cache
37        AddDir::adddir($cache);
39          $self->{cache}=$cache;
40 +        $self->{dummybase}=URL::URLbase->new({});
41 +        $self->{cachestore}=$self->{cache}->filestore();
42          use URL::URL_cvs;
43          use URL::URL_cvsfile;
44          use URL::URL_file;
45 +        use URL::URL_filed;
46          $self->{urlmodules}={
47                          'cvsfile' => 'URL::URL_cvsfile',
48                          'cvs' => 'URL::URL_cvs',
49 <                        'file' => 'URL::URL_file'
49 >                        'file' => 'URL::URL_file',
50 >                        'filed' => 'URL::URL_filed'
51                  };
52          $self->{filebase}="";
53          $self->setbase("file", {}); # Base file as default
54 +        $self->setbase("filed", {}); # Base file as default
55   }
56  
57 < sub setcache {
52 <        my $self=shift;
53 <        my $cache=shift;
54 <
55 <        $self->{cache}=$cache;
56 < }
57 <
58 < sub get ($@) {
57 > sub get {
58          my $self=shift;
59          my $origurl=shift;
60 <        my $dirname=shift;
61 <        my $rest;
62 <        my $type;
63 <        my $url;
64 <        my $version;
65 <        my $rv="";
66 <
67 <        if ( ! defined $dirname ) {
68 <          $dirname=$self->{cache};
69 <        }
71 <        chdir $dirname or carp "Unable to Enter Directory $dirname $!\n";
72 <        chomp $origurl;
73 <        # get our version info from the url (after last ??)
74 <        ( $url, $version) = split /\?\?/, $origurl;
75 <        if ( $url=~/:/ ) {
76 <          ($type,$rest) = split /:/, $url;
77 <        }
78 <        else {
79 <           $type='label';
80 <           $rest=$url.":".$version;
81 <        }
82 <        if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
83 <           print "URLhandler: Unsupported type $type\n";
84 <           carp;
85 <        }
86 <        else {
87 <           if ( $#{$self->{urlostack}{$type}} < 0 ) {
88 <                print "URLhandler : base not set for type $type \n";
89 <           }
90 <           else {
91 <             $rv=
92 <             eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}->
93 <                                get($rest);
94 <           }
60 >
61 >        # Process the URL string
62 >        $url=URL::URLclass->new($origurl);
63 >        my $type=$url->type();
64 >        $urltypehandler=$self->_typehandler($type);
65 >        $url->expandurl($self->currentbase($type));
66 >
67 >        # Generate a location name if not provided
68 >        if ( @_ ) {
69 >           $location=shift;
70          }
71 <        if ( $rv ne "" ) {
72 <                $rv=$dirname."/".$rv;
71 >        else {
72 >           $location=$self->{cache}->filename($url->url());
73          }
74 <        return $rv;
74 >
75 >        # -- get the file form the appropriate handler
76 >        if ( defined $urltypehandler ) {
77 >             # Call the download module
78 >             $file=eval{$urltypehandler->get($url, $location)};
79 >        }
80 >
81 >        # now register it in the cache if successful
82 >        if ( $file ) {
83 >          $self->{cache}->store($fullurl, $rv);
84 >        }
85 >        return ($fullurl, $file);
86   }
87  
88   sub setbase {
# Line 105 | Line 91 | sub setbase {
91          my @args=@_;
92          my $oref;
93  
94 <        # Check type is supported
95 <        if ( ! exists $self->{urlmodules}{$type} ) {
96 <          print "URLhandler error: Unsupported type $type\n";
97 <          return 1;
112 <        }
113 <        else {
114 <          # A new URL object - pushed onto the stack
115 <          $oref=eval{$self->{urlmodules}{$type}}->new();
116 <          push @{$self->{urlostack}{$type}}, $oref;
117 <          $oref->setbase(@args);
118 <        }
94 >        $self->checktype($type);
95 >        # make a new base object
96 >        my $base=URL::URLbase->new(@_);
97 >        push @{$self->{basestack}{$type}}, $base;
98   }
99  
100   sub unsetbase {
# Line 123 | Line 102 | sub unsetbase {
102          my $type=shift;
103          my $oref;
104  
105 <        # Check type is supported
105 >        $self->checktype($type);
106 >        # pop off the stack and call the unset base method
107 >        if ( $#{$self->{basestack}{$type}} >=0 ) {
108 >           my $base=pop @{$self->{basestack}{$type}};
109 >           undef $base;
110 >        }
111 >        else {
112 >           die "URLhandler error: Unable to unset type $type\n";
113 >        }
114 > }
115 >
116 > sub currentbase {
117 >        my $self=shift;
118 >        my $type=shift;
119 >        my $rv;
120 >
121 >        if ( exists $self->{basestack}{$type} ) {
122 >          $rv=$self->{basestack}{$type}[$#$self->{basestack}{$type}];
123 >        }
124 >        else {
125 >          $rv=$self->{dummybase};
126 >        }
127 >        return $rv;
128 > }
129 >
130 > sub checktype($type) {
131 >        my $self=shift;
132 >        my $type=shift;
133 >
134 >        # Check type is supported
135          if ( ! exists $self->{urlmodules}{$type} ) {
136 <          print "URLhandler error: Unsupported type $type\n";
129 <          return 1;
130 <        }
131 <        else {
132 <          # pop off the stack and call the unset base method
133 <          if ( $#{$self->{urlostack}{$type}} >=0 ) {
134 <            $oref=pop @{$self->{urlostack}{$type}};
135 <            $oref->unsetbase();
136 <            undef $oref;
137 <          }
138 <          else {
139 <           print "URLhandler error: Unable to unset type $type\n";
140 <           return 1;
141 <          }
136 >          die "URLhandler error: Unsupported type $type\n";
137          }
138   }
139  
140 < # ------------------------ General Support Routines ----------------------------
140 > sub _typehandler {
141 >        my $self=shift;
142 >        my $type=shift;
143  
144 < sub cachefilename {
148 <         my $self=shift;
149 <             use File::Basename;
150 <             use Utilities::AddDir;
151 <             my $filebase=dirname($rest);
152 <             $cache="/tmp/williamc/urlhandler$$";
153 <             adddir($cache);
154 <             $filename=$cache."/".$filebase;
155 < }
144 >        $self->checktype($type);
145  
146 +        # instantiate only if it dosnt already exist;
147 +        (exists $self->{urlobjs}{$type})?$self->{urlobjs}{$type}
148 +                :$self->{urlobjs}{$type}=$self->{urlmodules}{$type}->new();
149 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines