source: main/trunk/greenstone2/perllib/plugins/MetadataXMLPlugin.pm@ 22853

Last change on this file since 22853 was 22853, checked in by kjdon, 14 years ago

print parse errors to failhandle and GLI xml as well as to outhandle

  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
RevLine 
[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]87package MetadataXMLPlugin;
[13189]88
89use strict;
90no strict 'refs';
[15872]91use BasePlugin;
[13189]92use util;
93use metadatautil;
94
95sub BEGIN {
[15872]96 @MetadataXMLPlugin::ISA = ('BasePlugin');
[13189]97 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
98}
99
100use XMLParser;
101
102my $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]111my $options = { 'name' => "MetadataXMLPlugin",
112 'desc' => "{MetadataXMLPlugin.desc}",
[13189]113 'abstract' => "no",
114 'inherits' => "yes",
115 'args' => $arguments };
116
117sub new {
118 my ($class) = shift (@_);
119 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
120 push(@$pluginlist, $class);
121
[15872]122 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
123 push(@{$hashArgOptLists->{"OptList"}},$options);
[13189]124
[21905]125 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
[13189]126
127 if ($self->{'info_only'}) {
128 # don't worry about any options or initialisations etc
129 return bless $self, $class;
130 }
131
132 # create XML::Parser object for parsing metadata.xml files
[13822]133 my $parser;
134 if ($]<5.008) {
135 # Perl 5.6
136 $parser = new XML::Parser('Style' => 'Stream',
[21905]137 'PluginObj' => $self,
[13822]138 'Handlers' => {'Char' => \&Char,
139 'Doctype' => \&Doctype
140 });
141 }
142 else {
[22412]143 # Perl 5.8 or greater
[13822]144 $parser = new XML::Parser('Style' => 'Stream',
[21905]145 'PluginObj' => $self,
[13822]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
160sub get_default_process_exp
161{
162 return q^metadata\.xml$^;
163}
164
[21916]165sub get_doctype {
166 my $self = shift(@_);
167
168 return "(Greenstone)?DirectoryMetadata"
169}
[13189]170
[21916]171sub can_process_this_file {
172 my $self = shift(@_);
173 my ($filename) = @_;
174
175 if (-f $filename && $self->SUPER::can_process_this_file($filename) && $self->check_doctype($filename)) {
176 return 1; # its a file for us
177 }
178 return 0;
179}
180
181sub check_doctype {
182 my $self = shift (@_);
183
184 my ($filename) = @_;
185
186 if (open(XMLIN,"<$filename")) {
187 my $doctype = $self->get_doctype();
188 ## check whether the doctype has the same name as the root element tag
189 while (defined (my $line = <XMLIN>)) {
190 ## find the root element
191 if ($line =~ /<([\w\d:]+)[\s>]/){
192 my $root = $1;
193 if ($root !~ $doctype){
194 close(XMLIN);
195 return 0;
196 }
197 else {
198 close(XMLIN);
199 return 1;
200 }
201 }
202 }
203 close(XMLIN);
204 }
205
206 return undef; # haven't found a valid line
207
208}
209
[20577]210sub file_block_read {
211 my $self = shift (@_);
212 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
213
[21916]214 my $filename_full_path = &util::filename_cat($base_dir, $file);
215 return undef unless $self->can_process_this_file($filename_full_path);
[20577]216
[21916]217 $block_hash->{'metadata_files'}->{$filename_full_path} = 1;
[20577]218
219 return 1;
220}
221
[13189]222sub metadata_read
223{
224 my $self = shift (@_);
[19493]225 my ($pluginfo, $base_dir, $file, $block_hash,
226 $extrametakeys, $extrametadata,$extrametafile,
227 $processor, $maxdocs, $gli) = @_;
[13189]228
229 my $filename = &util::filename_cat($base_dir, $file);
[21916]230 return undef unless $self->can_process_this_file($filename);
[13189]231
[19493]232 $self->{'metadata-file'} = $file;
233 $self->{'metadata-filename'} = $filename;
234
[16850]235 my $outhandle = $self->{'outhandle'};
236
[15872]237 print STDERR "\n<Processing n='$file' p='MetadataXMLPlugin'>\n" if ($gli);
[16850]238 print $outhandle "MetadataXMLPlugin: processing $file\n" if ($self->{'verbosity'})> 1;
[16386]239 # 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
240 $block_hash->{'file_blocks'}->{$filename} = 1;
[20577]241
[13189]242 $self->{'metadataref'} = $extrametadata;
[19493]243 $self->{'metafileref'} = $extrametafile;
[13189]244 $self->{'metakeysref'} = $extrametakeys;
245
246 eval {
247 $self->{'parser'}->parsefile($filename);
248 };
249
250 if ($@) {
251 my $plugin_name = ref ($self);
[22853]252 my $failhandle = $self->{'failhandle'};
[13189]253 print $outhandle "$plugin_name failed to process $file ($@)\n";
[22853]254 print $failhandle "$plugin_name failed to process $file ($@)\n";
255 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
[13189]256 return -1; #error
257 }
[16386]258
[13189]259 return 1;
260
261}
262
[21905]263
264# Updated by Jeffrey 2010/04/16 @ DL Consulting Ltd.
265# Get rid off the global $self as it cause problems when there are 2+ MetadataXMLPlugin in your collect.cfg...
266# For example when you have an OAIMetadataXMLPlugin that is a child of MetadataXMLPlugin
267sub Doctype {$_[0]->{'PluginObj'}->xml_doctype(@_);}
268sub StartTag {$_[0]->{'PluginObj'}->xml_start_tag(@_);}
269sub EndTag {$_[0]->{'PluginObj'}->xml_end_tag(@_);}
270sub Text {$_[0]->{'PluginObj'}->xml_text(@_);}
271
272
273sub xml_doctype {
274 my $self = shift(@_);
[13189]275 my ($expat, $name, $sysid, $pubid, $internal) = @_;
276
277 # allow the short-lived and badly named "GreenstoneDirectoryMetadata" files
278 # to be processed as well as the "DirectoryMetadata" files which should now
279 # be created by import.pl
280 die if ($name !~ /^(Greenstone)?DirectoryMetadata$/);
281}
282
[21905]283sub xml_start_tag {
284 my $self = shift(@_);
[13189]285 my ($expat, $element) = @_;
286
287 if ($element eq "FileSet") {
288 $self->{'saved_targets'} = [];
289 $self->{'saved_metadata'} = {};
290 }
291 elsif ($element eq "FileName") {
292 $self->{'in_filename'} = 1;
293 }
294 elsif ($element eq "Metadata") {
295 $self->{'metadata_name'} = $_{'name'};
[14955]296 $self->{'metadata_value'} = "";
[13189]297 if ((defined $_{'mode'}) && ($_{'mode'} eq "accumulate")) {
298 $self->{'metadata_accumulate'} = 1;
299 } else {
300 $self->{'metadata_accumulate'} = 0;
301 }
302 }
303}
304
[21905]305sub xml_end_tag {
306 my $self = shift(@_);
[13189]307 my ($expat, $element) = @_;
308
309 if ($element eq "FileSet") {
310 foreach my $target (@{$self->{'saved_targets'}}) {
311 my $file_metadata = $self->{'metadataref'}->{$target};
312 my $saved_metadata = $self->{'saved_metadata'};
[19493]313
[13189]314 if (!defined $file_metadata) {
315 $self->{'metadataref'}->{$target} = $saved_metadata;
[15004]316
317 # not had target before
318 push (@{$self->{'metakeysref'}}, $target);
[13189]319 }
320 else {
[15004]321 &metadatautil::combine_metadata_structures($file_metadata,$saved_metadata);
[13189]322 }
[19493]323
324
325 # now record which metadata.xml file it came from
326
[20803]327 my $file = $self->{'metadata-file'};
328 my $filename = $self->{'metadata-filename'};
[19493]329
[20803]330 if (!defined $self->{'metafileref'}->{$target}) {
331 $self->{'metafileref'}->{$target} = {};
332 }
[19493]333
[20803]334 $self->{'metafileref'}->{$target}->{$file} = $filename
[13189]335 }
336 }
337 elsif ($element eq "FileName") {
338 $self->{'in_filename'} = 0;
339 }
340 elsif ($element eq "Metadata") {
[15004]341 &metadatautil::store_saved_metadata($self,$self->{'metadata_name'}, $self->{'metadata_value'}, $self->{'metadata_accumulate'});
[13189]342 $self->{'metadata_name'} = "";
343 }
344
345}
346
[21905]347sub xml_text {
348 my $self = shift(@_);
[13189]349
350 if ($self->{'in_filename'}) {
351 # $_ == FileName content
352 push (@{$self->{'saved_targets'}}, $_);
353 }
354 elsif (defined ($self->{'metadata_name'}) && $self->{'metadata_name'} ne "") {
355 # $_ == Metadata content
[14955]356 $self->{'metadata_value'} = $_;
[13189]357 }
358}
359
360# This Char function overrides the one in XML::Parser::Stream to overcome a
361# problem where $expat->{Text} is treated as the return value, slowing
362# things down significantly in some cases.
363sub Char {
[13822]364 if ($]<5.008) {
365 use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+ and Perl 5.6
366 }
[13189]367 $_[0]->{'Text'} .= $_[1];
368 return undef;
369}
370
371
372
3731;
Note: See TracBrowser for help on using the repository browser.