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) : A new SimpleURLDoc object
|
12 |
# addbasetags(parse) : Add Base Tags to the given parse
|
13 |
# urlget(urlstring[,location]) : get the given url. Returns (url, filename)
|
14 |
# cache([cache]) : get/set the current URL cache
|
15 |
|
16 |
package ActiveDoc::SimpleURLDoc;
|
17 |
use ActiveDoc::SimpleDoc;
|
18 |
use URL::URLhandler;
|
19 |
require 5.001;
|
20 |
|
21 |
sub new {
|
22 |
my $class=shift;
|
23 |
my $self={};
|
24 |
bless $self, $class;
|
25 |
$self->cache(shift);
|
26 |
$self->{switch}=ActiveDoc::SimpleDoc->new();
|
27 |
return $self;
|
28 |
}
|
29 |
|
30 |
sub addbasetags {
|
31 |
my $self=shift;
|
32 |
my $parse=shift;
|
33 |
|
34 |
$self->{switch}->addtag($parse,"base", \&Base_start, $self,
|
35 |
"", $self,
|
36 |
\&Base_end,$self);
|
37 |
}
|
38 |
|
39 |
sub cache {
|
40 |
my $self=shift;
|
41 |
if ( @_ ) {
|
42 |
$self->{cache}=shift;
|
43 |
$self->{urlhandler}=URL::URLhandler->new($self->{cache});
|
44 |
}
|
45 |
return $self->{cache};
|
46 |
}
|
47 |
|
48 |
sub urlget {
|
49 |
my $self=shift;
|
50 |
my $urlstring=shift;
|
51 |
|
52 |
($fullurl,$filename)=$self->{urlhandler}->get($urlstring, @_);
|
53 |
if ( ( ! defined $filename ) || ( $filename eq "" ) ) {
|
54 |
$self->{switch}->parseerror("Failed to get $fullurl");
|
55 |
}
|
56 |
return ($fullurl,$filename);
|
57 |
}
|
58 |
|
59 |
sub AUTOLOAD {
|
60 |
my $self=shift;
|
61 |
|
62 |
# dont propogate destroy methods
|
63 |
return if $AUTOLOAD=~/::DESTROY/;
|
64 |
|
65 |
# remove this package name
|
66 |
($name=$AUTOLOAD)=~s/ActiveDoc::SimpleURLDoc:://;
|
67 |
|
68 |
# pass the message to SimpleDoc
|
69 |
$self->{switch}->$name(@_);
|
70 |
}
|
71 |
|
72 |
# ------------------------ Support Routines ---------------------------
|
73 |
|
74 |
# ------------------------ Tag Routines -------------------------------
|
75 |
|
76 |
sub Base_start {
|
77 |
my $self=shift;
|
78 |
my $name=shift;
|
79 |
my $hashref=shift;
|
80 |
|
81 |
$self->{switch}->checktag($name, $hashref, "url");
|
82 |
my $url=$self->{urlhandler}->setbase($$hashref{'url'});
|
83 |
push @{$self->{basestack}}, $url->type();
|
84 |
}
|
85 |
|
86 |
sub Base_end {
|
87 |
my $self=shift;
|
88 |
if ( $#{$self->{basestack}} >= 0 ) {
|
89 |
my $type=pop @{$self->{basestack}};
|
90 |
$self->{urlhandler}->unsetbase($type);
|
91 |
}
|
92 |
else {
|
93 |
$self->{switch}->parseerror("Unmatched <$name>");
|
94 |
}
|
95 |
}
|
96 |
|