source: gsdl/trunk/perllib/plugins/MetadataCSVPlugin.pm@ 16386

Last change on this file since 16386 was 16386, checked in by kjdon, 16 years ago

global block pass: now uses process_exp instead of block_exp. during metadata_read the file is added to the block list in so its not processed again in read

  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1###########################################################################
2#
3# MetadataCSVPlugin.pm -- A plugin for metadata in comma-separated value format
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright 2006 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27package MetadataCSVPlugin;
28
29
30use BasePlugin;
31use strict;
32
33
34sub BEGIN {
35 @MetadataCSVPlugin::ISA = ('BasePlugin');
36}
37
38
39my $arguments = [
40 { 'name' => "process_exp",
41 'desc' => "{BasePlugin.process_exp}",
42 'type' => "regexp",
43 'reqd' => "no",
44 'deft' => &get_default_process_exp() }
45
46];
47
48
49my $options = { 'name' => "MetadataCSVPlugin",
50 'desc' => "{MetadataCSVPlugin.desc}",
51 'abstract' => "no",
52 'inherits' => "yes",
53 'args' => $arguments };
54
55
56sub new
57{
58 my ($class) = shift (@_);
59 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
60 push(@$pluginlist, $class);
61
62 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
63 push(@{$hashArgOptLists->{"OptList"}},$options);
64
65 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
66
67 return bless $self, $class;
68}
69
70
71sub get_default_process_exp
72{
73 return q^(?i)\.csv$^;
74}
75
76sub metadata_read
77{
78 my $self = shift (@_);
79 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
80
81 # Read metadata from CSV files
82 my $filename = &util::filename_cat($base_dir, $file);
83 if ($filename !~ /\.csv$/ || !-f $filename) {
84 return undef;
85 }
86 print STDERR "\n<Processing n='$file' p='MetadataCSVPlugin'>\n" if ($gli);
87 print STDERR "MetadataCSVPlugin: processing $file\n" if ($self->{'verbosity'}) > 1;
88
89 # add the file to the block list so that it won't be processed in read, as we will do all we can with it here
90 $block_hash->{'file_blocks'}->{$filename} = 1;
91
92 # Read the CSV file to get the metadata
93 my $csv_file_content;
94 open(CSV_FILE, "$filename");
95 my $csv_file_reader = new multiread();
96 $csv_file_reader->set_handle('MetadataCSVPlugin::CSV_FILE');
97 $csv_file_reader->read_file(\$csv_file_content);
98 close(CSV_FILE);
99
100 # Split the file into lines and read the first line (contains the metadata names)
101 $csv_file_content =~ s/\r/\n/g; # Handle non-Unix line endings
102 $csv_file_content =~ s/\n+/\n/g;
103 my @csv_file_lines = split(/\n/, $csv_file_content);
104 my $csv_file_field_line = shift(@csv_file_lines);
105 my @csv_file_fields = split(/\,/, $csv_file_field_line);
106 my $found_filename_field = 0;
107 for (my $i = 0; $i < scalar(@csv_file_fields); $i++) {
108 # Remove any spaces from the field names
109 $csv_file_fields[$i] =~ s/ //g;
110 if ($csv_file_fields[$i] eq "Filename") {
111 $found_filename_field = 1;
112 }
113 }
114
115 if (!$found_filename_field) {
116 print STDERR "MetadataCSVPlugin Error: No Filename field in CSV file: $filename\n";
117 return -1; # error
118 }
119 # Read each line of the file and assign the metadata appropriately
120 foreach my $csv_line (@csv_file_lines) {
121 # Ignore lines containing only whitespace
122 next if ($csv_line =~ /^\s*$/);
123 my $orig_csv_line = $csv_line;
124 # Build a hash of metadata name to metadata value for this line
125 my %csv_line_metadata;
126 my $i = 0;
127 $csv_line .= ","; # To make the regular expressions simpler
128 while ($csv_line ne "") {
129 # Metadata values containing commas are quoted
130 if ($csv_line =~ s/^\"(.*?)\"\,//) {
131 # Only bother with non-empty values
132 if ($1 ne "" && defined($csv_file_fields[$i])) {
133 if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
134 $csv_line_metadata{$csv_file_fields[$i]} = [];
135 }
136 push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
137 }
138 }
139 # Normal comma-separated case
140 elsif ($csv_line =~ s/^(.*?)\,//) {
141 # Only bother with non-empty values
142 if ($1 ne "" && defined($csv_file_fields[$i])) {
143 if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
144 $csv_line_metadata{$csv_file_fields[$i]} = [];
145 }
146 push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
147 }
148 }
149 # The line must be formatted incorrectly
150 else {
151 print STDERR "MetadataCSVPlugin Error: Badly formatted CSV line: $csv_line.\n";
152 last;
153 }
154
155 $i++;
156 }
157
158 # We can't associate any metadata without knowing the file to associate it with
159 my $csv_line_filename_array = $csv_line_metadata{"Filename"};
160 if (!defined $csv_line_filename_array) {
161 print STDERR "MetadataCSVPlugin Error: No Filename metadata in CSV line: $orig_csv_line\n";
162 next;
163 }
164 my $csv_line_filename = shift(@$csv_line_filename_array);
165 delete $csv_line_metadata{"Filename"};
166
167 # Associate the metadata now
168 $extrametadata->{$csv_line_filename} = \%csv_line_metadata;
169 push(@$extrametakeys, $csv_line_filename);
170 }
171}
172
173
1741;
Note: See TracBrowser for help on using the repository browser.