| 1 | package Alvis::Document::Links; | ||
| 2 | |||
| 3 | ######################################################################## | ||
| 4 | # | ||
| 5 | # Storage class for link information | ||
| 6 | # | ||
| 7 | # -- Kimmo Valtonen | ||
| 8 | # | ||
| 9 | ######################################################################## | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use Data::Dumper; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | |||
| 19 | ######################################################################### | ||
| 20 | |||
| 21 | my ($ERR_OK, | ||
| 22 | $ERR_NO_URL, | ||
| 23 | $ERR_NO_TYPE | ||
| 24 | )=(0..2); | ||
| 25 | |||
| 26 | my %ErrMsgs=($ERR_OK=>"", | ||
| 27 | $ERR_NO_URL=>"It would be nice if a URL actually pointed " . | ||
| 28 | "at something.", | ||
| 29 | $ERR_NO_TYPE=>"No type given." | ||
| 30 | ); | ||
| 31 | |||
| 32 | sub new | ||
| 33 | { | ||
| 34 | my $proto=shift; | ||
| 35 | |||
| 36 | my $class=ref($proto)||$proto; | ||
| 37 | my $parent=ref($proto)&&$proto; | ||
| 38 | my $self={}; | ||
| 39 | bless($self,$class); | ||
| 40 | |||
| 41 | $self->_set_err_state($ERR_OK); | ||
| 42 | |||
| 43 | $self->_init(@_); | ||
| 44 | |||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _init | ||
| 49 | { | ||
| 50 | my $self=shift; | ||
| 51 | |||
| 52 | if (defined(@_)) | ||
| 53 | { | ||
| 54 | my %args=@_; | ||
| 55 | @$self{ keys %args }=values(%args); | ||
| 56 | } | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | sub _set_err_state | ||
| 61 | { | ||
| 62 | my $self=shift; | ||
| 63 | my $errcode=shift; | ||
| 64 | my $errmsg=shift; | ||
| 65 | |||
| 66 | if (!defined($errcode)) | ||
| 67 | { | ||
| 68 | confess("set_err_state() called with an undefined argument."); | ||
| 69 | } | ||
| 70 | |||
| 71 | if (exists($ErrMsgs{$errcode})) | ||
| 72 | { | ||
| 73 | if ($errcode==$ERR_OK) | ||
| 74 | { | ||
| 75 | $self->{errstr}=""; | ||
| 76 | } | ||
| 77 | else | ||
| 78 | { | ||
| 79 | $self->{errstr}.=" " . $ErrMsgs{$errcode}; | ||
| 80 | if (defined($errmsg)) | ||
| 81 | { | ||
| 82 | $self->{errstr}.=" " . $errmsg; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | else | ||
| 87 | { | ||
| 88 | confess("Internal error: set_err_state() called with an " . | ||
| 89 | "unrecognized argument ($errcode).") | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub errmsg | ||
| 94 | { | ||
| 95 | my $self=shift; | ||
| 96 | |||
| 97 | return $self->{errstr}; | ||
| 98 | } | ||
| 99 | |||
| 100 | ############################################################################ | ||
| 101 | # | ||
| 102 | # Public methods | ||
| 103 | # | ||
| 104 | ############################################################################ | ||
| 105 | |||
| 106 | sub add | ||
| 107 | { | ||
| 108 | my $self=shift; | ||
| 109 | my $url=shift; | ||
| 110 | my $anchor_txt=shift; | ||
| 111 | my $type=shift; | ||
| 112 | |||
| 113 | if (defined($url)) | ||
| 114 | { | ||
| 115 | if (!defined($type)) | ||
| 116 | { | ||
| 117 | $self->_set_err_state($ERR_NO_TYPE,"URL:$url"); | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | if (defined($anchor_txt)) | ||
| 121 | { | ||
| 122 | $self->{url}{$url}{texts}{$anchor_txt}{type}{$type}++; | ||
| 123 | } | ||
| 124 | else | ||
| 125 | { | ||
| 126 | $self->{url}{$url}{noText}{$type}++; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | else | ||
| 130 | { | ||
| 131 | my $err_str; | ||
| 132 | if (defined($anchor_txt)) | ||
| 133 | { | ||
| 134 | $err_str="Text:\"$anchor_txt\""; | ||
| 135 | } | ||
| 136 | $self->_set_err_state($ERR_NO_URL,$err_str); | ||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | return 1; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub get | ||
| 144 | { | ||
| 145 | my $self=shift; | ||
| 146 | |||
| 147 | my @links=(); | ||
| 148 | |||
| 149 | for my $url (keys %{$self->{url}}) | ||
| 150 | { | ||
| 151 | for my $text (keys %{$self->{url}{$url}{texts}}) | ||
| 152 | { | ||
| 153 | for my $type (keys %{$self->{url}{$url}{texts}{$text}{type}}) | ||
| 154 | { | ||
| 155 | push(@links,[$url,$text,$type]); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | if (exists($self->{url}{$url}{noText})) | ||
| 159 | { | ||
| 160 | for my $type (keys %{$self->{url}{$url}{noText}}) | ||
| 161 | { | ||
| 162 | push(@links,[$url,undef,$type]); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | return @links; | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; | ||
| 171 | __END__ |