[14006] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # CONTENTdmPlugin.pm -- reasonably with-it pdf plugin
|
---|
[14006] | 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999-2001 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
[15872] | 25 | package CONTENTdmPlugin;
|
---|
[14006] | 26 |
|
---|
[15872] | 27 | use ConvertBinaryFile;
|
---|
| 28 | use ReadXMLFile;
|
---|
| 29 |
|
---|
[14006] | 30 | use unicode;
|
---|
| 31 | use ghtml;
|
---|
| 32 |
|
---|
| 33 | use strict;
|
---|
| 34 | no strict 'refs'; # so we can use a var for filehandles (eg STDERR)
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | use XMLParser;
|
---|
| 38 |
|
---|
[15872] | 39 | # inherit ReadXMLFile for the apply_xslt method
|
---|
[14006] | 40 | sub BEGIN {
|
---|
[15872] | 41 | @CONTENTdmPlugin::ISA = ('ConvertBinaryFile', 'ReadXMLFile');
|
---|
[14006] | 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | my $convert_to_list =
|
---|
| 46 | [ { 'name' => "auto",
|
---|
[15872] | 47 | 'desc' => "{ConvertBinaryFile.convert_to.auto}" },
|
---|
[14006] | 48 | { 'name' => "html",
|
---|
[15872] | 49 | 'desc' => "{ConvertBinaryFile.convert_to.html}" },
|
---|
[14006] | 50 | { 'name' => "text",
|
---|
[15872] | 51 | 'desc' => "{ConvertBinaryFile.convert_to.text}" },
|
---|
[14006] | 52 | { 'name' => "pagedimg",
|
---|
[15872] | 53 | 'desc' => "{ConvertBinaryFile.convert_to.pagedimg}"},
|
---|
[14006] | 54 | ];
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | my $arguments =
|
---|
| 59 | [
|
---|
| 60 | { 'name' => "convert_to",
|
---|
[15872] | 61 | 'desc' => "{ConvertBinaryFile.convert_to}",
|
---|
[14006] | 62 | 'type' => "enum",
|
---|
| 63 | 'reqd' => "yes",
|
---|
| 64 | 'list' => $convert_to_list,
|
---|
| 65 | 'deft' => "html" },
|
---|
| 66 | { 'name' => "xslt",
|
---|
[15872] | 67 | 'desc' => "{ReadXMLFile.xslt}",
|
---|
[14006] | 68 | 'type' => "string",
|
---|
| 69 | 'deft' => "",
|
---|
| 70 | 'reqd' => "no" },
|
---|
| 71 | { 'name' => "process_exp",
|
---|
[15872] | 72 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[14006] | 73 | 'type' => "regexp",
|
---|
| 74 | 'deft' => &get_default_process_exp(),
|
---|
| 75 | 'reqd' => "no" },
|
---|
| 76 | { 'name' => "block_exp",
|
---|
[15872] | 77 | 'desc' => "{BasePlugin.block_exp}",
|
---|
[14006] | 78 | 'type' => "regexp",
|
---|
| 79 | 'deft' => &get_default_block_exp() }
|
---|
| 80 | ];
|
---|
| 81 |
|
---|
[15872] | 82 | my $options = { 'name' => "CONTENTdmPlugin",
|
---|
| 83 | 'desc' => "{CONTENTdmPlugin.desc}",
|
---|
[14006] | 84 | 'abstract' => "no",
|
---|
| 85 | 'inherits' => "yes",
|
---|
[15872] | 86 | # CONTENTdmPlugin is one of the few ConvertBinaryFile subclasses whose source doc can't be replaced by a GS-generated html
|
---|
[15114] | 87 | 'srcreplaceable' => "no",
|
---|
[14006] | 88 | 'args' => $arguments };
|
---|
| 89 |
|
---|
| 90 | sub new {
|
---|
| 91 | my ($class) = shift (@_);
|
---|
| 92 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 93 | push(@$pluginlist, $class);
|
---|
| 94 |
|
---|
| 95 | push(@$inputargs,"-title_sub");
|
---|
| 96 | push(@$inputargs,'^(Page\s+\d+)?(\s*1\s+)?');
|
---|
| 97 |
|
---|
[15872] | 98 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 99 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[14006] | 100 |
|
---|
| 101 | my @arg_array = @$inputargs;
|
---|
[15872] | 102 | my $self = new ConvertBinaryFile($pluginlist,$inputargs,$hashArgOptLists);
|
---|
[14006] | 103 |
|
---|
| 104 | if ($self->{'info_only'}) {
|
---|
| 105 | # don't worry about any options etc
|
---|
| 106 | return bless $self, $class;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | my $parser = new XML::Parser('Style' => 'Stream',
|
---|
[19052] | 110 | 'Pkg' => 'ReadXMLFile',
|
---|
[15872] | 111 | 'PluginObj' => $self,
|
---|
[19052] | 112 | 'Handlers' => {'Char' => \&ReadXMLFile::Char,
|
---|
| 113 | 'XMLDecl' => \&ReadXMLFile::XMLDecl,
|
---|
| 114 | 'Entity' => \&ReadXMLFile::Entity,
|
---|
| 115 | 'Doctype' => \&ReadXMLFile::Doctype,
|
---|
| 116 | 'Default' => \&ReadXMLFile::Default
|
---|
[14006] | 117 | });
|
---|
| 118 | $self->{'parser'} = $parser;
|
---|
[19052] | 119 |
|
---|
| 120 |
|
---|
[14006] | 121 | $self->{'rdf_desc'} = undef;
|
---|
| 122 | $self->{'about_key'} = undef;
|
---|
| 123 | $self->{'metadata_name'} = undef;
|
---|
| 124 | $self->{'metadata_value'} = undef;
|
---|
| 125 |
|
---|
[15872] | 126 | $self->{'convert_to'} = "PagedImage";
|
---|
[14006] | 127 | my $secondary_plugin_options = $self->{'secondary_plugin_options'};
|
---|
| 128 |
|
---|
[15872] | 129 | if (!defined $secondary_plugin_options->{'PagedImagePlugin'}){
|
---|
| 130 | $secondary_plugin_options->{'PagedImagePlugin'} = [];
|
---|
[14006] | 131 | }
|
---|
[15872] | 132 | my $pagedimg_options = $secondary_plugin_options->{'PagedImagePlugin'};
|
---|
| 133 | push(@$pagedimg_options, "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
|
---|
[15925] | 134 | push(@$pagedimg_options, "-create_thumbnail", "true", "-create_screenview", "true");
|
---|
[18406] | 135 | push(@$pagedimg_options, "-file_rename_method", "none");
|
---|
[20790] | 136 | push(@$pagedimg_options, "-processing_tmp_files");
|
---|
[14006] | 137 | $self = bless $self, $class;
|
---|
[19052] | 138 |
|
---|
| 139 | # ***** no longer needed!
|
---|
| 140 | # # This needs to be done after blss, to $self passed to XML::Parser
|
---|
| 141 | # # can correctly resolve the right call-back methods during XML parsing
|
---|
| 142 |
|
---|
| 143 |
|
---|
[14006] | 144 | $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
|
---|
| 145 | return $self;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | sub get_default_process_exp {
|
---|
| 149 | my $self = shift (@_);
|
---|
| 150 |
|
---|
| 151 | return q^(?i)\.rdf$^;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | sub get_default_block_exp {
|
---|
| 155 | return q^(?i)\.(jpg|jpeg|gif)$^;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | sub rdf_desc_to_id
|
---|
| 161 | {
|
---|
| 162 | my $self = shift (@_);
|
---|
| 163 | my ($rdf_desc) = @_;
|
---|
| 164 |
|
---|
| 165 | my $rdf_id = {};
|
---|
| 166 |
|
---|
| 167 | # initialise any .cpd (=complex multi page) structures
|
---|
| 168 |
|
---|
| 169 | foreach my $about_key (keys %{$rdf_desc}) {
|
---|
| 170 | if ($about_key =~ m/\.cpd$/) {
|
---|
| 171 | my $about = $rdf_desc->{$about_key};
|
---|
| 172 | my $id = $about->{'dc:identifier'};
|
---|
| 173 |
|
---|
| 174 | if ($id =~ m/^\s*$/) {
|
---|
| 175 | # missing id, make one up based on about attribute
|
---|
| 176 |
|
---|
| 177 | my ($tailname, $dirname, $suffix)
|
---|
| 178 | = &File::Basename::fileparse($about_key, "\\.[^\\.]+\$");
|
---|
| 179 |
|
---|
| 180 | $id = "about:$tailname";
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | $rdf_id->{$id} = $about;
|
---|
| 184 | $rdf_id->{$id}->{'ex:filename'} = $about_key;
|
---|
| 185 | $rdf_id->{$id}->{'ex:type'} = "complex";
|
---|
| 186 | $rdf_id->{$id}->{'pages'} = [];
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | # now add in *non* .cpd items
|
---|
| 192 |
|
---|
| 193 | foreach my $about_key (keys %{$rdf_desc}) {
|
---|
| 194 | if ($about_key !~ m/\.cpd$/) {
|
---|
| 195 | my $about = $rdf_desc->{$about_key};
|
---|
| 196 | my $id = $about->{'dc:identifier'};
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | if ($id =~ m/^\s*$/) {
|
---|
| 200 | # missing id, make one up based on about attribute
|
---|
| 201 |
|
---|
| 202 | my ($tailname, $dirname, $suffix)
|
---|
| 203 | = &File::Basename::fileparse($about_key, "\\.[^\\.]+\$");
|
---|
| 204 |
|
---|
| 205 | $id = "about:$tailname";
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | if (defined $rdf_id->{$id}) {
|
---|
| 209 | $about->{'ex:filename'} = $about_key;
|
---|
| 210 |
|
---|
| 211 | # dealing with complex multi-page situation
|
---|
| 212 | # Add to existing structure
|
---|
| 213 |
|
---|
| 214 | my $pages = $rdf_id->{$id}->{'pages'};
|
---|
| 215 | push(@$pages,$about)
|
---|
| 216 | }
|
---|
| 217 | else {
|
---|
| 218 | # New entry
|
---|
| 219 |
|
---|
| 220 | $rdf_id->{$id} = $about;
|
---|
| 221 | $rdf_id->{$id}->{'ex:type'} = "simple";
|
---|
| 222 | $rdf_id->{$id}->{'ex:filename'} = $about_key;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | return $rdf_id;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | sub metadata_table_txt_file
|
---|
| 233 | {
|
---|
| 234 | my $self = shift (@_);
|
---|
| 235 | my ($output_root,$page_num) = @_;
|
---|
| 236 |
|
---|
| 237 | my $txt_filename = $output_root."_page_$page_num.txt";
|
---|
| 238 |
|
---|
| 239 | my ($tailname, $dirname, $suffix)
|
---|
| 240 | = &File::Basename::fileparse($txt_filename, "\\.[^\\.]+\$");
|
---|
| 241 |
|
---|
[19052] | 242 | my $txt_file = "$tailname$suffix";
|
---|
[14006] | 243 |
|
---|
| 244 | return $txt_file;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | sub output_metadata_table
|
---|
| 249 | {
|
---|
| 250 | my $self = shift (@_);
|
---|
| 251 | my ($page,$page_num,$tmp_dirname,$txt_file) = @_;
|
---|
| 252 |
|
---|
| 253 | my $txt_filename = &util::filename_cat($tmp_dirname,$txt_file);
|
---|
| 254 |
|
---|
| 255 | open(TOUT,">$txt_filename")
|
---|
| 256 | || die "Error: unable to write metadata data out as txt file $txt_filename: $!\n";
|
---|
| 257 |
|
---|
| 258 | print TOUT $page->{'MetadataTable'};
|
---|
| 259 | delete $page->{'MetadataTable'};
|
---|
| 260 |
|
---|
| 261 | close (TOUT);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 | sub rdf_id_to_item_file
|
---|
| 266 | {
|
---|
| 267 | my $self = shift (@_);
|
---|
| 268 | my ($rdf_id,$tmp_dirname,$output_root) = @_;
|
---|
| 269 |
|
---|
| 270 | my $item_file_list = [];
|
---|
| 271 |
|
---|
| 272 | foreach my $id (keys %{$rdf_id}) {
|
---|
| 273 |
|
---|
| 274 | my $id_safe = $id;
|
---|
| 275 | $id_safe =~ s/ /-/g;
|
---|
| 276 |
|
---|
| 277 | my $output_filename = $output_root."_$id_safe.item";
|
---|
| 278 | open(FOUT,">$output_filename")
|
---|
| 279 | || die "Unable to open $output_filename: $!\n";
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | print FOUT "<PagedDocument>\n";
|
---|
| 283 |
|
---|
| 284 | my $rdf_doc = $rdf_id->{$id};
|
---|
| 285 | foreach my $metadata_name (keys %$rdf_doc) {
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | next if ($metadata_name eq "pages");
|
---|
| 289 |
|
---|
| 290 | my $metadata_value = $rdf_doc->{$metadata_name};
|
---|
| 291 |
|
---|
| 292 | # convert ns:name to ns.Name
|
---|
| 293 | $metadata_name =~ s/^(.*?):(.*)/$1\.\u$2/;
|
---|
| 294 |
|
---|
| 295 | print FOUT " <Metadata name=\"$metadata_name\">$metadata_value</Metadata>\n";
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | if ($rdf_doc->{'ex:type'} eq "complex") {
|
---|
| 299 | my $pages = $rdf_doc->{'pages'};
|
---|
| 300 | my $page_num = 1;
|
---|
| 301 |
|
---|
| 302 | foreach my $page (@$pages) {
|
---|
| 303 |
|
---|
| 304 | my $imgfile = $page->{'ex:filename'};
|
---|
| 305 | if ($imgfile =~ m/(http|ftp):/) {
|
---|
| 306 | $imgfile = "empty.jpg";
|
---|
| 307 | }
|
---|
| 308 | else {
|
---|
| 309 | $imgfile = &util::filename_cat("..","import",$imgfile);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | my $txt_file
|
---|
| 313 | = $self->metadata_table_txt_file($output_root,$page_num);
|
---|
| 314 |
|
---|
| 315 | $self->output_metadata_table($page,$page_num,
|
---|
| 316 | $tmp_dirname,$txt_file);
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | print FOUT " <Page pagenum=\"$page_num\" imgfile=\"$imgfile\" txtfile=\"$txt_file\">\n";
|
---|
| 320 |
|
---|
| 321 | foreach my $metadata_name (keys %$page) {
|
---|
| 322 |
|
---|
| 323 | my $metadata_value = $rdf_doc->{$metadata_name};
|
---|
| 324 | # convert ns:name to ns.Name
|
---|
| 325 | $metadata_name =~ s/^(.*?):(.*)/$1\.\u$2/;
|
---|
| 326 |
|
---|
| 327 | print FOUT " <Metadata name=\"$metadata_name\">$metadata_value</Metadata>\n";
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | $page_num++;
|
---|
| 332 |
|
---|
| 333 |
|
---|
| 334 | print FOUT " </Page>\n";
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 | else {
|
---|
| 338 | # simple
|
---|
| 339 | # duplicate top-level metadata for now plus image to bind to
|
---|
| 340 |
|
---|
| 341 | my $imgfile = $rdf_doc->{'ex:filename'};
|
---|
| 342 | if ($imgfile =~ m/(http|ftp):/) {
|
---|
| 343 | $imgfile = "empty.jpg";
|
---|
| 344 | }
|
---|
| 345 | else {
|
---|
| 346 | $imgfile = &util::filename_cat("..","import",$imgfile);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 | my $txt_file = $self->metadata_table_txt_file($output_root,1);
|
---|
| 351 | $self->output_metadata_table($rdf_doc,1,$tmp_dirname,$txt_file);
|
---|
| 352 |
|
---|
| 353 | print FOUT " <Page pagenum=\"1\" imgfile=\"$imgfile\" txtfile=\"$txt_file\">\n";
|
---|
| 354 | foreach my $metadata_name (keys %$rdf_doc) {
|
---|
| 355 |
|
---|
| 356 | my $metadata_value = $rdf_doc->{$metadata_name};
|
---|
| 357 |
|
---|
| 358 | # convert ns:name to ns.Name
|
---|
| 359 | $metadata_name =~ s/^(.*?):(.*)/$1\.\u$2/;
|
---|
| 360 |
|
---|
| 361 | print FOUT " <Metadata name=\"$metadata_name\">$metadata_value</Metadata>\n";
|
---|
| 362 | }
|
---|
| 363 | print FOUT " </Page>\n";
|
---|
| 364 |
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | print FOUT "</PagedDocument>\n";
|
---|
| 368 | close(FOUT);
|
---|
| 369 |
|
---|
| 370 | push(@$item_file_list,$output_filename);
|
---|
| 371 |
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 |
|
---|
| 375 | return $item_file_list;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | sub xml_area_convert_file
|
---|
| 381 | {
|
---|
| 382 | my $self = shift (@_);
|
---|
| 383 | my ($input_filename, $tmp_dirname, $output_root) = @_;
|
---|
| 384 |
|
---|
| 385 | eval {
|
---|
| 386 | # Build up hash table/tree of all records
|
---|
| 387 |
|
---|
| 388 | my $xslt = $self->{'xslt'};
|
---|
| 389 | if (defined $xslt && ($xslt ne "")) {
|
---|
| 390 | # perform xslt
|
---|
| 391 | my $transformed_xml = $self->apply_xslt($xslt,$input_filename);
|
---|
| 392 |
|
---|
[19052] | 393 | open(TOUT,">/tmp/tout.xml")
|
---|
| 394 | || die "Unable to open /tmp/tout.xml: $!\n";
|
---|
| 395 | print TOUT $transformed_xml;
|
---|
| 396 | close(TOUT);
|
---|
| 397 |
|
---|
| 398 |
|
---|
[14006] | 399 | # feed transformed file (now in memory as string) into XML parser
|
---|
| 400 | $self->{'parser'}->parse($transformed_xml);
|
---|
| 401 | }
|
---|
| 402 | else {
|
---|
| 403 | $self->{'parser'}->parsefile($input_filename);
|
---|
| 404 | }
|
---|
| 405 | };
|
---|
| 406 |
|
---|
| 407 | if ($@) {
|
---|
| 408 |
|
---|
| 409 | # parsefile may either croak somewhere in XML::Parser (e.g. because
|
---|
| 410 | # the document is not well formed) or die somewhere in XMLPlug or a
|
---|
| 411 | # derived plugin (e.g. because we're attempting to process a
|
---|
| 412 | # document whose DOCTYPE is not meant for this plugin). For the
|
---|
| 413 | # first case we'll print a warning and continue, for the second
|
---|
| 414 | # we'll just continue quietly
|
---|
| 415 |
|
---|
| 416 | print STDERR "**** Error is: $@\n";
|
---|
| 417 |
|
---|
| 418 | my $file = $self->{'file'};
|
---|
| 419 |
|
---|
| 420 | my ($msg) = $@ =~ /Carp::croak\(\'(.*?)\'\)/;
|
---|
| 421 | if (defined $msg) {
|
---|
| 422 | my $outhandle = $self->{'outhandle'};
|
---|
| 423 | my $plugin_name = ref ($self);
|
---|
| 424 | print $outhandle "$plugin_name failed to process $file ($msg)\n";
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | my $gli = $self->{'gli'};
|
---|
| 428 |
|
---|
| 429 | # reset ourself for the next document
|
---|
| 430 | $self->{'section_level'}=0;
|
---|
| 431 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
| 432 | return ("fail",undef); # error during processing
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | my $rdf_desc = $self->{'rdf_desc'};
|
---|
| 436 |
|
---|
| 437 | # foreach my $about_key (keys %{$rdf_desc}) {
|
---|
| 438 | # my $about = $rdf_desc->{$about_key};
|
---|
| 439 | # foreach my $metadata_name (keys %{$about}) {
|
---|
| 440 | #
|
---|
| 441 | # my $metadata_value = $about->{$metadata_name};
|
---|
| 442 | ## print STDERR " $metadata_name: $metadata_value\n";
|
---|
| 443 | # }
|
---|
| 444 | # }
|
---|
| 445 |
|
---|
| 446 |
|
---|
| 447 | # Merge entries with same name
|
---|
| 448 |
|
---|
| 449 |
|
---|
| 450 | my $merged_rdf_id = $self->rdf_desc_to_id($rdf_desc);
|
---|
| 451 |
|
---|
| 452 | # foreach my $about_key (keys %{$merged_rdf_id}) {
|
---|
| 453 | # my $about = $merged_rdf_id->{$about_key};
|
---|
| 454 | # foreach my $metadata_name (keys %{$about}) {
|
---|
| 455 | #
|
---|
| 456 | # my $metadata_value = $about->{$metadata_name};
|
---|
| 457 | ## print STDERR " $metadata_name: $metadata_value\n";
|
---|
| 458 | # }
|
---|
| 459 | # }
|
---|
| 460 |
|
---|
| 461 |
|
---|
| 462 |
|
---|
| 463 | my $item_files = $self->rdf_id_to_item_file($merged_rdf_id,$tmp_dirname,
|
---|
| 464 | $output_root);
|
---|
| 465 |
|
---|
| 466 | return ("item",$item_files);
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 |
|
---|
[15872] | 470 | # Override ConvertBinaryFile tmp_area_convert_file() to provide solution specific
|
---|
[14006] | 471 | # to CONTENTdm
|
---|
| 472 | #
|
---|
| 473 | # A better (i.e. in the future) solution would be to see if this can be
|
---|
| 474 | # shifted into gsConvert.pl so there is no need to override the
|
---|
| 475 | # default tmp_area_convert_file()
|
---|
| 476 |
|
---|
| 477 |
|
---|
| 478 | sub tmp_area_convert_file {
|
---|
| 479 | my $self = shift (@_);
|
---|
| 480 | my ($output_ext, $input_filename, $textref) = @_;
|
---|
| 481 |
|
---|
| 482 | # is textref ever used?!?
|
---|
| 483 |
|
---|
| 484 | my $outhandle = $self->{'outhandle'};
|
---|
| 485 | my $convert_to = $self->{'convert_to'};
|
---|
| 486 | my $failhandle = $self->{'failhandle'};
|
---|
| 487 | my $convert_to_ext = $self->{'convert_to_ext'};
|
---|
| 488 |
|
---|
| 489 | # softlink to collection tmp dir
|
---|
| 490 | my $tmp_dirname
|
---|
| 491 | = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
|
---|
| 492 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
| 493 |
|
---|
| 494 | # derive tmp filename from input filename
|
---|
| 495 | my ($tailname, $dirname, $suffix)
|
---|
| 496 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
| 497 |
|
---|
| 498 | # Remove any white space from filename -- no risk of name collision, and
|
---|
| 499 | # makes later conversion by utils simpler. Leave spaces in path...
|
---|
| 500 | # tidy up the filename with space, dot, hyphen between
|
---|
| 501 | $tailname =~ s/\s+//g;
|
---|
| 502 | $tailname =~ s/\.+//g;
|
---|
| 503 | $tailname =~ s/\-+//g;
|
---|
[16580] | 504 |
|
---|
| 505 | $tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
|
---|
[14006] | 506 | $suffix = lc($suffix);
|
---|
| 507 | my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
|
---|
| 508 |
|
---|
| 509 | &util::soft_link($input_filename, $tmp_filename);
|
---|
| 510 | my $verbosity = $self->{'verbosity'};
|
---|
| 511 | if ($verbosity > 0) {
|
---|
| 512 | print $outhandle "Converting $tailname$suffix to $convert_to format\n";
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | my $errlog = &util::filename_cat($tmp_dirname, "err.log");
|
---|
| 516 |
|
---|
| 517 | # call xml_area_convert_file rather than gsConvert.pl
|
---|
| 518 |
|
---|
| 519 | my $output_root = &util::filename_cat($tmp_dirname, "$tailname");
|
---|
| 520 |
|
---|
| 521 | my ($output_type,$item_files)
|
---|
| 522 | = $self->xml_area_convert_file($tmp_filename,$tmp_dirname,$output_root);
|
---|
| 523 |
|
---|
| 524 |
|
---|
| 525 | my $fakeimg_filename = &util::filename_cat($dirname, "empty.jpg");
|
---|
| 526 | my $fakeimg_tmp_filename = &util::filename_cat($tmp_dirname, "empty.jpg");
|
---|
| 527 |
|
---|
[19052] | 528 | print STDERR "***** No source image identified with item\n";
|
---|
[14006] | 529 |
|
---|
[19052] | 530 | print STDERR "***** Using default \"no image available\" $fakeimg_filename -> $fakeimg_tmp_filename\n";
|
---|
| 531 |
|
---|
[14006] | 532 | &util::soft_link($fakeimg_filename, $fakeimg_tmp_filename);
|
---|
| 533 |
|
---|
| 534 | # continue as before ...
|
---|
| 535 |
|
---|
| 536 | # remove symbolic link to original file
|
---|
| 537 | &util::rm($tmp_filename);
|
---|
| 538 |
|
---|
| 539 | # Check STDERR here
|
---|
| 540 | chomp $output_type;
|
---|
| 541 | if ($output_type eq "fail") {
|
---|
| 542 | print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
|
---|
| 543 | print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
|
---|
| 544 | $self->{'num_not_processed'} ++;
|
---|
| 545 | if (-s "$errlog") {
|
---|
| 546 | open(ERRLOG, "$errlog");
|
---|
| 547 | while (<ERRLOG>) {
|
---|
| 548 | print $outhandle "$_";
|
---|
| 549 | }
|
---|
| 550 | print $outhandle "\n";
|
---|
| 551 | close ERRLOG;
|
---|
| 552 | }
|
---|
| 553 | &util::rm("$errlog") if (-e "$errlog");
|
---|
| 554 | return [];
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | # store the *actual* output type and return the output filename
|
---|
| 558 | # it's possible we requested conversion to html, but only to text succeeded
|
---|
| 559 | #$self->{'convert_to_ext'} = $output_type;
|
---|
| 560 | if ($output_type =~ /html/i) {
|
---|
| 561 | $self->{'converted_to'} = "HTML";
|
---|
| 562 | } elsif ($output_type =~ /te?xt/i) {
|
---|
[15872] | 563 | $self->{'converted_to'} = "Text";
|
---|
[14006] | 564 | } elsif ($output_type =~ /item/i){
|
---|
[15872] | 565 | $self->{'converted_to'} = "PagedImage";
|
---|
[14006] | 566 | }
|
---|
| 567 |
|
---|
| 568 |
|
---|
| 569 | return $item_files;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 |
|
---|
| 573 |
|
---|
| 574 |
|
---|
[15872] | 575 | # Override ConvertBinaryFile read
|
---|
[16392] | 576 | # Needed so multiple .item files generated are sent down secondary plugin
|
---|
[14006] | 577 |
|
---|
| 578 | sub read {
|
---|
| 579 | my $self = shift (@_);
|
---|
[16392] | 580 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[14006] | 581 |
|
---|
[19052] | 582 |
|
---|
[14006] | 583 | $self->{'gli'} = $gli;
|
---|
| 584 | $self->{'file'} = $file;
|
---|
| 585 |
|
---|
| 586 | my $successful_rv = -1;
|
---|
| 587 |
|
---|
| 588 | my $outhandle = $self->{'outhandle'};
|
---|
| 589 |
|
---|
[16392] | 590 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
| 591 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
| 592 |
|
---|
[15872] | 593 | $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
|
---|
[19052] | 594 |
|
---|
[15872] | 595 | # read() deviates at this point from ConvertBinaryFile
|
---|
[14006] | 596 | # Need to work with list of filename returned
|
---|
| 597 |
|
---|
| 598 | my $output_ext = $self->{'convert_to_ext'};
|
---|
| 599 | my $conv_filename_list = [];
|
---|
| 600 |
|
---|
[16392] | 601 | $conv_filename_list = $self->tmp_area_convert_file($output_ext, $filename_full_path);
|
---|
[19052] | 602 |
|
---|
[14006] | 603 | if (scalar(@$conv_filename_list)==0) {
|
---|
| 604 | return -1;
|
---|
| 605 | } # had an error, will be passed down pipeline
|
---|
| 606 |
|
---|
| 607 | foreach my $conv_filename ( @$conv_filename_list ) {
|
---|
| 608 | if (! -e "$conv_filename") {return -1;}
|
---|
| 609 | $self->{'conv_filename'} = $conv_filename; # is this used anywhere?
|
---|
| 610 | $self->convert_post_process($conv_filename);
|
---|
| 611 |
|
---|
| 612 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 613 | my $num_secondary_plugins = scalar(keys %$secondary_plugins);
|
---|
| 614 |
|
---|
| 615 | if ($num_secondary_plugins == 0) {
|
---|
| 616 | print $outhandle "Warning: No secondary plugin to use in conversion. Skipping $file\n";
|
---|
| 617 | return 0; # effectively block it
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | my @plugin_names = keys %$secondary_plugins;
|
---|
| 621 | my $plugin_name = shift @plugin_names;
|
---|
| 622 |
|
---|
| 623 | if ($num_secondary_plugins > 1) {
|
---|
| 624 | print $outhandle "Warning: Multiple secondary plugins not supported yet! Choosing $plugin_name\n.";
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | my $secondary_plugin = $secondary_plugins->{$plugin_name};
|
---|
[19052] | 628 |
|
---|
[14006] | 629 | # note: metadata is not carried on to the next level
|
---|
| 630 | my ($rv,$doc_obj)
|
---|
| 631 | = $secondary_plugin->read_into_doc_obj ($pluginfo,"", $conv_filename,
|
---|
[16392] | 632 | $block_hash, $metadata, $processor, $maxdocs, $total_count,
|
---|
[14006] | 633 | $gli);
|
---|
| 634 |
|
---|
[19052] | 635 | print STDERR "**** $conv_filename => returned rv = $rv\n";
|
---|
| 636 |
|
---|
[14006] | 637 | if ((defined $rv) && ($rv>=0)) {
|
---|
| 638 | $successful_rv = 1;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | # Override previous gsdlsourcefilename set by secondary plugin
|
---|
[16392] | 642 | my $collect_file = &util::filename_within_collection($filename_full_path);
|
---|
[14006] | 643 | my $collect_conv_file = &util::filename_within_collection($conv_filename);
|
---|
[18328] | 644 | $doc_obj->set_source_filename ($collect_file, $self->{'file_rename_method'});
|
---|
[14006] | 645 | $doc_obj->set_converted_filename($collect_conv_file);
|
---|
| 646 |
|
---|
| 647 | my ($filemeta) = $file =~ /([^\\\/]+)$/;
|
---|
[15872] | 648 | $self->set_Source_metadata($doc_obj, $filemeta);
|
---|
[14006] | 649 | $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
|
---|
[16392] | 650 | $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename_full_path));
|
---|
[14006] | 651 |
|
---|
| 652 | if ($self->{'cover_image'}) {
|
---|
[16392] | 653 | $self->associate_cover_image($doc_obj, $filename_full_path);
|
---|
[14006] | 654 | }
|
---|
| 655 |
|
---|
| 656 | # do plugin specific processing of doc_obj
|
---|
| 657 | unless (defined ($self->process(undef, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
|
---|
[19052] | 658 | print STDERR "***** process returned undef: $base_dir $file\n";
|
---|
[14006] | 659 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
| 660 | return -1;
|
---|
| 661 | }
|
---|
| 662 | # do any automatic metadata extraction
|
---|
| 663 | $self->auto_extract_metadata ($doc_obj);
|
---|
| 664 |
|
---|
| 665 | # have we found a Title??
|
---|
| 666 | $self->title_fallback($doc_obj,$doc_obj->get_top_section(),$filemeta);
|
---|
| 667 |
|
---|
| 668 | # add an OID
|
---|
[15872] | 669 | $self->add_OID($doc_obj);
|
---|
[14006] | 670 | # process the document
|
---|
| 671 | $processor->process($doc_obj);
|
---|
| 672 |
|
---|
| 673 | $self->{'num_processed'} ++;
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | return $successful_rv;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
[15872] | 679 | sub process {
|
---|
[14006] | 680 |
|
---|
[19052] | 681 | return 1;
|
---|
[15872] | 682 | }
|
---|
[19052] | 683 |
|
---|
[15872] | 684 | # do we need this? sec pluginn process would have already been called as part of read_into_doc_obj??
|
---|
| 685 | sub process_old {
|
---|
[14006] | 686 | my $self = shift (@_);
|
---|
[15872] | 687 | my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
[14006] | 688 |
|
---|
| 689 |
|
---|
| 690 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 691 | my @plugin_names = keys %$secondary_plugins;
|
---|
| 692 | my $plugin_name = shift @plugin_names; # already checked there is only one
|
---|
| 693 |
|
---|
| 694 | my $secondary_plugin = $secondary_plugins->{$plugin_name};
|
---|
| 695 |
|
---|
| 696 | my $result = $secondary_plugin->process(@_);
|
---|
| 697 |
|
---|
| 698 | return $result;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 |
|
---|
| 702 | # Called at the beginning of the XML document.
|
---|
| 703 | sub xml_start_document {
|
---|
| 704 | my $self = shift(@_);
|
---|
| 705 | my ($expat) = @_;
|
---|
| 706 |
|
---|
| 707 | $self->{'rdf_desc'} = {};
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 |
|
---|
| 711 | # Called for DOCTYPE declarations - use die to bail out if this doctype
|
---|
| 712 | # is not meant for this plugin
|
---|
| 713 | sub xml_doctype {
|
---|
| 714 | my $self = shift(@_);
|
---|
| 715 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
| 716 |
|
---|
| 717 | die "" if ($name !~ /^rdf:RDF$/);
|
---|
| 718 |
|
---|
| 719 | my $outhandle = $self->{'outhandle'};
|
---|
[15872] | 720 | print $outhandle "CONTENTdmPlugin: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
|
---|
[14006] | 721 |
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | # Called for every start tag. The $_ variable will contain a copy of the
|
---|
| 725 | # tag and the %_ variable will contain the element's attributes.
|
---|
| 726 | sub xml_start_tag {
|
---|
| 727 | my $self = shift(@_);
|
---|
| 728 | my ($expat, $element) = @_;
|
---|
| 729 |
|
---|
| 730 | if ($element eq "rdf:Description") {
|
---|
| 731 |
|
---|
| 732 | my $about_key = $_{'about'};
|
---|
| 733 |
|
---|
| 734 | my $rdf_desc = $self->{'rdf_desc'};
|
---|
| 735 | $rdf_desc->{$about_key} = {};
|
---|
| 736 |
|
---|
| 737 | $self->{'about_key'} = $about_key;
|
---|
| 738 | $self->{'index_text'} = "";
|
---|
| 739 | $self->{'pp_text'} = "<table width=\"100%\">\n";
|
---|
| 740 |
|
---|
| 741 |
|
---|
| 742 | }
|
---|
| 743 | elsif (defined $self->{'about_key'}) {
|
---|
| 744 | $self->{'metadata_name'} = $element;
|
---|
| 745 | $self->{'metadata_value'} = "";
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | # Called for every end tag. The $_ variable will contain a copy of the tag.
|
---|
| 751 | sub xml_end_tag {
|
---|
| 752 | my $self = shift(@_);
|
---|
| 753 | my ($expat, $element) = @_;
|
---|
| 754 |
|
---|
| 755 | if ($element eq "rdf:Description") {
|
---|
| 756 | $self->{'pp_text'} .= "</table>\n";
|
---|
| 757 | ## ghtml::htmlsafe($self->{'pp_text'});
|
---|
| 758 |
|
---|
| 759 |
|
---|
| 760 | my $about_key = $self->{'about_key'};
|
---|
| 761 | my $about = $self->{'rdf_desc'}->{$about_key};
|
---|
| 762 | $about->{'IndexText'} = $self->{'index_text'};
|
---|
| 763 | $about->{'MetadataTable'} = $self->{'pp_text'};
|
---|
| 764 |
|
---|
| 765 |
|
---|
| 766 | $self->{'about_key'} = undef;
|
---|
| 767 | $self->{'index_text'} = undef;
|
---|
| 768 | $self->{'pp_text'} = undef;
|
---|
| 769 |
|
---|
| 770 | }
|
---|
| 771 | elsif (defined $self->{'metadata_name'}) {
|
---|
| 772 | my $metadata_name = $self->{'metadata_name'};
|
---|
| 773 | if ($element eq $metadata_name) {
|
---|
| 774 | my $metadata_value = $self->{'metadata_value'};
|
---|
| 775 |
|
---|
| 776 | my $about_key = $self->{'about_key'};
|
---|
| 777 | my $about = $self->{'rdf_desc'}->{$about_key};
|
---|
| 778 | $about->{$metadata_name} = $metadata_value;
|
---|
| 779 |
|
---|
| 780 | $self->{'index_text'} .= "$metadata_value\n";
|
---|
| 781 | $self->{'pp_text'} .= " <tr><td>$metadata_name</td><td>$metadata_value</td></tr>\n";
|
---|
| 782 |
|
---|
| 783 | $self->{'metadata_name'} = undef;
|
---|
| 784 | $self->{'metadata_value'} = undef;
|
---|
| 785 | }
|
---|
| 786 | }
|
---|
| 787 | }
|
---|
| 788 |
|
---|
| 789 | # Called just before start or end tags with accumulated non-markup text in
|
---|
| 790 | # the $_ variable.
|
---|
| 791 | sub xml_text {
|
---|
| 792 | my $self = shift(@_);
|
---|
| 793 | my ($expat) = @_;
|
---|
| 794 |
|
---|
| 795 | if (defined $self->{'metadata_name'}) {
|
---|
| 796 | $self->{'metadata_value'} .= $_;
|
---|
| 797 | }
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | # Called at the end of the XML document.
|
---|
| 801 | sub xml_end_document {
|
---|
| 802 | my $self = shift(@_);
|
---|
| 803 | my ($expat) = @_;
|
---|
| 804 | }
|
---|
| 805 |
|
---|
| 806 |
|
---|
| 807 | 1;
|
---|