[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';
|
---|
[15872] | 91 | use BasePlugin;
|
---|
[13189] | 92 | use util;
|
---|
| 93 | use metadatautil;
|
---|
| 94 |
|
---|
| 95 | sub BEGIN {
|
---|
[15872] | 96 | @MetadataXMLPlugin::ISA = ('BasePlugin');
|
---|
[13189] | 97 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | use XMLParser;
|
---|
| 101 |
|
---|
| 102 | my $arguments = [
|
---|
[16386] | 103 | { 'name' => "process_exp",
|
---|
| 104 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[13189] | 105 | 'type' => "regexp",
|
---|
| 106 | 'reqd' => "no",
|
---|
[16386] | 107 | 'deft' => &get_default_process_exp() }
|
---|
| 108 |
|
---|
[13189] | 109 | ];
|
---|
| 110 |
|
---|
[15872] | 111 | my $options = { 'name' => "MetadataXMLPlugin",
|
---|
| 112 | 'desc' => "{MetadataXMLPlugin.desc}",
|
---|
[13189] | 113 | 'abstract' => "no",
|
---|
| 114 | 'inherits' => "yes",
|
---|
| 115 | 'args' => $arguments };
|
---|
| 116 |
|
---|
| 117 | my ($self);
|
---|
| 118 |
|
---|
| 119 | sub new {
|
---|
| 120 | my ($class) = shift (@_);
|
---|
| 121 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 122 | push(@$pluginlist, $class);
|
---|
| 123 |
|
---|
[15872] | 124 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 125 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[13189] | 126 |
|
---|
[15872] | 127 | $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[13189] | 128 |
|
---|
| 129 | if ($self->{'info_only'}) {
|
---|
| 130 | # don't worry about any options or initialisations etc
|
---|
| 131 | return bless $self, $class;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | # create XML::Parser object for parsing metadata.xml files
|
---|
[13822] | 135 | my $parser;
|
---|
| 136 | if ($]<5.008) {
|
---|
| 137 | # Perl 5.6
|
---|
| 138 | $parser = new XML::Parser('Style' => 'Stream',
|
---|
| 139 | 'Handlers' => {'Char' => \&Char,
|
---|
| 140 | 'Doctype' => \&Doctype
|
---|
| 141 | });
|
---|
| 142 | }
|
---|
| 143 | else {
|
---|
| 144 | # Perl 5.8
|
---|
| 145 | $parser = new XML::Parser('Style' => 'Stream',
|
---|
| 146 | 'ProtocolEncoding' => 'ISO-8859-1',
|
---|
| 147 | 'Handlers' => {'Char' => \&Char,
|
---|
| 148 | 'Doctype' => \&Doctype
|
---|
| 149 | });
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[13189] | 152 | $self->{'parser'} = $parser;
|
---|
| 153 | $self->{'in_filename'} = 0;
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | return bless $self, $class;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | sub get_default_process_exp
|
---|
| 161 | {
|
---|
| 162 | return q^metadata\.xml$^;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | sub metadata_read
|
---|
| 167 | {
|
---|
| 168 | my $self = shift (@_);
|
---|
[16386] | 169 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $extrametakeys, $extrametadata,
|
---|
[13189] | 170 | $processor, $maxdocs, $gli) = @_;
|
---|
| 171 |
|
---|
| 172 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
| 173 | if ($filename !~ /metadata\.xml$/ || !-f $filename) {
|
---|
| 174 | return undef;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[15872] | 177 | print STDERR "\n<Processing n='$file' p='MetadataXMLPlugin'>\n" if ($gli);
|
---|
| 178 | print STDERR "MetadataXMLPlugin: processing $file\n" if ($self->{'verbosity'})> 1;
|
---|
[16386] | 179 | # 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
|
---|
| 180 | $block_hash->{'file_blocks'}->{$filename} = 1;
|
---|
[13189] | 181 | $self->{'metadataref'} = $extrametadata;
|
---|
| 182 | $self->{'metakeysref'} = $extrametakeys;
|
---|
| 183 |
|
---|
| 184 | eval {
|
---|
| 185 | $self->{'parser'}->parsefile($filename);
|
---|
| 186 | };
|
---|
| 187 |
|
---|
| 188 | if ($@) {
|
---|
| 189 | my $outhandle = $self->{'outhandle'};
|
---|
| 190 | my $plugin_name = ref ($self);
|
---|
| 191 | print $outhandle "$plugin_name failed to process $file ($@)\n";
|
---|
| 192 |
|
---|
| 193 | return -1; #error
|
---|
| 194 | }
|
---|
[16386] | 195 |
|
---|
[13189] | 196 | return 1;
|
---|
| 197 |
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | sub Doctype {
|
---|
| 201 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
| 202 |
|
---|
| 203 | # allow the short-lived and badly named "GreenstoneDirectoryMetadata" files
|
---|
| 204 | # to be processed as well as the "DirectoryMetadata" files which should now
|
---|
| 205 | # be created by import.pl
|
---|
| 206 | die if ($name !~ /^(Greenstone)?DirectoryMetadata$/);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | sub StartTag {
|
---|
| 210 | my ($expat, $element) = @_;
|
---|
| 211 |
|
---|
| 212 | if ($element eq "FileSet") {
|
---|
| 213 | $self->{'saved_targets'} = [];
|
---|
| 214 | $self->{'saved_metadata'} = {};
|
---|
| 215 | }
|
---|
| 216 | elsif ($element eq "FileName") {
|
---|
| 217 | $self->{'in_filename'} = 1;
|
---|
| 218 | }
|
---|
| 219 | elsif ($element eq "Metadata") {
|
---|
| 220 | $self->{'metadata_name'} = $_{'name'};
|
---|
[14955] | 221 | $self->{'metadata_value'} = "";
|
---|
[13189] | 222 | if ((defined $_{'mode'}) && ($_{'mode'} eq "accumulate")) {
|
---|
| 223 | $self->{'metadata_accumulate'} = 1;
|
---|
| 224 | } else {
|
---|
| 225 | $self->{'metadata_accumulate'} = 0;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | sub EndTag {
|
---|
| 231 | my ($expat, $element) = @_;
|
---|
| 232 |
|
---|
| 233 | if ($element eq "FileSet") {
|
---|
| 234 | foreach my $target (@{$self->{'saved_targets'}}) {
|
---|
| 235 | my $file_metadata = $self->{'metadataref'}->{$target};
|
---|
| 236 | my $saved_metadata = $self->{'saved_metadata'};
|
---|
| 237 | if (!defined $file_metadata) {
|
---|
| 238 | $self->{'metadataref'}->{$target} = $saved_metadata;
|
---|
[15004] | 239 |
|
---|
| 240 | # not had target before
|
---|
| 241 | push (@{$self->{'metakeysref'}}, $target);
|
---|
[13189] | 242 | }
|
---|
| 243 | else {
|
---|
[15004] | 244 | &metadatautil::combine_metadata_structures($file_metadata,$saved_metadata);
|
---|
[13189] | 245 | }
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | elsif ($element eq "FileName") {
|
---|
| 249 | $self->{'in_filename'} = 0;
|
---|
| 250 | }
|
---|
| 251 | elsif ($element eq "Metadata") {
|
---|
[15004] | 252 | &metadatautil::store_saved_metadata($self,$self->{'metadata_name'}, $self->{'metadata_value'}, $self->{'metadata_accumulate'});
|
---|
[13189] | 253 | $self->{'metadata_name'} = "";
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | sub Text {
|
---|
| 259 |
|
---|
| 260 | if ($self->{'in_filename'}) {
|
---|
| 261 | # $_ == FileName content
|
---|
| 262 | push (@{$self->{'saved_targets'}}, $_);
|
---|
| 263 | }
|
---|
| 264 | elsif (defined ($self->{'metadata_name'}) && $self->{'metadata_name'} ne "") {
|
---|
| 265 | # $_ == Metadata content
|
---|
[14955] | 266 | $self->{'metadata_value'} = $_;
|
---|
[13189] | 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | # This Char function overrides the one in XML::Parser::Stream to overcome a
|
---|
| 271 | # problem where $expat->{Text} is treated as the return value, slowing
|
---|
| 272 | # things down significantly in some cases.
|
---|
| 273 | sub Char {
|
---|
[13822] | 274 | if ($]<5.008) {
|
---|
| 275 | use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+ and Perl 5.6
|
---|
| 276 | }
|
---|
[13189] | 277 | $_[0]->{'Text'} .= $_[1];
|
---|
| 278 | return undef;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[15004] | 281 | #sub combine_metadata_structures
|
---|
| 282 | #{
|
---|
| 283 | # my $self = shift(@_);
|
---|
| 284 | #
|
---|
| 285 | # my ($mdref1, $mdref2) = @_;
|
---|
| 286 | # &metadatautil::combine_metadata_structures($mdref1, $mdref2);
|
---|
| 287 | #}
|
---|
[13189] | 288 |
|
---|
| 289 |
|
---|
| 290 | 1;
|
---|