| 1 | package Alvis::Document::Type; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | $Alvis::Document::Type::VERSION = '0.1'; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use File::Type; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | ############################################################################# | ||||||||||||||||||||
| 11 | # | ||||||||||||||||||||
| 12 | # Tries to predict the type of a document. Currently pretty crude. | ||||||||||||||||||||
| 13 | # | ||||||||||||||||||||
| 14 | # Kimmo Valtonen | ||||||||||||||||||||
| 15 | # | ||||||||||||||||||||
| 16 | ############################################################################# | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | ############################################################################# | ||||||||||||||||||||
| 19 | # | ||||||||||||||||||||
| 20 | # Error message stuff | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | ############################################################################# | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | my $ErrStr; | ||||||||||||||||||||
| 25 | my ($ERR_OK, | ||||||||||||||||||||
| 26 | $ERR_DOC, | ||||||||||||||||||||
| 27 | $ERR_FILE_TYPE | ||||||||||||||||||||
| 28 | )=(0..2); | ||||||||||||||||||||
| 29 | my %ErrMsgs=($ERR_OK=>"", | ||||||||||||||||||||
| 30 | $ERR_DOC=>"No document.", | ||||||||||||||||||||
| 31 | $ERR_FILE_TYPE=>"Unable to instantiate File::Type" | ||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | sub _set_err_state | ||||||||||||||||||||
| 35 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_set_err_state: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 36 | 0 | my $self=shift; | |||||||||||||||||||
| 37 | 0 | my $errcode=shift; | |||||||||||||||||||
| 38 | 0 | my $errmsg=shift; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | if (!defined($errcode)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | { | ||||||||||||||||||||
| 42 | 0 | confess("set_err_state() called with an undefined argument."); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | if (exists($ErrMsgs{$errcode})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | { | ||||||||||||||||||||
| 47 | 0 | ▶ | if ($errcode==$ERR_OK) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | { | ||||||||||||||||||||
| 49 | 0 | $self->{errstr}=""; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | else | ||||||||||||||||||||
| 52 | { | ||||||||||||||||||||
| 53 | 0 | $self->{errstr}.=" " . $ErrMsgs{$errcode}; | |||||||||||||||||||
| 54 | 0 | ▶ | if (defined($errmsg)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | { | ||||||||||||||||||||
| 56 | 0 | $self->{errstr}.=" " . $errmsg; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | else | ||||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | 0 | confess("Internal error: set_err_state() called with an " . | |||||||||||||||||||
| 63 | "unrecognized argument ($errcode).") | ||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | sub errmsg | ||||||||||||||||||||
| 68 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
errmsg: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 69 | 0 | my $self=shift; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | return $self->{errstr}; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | ############################################################################# | ||||||||||||||||||||
| 75 | # | ||||||||||||||||||||
| 76 | # Methods | ||||||||||||||||||||
| 77 | # | ||||||||||||||||||||
| 78 | ############################################################################## | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | sub new | ||||||||||||||||||||
| 81 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 82 | 0 | my $proto=shift; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | my $class=ref($proto)||$proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 85 | 0 | ▶ | my $parent=ref($proto)&&$proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto && $proto
MC/DC0%0/2
ref $proto && $proto
ref $proto
$proto
Truth table0%0/3
A && B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 86 | 0 | my $self={}; | |||||||||||||||||||
| 87 | 0 | bless($self,$class); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | $self->_init(@_); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | $self->_set_err_state($ERR_OK); | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | $self->{fileType}=File::Type->new(); | |||||||||||||||||||
| 94 | 0 | ▶ | if (!defined($self->{fileType})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | { | ||||||||||||||||||||
| 96 | 0 | $self->_set_err_state($ERR_FILE_TYPE); | |||||||||||||||||||
| 97 | 0 | return undef; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | return $self; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub _init | ||||||||||||||||||||
| 104 | { | ||||||||||||||||||||
| 105 | my $self=shift; | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | $self->{defaultType}='text'; | ||||||||||||||||||||
| 108 | $self->{defaultSubType}='plain'; | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | if (defined(@_)) | ||||||||||||||||||||
| 111 | { | ||||||||||||||||||||
| 112 | my %args=@_; | ||||||||||||||||||||
| 113 | @$self{ keys %args }=values(%args); | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | # | ||||||||||||||||||||
| 118 | # Returns similarly to MIME ($type,$sub_type), but is broader | ||||||||||||||||||||
| 119 | # | ||||||||||||||||||||
| 120 | sub guess | ||||||||||||||||||||
| 121 | { | ||||||||||||||||||||
| 122 | my $self=shift; | ||||||||||||||||||||
| 123 | my $text=shift; | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | $self->_set_err_state($ERR_OK); # clean the slate | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | if (!defined($text)) | ||||||||||||||||||||
| 128 | { | ||||||||||||||||||||
| 129 | $self->_set_err_state($ERR_DOC); | ||||||||||||||||||||
| 130 | return undef; | ||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | my ($type,$sub_type); | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # Try File::Type first | ||||||||||||||||||||
| 136 | my $mime_type=$self->{fileType}->mime_type($text); | ||||||||||||||||||||
| 137 | if (!defined($mime_type)) | ||||||||||||||||||||
| 138 | { | ||||||||||||||||||||
| 139 | $type=$self->{defaultType}; | ||||||||||||||||||||
| 140 | $sub_type=$self->{defaultSubType}; | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | else | ||||||||||||||||||||
| 143 | { | ||||||||||||||||||||
| 144 | ($type,$sub_type)=split(/\//,$mime_type,-1); | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # If the result is a generic one, check for our types of interest | ||||||||||||||||||||
| 148 | # by other means | ||||||||||||||||||||
| 149 | # BTW, File::Type should make it clear and checkable what its | ||||||||||||||||||||
| 150 | # "I dunno" reply is | ||||||||||||||||||||
| 151 | if ($type eq 'application' && $sub_type eq 'octet-stream') | ||||||||||||||||||||
| 152 | { | ||||||||||||||||||||
| 153 | if ($self->_looks_like_HTML($text)) | ||||||||||||||||||||
| 154 | { | ||||||||||||||||||||
| 155 | ($type,$sub_type)=('text','html'); | ||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | elsif ($self->_looks_like_RSS($text)) | ||||||||||||||||||||
| 158 | { | ||||||||||||||||||||
| 159 | # not a MIME type | ||||||||||||||||||||
| 160 | ($type,$sub_type)=('text','rss') | ||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | return ($type,$sub_type); | ||||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | sub _looks_like_HTML | ||||||||||||||||||||
| 169 | { | ||||||||||||||||||||
| 170 | my $self=shift; | ||||||||||||||||||||
| 171 | my $text=shift; | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | # | ||||||||||||||||||||
| 174 | # If we're lucky... | ||||||||||||||||||||
| 175 | # | ||||||||||||||||||||
| 176 | if ($text=~/<!DOCTYPE\s+(\S+)/isgo) | ||||||||||||||||||||
| 177 | { | ||||||||||||||||||||
| 178 | my $type=$1; | ||||||||||||||||||||
| 179 | if ($type=~/(?:html|wml)/igo) | ||||||||||||||||||||
| 180 | { | ||||||||||||||||||||
| 181 | return 1; | ||||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | # Otherwise, use a weaker way of checking... a single | ||||||||||||||||||||
| 185 | # signature start tag will do. | ||||||||||||||||||||
| 186 | # | ||||||||||||||||||||
| 187 | if ($text=~/<(?:(?i)html|body)\W/sgo) | ||||||||||||||||||||
| 188 | { | ||||||||||||||||||||
| 189 | return 1; | ||||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | return 0; | ||||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | sub _looks_like_RSS | ||||||||||||||||||||
| 196 | { | ||||||||||||||||||||
| 197 | my $self=shift; | ||||||||||||||||||||
| 198 | my $text=shift; | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | # | ||||||||||||||||||||
| 201 | # If we're lucky... | ||||||||||||||||||||
| 202 | # | ||||||||||||||||||||
| 203 | if ($text=~/<!DOCTYPE\s+(\S+)/isgo) | ||||||||||||||||||||
| 204 | { | ||||||||||||||||||||
| 205 | my $type=$1; | ||||||||||||||||||||
| 206 | if ($type=~/(?:rss)/igo) | ||||||||||||||||||||
| 207 | { | ||||||||||||||||||||
| 208 | return 1; | ||||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | # Otherwise, use a weaker way of checking... a single | ||||||||||||||||||||
| 212 | # signature start tag will do. | ||||||||||||||||||||
| 213 | # | ||||||||||||||||||||
| 214 | if ($text=~/<(?:(?i)rss|channel)\W/sgo) | ||||||||||||||||||||
| 215 | { | ||||||||||||||||||||
| 216 | return 1; | ||||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | return 0; | ||||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 1; | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | 1; | ||||||||||||||||||||
| 228 | __END__ | ||||||||||||||||||||