[13189] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # MetadataXMLPlugin.pm --
|
---|
[13189] | 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) 2006 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 | ###########################################################################
|
---|
| 25 |
|
---|
[15872] | 26 | # MetadataXMLPlugin process metadata.xml files in a collection
|
---|
[13189] | 27 |
|
---|
| 28 | # Here's an example of a metadata file that uses three FileSet structures
|
---|
| 29 | # (ignore the # characters):
|
---|
| 30 |
|
---|
| 31 | #<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
---|
| 32 | #<!DOCTYPE DirectoryMetadata SYSTEM "http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd">
|
---|
| 33 | #<DirectoryMetadata>
|
---|
| 34 | # <FileSet>
|
---|
| 35 | # <FileName>nugget.*</FileName>
|
---|
| 36 | # <Description>
|
---|
| 37 | # <Metadata name="Title">Nugget Point, The Catlins</Metadata>
|
---|
| 38 | # <Metadata name="Place" mode="accumulate">Nugget Point</Metadata>
|
---|
| 39 | # </Description>
|
---|
| 40 | # </FileSet>
|
---|
| 41 | # <FileSet>
|
---|
| 42 | # <FileName>nugget-point-1.jpg</FileName>
|
---|
| 43 | # <Description>
|
---|
| 44 | # <Metadata name="Title">Nugget Point Lighthouse, The Catlins</Metadata>
|
---|
| 45 | # <Metadata name="Subject">Lighthouse</Metadata>
|
---|
| 46 | # </Description>
|
---|
| 47 | # </FileSet>
|
---|
| 48 | # <FileSet>
|
---|
| 49 | # <FileName>kaka-point-dir</FileName>
|
---|
| 50 | # <Description>
|
---|
| 51 | # <Metadata name="Title">Kaka Point, The Catlins</Metadata>
|
---|
| 52 | # </Description>
|
---|
| 53 | # </FileSet>
|
---|
| 54 | #</DirectoryMetadata>
|
---|
| 55 |
|
---|
| 56 | # Metadata elements are read and applied to files in the order they appear
|
---|
| 57 | # in the file.
|
---|
| 58 | #
|
---|
| 59 | # The FileName element describes the subfiles in the directory that the
|
---|
| 60 | # metadata applies to as a perl regular expression (a FileSet group may
|
---|
| 61 | # contain multiple FileName elements). So, <FileName>nugget.*</FileName>
|
---|
| 62 | # indicates that the metadata records in the following Description block
|
---|
| 63 | # apply to every subfile that starts with "nugget". For these files, a
|
---|
| 64 | # Title metadata element is set, overriding any old value that the Title
|
---|
| 65 | # might have had.
|
---|
| 66 | #
|
---|
| 67 | # Occasionally, we want to have multiple metadata values applied to a
|
---|
| 68 | # document; in this case we use the "mode=accumulate" attribute of the
|
---|
| 69 | # particular Metadata element. In the second metadata element of the first
|
---|
| 70 | # FileSet above, the "Place" metadata is accumulating, and may therefore be
|
---|
| 71 | # given several values. If we wanted to override these values and use a
|
---|
| 72 | # single metadata element again, we could set the mode attribute to
|
---|
| 73 | # "override" instead. Remember: every element is assumed to be in override
|
---|
| 74 | # mode unless you specify otherwise, so if you want to accumulate metadata
|
---|
| 75 | # for some field, every occurance must have "mode=accumulate" specified.
|
---|
| 76 | #
|
---|
| 77 | # The second FileSet element above applies to a specific file, called
|
---|
| 78 | # nugget-point-1.jpg. This element overrides the Title metadata set in the
|
---|
| 79 | # first FileSet, and adds a "Subject" metadata field.
|
---|
| 80 | #
|
---|
| 81 | # The third and final FileSet sets metadata for a subdirectory rather than
|
---|
| 82 | # a file. The metadata specified (a Title) will be passed into the
|
---|
| 83 | # subdirectory and applied to every file that occurs in the subdirectory
|
---|
| 84 | # (and to every subsubdirectory and its contents, and so on) unless the
|
---|
| 85 | # metadata is explictly overridden later in the import.
|
---|
| 86 |
|
---|
[15872] | 87 | package MetadataXMLPlugin;
|
---|
[13189] | 88 |
|
---|
| 89 | use strict;
|
---|
| 90 | no strict 'refs';
|
---|
[22857] | 91 |
|
---|
| 92 | use Encode;
|
---|
| 93 |
|
---|
[15872] | 94 | use BasePlugin;
|
---|
[24951] | 95 | use extrametautil;
|
---|
[13189] | 96 | use util;
|
---|
[27306] | 97 | use FileUtils;
|
---|
[13189] | 98 | use metadatautil;
|
---|
| 99 |
|
---|
| 100 | sub BEGIN {
|
---|
[15872] | 101 | @MetadataXMLPlugin::ISA = ('BasePlugin');
|
---|
[13189] | 102 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | use XMLParser;
|
---|
| 106 |
|
---|
| 107 | my $arguments = [
|
---|
[16386] | 108 | { 'name' => "process_exp",
|
---|
| 109 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[13189] | 110 | 'type' => "regexp",
|
---|
| 111 | 'reqd' => "no",
|
---|
[16386] | 112 | 'deft' => &get_default_process_exp() }
|
---|
| 113 |
|
---|
[13189] | 114 | ];
|
---|
| 115 |
|
---|
[15872] | 116 | my $options = { 'name' => "MetadataXMLPlugin",
|
---|
| 117 | 'desc' => "{MetadataXMLPlugin.desc}",
|
---|
[13189] | 118 | 'abstract' => "no",
|
---|
| 119 | 'inherits' => "yes",
|
---|
| 120 | 'args' => $arguments };
|
---|
| 121 |
|
---|
| 122 | sub new {
|
---|
| 123 | my ($class) = shift (@_);
|
---|
| 124 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 125 | push(@$pluginlist, $class);
|
---|
| 126 |
|
---|
[15872] | 127 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 128 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[13189] | 129 |
|
---|
[21905] | 130 | my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[13189] | 131 |
|
---|
| 132 | if ($self->{'info_only'}) {
|
---|
| 133 | # don't worry about any options or initialisations etc
|
---|
| 134 | return bless $self, $class;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[23895] | 137 | # The following used to be passed in as a parameter to XML::Parser,
|
---|
| 138 | # if the version of perl was greater than or equal to 5.8.
|
---|
| 139 | # The svn commit comment explaining the reason for adding this was
|
---|
| 140 | # not very clear and also said that it was quick fix and hadn't
|
---|
| 141 | # been tested under windows.
|
---|
| 142 | # More recent work has been to make strings in Perl "Unicode-aware"
|
---|
| 143 | # and so this line might actually be potentially harmful, however
|
---|
| 144 | # it is not the case that we encountered an actual error leading to
|
---|
| 145 | # its removal, rather it has been eliminated in an attempt to tighten
|
---|
| 146 | # up the code. For example, this protocol encoding is not used in
|
---|
| 147 | # ReadXMLFile.
|
---|
| 148 | # 'ProtocolEncoding' => 'ISO-8859-1',
|
---|
| 149 |
|
---|
[13189] | 150 | # create XML::Parser object for parsing metadata.xml files
|
---|
[24060] | 151 | my $parser = new XML::Parser('Style' => 'Stream',
|
---|
| 152 | 'Pkg' => 'MetadataXMLPlugin',
|
---|
[21905] | 153 | 'PluginObj' => $self,
|
---|
[24060] | 154 | 'Handlers' => {'Char' => \&Char,
|
---|
[13822] | 155 | 'Doctype' => \&Doctype
|
---|
| 156 | });
|
---|
| 157 |
|
---|
[13189] | 158 | $self->{'parser'} = $parser;
|
---|
| 159 | $self->{'in_filename'} = 0;
|
---|
| 160 |
|
---|
| 161 | return bless $self, $class;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | sub get_default_process_exp
|
---|
| 166 | {
|
---|
| 167 | return q^metadata\.xml$^;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[21916] | 170 | sub get_doctype {
|
---|
| 171 | my $self = shift(@_);
|
---|
| 172 |
|
---|
| 173 | return "(Greenstone)?DirectoryMetadata"
|
---|
| 174 | }
|
---|
[13189] | 175 |
|
---|
[21916] | 176 | sub can_process_this_file {
|
---|
| 177 | my $self = shift(@_);
|
---|
| 178 | my ($filename) = @_;
|
---|
| 179 |
|
---|
| 180 | if (-f $filename && $self->SUPER::can_process_this_file($filename) && $self->check_doctype($filename)) {
|
---|
| 181 | return 1; # its a file for us
|
---|
| 182 | }
|
---|
| 183 | return 0;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | sub check_doctype {
|
---|
| 187 | my $self = shift (@_);
|
---|
| 188 |
|
---|
| 189 | my ($filename) = @_;
|
---|
| 190 |
|
---|
| 191 | if (open(XMLIN,"<$filename")) {
|
---|
| 192 | my $doctype = $self->get_doctype();
|
---|
| 193 | ## check whether the doctype has the same name as the root element tag
|
---|
| 194 | while (defined (my $line = <XMLIN>)) {
|
---|
| 195 | ## find the root element
|
---|
| 196 | if ($line =~ /<([\w\d:]+)[\s>]/){
|
---|
| 197 | my $root = $1;
|
---|
| 198 | if ($root !~ $doctype){
|
---|
| 199 | close(XMLIN);
|
---|
| 200 | return 0;
|
---|
| 201 | }
|
---|
| 202 | else {
|
---|
| 203 | close(XMLIN);
|
---|
| 204 | return 1;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | close(XMLIN);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | return undef; # haven't found a valid line
|
---|
| 212 |
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[20577] | 215 | sub file_block_read {
|
---|
| 216 | my $self = shift (@_);
|
---|
| 217 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
[23419] | 218 |
|
---|
[27306] | 219 | my $filename_full_path = &FileUtils::filenameConcatenate($base_dir, $file);
|
---|
[21916] | 220 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
[20577] | 221 |
|
---|
[23419] | 222 | if ($ENV{'GSDLOS'} =~ m/^windows$/) {
|
---|
| 223 |
|
---|
| 224 | my $lower_drive = $filename_full_path;
|
---|
| 225 | $lower_drive =~ s/^([A-Z]):/\l$1:/i;
|
---|
| 226 |
|
---|
| 227 | my $upper_drive = $filename_full_path;
|
---|
| 228 | $upper_drive =~ s/^([A-Z]):/\u$1:/i;
|
---|
| 229 |
|
---|
| 230 | $block_hash->{'metadata_files'}->{$lower_drive} = 1;
|
---|
| 231 | $block_hash->{'metadata_files'}->{$upper_drive} = 1;
|
---|
| 232 | }
|
---|
| 233 | else {
|
---|
| 234 | $block_hash->{'metadata_files'}->{$filename_full_path} = 1;
|
---|
| 235 | }
|
---|
[20577] | 236 |
|
---|
| 237 | return 1;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[13189] | 240 | sub metadata_read
|
---|
| 241 | {
|
---|
| 242 | my $self = shift (@_);
|
---|
[19493] | 243 | my ($pluginfo, $base_dir, $file, $block_hash,
|
---|
| 244 | $extrametakeys, $extrametadata,$extrametafile,
|
---|
[23212] | 245 | $processor, $gli, $aux) = @_;
|
---|
[13189] | 246 |
|
---|
[27306] | 247 | my $filename = &FileUtils::filenameConcatenate($base_dir, $file);
|
---|
[21916] | 248 | return undef unless $self->can_process_this_file($filename);
|
---|
[24060] | 249 |
|
---|
[19493] | 250 | $self->{'metadata-file'} = $file;
|
---|
| 251 | $self->{'metadata-filename'} = $filename;
|
---|
[23419] | 252 |
|
---|
[16850] | 253 | my $outhandle = $self->{'outhandle'};
|
---|
| 254 |
|
---|
[15872] | 255 | print STDERR "\n<Processing n='$file' p='MetadataXMLPlugin'>\n" if ($gli);
|
---|
[16850] | 256 | print $outhandle "MetadataXMLPlugin: processing $file\n" if ($self->{'verbosity'})> 1;
|
---|
[16386] | 257 | # add the file to the block list so that it won't be processed in read, as we will do all we can with it here
|
---|
[23561] | 258 | &util::block_filename($block_hash,$filename);
|
---|
[20577] | 259 |
|
---|
[13189] | 260 | $self->{'metadataref'} = $extrametadata;
|
---|
[19493] | 261 | $self->{'metafileref'} = $extrametafile;
|
---|
[13189] | 262 | $self->{'metakeysref'} = $extrametakeys;
|
---|
| 263 |
|
---|
| 264 | eval {
|
---|
| 265 | $self->{'parser'}->parsefile($filename);
|
---|
| 266 | };
|
---|
| 267 |
|
---|
| 268 | if ($@) {
|
---|
[24060] | 269 | print STDERR "**** Error is: $@\n";
|
---|
[13189] | 270 | my $plugin_name = ref ($self);
|
---|
[22853] | 271 | my $failhandle = $self->{'failhandle'};
|
---|
[13189] | 272 | print $outhandle "$plugin_name failed to process $file ($@)\n";
|
---|
[22853] | 273 | print $failhandle "$plugin_name failed to process $file ($@)\n";
|
---|
| 274 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
[13189] | 275 | return -1; #error
|
---|
| 276 | }
|
---|
[16386] | 277 |
|
---|
[13189] | 278 | return 1;
|
---|
| 279 |
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[21905] | 282 |
|
---|
| 283 | # Updated by Jeffrey 2010/04/16 @ DL Consulting Ltd.
|
---|
| 284 | # Get rid off the global $self as it cause problems when there are 2+ MetadataXMLPlugin in your collect.cfg...
|
---|
| 285 | # For example when you have an OAIMetadataXMLPlugin that is a child of MetadataXMLPlugin
|
---|
| 286 | sub Doctype {$_[0]->{'PluginObj'}->xml_doctype(@_);}
|
---|
| 287 | sub StartTag {$_[0]->{'PluginObj'}->xml_start_tag(@_);}
|
---|
| 288 | sub EndTag {$_[0]->{'PluginObj'}->xml_end_tag(@_);}
|
---|
| 289 | sub Text {$_[0]->{'PluginObj'}->xml_text(@_);}
|
---|
| 290 |
|
---|
| 291 |
|
---|
| 292 | sub xml_doctype {
|
---|
| 293 | my $self = shift(@_);
|
---|
[13189] | 294 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
| 295 |
|
---|
| 296 | # allow the short-lived and badly named "GreenstoneDirectoryMetadata" files
|
---|
| 297 | # to be processed as well as the "DirectoryMetadata" files which should now
|
---|
| 298 | # be created by import.pl
|
---|
| 299 | die if ($name !~ /^(Greenstone)?DirectoryMetadata$/);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[21905] | 302 | sub xml_start_tag {
|
---|
| 303 | my $self = shift(@_);
|
---|
[13189] | 304 | my ($expat, $element) = @_;
|
---|
[24060] | 305 |
|
---|
[13189] | 306 | if ($element eq "FileSet") {
|
---|
| 307 | $self->{'saved_targets'} = [];
|
---|
| 308 | $self->{'saved_metadata'} = {};
|
---|
| 309 | }
|
---|
| 310 | elsif ($element eq "FileName") {
|
---|
| 311 | $self->{'in_filename'} = 1;
|
---|
| 312 | }
|
---|
| 313 | elsif ($element eq "Metadata") {
|
---|
| 314 | $self->{'metadata_name'} = $_{'name'};
|
---|
[14955] | 315 | $self->{'metadata_value'} = "";
|
---|
[13189] | 316 | if ((defined $_{'mode'}) && ($_{'mode'} eq "accumulate")) {
|
---|
| 317 | $self->{'metadata_accumulate'} = 1;
|
---|
| 318 | } else {
|
---|
| 319 | $self->{'metadata_accumulate'} = 0;
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[21905] | 324 | sub xml_end_tag {
|
---|
| 325 | my $self = shift(@_);
|
---|
[13189] | 326 | my ($expat, $element) = @_;
|
---|
| 327 |
|
---|
| 328 | if ($element eq "FileSet") {
|
---|
| 329 | foreach my $target (@{$self->{'saved_targets'}}) {
|
---|
[24971] | 330 |
|
---|
[25094] | 331 | # FileNames must be regex, but we allow \\ for path separator on windows. convert to /
|
---|
| 332 | $target = &util::filepath_regex_to_url_format($target);
|
---|
| 333 | my $file_metadata = &extrametautil::getmetadata($self->{'metadataref'}, $target);
|
---|
[13189] | 334 | my $saved_metadata = $self->{'saved_metadata'};
|
---|
[19493] | 335 |
|
---|
[13189] | 336 | if (!defined $file_metadata) {
|
---|
[24951] | 337 | &extrametautil::setmetadata($self->{'metadataref'}, $target, $saved_metadata);
|
---|
[15004] | 338 |
|
---|
| 339 | # not had target before
|
---|
[24951] | 340 | &extrametautil::addmetakey($self->{'metakeysref'}, $target);
|
---|
[13189] | 341 | }
|
---|
| 342 | else {
|
---|
[15004] | 343 | &metadatautil::combine_metadata_structures($file_metadata,$saved_metadata);
|
---|
[13189] | 344 | }
|
---|
[19493] | 345 |
|
---|
| 346 |
|
---|
| 347 | # now record which metadata.xml file it came from
|
---|
| 348 |
|
---|
[20803] | 349 | my $file = $self->{'metadata-file'};
|
---|
| 350 | my $filename = $self->{'metadata-filename'};
|
---|
[19493] | 351 |
|
---|
[24951] | 352 | if (!defined &extrametautil::getmetafile($self->{'metafileref'}, $target)) {
|
---|
| 353 | &extrametautil::setmetafile($self->{'metafileref'}, $target, {});
|
---|
[20803] | 354 | }
|
---|
[19493] | 355 |
|
---|
[24951] | 356 | &extrametautil::setmetafile_for_named_file($self->{'metafileref'}, $target, $file, $filename);
|
---|
[13189] | 357 | }
|
---|
| 358 | }
|
---|
| 359 | elsif ($element eq "FileName") {
|
---|
| 360 | $self->{'in_filename'} = 0;
|
---|
| 361 | }
|
---|
| 362 | elsif ($element eq "Metadata") {
|
---|
[24060] | 363 | # text read in by XML::Parser is in Perl's binary byte value
|
---|
| 364 | # form ... need to explicitly make it UTF-8
|
---|
[22857] | 365 |
|
---|
[24060] | 366 | my $metadata_name = decode("utf-8",$self->{'metadata_name'});
|
---|
| 367 | my $metadata_value = decode("utf-8",$self->{'metadata_value'});
|
---|
| 368 |
|
---|
[22857] | 369 | &metadatautil::store_saved_metadata($self,
|
---|
| 370 | $metadata_name, $metadata_value,
|
---|
| 371 | $self->{'metadata_accumulate'});
|
---|
[13189] | 372 | $self->{'metadata_name'} = "";
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | }
|
---|
| 376 |
|
---|
[21905] | 377 | sub xml_text {
|
---|
| 378 | my $self = shift(@_);
|
---|
[13189] | 379 |
|
---|
| 380 | if ($self->{'in_filename'}) {
|
---|
| 381 | # $_ == FileName content
|
---|
| 382 | push (@{$self->{'saved_targets'}}, $_);
|
---|
| 383 | }
|
---|
| 384 | elsif (defined ($self->{'metadata_name'}) && $self->{'metadata_name'} ne "") {
|
---|
| 385 | # $_ == Metadata content
|
---|
[14955] | 386 | $self->{'metadata_value'} = $_;
|
---|
[13189] | 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | # This Char function overrides the one in XML::Parser::Stream to overcome a
|
---|
| 391 | # problem where $expat->{Text} is treated as the return value, slowing
|
---|
| 392 | # things down significantly in some cases.
|
---|
| 393 | sub Char {
|
---|
[24060] | 394 | use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+
|
---|
| 395 |
|
---|
| 396 | # if ($]<5.008) {
|
---|
| 397 | # use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+ and Perl 5.6
|
---|
| 398 | # }
|
---|
[13189] | 399 | $_[0]->{'Text'} .= $_[1];
|
---|
| 400 | return undef;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | 1;
|
---|