Changeset 13174


Ignore:
Timestamp:
2006-10-27T14:33:59+13:00 (18 years ago)
Author:
kjdon
Message:

now checks for Filename field when reading through the first line. Also, puts values into an array, so that multiple values can be set for a single metadata field

File:
1 edited

Legend:

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

    r13173 r13174  
    108108    my $csv_file_field_line = shift(@csv_file_lines);
    109109    my @csv_file_fields = split(/\,/, $csv_file_field_line);
     110    my $found_filename_field = 0;
    110111    for (my $i = 0; $i < scalar(@csv_file_fields); $i++) {
    111112    # Remove any spaces from the field names
    112113    $csv_file_fields[$i] =~ s/ //g;
     114    if ($csv_file_fields[$i] eq "Filename") {
     115        $found_filename_field = 1;
     116    }
    113117    }
    114118
     119    if (!$found_filename_field) {
     120    print STDERR "MetadataCSVPlug Error: No Filename field in CSV file: $filename\n";
     121    return -1; # error
     122    }
    115123    # Read each line of the file and assign the metadata appropriately
    116124    foreach my $csv_line (@csv_file_lines) {
    117125    # Ignore lines containing only whitespace
    118126    next if ($csv_line =~ /^\s*$/);
    119 
     127    my $orig_csv_line = $csv_line;
    120128    # Build a hash of metadata name to metadata value for this line
    121129    my %csv_line_metadata;
     
    127135        # Only bother with non-empty values
    128136        if ($1 ne "" && defined($csv_file_fields[$i])) {
    129             $csv_line_metadata{$csv_file_fields[$i]} = $1;
     137            if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
     138            $csv_line_metadata{$csv_file_fields[$i]} = [];
     139            }
     140            push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
    130141        }
    131142        }
     
    134145        # Only bother with non-empty values
    135146        if ($1 ne "" && defined($csv_file_fields[$i])) {
    136             $csv_line_metadata{$csv_file_fields[$i]} = $1;
     147            if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
     148            $csv_line_metadata{$csv_file_fields[$i]} = [];
     149            }
     150            push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
    137151        }
    138152        }
    139153        # The line must be formatted incorrectly
    140154        else {
    141         print STDERR "Error: Badly formatted CSV line: $csv_line.\n";
     155        print STDERR "MetadataCSVPlug Error: Badly formatted CSV line: $csv_line.\n";
    142156        last;
    143157        }
     
    147161
    148162    # We can't associate any metadata without knowing the file to associate it with
    149     my $csv_line_filename = $csv_line_metadata{"Filename"};
    150     if (!defined($csv_line_filename)) {
    151         print STDERR "Error: No Filename metadata in CSV line: $csv_line\n";
     163    my $csv_line_filename_array = $csv_line_metadata{"Filename"};
     164    if (!defined $csv_line_filename_array) {
     165        print STDERR "MetadataCSVPlug Error: No Filename metadata in CSV line: $orig_csv_line\n";
    152166        next;
    153167    }
     168    my $csv_line_filename = shift(@$csv_line_filename_array);
    154169    delete $csv_line_metadata{"Filename"};
    155170
Note: See TracChangeset for help on using the changeset viewer.