########################################################################### # # ISISPlug.pm -- A plugin for CDS/ISIS databases # # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright 1999-2003 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### package ISISPlug; use multiread; use SplitPlug; # ISISPlug is a sub-class of SplitPlug. sub BEGIN { @ISA = ('SplitPlug'); } my $arguments = [ { 'name' => "process_exp", 'desc' => "{BasPlug.process_exp}", 'type' => "string", 'reqd' => "no", 'deft' => &get_default_process_exp() }, { 'name' => "block_exp", 'desc' => "{BasPlug.block_exp}", 'type' => "string", 'deft' => &get_default_block_exp() }, { 'name' => "subfield_separator", 'desc' => "{ISISPlug.subfield_separator}", 'type' => "string", 'reqd' => "no", 'deft' => ", " }, { 'name' => "entry_separator", 'desc' => "{ISISPlug.entry_separator}", 'type' => "string", 'reqd' => "no", 'deft' => "
" } ]; my $options = { 'name' => "ISISPlug", 'desc' => "{ISISPlug.desc}", 'inherits' => "Yes", 'args' => $arguments }; # This plugin processes files with the suffix ".mst" sub get_default_process_exp { return q^(?i)(\.mst)$^; } # This plugin blocks files with the suffix ".fdt" and ".xrf" sub get_default_block_exp { return q^(?i)(\.fdt|\.xrf)$^; } # This plugin splits the input text at the "----------" lines sub get_default_split_exp { return q^\n----------\n^; } sub new { my $class = shift(@_); my $self = new SplitPlug($class, @_); if (!parsargv::parse(\@_, q^subfield_separator/.*/, ^, \$self->{'subfield_separator'}, q^entry_separator/.*/
^, \$self->{'entry_separator'}, "allow_extra_options")) { print STDERR "\nIncorrect options passed to ISISPlug, check your collect.cfg configuration file\n"; die "\n"; } # To allow for proper inheritance of arguments my $option_list = $self->{'option_list'}; push(@{$option_list}, $options); $self->{'plugin_type'} = "ISISPlug"; return bless $self, $class; } sub read_file { my $self = shift (@_); my ($filename, $encoding, $language, $textref) = @_; my ($databasename) = ($filename =~ /([^\.]+)\.mst$/i); # The text to split is exported from the database by the IsisGdl program open(FILE, "IsisGdl $filename |"); my $reader = new multiread(); $reader->set_handle ('ISISPlug::FILE'); $reader->set_encoding ($encoding); $reader->read_file ($textref); close(FILE); # Parse the associated ISIS database Field Definition Table file (.fdt) my $fdtfilename = $databasename . ".fdt"; my %fdtmapping = &parse_field_definition_table($fdtfilename); # Map the tag numbers to tag names, using the FDT mapping $$textref =~ s/\ntag=(\d+) /\ntag=$fdtmapping{$1}{'title'} /g; # Add a newline at the start so it is split properly $$textref = "\n" . $$textref; } sub process { my $self = shift (@_); my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_; my $outhandle = $self->{'outhandle'}; my $subfield_separator = $self->{'subfield_separator'}; my $entry_separator = $self->{'entry_separator'}; # Report that we're processing the file print $outhandle "IsisPlug: processing $file\n" if ($self->{'verbosity'}) > 1; # Process each line of the ISIS record, one at a time foreach $line (split(/\n/, $$textref)) { $line =~ /^tag=(.+) data=(.+)$/; local $rawtagname = $1; local $rawtagdata = $2; # print "Raw tag: $rawtagname, Raw data: $rawtagdata\n"; # Metadata field names: title case, then remove spaces local $tagname = ""; foreach $word (split(/\s+/, $rawtagname)) { substr($word, 0, 1) =~ tr/a-z/A-Z/; $tagname .= $word; } # Make sure there is nothing bad in the tag names $tagname =~ s/&//g; # Handle each piece of metadata ('%' separated) local $completetagvalue = ""; foreach $rawtagvalue (split(/%/, $rawtagdata)) { $completetagvalue .= $entry_separator unless ($completetagvalue eq ""); # Metadata field values: take care with subfields local $completeentryvalue = ""; while ($rawtagvalue ne "") { # If there is a subfield specifier, parse it off local $subfieldname = ""; if ($rawtagvalue =~ s/^\^([a-z])//) { $subfieldname = "." . $1; } # Parse the metadata value off $rawtagvalue =~ s/^([^\^]*)//; local $metadatafieldname = $tagname . $subfieldname; local $metadatafieldvalue = $1; # print "Metadata: $metadatafieldname -> $metadatafieldvalue\n"; # Handle Keywords specially if ($metadatafieldname eq "Keywords") { local $keywordmetadatavalue = $metadatafieldvalue; local $keywordlist = ""; while ($keywordmetadatavalue =~ s/\<([^\>]+)\>//) { local $keyword = $1; $doc_obj->add_utf8_metadata($cursection, $metadatafieldname, $keyword); $keywordlist .= ", " unless ($keywordlist eq ""); $keywordlist .= $keyword; } $metadatafieldvalue = $keywordlist; } else { $doc_obj->add_utf8_metadata($cursection, $metadatafieldname, $metadatafieldvalue); } $completeentryvalue .= $subfield_separator unless ($completeentryvalue eq ""); $completeentryvalue .= $metadatafieldvalue; } $completetagvalue .= $completeentryvalue; } # print "Metadata: $tagname.all -> $completetagvalue\n"; $doc_obj->add_utf8_metadata($cursection, $tagname . ".all", $completetagvalue); } # print "\n"; # Add the full record as the document text $$textref =~ s/\/>/g; $doc_obj->add_utf8_text ($cursection, $$textref); # Document was processed successfully return 1; } sub parse_field_definition_table { local $fdtfilename = shift(@_); local %fdtmapping = (); open(FDT_FILE, "<$fdtfilename") || die "Error: Could not open file $fdtfilename.\n"; local $amongstdefinitions = 0; foreach $fdtfileline () { $fdtfileline =~ s/(\s*)$//; # Remove any nasty spaces at the end of the lines if ($amongstdefinitions) { local $fieldtitle = substr($fdtfileline, 0, 30); local $fieldsubfields = substr($fdtfileline, 30, 20); local $fieldspecs = substr($fdtfileline, 50); # Remove extra spaces $fieldtitle =~ s/(\s*)$//; $fieldsubfields =~ s/(\s*)$//; # Map from tag number to metadata field title and subfields local $fieldtag = (split(/ /, $fieldspecs))[0]; $fdtmapping{$fieldtag} = { 'title' => $fieldtitle, 'subfields' => $fieldsubfields }; } elsif ($fdtfileline eq "***") { $amongstdefinitions = 1; } } close(FDT_FILE); return %fdtmapping; } 1;