| 1 | package Alvis::Document::Meta; | ||
| 2 | |||
| 3 | ######################################################################## | ||
| 4 | # | ||
| 5 | # Parses a Meta specification for a document and maps fields to | ||
| 6 | # Dublin Core. | ||
| 7 | # | ||
| 8 | # -- Kimmo Valtonen | ||
| 9 | # | ||
| 10 | ######################################################################## | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | |||
| 14 | $Alvis::Document::Meta::VERSION = '0.1'; | ||
| 15 | |||
| 16 | ######################################################################### | ||
| 17 | # | ||
| 18 | # Mappings | ||
| 19 | # | ||
| 20 | ######################################################################### | ||
| 21 | |||
| 22 | # | ||
| 23 | # Metadata mappings. Ripped off Anders's e-mail. | ||
| 24 | # | ||
| 25 | my %MetaMap=( | ||
| 26 | "rights" => "dc:rights", | ||
| 27 | "coverage" => "dc:coverage", | ||
| 28 | "creator" => "dc:creator", | ||
| 29 | "content" => "dc:description", | ||
| 30 | "geo.country" => "dc:coverage", | ||
| 31 | "email" => "dc:publisher", | ||
| 32 | "language " => "dc:language", | ||
| 33 | "identifier-url" => "dc:identifier", | ||
| 34 | "timemodified" => "dc:date", | ||
| 35 | "last-modified" => "dc:date", | ||
| 36 | "copyright " => "dc:copyright", | ||
| 37 | "classification " => "dc:subject", | ||
| 38 | "url" => "dc:identifier", | ||
| 39 | "timecreated " => "dc:date", | ||
| 40 | "category" => "dc:subject", | ||
| 41 | "description " => "dc:description", | ||
| 42 | "location" => "dc:coverage", | ||
| 43 | "originator" => "dc:creator", | ||
| 44 | "subject" => "dc:subject", | ||
| 45 | "author " => "dc:creator", | ||
| 46 | "publisher " => "dc:publisher", | ||
| 47 | "pd" => "dc:date", | ||
| 48 | "publisher-email" => "dc:publisher", | ||
| 49 | "abstract" => "dc:description", | ||
| 50 | "documenttype" => "dc:type", | ||
| 51 | "content-type"=>"dc:type", | ||
| 52 | "doc-rights" => "dc:rights", | ||
| 53 | "page-topic" => "dc:subject", | ||
| 54 | "keyword" => "dc:subject", | ||
| 55 | "document-rights" => "dc:rights", | ||
| 56 | "keywords " => "dc:subject", | ||
| 57 | "resource-type " => "dc:type", | ||
| 58 | "summary" => "dc:description", | ||
| 59 | "creation-date" => "dc:date", | ||
| 60 | "type " => "dc:type", | ||
| 61 | "document-classification" => "dc:subject", | ||
| 62 | "country" => "dc:coverage", | ||
| 63 | "progid" => "dc:format", | ||
| 64 | "content-language " => "dc:language", | ||
| 65 | "title " => "dc:title", | ||
| 66 | "created" => "dc:date", | ||
| 67 | "doc-type" => "dc:type", | ||
| 68 | "mimetype" => "dc:type", | ||
| 69 | "server"=>"server" | ||
| 70 | ); | ||
| 71 | |||
| 72 | my %DCMap=( | ||
| 73 | "dc:coverage" => "dc:coverage", | ||
| 74 | "dc:date.x-metadatalastmodified" => "dc:date", | ||
| 75 | "dc:language" => "dc:language", | ||
| 76 | "dc:title" => "dc:title", | ||
| 77 | "dc:date.created" => "dc:date", | ||
| 78 | "dc:format" => "dc:format", | ||
| 79 | "dc:description" => "dc:description", | ||
| 80 | "dc:source" => "dc:source", | ||
| 81 | "dc:date.modified" => "dc:date", | ||
| 82 | "dc:creator" => "dc:creator", | ||
| 83 | "dc:coverage.placename" => "dc:coverage", | ||
| 84 | "dc:rights" => "dc:rights", | ||
| 85 | "dc:subject" => "dc:subject", | ||
| 86 | "dc:contributor" => "dc:contributor", | ||
| 87 | "dc:type" => "dc:type", | ||
| 88 | "dc:identifier" => "dc:identifier", | ||
| 89 | "dc:publisher" => "dc:publisher", | ||
| 90 | "dc:date" => "dc:date" | ||
| 91 | ); | ||
| 92 | |||
| 93 | ######################################################################### | ||
| 94 | |||
| 95 | my ($ERR_OK, | ||
| 96 | $ERR_UNK_FIELD_NAME, | ||
| 97 | $ERR_PARSE | ||
| 98 | )=(0..2); | ||
| 99 | |||
| 100 | my %ErrMsgs=($ERR_OK=>"", | ||
| 101 | $ERR_UNK_FIELD_NAME=>"Unrecognized field name.", | ||
| 102 | $ERR_PARSE=>"Parsing the meta text failed." | ||
| 103 | ); | ||
| 104 | |||
| 105 | sub new | ||
| 106 | { | ||
| 107 | my $proto=shift; | ||
| 108 | |||
| 109 | my $class=ref($proto)||$proto; | ||
| 110 | my $parent=ref($proto)&&$proto; | ||
| 111 | my $self={}; | ||
| 112 | bless($self,$class); | ||
| 113 | |||
| 114 | $self->_set_err_state($ERR_OK); | ||
| 115 | |||
| 116 | $self->_init(@_); | ||
| 117 | |||
| 118 | if (defined($self->{text})) | ||
| 119 | { | ||
| 120 | if (!$self->parse($self->{text})) | ||
| 121 | { | ||
| 122 | $self->_set_err_state($ERR_PARSE, | ||
| 123 | "Text:\"$self->{text}\"."); | ||
| 124 | return undef; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | return $self; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _init | ||
| 132 | { | ||
| 133 | my $self=shift; | ||
| 134 | |||
| 135 | if (defined(@_)) | ||
| 136 | { | ||
| 137 | my %args=@_; | ||
| 138 | @$self{ keys %args }=values(%args); | ||
| 139 | } | ||
| 140 | |||
| 141 | } | ||
| 142 | |||
| 143 | sub _set_err_state | ||
| 144 | { | ||
| 145 | my $self=shift; | ||
| 146 | my $errcode=shift; | ||
| 147 | my $errmsg=shift; | ||
| 148 | |||
| 149 | if (!defined($errcode)) | ||
| 150 | { | ||
| 151 | confess("set_err_state() called with an undefined argument."); | ||
| 152 | } | ||
| 153 | |||
| 154 | if (exists($ErrMsgs{$errcode})) | ||
| 155 | { | ||
| 156 | if ($errcode==$ERR_OK) | ||
| 157 | { | ||
| 158 | $self->{errstr}=""; | ||
| 159 | } | ||
| 160 | else | ||
| 161 | { | ||
| 162 | $self->{errstr}.=" " . $ErrMsgs{$errcode}; | ||
| 163 | if (defined($errmsg)) | ||
| 164 | { | ||
| 165 | $self->{errstr}.=" " . $errmsg; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | else | ||
| 170 | { | ||
| 171 | confess("Internal error: set_err_state() called with an " . | ||
| 172 | "unrecognized argument ($errcode).") | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | sub errmsg | ||
| 177 | { | ||
| 178 | my $self=shift; | ||
| 179 | |||
| 180 | return $self->{errstr}; | ||
| 181 | } | ||
| 182 | |||
| 183 | ############################################################################ | ||
| 184 | # | ||
| 185 | # Public methods | ||
| 186 | # | ||
| 187 | ############################################################################ | ||
| 188 | |||
| 189 | # | ||
| 190 | # in UTF-8 | ||
| 191 | # | ||
| 192 | sub parse | ||
| 193 | { | ||
| 194 | my $self=shift; | ||
| 195 | my $meta=shift; | ||
| 196 | |||
| 197 | $self->_set_err_state($ERR_OK); | ||
| 198 | |||
| 199 | my @l=split(/\n/,$meta); | ||
| 200 | for my $l (@l) | ||
| 201 | { | ||
| 202 | chomp $l; | ||
| 203 | my ($name,$value)=split(/\t/,$l,-1); | ||
| 204 | if (!(defined($name) && defined($value))) | ||
| 205 | { | ||
| 206 | next; | ||
| 207 | } | ||
| 208 | |||
| 209 | if ($name=~/^\s*url\s*$/isgo) | ||
| 210 | { | ||
| 211 | $self->{attr}{url}=$value; | ||
| 212 | } | ||
| 213 | elsif ($name=~/^\s*date\s*$/isgo) | ||
| 214 | { | ||
| 215 | $self->{attr}{date}=$value; | ||
| 216 | } | ||
| 217 | elsif ($name=~/^\s*title\s*$/isgo) | ||
| 218 | { | ||
| 219 | $self->{attr}{title}=$value; | ||
| 220 | } | ||
| 221 | elsif ($name=~/^\s*detected\s*\-\s*charset\s*$/isgo) | ||
| 222 | { | ||
| 223 | $self->{attr}{detectedCharSet}=$value; | ||
| 224 | } | ||
| 225 | elsif ($name=~/^\s*Meta\-\s*(.*)$/isgo) | ||
| 226 | { | ||
| 227 | my $metafield=$1; | ||
| 228 | |||
| 229 | $metafield=lc($metafield); | ||
| 230 | if (exists($MetaMap{$metafield})) | ||
| 231 | { | ||
| 232 | $metafield=$MetaMap{$metafield}; | ||
| 233 | if (exists($DCMap{$metafield})) | ||
| 234 | { | ||
| 235 | $metafield=$DCMap{$metafield}; | ||
| 236 | } | ||
| 237 | |||
| 238 | $self->{attr}{$metafield}=$value; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | else | ||
| 242 | { | ||
| 243 | $self->_set_err_state($ERR_UNK_FIELD_NAME, | ||
| 244 | "Field name:\"$name\"."); | ||
| 245 | return 0; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | return 1; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub set | ||
| 253 | { | ||
| 254 | my $self=shift; | ||
| 255 | my $param=shift; | ||
| 256 | my $value=shift; | ||
| 257 | |||
| 258 | $self->{attr}{$param}=$value; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub get | ||
| 262 | { | ||
| 263 | my $self=shift; | ||
| 264 | my $param=shift; | ||
| 265 | |||
| 266 | if (exists($self->{attr}{$param})) | ||
| 267 | { | ||
| 268 | return $self->{attr}{$param}; | ||
| 269 | } | ||
| 270 | else | ||
| 271 | { | ||
| 272 | return undef; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | sub get_dcs | ||
| 277 | { | ||
| 278 | my $self=shift; | ||
| 279 | |||
| 280 | my @dcs=(); | ||
| 281 | |||
| 282 | for my $p (keys %{$self->{attr}}) | ||
| 283 | { | ||
| 284 | if ($p=~/^\s*dc\:.*\s*$/) | ||
| 285 | { | ||
| 286 | push(@dcs,[$p,$self->{attr}{$p}]); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | return @dcs; | ||
| 291 | } | ||
| 292 | |||
| 293 | ############################################################################ | ||
| 294 | # | ||
| 295 | # Private methods | ||
| 296 | # | ||
| 297 | ############################################################################ | ||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | 1; | ||
| 302 | |||
| 303 | __END__ |