[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # RecPlug.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) 1999 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 |
|
---|
[2228] | 26 | # RecPlug is a plugin which recurses through directories processing
|
---|
| 27 | # each file it finds.
|
---|
[4] | 28 |
|
---|
[2228] | 29 | # RecPlug has one option: use_metadata_files. When this is set, it will
|
---|
[2813] | 30 | # check each directory for an XML file called "metadata.xml" that specifies
|
---|
| 31 | # metadata for the files (and subdirectories) in the directory.
|
---|
[2228] | 32 | #
|
---|
[2813] | 33 | # Here's an example of a metadata file that uses three FileSet structures
|
---|
[2228] | 34 | # (ignore the # characters):
|
---|
| 35 |
|
---|
[2813] | 36 | #<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
---|
[2925] | 37 | #<!DOCTYPE DirectoryMetadata SYSTEM "http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd">
|
---|
[2813] | 38 | #<DirectoryMetadata>
|
---|
| 39 | # <FileSet>
|
---|
| 40 | # <FileName>nugget.*</FileName>
|
---|
| 41 | # <Description>
|
---|
| 42 | # <Metadata name="Title">Nugget Point, The Catlins</Metadata>
|
---|
| 43 | # <Metadata name="Place" mode="accumulate">Nugget Point</Metadata>
|
---|
| 44 | # </Description>
|
---|
| 45 | # </FileSet>
|
---|
| 46 | # <FileSet>
|
---|
| 47 | # <FileName>nugget-point-1.jpg</FileName>
|
---|
| 48 | # <Description>
|
---|
| 49 | # <Metadata name="Title">Nugget Point Lighthouse, The Catlins</Metadata>
|
---|
| 50 | # <Metadata name="Subject">Lighthouse</Metadata>
|
---|
| 51 | # </Description>
|
---|
| 52 | # </FileSet>
|
---|
| 53 | # <FileSet>
|
---|
| 54 | # <FileName>kaka-point-dir</FileName>
|
---|
| 55 | # <Description>
|
---|
| 56 | # <Metadata name="Title">Kaka Point, The Catlins</Metadata>
|
---|
| 57 | # </Description>
|
---|
| 58 | # </FileSet>
|
---|
| 59 | #</DirectoryMetadata>
|
---|
[2228] | 60 |
|
---|
| 61 | # Metadata elements are read and applied to files in the order they appear
|
---|
[2813] | 62 | # in the file.
|
---|
[2228] | 63 | #
|
---|
[2813] | 64 | # The FileName element describes the subfiles in the directory that the
|
---|
| 65 | # metadata applies to as a perl regular expression (a FileSet group may
|
---|
| 66 | # contain multiple FileName elements). So, <FileName>nugget.*</FileName>
|
---|
| 67 | # indicates that the metadata records in the following Description block
|
---|
| 68 | # apply to every subfile that starts with "nugget". For these files, a
|
---|
[2228] | 69 | # Title metadata element is set, overriding any old value that the Title
|
---|
| 70 | # might have had.
|
---|
| 71 | #
|
---|
| 72 | # Occasionally, we want to have multiple metadata values applied to a
|
---|
| 73 | # document; in this case we use the "mode=accumulate" attribute of the
|
---|
[2813] | 74 | # particular Metadata element. In the second metadata element of the first
|
---|
| 75 | # FileSet above, the "Place" metadata is accumulating, and may therefore be
|
---|
| 76 | # given several values. If we wanted to override these values and use a
|
---|
| 77 | # single metadata element again, we could set the mode attribute to
|
---|
| 78 | # "override" instead. Remember: every element is assumed to be in override
|
---|
| 79 | # mode unless you specify otherwise, so if you want to accumulate metadata
|
---|
| 80 | # for some field, every occurance must have "mode=accumulate" specified.
|
---|
[2228] | 81 | #
|
---|
[2813] | 82 | # The second FileSet element above applies to a specific file, called
|
---|
| 83 | # nugget-point-1.jpg. This element overrides the Title metadata set in the
|
---|
| 84 | # first FileSet, and adds a "Subject" metadata field.
|
---|
[2228] | 85 | #
|
---|
[2813] | 86 | # The third and final FileSet sets metadata for a subdirectory rather than
|
---|
| 87 | # a file. The metadata specified (a Title) will be passed into the
|
---|
| 88 | # subdirectory and applied to every file that occurs in the subdirectory
|
---|
| 89 | # (and to every subsubdirectory and its contents, and so on) unless the
|
---|
| 90 | # metadata is explictly overridden later in the import.
|
---|
[2228] | 91 |
|
---|
| 92 |
|
---|
| 93 |
|
---|
[4] | 94 | package RecPlug;
|
---|
| 95 |
|
---|
| 96 | use BasPlug;
|
---|
| 97 | use plugin;
|
---|
[136] | 98 | use util;
|
---|
[4] | 99 |
|
---|
| 100 |
|
---|
| 101 | BEGIN {
|
---|
| 102 | @ISA = ('BasPlug');
|
---|
[2813] | 103 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
[4] | 104 | }
|
---|
| 105 |
|
---|
[2813] | 106 | use XML::Parser;
|
---|
| 107 |
|
---|
[4744] | 108 | my $arguments =
|
---|
| 109 | [ { 'name' => "block_exp",
|
---|
[4873] | 110 | 'desc' => "{BasPlug.block_exp}",
|
---|
[6408] | 111 | 'type' => "regexp",
|
---|
[4744] | 112 | 'deft' => &get_default_block_exp(),
|
---|
| 113 | 'reqd' => "no" },
|
---|
| 114 | { 'name' => "use_metadata_files",
|
---|
[4873] | 115 | 'desc' => "{RecPlug.use_metadata_files}",
|
---|
[4744] | 116 | 'type' => "flag",
|
---|
[7686] | 117 | 'reqd' => "no" },
|
---|
| 118 | { 'name' => "recheck_directories",
|
---|
| 119 | 'desc' => "{RecPlug.recheck_directories}",
|
---|
| 120 | 'type' => "flag",
|
---|
[4744] | 121 | 'reqd' => "no" } ];
|
---|
[3540] | 122 |
|
---|
| 123 | my $options = { 'name' => "RecPlug",
|
---|
[5680] | 124 | 'desc' => "{RecPlug.desc}",
|
---|
[6408] | 125 | 'abstract' => "no",
|
---|
[4744] | 126 | 'inherits' => "yes",
|
---|
| 127 | 'args' => $arguments };
|
---|
[3540] | 128 |
|
---|
[2228] | 129 |
|
---|
[2813] | 130 | my ($self);
|
---|
[4] | 131 | sub new {
|
---|
[2228] | 132 | my $class = shift (@_);
|
---|
[2813] | 133 |
|
---|
| 134 | # $self is global for use within subroutines called by XML::Parser
|
---|
| 135 | $self = new BasPlug ($class, @_);
|
---|
| 136 |
|
---|
[3540] | 137 | # 14-05-02 To allow for proper inheritance of arguments - John Thompson
|
---|
| 138 | my $option_list = $self->{'option_list'};
|
---|
| 139 | push( @{$option_list}, $options );
|
---|
| 140 |
|
---|
[2228] | 141 | if (!parsargv::parse(\@_,
|
---|
| 142 | q^use_metadata_files^, \$self->{'use_metadata_files'},
|
---|
[7686] | 143 | q^recheck_directories^, \$self->{'recheck_directories'},
|
---|
[2228] | 144 | "allow_extra_options")) {
|
---|
| 145 | print STDERR "\nRecPlug uses an incorrect option.\n";
|
---|
| 146 | print STDERR "Check your collect.cfg configuration file.\n\n";
|
---|
[4873] | 147 | $self->print_txt_usage(""); # Use default resource bundle
|
---|
[2228] | 148 | die "\n";
|
---|
| 149 | }
|
---|
[2813] | 150 |
|
---|
| 151 | if ($self->{'use_metadata_files'}) {
|
---|
| 152 | # create XML::Parser object for parsing metadata.xml files
|
---|
| 153 | my $parser = new XML::Parser('Style' => 'Stream',
|
---|
| 154 | 'Handlers' => {'Char' => \&Char,
|
---|
| 155 | 'Doctype' => \&Doctype
|
---|
| 156 | });
|
---|
| 157 | $self->{'parser'} = $parser;
|
---|
| 158 | $self->{'in_filename'} = 0;
|
---|
| 159 |
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[4] | 162 | return bless $self, $class;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | # return 1 if this class might recurse using $pluginfo
|
---|
| 166 | sub is_recursive {
|
---|
| 167 | my $self = shift (@_);
|
---|
[2813] | 168 |
|
---|
[4] | 169 | return 1;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[2228] | 172 | sub get_default_block_exp {
|
---|
| 173 | my $self = shift (@_);
|
---|
[2813] | 174 |
|
---|
[2228] | 175 | return 'CVS';
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[317] | 178 | # return number of files processed, undef if can't process
|
---|
[4] | 179 | # Note that $base_dir might be "" and that $file might
|
---|
| 180 | # include directories
|
---|
[2228] | 181 |
|
---|
| 182 | # This function passes around metadata hash structures. Metadata hash
|
---|
| 183 | # structures are hashes that map from a (scalar) key (the metadata element
|
---|
| 184 | # name) to either a scalar metadata value or a reference to an array of
|
---|
| 185 | # such values.
|
---|
| 186 |
|
---|
[4] | 187 | sub read {
|
---|
| 188 | my $self = shift (@_);
|
---|
[6332] | 189 | my ($pluginfo, $base_dir, $file, $in_metadata, $processor, $maxdocs, $gli) = @_;
|
---|
[2813] | 190 |
|
---|
[1424] | 191 | my $outhandle = $self->{'outhandle'};
|
---|
[2228] | 192 | my $verbosity = $self->{'verbosity'};
|
---|
| 193 | my $read_metadata_files = $self->{'use_metadata_files'};
|
---|
[2813] | 194 |
|
---|
[2228] | 195 | # Calculate the directory name and ensure it is a directory and
|
---|
| 196 | # that it is not explicitly blocked.
|
---|
[2795] | 197 | my $dirname = $file;
|
---|
| 198 | $dirname = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
|
---|
[2228] | 199 | return undef unless (-d $dirname);
|
---|
| 200 | return 0 if ($self->{'block_exp'} ne "" && $dirname =~ /$self->{'block_exp'}/);
|
---|
[2813] | 201 |
|
---|
[2228] | 202 | # check to make sure we're not reading the archives or index directory
|
---|
| 203 | my $gsdlhome = quotemeta($ENV{'GSDLHOME'});
|
---|
[2813] | 204 | if ($dirname =~ m/^$gsdlhome\/.*?\/import.*?\/(archives|index)$/) {
|
---|
| 205 | print $outhandle "RecPlug: $dirname appears to be a reference to a Greenstone collection, skipping.\n";
|
---|
[2228] | 206 | return 0;
|
---|
[1755] | 207 | }
|
---|
[2813] | 208 |
|
---|
[1755] | 209 | # check to see we haven't got a cyclic path...
|
---|
| 210 | if ($dirname =~ m%(/.*){,41}%) {
|
---|
| 211 | print $outhandle "RecPlug: $dirname is 40 directories deep, is this a recursive path? if not increase constant in RecPlug.pm.\n";
|
---|
[2228] | 212 | return 0;
|
---|
[1755] | 213 | }
|
---|
[2813] | 214 |
|
---|
[1755] | 215 | # check to see we haven't got a cyclic path...
|
---|
| 216 | if ($dirname =~ m%.*?import/(.+?)/import/\1.*%) {
|
---|
[2228] | 217 | print $outhandle "RecPlug: $dirname appears to be in a recursive loop...\n";
|
---|
| 218 | return 0;
|
---|
[1755] | 219 | }
|
---|
[2813] | 220 |
|
---|
[2228] | 221 | if (($verbosity > 2) && ((scalar keys %$in_metadata) > 0)) {
|
---|
| 222 | print $outhandle "RecPlug: metadata passed in: ",
|
---|
[2813] | 223 | join(", ", keys %$in_metadata), "\n";
|
---|
[2228] | 224 | }
|
---|
[2813] | 225 |
|
---|
[2228] | 226 | # Recur over directory contents.
|
---|
| 227 | my (@dir, $subfile);
|
---|
| 228 | my $count = 0;
|
---|
[6332] | 229 |
|
---|
[2228] | 230 | print $outhandle "RecPlug: getting directory $dirname\n" if ($verbosity);
|
---|
[2813] | 231 |
|
---|
[2228] | 232 | # find all the files in the directory
|
---|
| 233 | if (!opendir (DIR, $dirname)) {
|
---|
| 234 | print $outhandle "RecPlug: WARNING - couldn't read directory $dirname\n";
|
---|
[7362] | 235 | return -1; # error in processing
|
---|
[2228] | 236 | }
|
---|
| 237 | @dir = readdir (DIR);
|
---|
| 238 | closedir (DIR);
|
---|
[7686] | 239 |
|
---|
| 240 | # Re-order the files in the list so any directories ending with .all are moved to the end
|
---|
| 241 | for ($i = scalar(@dir) - 1; $i >= 0; $i--) {
|
---|
[7688] | 242 | if (-d &util::filename_cat($dirname, $dir[$i]) && $dir[$i] =~ /\.all$/) {
|
---|
[7686] | 243 | push(@dir, splice(@dir, $i, 1));
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[2228] | 247 | # read XML metadata files (if supplied)
|
---|
| 248 | my $additionalmetadata = 0; # is there extra metadata available?
|
---|
| 249 | my %extrametadata; # maps from filespec to extra metadata keys
|
---|
| 250 | my @extrametakeys; # keys of %extrametadata in order read
|
---|
[2813] | 251 |
|
---|
[2228] | 252 | if ($read_metadata_files) {
|
---|
| 253 |
|
---|
[2813] | 254 | # read the directory "metadata.xml" file
|
---|
| 255 | my $metadatafile = &util::filename_cat ($dirname, 'metadata.xml');
|
---|
[2228] | 256 | if (-e $metadatafile) {
|
---|
| 257 | print $outhandle "RecPlug: found metadata in $metadatafile\n"
|
---|
| 258 | if ($verbosity);
|
---|
[2813] | 259 | $self->read_metadata_xml_file($metadatafile, \%extrametadata, \@extrametakeys);
|
---|
[2228] | 260 | $additionalmetadata = 1;
|
---|
[1755] | 261 | }
|
---|
[2228] | 262 | }
|
---|
[4] | 263 |
|
---|
[2228] | 264 | # import each of the files in the directory
|
---|
| 265 | my $out_metadata;
|
---|
[7686] | 266 | my $num_files = scalar(@dir);
|
---|
| 267 | for (my $i = 0; $i <= scalar(@dir); $i++) {
|
---|
| 268 | # When every file in the directory has been done, pause for a moment (figuratively!)
|
---|
| 269 | # If the -recheck_directories argument hasn't been provided, stop now (default)
|
---|
| 270 | # Otherwise, re-read the contents of the directory to check for new files
|
---|
| 271 | # Any new files are added to the @dir list and are processed as normal
|
---|
| 272 | # This is necessary when documents to be indexed are specified in bibliographic DBs
|
---|
| 273 | # These files are copied/downloaded and stored in a new folder at import time
|
---|
| 274 | if ($i == $num_files) {
|
---|
| 275 | last unless $self->{'recheck_directories'};
|
---|
| 276 |
|
---|
| 277 | # Re-read the files in the directory to see if there are any new files
|
---|
| 278 | last if (!opendir (DIR, $dirname));
|
---|
| 279 | my @dirnow = readdir (DIR);
|
---|
| 280 | closedir (DIR);
|
---|
| 281 |
|
---|
| 282 | # We're only interested if there are more files than there were before
|
---|
| 283 | last if (scalar(@dirnow) <= scalar(@dir));
|
---|
| 284 |
|
---|
| 285 | # Any new files are added to the end of @dir to get processed by the loop
|
---|
| 286 | foreach my $subfilenow (@dirnow) {
|
---|
| 287 | for ($j = 0; $j < $num_files; $j++) {
|
---|
| 288 | last if ($subfilenow eq $dir[$j]);
|
---|
| 289 | }
|
---|
| 290 | if ($j == $num_files) {
|
---|
| 291 | # New file
|
---|
| 292 | push(@dir, $subfilenow);
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | # When the new files have been processed, check again
|
---|
| 297 | $num_files = scalar(@dir);
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | my $subfile = $dir[$i];
|
---|
[2228] | 301 | last if ($maxdocs != -1 && $count >= $maxdocs);
|
---|
| 302 | next if ($subfile =~ /^\.\.?$/);
|
---|
[2813] | 303 | next if ($read_metadata_files && $subfile =~ /metadata\.xml$/);
|
---|
[3108] | 304 |
|
---|
| 305 | # check for a symlink pointing back to a leading directory
|
---|
| 306 | if (-d "$dirname/$subfile" && -l "$dirname/$subfile") {
|
---|
| 307 | # readlink gives a "fatal error" on systems that don't implement
|
---|
| 308 | # symlinks. This assumes the the -l test above would fail on those.
|
---|
| 309 | my $linkdest=readlink "$dirname/$subfile";
|
---|
| 310 | if (!defined ($linkdest)) {
|
---|
| 311 | # system error - file not found?
|
---|
| 312 | warn "RecPlug: symlink problem - $!";
|
---|
| 313 | } else {
|
---|
| 314 | # see if link points to current or a parent directory
|
---|
| 315 | if ($linkdest =~ m@^[\./\\]+$@ ||
|
---|
| 316 | index($dirname, $linkdest) != -1) {
|
---|
| 317 | warn "RecPlug: Ignoring recursive symlink ($dirname/$subfile -> $linkdest)\n";
|
---|
| 318 | next;
|
---|
| 319 | ;
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[2813] | 324 | print $outhandle "RecPlug: preparing metadata for $subfile\n" if ($verbosity > 2);
|
---|
[317] | 325 |
|
---|
[2228] | 326 | # Make a copy of $in_metadata to pass to $subfile
|
---|
| 327 | $out_metadata = {};
|
---|
| 328 | &combine_metadata_structures($out_metadata, $in_metadata);
|
---|
[317] | 329 |
|
---|
[2228] | 330 | # Next add metadata read in XML files (if it is supplied)
|
---|
| 331 | if ($additionalmetadata == 1) {
|
---|
| 332 |
|
---|
| 333 | my ($filespec, $mdref);
|
---|
| 334 | foreach $filespec (@extrametakeys) {
|
---|
[2818] | 335 | if ($subfile =~ /^$filespec$/) {
|
---|
[2228] | 336 | print $outhandle "File \"$subfile\" matches filespec \"$filespec\"\n"
|
---|
| 337 | if ($verbosity > 2);
|
---|
| 338 | $mdref = $extrametadata{$filespec};
|
---|
| 339 | &combine_metadata_structures($out_metadata, $mdref);
|
---|
| 340 | }
|
---|
[4] | 341 | }
|
---|
| 342 | }
|
---|
[2228] | 343 |
|
---|
| 344 | # Recursively read each $subfile
|
---|
| 345 | print $outhandle "RecPlug recurring: $subfile\n" if ($verbosity > 2);
|
---|
[5295] | 346 |
|
---|
[2228] | 347 | $count += &plugin::read ($pluginfo, $base_dir,
|
---|
| 348 | &util::filename_cat($file, $subfile),
|
---|
[6332] | 349 | $out_metadata, $processor, $maxdocs, $gli);
|
---|
[4] | 350 | }
|
---|
[7686] | 351 |
|
---|
| 352 | return $count;
|
---|
[2228] | 353 | }
|
---|
[4] | 354 |
|
---|
[2228] | 355 |
|
---|
| 356 |
|
---|
| 357 | # Read a manually-constructed metadata file and store the data
|
---|
[2813] | 358 | # it contains in the $metadataref structure.
|
---|
[2228] | 359 | #
|
---|
| 360 | # (metadataref is a reference to a hash whose keys are filenames
|
---|
| 361 | # and whose values are metadata hash structures.)
|
---|
| 362 |
|
---|
[2813] | 363 | sub read_metadata_xml_file {
|
---|
| 364 | my $self = shift(@_);
|
---|
[2228] | 365 | my ($filename, $metadataref, $metakeysref) = @_;
|
---|
[2813] | 366 | $self->{'metadataref'} = $metadataref;
|
---|
| 367 | $self->{'metakeysref'} = $metakeysref;
|
---|
[2228] | 368 |
|
---|
[2813] | 369 | eval {
|
---|
| 370 | $self->{'parser'}->parsefile($filename);
|
---|
| 371 | };
|
---|
| 372 | if ($@) {
|
---|
[3116] | 373 | die "RecPlug: ERROR $filename is not a well formed metadata.xml file ($@)\n";
|
---|
[2813] | 374 | }
|
---|
| 375 | }
|
---|
[2228] | 376 |
|
---|
[2813] | 377 | sub Doctype {
|
---|
| 378 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
[2925] | 379 |
|
---|
| 380 | # allow the short-lived and badly named "GreenstoneDirectoryMetadata" files
|
---|
| 381 | # to be processed as well as the "DirectoryMetadata" files which should now
|
---|
| 382 | # be created by import.pl
|
---|
| 383 | die if ($name !~ /^(Greenstone)?DirectoryMetadata$/);
|
---|
[2813] | 384 | }
|
---|
| 385 |
|
---|
| 386 | sub StartTag {
|
---|
| 387 | my ($expat, $element) = @_;
|
---|
| 388 |
|
---|
| 389 | if ($element eq "FileSet") {
|
---|
| 390 | $self->{'saved_targets'} = [];
|
---|
| 391 | $self->{'saved_metadata'} = {};
|
---|
[2228] | 392 | }
|
---|
[2813] | 393 | elsif ($element eq "FileName") {
|
---|
| 394 | $self->{'in_filename'} = 1;
|
---|
| 395 | }
|
---|
| 396 | elsif ($element eq "Metadata") {
|
---|
| 397 | $self->{'metadata_name'} = $_{'name'};
|
---|
| 398 | if ((defined $_{'mode'}) && ($_{'mode'} eq "accumulate")) {
|
---|
| 399 | $self->{'metadata_accumulate'} = 1;
|
---|
| 400 | } else {
|
---|
| 401 | $self->{'metadata_accumulate'} = 0;
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
[2228] | 405 |
|
---|
[2813] | 406 | sub EndTag {
|
---|
| 407 | my ($expat, $element) = @_;
|
---|
[2228] | 408 |
|
---|
[2813] | 409 | if ($element eq "FileSet") {
|
---|
| 410 | push (@{$self->{'metakeysref'}}, @{$self->{'saved_targets'}});
|
---|
| 411 | foreach my $target (@{$self->{'saved_targets'}}) {
|
---|
| 412 | $self->{'metadataref'}->{$target} = $self->{'saved_metadata'};
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 | elsif ($element eq "FileName") {
|
---|
| 416 | $self->{'in_filename'} = 0;
|
---|
| 417 | }
|
---|
| 418 | elsif ($element eq "Metadata") {
|
---|
| 419 | $self->{'metadata_name'} = "";
|
---|
| 420 | }
|
---|
[2228] | 421 |
|
---|
[2813] | 422 | }
|
---|
[2228] | 423 |
|
---|
[2813] | 424 | sub Text {
|
---|
| 425 |
|
---|
| 426 | if ($self->{'in_filename'}) {
|
---|
| 427 | # $_ == FileName content
|
---|
| 428 | push (@{$self->{'saved_targets'}}, $_);
|
---|
| 429 | }
|
---|
| 430 | elsif (defined ($self->{'metadata_name'}) && $self->{'metadata_name'} ne "") {
|
---|
| 431 | # $_ == Metadata content
|
---|
| 432 | my $mname = $self->{'metadata_name'};
|
---|
| 433 | if (defined $self->{'saved_metadata'}->{$mname}) {
|
---|
| 434 | if ($self->{'metadata_accumulate'}) {
|
---|
| 435 | # accumulate mode - add value to existing value(s)
|
---|
| 436 | if (ref ($self->{'saved_metadata'}->{$mname}) eq "ARRAY") {
|
---|
| 437 | push (@{$self->{'saved_metadata'}->{$mname}}, $_);
|
---|
[2228] | 438 | } else {
|
---|
[2813] | 439 | $self->{'saved_metadata'}->{$mname} =
|
---|
| 440 | [$self->{'saved_metadata'}->{$mname}, $_];
|
---|
[2228] | 441 | }
|
---|
[2813] | 442 | } else {
|
---|
| 443 | # override mode
|
---|
| 444 | $self->{'saved_metadata'}->{$mname} = $_;
|
---|
[2228] | 445 | }
|
---|
[2813] | 446 | } else {
|
---|
| 447 | if ($self->{'metadata_accumulate'}) {
|
---|
| 448 | # accumulate mode - add value into (currently empty) array
|
---|
| 449 | $self->{'saved_metadata'}->{$mname} = [$_];
|
---|
| 450 | } else {
|
---|
| 451 | # override mode
|
---|
| 452 | $self->{'saved_metadata'}->{$mname} = $_;
|
---|
| 453 | }
|
---|
[2228] | 454 | }
|
---|
| 455 | }
|
---|
[4] | 456 | }
|
---|
| 457 |
|
---|
[2813] | 458 | # This Char function overrides the one in XML::Parser::Stream to overcome a
|
---|
| 459 | # problem where $expat->{Text} is treated as the return value, slowing
|
---|
| 460 | # things down significantly in some cases.
|
---|
| 461 | sub Char {
|
---|
| 462 | $_[0]->{'Text'} .= $_[1];
|
---|
| 463 | return undef;
|
---|
| 464 | }
|
---|
[4] | 465 |
|
---|
[2228] | 466 | # Combine two metadata structures. Given two references to metadata
|
---|
| 467 | # element structures, add every field of the second ($mdref2) to the first
|
---|
| 468 | # ($mdref1).
|
---|
| 469 | #
|
---|
| 470 | # Afterwards $mdref1 will be updated, and $mdref2 will be unchanged.
|
---|
| 471 | #
|
---|
| 472 | # We have to be acreful about the way we merge metadata when one metadata
|
---|
| 473 | # structure is in "override" mode and one is in "merge" mode. In fact, we
|
---|
| 474 | # use the mode from the second structure, $mdref2, because it is generally
|
---|
| 475 | # defined later (lower in the directory structure) and is therefore more
|
---|
| 476 | # "local" to the document concerned.
|
---|
| 477 | #
|
---|
| 478 | # Another issue is the use of references to pass metadata around. If we
|
---|
| 479 | # simply copy one metadata structure reference to another, then we're
|
---|
| 480 | # effectively justr copyinga pointer, and changes to the new referene
|
---|
| 481 | # will affect the old (copied) one also. This also applies to ARRAY
|
---|
| 482 | # references used as metadata element values (hence the "clonedata"
|
---|
| 483 | # function below).
|
---|
| 484 |
|
---|
| 485 | sub combine_metadata_structures {
|
---|
| 486 | my ($mdref1, $mdref2) = @_;
|
---|
| 487 | my ($key, $value1, $value2);
|
---|
| 488 |
|
---|
| 489 | foreach $key (keys %$mdref2) {
|
---|
| 490 |
|
---|
| 491 | $value1 = $mdref1->{$key};
|
---|
| 492 | $value2 = $mdref2->{$key};
|
---|
| 493 |
|
---|
| 494 | # If there is no existing value for this metadata field in
|
---|
| 495 | # $mdref1, so we simply copy the value from $mdref2 over.
|
---|
| 496 | if (!defined $value1) {
|
---|
| 497 | $mdref1->{$key} = &clonedata($value2);
|
---|
| 498 | }
|
---|
| 499 | # Otherwise we have to add the new values to the existing ones.
|
---|
| 500 | # If the second structure is accumulated, then acculate all the
|
---|
| 501 | # values into the first structure
|
---|
| 502 | elsif ((ref $value2) eq "ARRAY") {
|
---|
| 503 | # If the first metadata element is a scalar we have to
|
---|
| 504 | # convert it into an array before we add anything more.
|
---|
| 505 | if ((ref $value1) ne 'ARRAY') {
|
---|
| 506 | $mdref1->{$key} = [$value1];
|
---|
| 507 | $value1 = $mdref1->{$key};
|
---|
| 508 | }
|
---|
| 509 | # Now add the value(s) from the second array to the first
|
---|
| 510 | $value2 = &clonedata($value2);
|
---|
| 511 | push @$value1, @$value2;
|
---|
| 512 | }
|
---|
| 513 | # Finally, If the second structure is not an array erference, we
|
---|
| 514 | # know it is in override mode, so override the first structure.
|
---|
| 515 | else {
|
---|
| 516 | $mdref1->{$key} = &clonedata($value2);
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 |
|
---|
| 522 | # Make a "cloned" copy of a metadata value.
|
---|
| 523 | # This is trivial for a simple scalar value,
|
---|
| 524 | # but not for an array reference.
|
---|
| 525 |
|
---|
| 526 | sub clonedata {
|
---|
| 527 | my ($value) = @_;
|
---|
| 528 | my $result;
|
---|
| 529 |
|
---|
| 530 | if ((ref $value) eq 'ARRAY') {
|
---|
| 531 | $result = [];
|
---|
| 532 | foreach my $item (@$value) {
|
---|
| 533 | push @$result, $item;
|
---|
| 534 | }
|
---|
| 535 | } else {
|
---|
| 536 | $result = $value;
|
---|
| 537 | }
|
---|
| 538 | return $result;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 |
|
---|
[4] | 542 | 1;
|
---|