1 |
– |
# |
2 |
– |
# SimpleURLDoc.pm. - Extends SimpleDoc with URL download functionality |
3 |
– |
# |
4 |
– |
# Originally Written by Christopher Williams |
5 |
– |
# |
6 |
– |
# Description |
7 |
– |
# ----------- |
8 |
– |
# |
9 |
– |
# Interface |
10 |
– |
# --------- |
11 |
– |
# new(URLcache[,DocVersionTag] : A new SimpleURLDoc object. You can also |
12 |
– |
# specify an alternative doc version tag |
13 |
– |
# addbasetags(parse) : Add Base Tags to the given parse |
14 |
– |
# urlget(urlstring[,location]) : get the given url - using the cache. |
15 |
– |
# Returns (url, filename) |
16 |
– |
# urldownload(urlstring[,location]) : get the given url ignoring any cached |
17 |
– |
# version. Returns (url, filename) |
18 |
– |
# expandurl(urlstring) : return a URLclass object of the given url expanded |
19 |
– |
# according to the base settings |
20 |
– |
# cache([cache]) : get/set the current URL cache |
21 |
– |
# doctype() : return the (type,version) of the document |
22 |
– |
# as specified by the DocVersionTag |
23 |
– |
|
1 |
|
package ActiveDoc::SimpleURLDoc; |
2 |
|
use ActiveDoc::SimpleDoc; |
26 |
– |
use URL::URLhandler; |
3 |
|
require 5.001; |
4 |
|
@ISA=qw(ActiveDoc::SimpleDoc); |
5 |
|
|
6 |
< |
sub new { |
7 |
< |
my $class=shift; |
8 |
< |
my $self={}; |
9 |
< |
bless $self, $class; |
10 |
< |
$self->cache(shift); |
11 |
< |
$self->_initdoc("doc",@_); |
12 |
< |
#$self->{switch}=ActiveDoc::SimpleDoc->new(@_); |
13 |
< |
return $self; |
14 |
< |
} |
15 |
< |
|
16 |
< |
sub addbasetags { |
17 |
< |
my $self=shift; |
18 |
< |
my $parse=shift; |
19 |
< |
|
20 |
< |
$self->addtag($parse,"base", \&Base_start, $self, |
21 |
< |
"", $self, |
22 |
< |
\&Base_end,$self); |
23 |
< |
} |
24 |
< |
|
25 |
< |
sub cache { |
26 |
< |
my $self=shift; |
27 |
< |
if ( @_ ) { |
28 |
< |
$self->{cache}=shift; |
29 |
< |
$self->{urlhandler}=URL::URLhandler->new($self->{cache}); |
30 |
< |
} |
31 |
< |
return $self->{cache}; |
32 |
< |
} |
33 |
< |
|
34 |
< |
sub expandurl { |
35 |
< |
my $self=shift; |
36 |
< |
my $urlstring=shift; |
37 |
< |
|
38 |
< |
return $self->{urlhandler}->expandurl($urlstring); |
39 |
< |
} |
40 |
< |
|
41 |
< |
sub urldownload { |
42 |
< |
my $self=shift; |
67 |
< |
my $urlstring=shift; |
68 |
< |
|
69 |
< |
($fullurl,$filename)=$self->{urlhandler}->download($urlstring, @_); |
70 |
< |
if ( ( ! defined $filename ) || ( $filename eq "" ) ) { |
71 |
< |
$self->parseerror("Failed to get $fullurl"); |
72 |
< |
} |
73 |
< |
return ($fullurl,$filename); |
74 |
< |
} |
75 |
< |
|
76 |
< |
sub urlget { |
77 |
< |
my $self=shift; |
78 |
< |
my $urlstring=shift; |
79 |
< |
|
80 |
< |
($fullurl,$filename)=$self->{urlhandler}->get($urlstring, @_); |
81 |
< |
if ( ( ! defined $filename ) || ( $filename eq "" ) ) { |
82 |
< |
$self->parseerror("Failed to get $fullurl"); |
83 |
< |
} |
84 |
< |
return ($fullurl,$filename); |
85 |
< |
} |
86 |
< |
|
87 |
< |
#sub AUTOLOAD { |
88 |
< |
# my $self=shift; |
89 |
< |
# |
90 |
< |
# dont propogate destroy methods |
91 |
< |
# return if $AUTOLOAD=~/::DESTROY/; |
92 |
< |
|
93 |
< |
# remove this package name |
94 |
< |
# ($name=$AUTOLOAD)=~s/ActiveDoc::SimpleURLDoc:://; |
95 |
< |
|
96 |
< |
# pass the message to SimpleDoc |
97 |
< |
# $self->{switch}->$name(@_); |
98 |
< |
#} |
99 |
< |
|
100 |
< |
# ------------------------ Support Routines --------------------------- |
6 |
> |
sub new() |
7 |
> |
{ |
8 |
> |
my $class=shift; |
9 |
> |
my $self={}; |
10 |
> |
bless $self, $class; |
11 |
> |
return $self; |
12 |
> |
} |
13 |
> |
|
14 |
> |
sub fullpath () |
15 |
> |
{ |
16 |
> |
my $self=shift; |
17 |
> |
my $src=shift; |
18 |
> |
$src=~s/^\s*file://; |
19 |
> |
my $base=$self->{urlbase}; |
20 |
> |
if ($base ne "") {$src="${base}/${src}";} |
21 |
> |
return $src; |
22 |
> |
} |
23 |
> |
|
24 |
> |
sub urldownload() |
25 |
> |
{ |
26 |
> |
my $self=shift; |
27 |
> |
my $src=$self->fullpath(shift); |
28 |
> |
my $des=shift; |
29 |
> |
$des=~s/^\s*file://; |
30 |
> |
use Utilities::AddDir; |
31 |
> |
if (-f $src) |
32 |
> |
{ |
33 |
> |
use File::Basename; |
34 |
> |
use File::Copy; |
35 |
> |
Utilities::AddDir::adddir(dirname($des)); |
36 |
> |
copy($src,$des); |
37 |
> |
} |
38 |
> |
elsif (-d $src) |
39 |
> |
{ |
40 |
> |
Utilities::AddDir::copydir($src,$des); |
41 |
> |
} |
42 |
> |
} |
43 |
|
|
44 |
|
# ------------------------ Tag Routines ------------------------------- |
45 |
+ |
sub base() |
46 |
+ |
{ |
47 |
+ |
my $self=shift; |
48 |
+ |
my (%attributes)=@_; |
49 |
+ |
if (!exists $self->{configurl}){$self->{configurl}=[];} |
50 |
+ |
my $url=$attributes{'url'}; |
51 |
+ |
$url=~s/^\s*file://; |
52 |
+ |
push @{$self->{configurl}},$url; |
53 |
+ |
$self->{urlbase}=$url; |
54 |
+ |
} |
55 |
+ |
|
56 |
+ |
sub base_() |
57 |
+ |
{ |
58 |
+ |
my $self=shift; |
59 |
+ |
$self->{urlbase}=pop @{$self->{configurl}}; |
60 |
+ |
} |
61 |
|
|
62 |
< |
sub Base_start { |
105 |
< |
my $self=shift; |
106 |
< |
my $name=shift; |
107 |
< |
my $hashref=shift; |
108 |
< |
|
109 |
< |
$self->checktag($name, $hashref, "url"); |
110 |
< |
my $url=$self->{urlhandler}->setbase($$hashref{'url'}); |
111 |
< |
push @{$self->{basestack}}, $url->type(); |
112 |
< |
} |
113 |
< |
|
114 |
< |
sub Base_end { |
115 |
< |
my $self=shift; |
116 |
< |
if ( $#{$self->{basestack}} >= 0 ) { |
117 |
< |
my $type=pop @{$self->{basestack}}; |
118 |
< |
$self->{urlhandler}->unsetbase($type); |
119 |
< |
} |
120 |
< |
else { |
121 |
< |
$self->parseerror("Unmatched <$name>"); |
122 |
< |
} |
123 |
< |
} |
62 |
> |
1; |