source: trunk/gsdl/perllib/plugins/MetadataXMLPlug.pm@ 13189

Last change on this file since 13189 was 13189, checked in by kjdon, 17 years ago

new plugin that reads metadata.xml files. Functionality moved here from RecPlug

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