source: trunk/gsdl/perllib/plugins/MetadataCSVPlug.pm@ 12969

Last change on this file since 12969 was 12606, checked in by mdewsnip, 18 years ago

Renamed CSVPlug.pm to MetadataCSVPlug.pm, as there will be a new CSVPlug.pm that processes CSV files normally (ie. splits into lines and creates a document for each).

  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1###########################################################################
2#
3# MetadataCSVPlug.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 MetadataCSVPlug;
28
29
30use BasPlug;
31use strict;
32
33
34sub BEGIN {
35 @MetadataCSVPlug::ISA = ('BasPlug');
36}
37
38
39my $arguments =
40 [ { 'name' => "block_exp",
41 'desc' => "{BasPlug.block_exp}",
42 'type' => "regexp",
43 'reqd' => "no",
44 'deft' => &get_default_block_exp() } ];
45
46
47my $options = { 'name' => "MetadataCSVPlug",
48 'desc' => "{MetadataCSVPlug.desc}",
49 'abstract' => "no",
50 'inherits' => "yes",
51 'args' => $arguments };
52
53
54sub new
55{
56 my ($class) = shift (@_);
57 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
58 push(@$pluginlist, $class);
59
60 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
61 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
62
63 my $self = new BasPlug($pluginlist, $inputargs, $hashArgOptLists);
64
65 return bless $self, $class;
66}
67
68
69# Not used, just here to prevent a warning
70sub get_default_process_exp
71{
72 return q^(?i)\.csv$^;
73}
74
75
76# We don't want any other plugins to see .csv files
77sub get_default_block_exp
78{
79 return q^(?i)\.csv$^;
80}
81
82
83sub metadata_read
84{
85 my $self = shift (@_);
86 my ($pluginfo, $base_dir, $file, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
87
88 # Read metadata from CSV files
89 my $filename = &util::filename_cat($base_dir, $file);
90 if ($filename !~ /\.csv$/ || !-f $filename) {
91 return undef;
92 }
93 print STDERR "\n<Processing n='$file' p='MetadataCSVPlug'>\n" if ($gli);
94 print STDERR "MetadataCSVPlug: processing $file\n" if ($self->{'verbosity'}) > 1;
95
96 # Read the CSV file to get the metadata
97 my $csv_file_content;
98 open(CSV_FILE, "$filename");
99 my $csv_file_reader = new multiread();
100 $csv_file_reader->set_handle('MetadataCSVPlug::CSV_FILE');
101 $csv_file_reader->read_file(\$csv_file_content);
102 close(CSV_FILE);
103
104 # Split the file into lines and read the first line (contains the metadata names)
105 $csv_file_content =~ s/\n//g;
106 my @csv_file_lines = split(/\r/, $csv_file_content);
107 my $csv_file_field_line = shift(@csv_file_lines);
108 my @csv_file_fields = split(/\,/, $csv_file_field_line);
109 for (my $i = 0; $i < scalar(@csv_file_fields); $i++) {
110 # Remove any spaces from the field names
111 $csv_file_fields[$i] =~ s/ //g;
112 }
113
114 # Read each line of the file and assign the metadata appropriately
115 foreach my $csv_line (@csv_file_lines) {
116 # Ignore lines containing only whitespace
117 next if ($csv_line =~ /^\s*$/);
118
119 # Build a hash of metadata name to metadata value for this line
120 my %csv_line_metadata;
121 my $i = 0;
122 $csv_line .= ","; # To make the regular expressions simpler
123 while ($csv_line ne "") {
124 # Metadata values containing commas are quoted
125 if ($csv_line =~ s/^\"(.*?)\"\,//) {
126 # Only bother with non-empty values
127 if ($1 ne "" && defined($csv_file_fields[$i])) {
128 $csv_line_metadata{$csv_file_fields[$i]} = $1;
129 }
130 }
131 # Normal comma-separated case
132 elsif ($csv_line =~ s/^(.*?)\,//) {
133 # Only bother with non-empty values
134 if ($1 ne "" && defined($csv_file_fields[$i])) {
135 $csv_line_metadata{$csv_file_fields[$i]} = $1;
136 }
137 }
138 # The line must be formatted incorrectly
139 else {
140 print STDERR "Error: Badly formatted CSV line: $csv_line.\n";
141 last;
142 }
143
144 $i++;
145 }
146
147 # We can't associate any metadata without knowing the file to associate it with
148 my $csv_line_filename = $csv_line_metadata{"Filename"};
149 if (!defined($csv_line_filename)) {
150 print STDERR "Error: No Filename metadata in CSV line: $csv_line\n";
151 next;
152 }
153 delete $csv_line_metadata{"Filename"};
154
155 # Associate the metadata now
156 $extrametadata->{$csv_line_filename} = \%csv_line_metadata;
157 push(@$extrametakeys, $csv_line_filename);
158 }
159}
160
161
1621;
Note: See TracBrowser for help on using the repository browser.