Ignore:
Timestamp:
2008-08-11T10:44:04+12:00 (16 years ago)
Author:
kjdon
Message:

code to read in marc mapping files moved from MARCXMLPlugin to marcmapping.pm, and its now also used by MARCPlugin.pm so that MARCplugin can use qualified dublin core

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/plugins/MARCXMLPlugin.pm

    r16521 r16692  
    3131
    3232use ReadXMLFile;
     33use marcmapping;
    3334
    3435use strict;
     
    8586
    8687
    87 sub _parse_marc_metadata_mapping
    88 {
    89     my $self = shift(@_);
    90     my ($mm_file,$metadata_mapping) = @_;
    91 
    92     my $outhandle = $self->{'outhandle'};
    93 
    94     if (open(MMIN, "<$mm_file"))
    95     {
    96     my $l=0;
    97     my $line;
    98     while (defined($line=<MMIN>))
    99     {
    100         $l++;
    101         chomp $line;
    102         $line =~ s/#.*$//; # strip out any comments, including end of line
    103         next if ($line =~ m/^\s*$/);
    104         $line =~ s/\s+$//; # remove any white space at end of line
    105 
    106         my $parse_error_count = 0;
    107         if ($line =~ m/^-(\d+)\s*$/) {
    108         # special "remove" rule syntax
    109         my $marc_info = $1;
    110         if (defined $metadata_mapping->{$marc_info}) {
    111             delete $metadata_mapping->{$marc_info};
    112         }
    113         else {
    114             print $outhandle "Parse Warning: Did not file pre-existing rule $marc_info to remove";
    115             print $outhandle " on line $l of $mm_file:\n";
    116             print $outhandle "  $line\n";
    117         }
    118         }
    119         elsif ($line =~ m/^(.*?)->\s*([\w\^]+)$/)
    120         {
    121         my $lhs = $1;
    122         my $gsdl_info = $2;
    123 
    124         my @fields = split(/,\s*/,$lhs);
    125         my $f;
    126         while ($f  = shift (@fields)) {
    127             $f =~ s/\s+$//; # remove any white space at end of line
    128 
    129             if ($f =~ m/^(\d+)\-(\d+)$/) {
    130             # number range => genrate number in range and
    131             # push on to array
    132             push(@fields,$1..$2);
    133             next;
    134             }
    135 
    136             if ($f =~ m/^(\d+)((?:(?:\$|\^)\w)*)\s*$/) {
    137 
    138             my $marc_info = $1;
    139             my $opt_sub_fields = $2;
    140 
    141             if ($opt_sub_fields ne "") {           
    142                 my @sub_fields = split(/\$|\^/,$opt_sub_fields);
    143                 shift @sub_fields; # skip first entry, which is blank
    144 
    145                 foreach my $sub_field (@sub_fields) {
    146                 $metadata_mapping->{$marc_info."\$".$sub_field} = $gsdl_info;
    147                 }
    148             }
    149             else {
    150                 # no subfields to worry about
    151                 $marc_info =~ s/\^/\$/;
    152                 $metadata_mapping->{$marc_info} = $gsdl_info;
    153             }
    154             }
    155             else {
    156             $parse_error_count++;
    157             }
    158         }
    159         }
    160         else
    161         {
    162         $parse_error_count++;
    163         }
    164 
    165         if ($parse_error_count>0) {
    166        
    167         print $outhandle "Parse Error: $parse_error_count syntax error(s) on line $l of $mm_file:\n";
    168         print $outhandle "  $line\n";
    169         }
    170     }
    171     close(MMIN);
    172     }
    173     else
    174     {
    175     print STDERR "Unable to open $mm_file: $!\n";
    176     }
    177 }
    178 
    179 
    180 sub parse_marc_metadata_mapping
    181 {
    182     my $self = shift(@_);
    183     my ($mm_file_or_files) = @_;
    184 
    185     my $metadata_mapping = {};
    186 
    187     if (ref ($mm_file_or_files) eq 'SCALAR') {
    188     my $mm_file = $mm_file_or_files;
    189     $self->_parse_marc_metadata_mapping($mm_file,$metadata_mapping);
    190     }
    191     else {
    192     my $mm_files = $mm_file_or_files;
    193    
    194     # Need to process files in reverse order.  This is so in the
    195     # case where we have both a "collect" and "main" version,
    196     # the "collect" one tops up the main one
    197 
    198     my $mm_file;
    199     while ($mm_file = pop(@$mm_files)) {
    200         $self->_parse_marc_metadata_mapping($mm_file,$metadata_mapping);
    201     }
    202     }
    203 
    204     return $metadata_mapping;
    205 }
    206 
    207 
    20888sub init {
    20989    my $self = shift (@_);
     
    21696    }
    21797
    218     # read in the metadata mapping file
     98    # read in the metadata mapping files
    21999    my $mm_files = &util::locate_config_files($self->{'metadata_mapping_file'});
    220100
     
    233113    }
    234114    else {
    235     $self->{'metadata_mapping'} = $self->parse_marc_metadata_mapping($mm_files);
     115    $self->{'metadata_mapping'} = &marcmapping::parse_marc_metadata_mapping($mm_files, $outhandle);
    236116    }
    237117
     
    316196    }
    317197    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
    318     $doc_obj->add_metadata($doc_obj->get_top_section(), "FileFormat", "XML");
     198    $doc_obj->add_metadata($doc_obj->get_top_section(), "FileFormat", "MARCXML");
    319199
    320200    my $outhandle = $self->{'outhandle'};
Note: See TracChangeset for help on using the changeset viewer.