source: main/trunk/greenstone2/perllib/plugins/ISISPlugin.pm@ 24349

Last change on this file since 24349 was 23564, checked in by kjdon, 13 years ago

added a bit more comment about store_block_files

  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1###########################################################################
2#
3# ISISPlugin.pm -- A plugin for CDS/ISIS databases
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 1999-2004 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 ISISPlugin;
28
29
30use multiread;
31use SplitTextFile;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36# ISISPlugin is a sub-class of SplitTextFile.
37sub BEGIN {
38 @ISISPlugin::ISA = ('SplitTextFile');
39}
40
41
42my $arguments =
43 [ { 'name' => "process_exp",
44 'desc' => "{BasePlugin.process_exp}",
45 'type' => "regexp",
46 'reqd' => "no",
47 'deft' => &get_default_process_exp() },
48 { 'name' => "block_exp",
49 'desc' => "{BasePlugin.block_exp}",
50 'type' => "regexp",
51 'reqd' => "no",
52 'deft' => &get_default_block_exp(),
53 'hiddengli' => "yes" },
54 { 'name' => "split_exp",
55 'desc' => "{SplitTextFile.split_exp}",
56 'type' => "regexp",
57 'reqd' => "no",
58 'deft' => &get_default_split_exp(),
59 'hiddengli' => "yes" },
60
61 # The interesting options
62 { 'name' => "entry_separator",
63 'desc' => "{ISISPlugin.entry_separator}",
64 'type' => "string",
65 'reqd' => "no",
66 'deft' => "<br>" },
67 { 'name' => "subfield_separator",
68 'desc' => "{ISISPlugin.subfield_separator}",
69 'type' => "string",
70 'reqd' => "no",
71 'deft' => ", " }
72 ];
73
74my $options = { 'name' => "ISISPlugin",
75 'desc' => "{ISISPlugin.desc}",
76 'abstract' => "no",
77 'inherits' => "yes",
78 'explodes' => "yes",
79 'args' => $arguments };
80
81
82# This plugin processes files with the suffix ".mst"
83sub get_default_process_exp {
84 return q^(?i)(\.mst)$^;
85}
86
87
88# This plugin blocks files with the suffix ".fdt" and ".xrf"
89sub get_default_block_exp {
90 return q^(?i)(\.fdt|\.xrf)$^;
91 #return "";
92}
93
94
95# This plugin splits the input text at the "----------" lines
96sub get_default_split_exp {
97 return q^\r?\n----------\r?\n^;
98}
99
100
101sub new
102{
103 my ($class) = shift (@_);
104 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
105 push(@$pluginlist, $class);
106
107 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
108 push(@{$hashArgOptLists->{"OptList"}},$options);
109
110 my $self = new SplitTextFile($pluginlist, $inputargs, $hashArgOptLists);
111
112 if ($self->{'info_only'}) {
113 # don't worry about any options etc
114 return bless $self, $class;
115 }
116
117 # isis plug doesn't care about encoding - it assumes ascii unless the user
118 # has specified an encoding
119 if ($self->{'input_encoding'} eq "auto") {
120 $self->{'input_encoding'} = "ascii";
121 }
122 return bless $self, $class;
123}
124
125# we block the corresponding fdt and xrf
126# a pain on windows. blocks xxx.FDT, but if actual file is xx.fdt then
127# complains that no plugin can process it. Have put it back to using
128# block exp for now
129# This works now, as are doing case insenstive blocking on windows. However,
130# a pain for GLI as will not know what plugin processes the fdt and xrf.
131# if add to process expression, then get more problems.
132sub store_block_files_tmp {
133
134 my $self =shift (@_);
135 my ($filename_full_path, $block_hash) = @_;
136 print STDERR "in store block files\n";
137 $self->check_auxiliary_files($filename_full_path);
138 if (-e $self->{'fdt_file_path'}) {
139 print STDERR "$self->{'fdt_file_path'}\n";
140 my $fdt_file = $self->{'fdt_file_path'};
141 &util::block_filename($block_hash,$fdt_file);
142 }
143 if (-e $self->{'xrf_file_path'}) {
144 print STDERR "$self->{'xrf_file_path'}\n";
145 my $xrf_file = $self->{'xrf_file_path'};
146 &util::block_filename($block_hash,$xrf_file);
147 }
148
149
150}
151
152sub check_auxiliary_files {
153 my $self = shift (@_);
154 my ($filename) = @_;
155
156 my ($database_file_path_root) = ($filename =~ /(.*)\.mst$/i);
157 # Check the associated .fdt and .xrf files exist
158 $self->{'fdt_file_path'} = $database_file_path_root . ".FDT";
159 if (!-e $self->{'fdt_file_path'}) {
160 $self->{'fdt_file_path'} = $database_file_path_root . ".fdt";
161 }
162 $self->{'xrf_file_path'} = $database_file_path_root . ".XRF";
163 if (!-e $self->{'xrf_file_path'}) {
164 $self->{'xrf_file_path'} = $database_file_path_root . ".xrf";
165 }
166}
167
168
169sub read_file
170{
171 my $self = shift (@_);
172 my ($filename, $encoding, $language, $textref) = @_;
173 my $outhandle = $self->{'outhandle'};
174
175 my ($database_file_path_root) = ($filename =~ /(.*)\.mst$/i);
176 my $mst_file_path_relative = $filename;
177 $mst_file_path_relative =~ s/^.+import.(.*?)$/$1/;
178
179 # Check the associated .fdt and .xrf files exist
180 $self->check_auxiliary_files($filename);
181
182 if (!-e $self->{'fdt_file_path'}) {
183 print STDERR "<ProcessingError n='$mst_file_path_relative' r='Could not find ISIS FDT file $self->{'fdt_file_path'}'>\n" if ($self->{'gli'});
184 print $outhandle "Error: Could not find ISIS FDT file " . $self->{'fdt_file_path'} . ".\n";
185 return;
186 }
187 if (!-e $self->{'xrf_file_path'}) {
188 print STDERR "<ProcessingError n='$mst_file_path_relative' r='Could not find ISIS XRF file $self->{'xrf_file_path'}'>\n" if ($self->{'gli'});
189 print $outhandle "Error: Could not find ISIS XRF file " . $self->{'xrf_file_path'} . ".\n";
190 return;
191 }
192
193 # The text to split is exported from the database by the IsisGdl program
194 open(FILE, "IsisGdl \"$filename\" |");
195
196 my $reader = new multiread();
197 $reader->set_handle('ISISPlugin::FILE');
198 $reader->set_encoding($encoding);
199 $reader->read_file($textref);
200
201 close(FILE);
202
203 # Parse the associated ISIS database Field Definition Table file (.fdt)
204 my %fdt_mapping = &parse_field_definition_table($self->{'fdt_file_path'}, $encoding);
205 $self->{'fdt_mapping'} = \%fdt_mapping;
206
207 # Remove the line at the start, and any blank lines, so the data is split and processed properly
208 $$textref =~ s/^----------\n//;
209 $$textref =~ s/\n\n/\n/g;
210}
211
212
213sub process
214{
215 my $self = shift (@_);
216 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
217 my $outhandle = $self->{'outhandle'};
218
219 # store the auxiliary files so we know which ones were used
220 # (mst file becomes the source file)
221 $doc_obj->associate_source_file($self->{'fdt_file_path'});
222 $doc_obj->associate_source_file($self->{'xrf_file_path'});
223
224 my $section = $doc_obj->get_top_section();
225 my $fdt_mapping = $self->{'fdt_mapping'};
226 my $subfield_separator = $self->{'subfield_separator'};
227 my $entry_separator = $self->{'entry_separator'};
228 my $isis_record_html_metadata_value = "<table cellpadding=\"4\" cellspacing=\"0\">";
229
230 # Process each line of the ISIS record, one at a time
231 foreach my $line (split(/\n/, $$textref)) {
232 $line =~ s/(\s*)$//; # Remove any nasty whitespace (very important for Windows)
233 $line =~ /^tag=(.*) data=(.+)$/;
234 my $tag = $1;
235 my $tag_data = $2;
236 # print STDERR "\nTag: $tag, Data: $tag_data\n";
237
238 # Convert the tag number into a name, and remove any invalid characters
239 my $raw_metadata_name = $fdt_mapping->{$tag}{'name'} || "";
240 $raw_metadata_name =~ s/[,&\#\.\-\/]/ /g;
241 next if ($raw_metadata_name eq "");
242
243 # Metadata field names: title case, then remove spaces
244 my $metadata_name = "";
245 foreach my $word (split(/\s+/, $raw_metadata_name)) {
246 substr($word, 0, 1) =~ tr/a-z/A-Z/;
247 $metadata_name .= $word;
248 }
249
250 my $all_metadata_name = $metadata_name . "^all";
251 my $all_metadata_value = "";
252
253 # Handle repeatable fields
254 if ($fdt_mapping->{$tag}{'repeatable'}) {
255 # Multiple values are separated using the '%' character
256 foreach my $raw_metadata_value (split(/%/, $tag_data)) {
257 my $metadata_value = "";
258
259 # Handle subfields
260 while ($raw_metadata_value ne "") {
261 # If there is a subfield specifier, parse it off
262 my $sub_metadata_name = $metadata_name;
263 if ($raw_metadata_value =~ s/^\^// && $raw_metadata_value =~ s/^([a-z])//) {
264 $sub_metadata_name .= "^$1";
265 }
266
267 # Parse the value off and add it as metadata
268 $raw_metadata_value =~ s/^([^\^]*)//;
269 my $sub_metadata_value = &escape_metadata_value($1);
270
271 # print STDERR "Sub metadata name: $sub_metadata_name, value: $sub_metadata_value\n";
272 if ($sub_metadata_name ne $metadata_name) {
273 $doc_obj->add_utf8_metadata($section, $sub_metadata_name, $sub_metadata_value);
274 }
275
276 # If this tag has subfields and this is the first, use the value for the CDS/ISIS ^* field
277 if ($fdt_mapping->{$tag}{'subfields'} ne "" && $metadata_value eq "") {
278 $doc_obj->add_utf8_metadata($section, $metadata_name . "^*", $sub_metadata_value);
279 }
280
281 $metadata_value .= $subfield_separator unless ($metadata_value eq "");
282 $metadata_value .= $sub_metadata_value;
283 }
284
285 # Add the metadata value
286 # print STDERR "Metadata name: $metadata_name, value: $metadata_value\n";
287 $doc_obj->add_utf8_metadata($section, $metadata_name, $metadata_value);
288
289 $all_metadata_value .= $entry_separator unless ($all_metadata_value eq "");
290 $all_metadata_value .= $metadata_value;
291 }
292 }
293
294 # Handle non-repeatable fields
295 else {
296 my $raw_metadata_value = $tag_data;
297 my $metadata_value = "";
298
299 # Handle subfields
300 while ($raw_metadata_value ne "") {
301 # If there is a subfield specifier, parse it off
302 my $sub_metadata_name = $metadata_name;
303 if ($raw_metadata_value =~ s/^\^// && $raw_metadata_value =~ s/^([a-z])//) {
304 $sub_metadata_name .= "^$1";
305 }
306
307 # Parse the value off and add it as metadata
308 $raw_metadata_value =~ s/^([^\^]*)//;
309 my $sub_metadata_value = $1;
310
311 # Deal with the case when multiple values are specified using <...>
312 if ($sub_metadata_value =~ /\<(.+)\>/) {
313 my $sub_sub_metadata_name = $sub_metadata_name . "^sub";
314 my $tmp_sub_metadata_value = $sub_metadata_value;
315 while ($tmp_sub_metadata_value =~ s/\<(.+?)\>//) {
316 my $sub_sub_metadata_value = $1;
317 $doc_obj->add_utf8_metadata($section, $sub_sub_metadata_name, $sub_sub_metadata_value);
318 }
319 }
320 # Deal with the legacy case when multiple values are specified using /.../
321 elsif ($sub_metadata_value =~ /\/(.+)\//) {
322 my $sub_sub_metadata_name = $sub_metadata_name . "^sub";
323 my $tmp_sub_metadata_value = $sub_metadata_value;
324 while ($tmp_sub_metadata_value =~ s/\/(.+?)\///) {
325 my $sub_sub_metadata_value = $1;
326 $doc_obj->add_utf8_metadata($section, $sub_sub_metadata_name, $sub_sub_metadata_value);
327 }
328 }
329
330 # Escape the metadata value so it appears correctly in the final collection
331 $sub_metadata_value = &escape_metadata_value($sub_metadata_value);
332
333 # print STDERR "Sub metadata name: $sub_metadata_name, value: $sub_metadata_value\n";
334 if ($sub_metadata_name ne $metadata_name) {
335 $doc_obj->add_utf8_metadata($section, $sub_metadata_name, $sub_metadata_value);
336 }
337
338 # If this tag has subfields and this is the first, use the value for the CDS/ISIS ^* field
339 if ($fdt_mapping->{$tag}{'subfields'} ne "" && $metadata_value eq "") {
340 $doc_obj->add_utf8_metadata($section, $metadata_name . "^*", $sub_metadata_value);
341 }
342
343 $metadata_value .= $subfield_separator unless ($metadata_value eq "");
344 $metadata_value .= $sub_metadata_value;
345 }
346
347 # Add the metadata value
348 # print STDERR "Metadata name: $metadata_name, value: $metadata_value\n";
349 $doc_obj->add_utf8_metadata($section, $metadata_name, $metadata_value);
350
351 $all_metadata_value .= $entry_separator unless ($all_metadata_value eq "");
352 $all_metadata_value .= $metadata_value;
353 }
354
355 # Add the "^all" metadata value
356 # print STDERR "All metadata name: $all_metadata_name, value: $all_metadata_value\n";
357 $doc_obj->add_utf8_metadata($section, $all_metadata_name, $all_metadata_value);
358
359 $isis_record_html_metadata_value .= "<tr><td valign=top><nobr><b>" . $fdt_mapping->{$tag}{'name'} . "</b></nobr></td><td valign=top>" . $all_metadata_value . "</td></tr>";
360 }
361
362 # Add a reasonably formatted HTML table view of the record as the document text
363 $isis_record_html_metadata_value .= "</table>";
364 $doc_obj->add_utf8_text($section, $isis_record_html_metadata_value);
365
366 # Add the full raw record as metadata
367 my $isis_raw_record_metadata_value = &escape_metadata_value($$textref);
368 $doc_obj->add_utf8_metadata($section, "ISISRawRecord", $isis_raw_record_metadata_value);
369
370 # Add FileFormat metadata
371 $doc_obj->add_utf8_metadata($section, "FileFormat", "CDS/ISIS");
372
373 # Record was processed successfully
374 return 1;
375}
376
377
378sub parse_field_definition_table
379{
380 my $fdtfilename = shift(@_);
381 my $encoding = shift(@_);
382
383 my %fdtmapping = ();
384
385 open(FDT_FILE, "<$fdtfilename") || die "Error: Could not open file $fdtfilename.\n";
386
387 my $fdtfiletext = "";
388 my $reader = new multiread();
389 $reader->set_handle('ISISPlugin::FDT_FILE');
390 $reader->set_encoding($encoding);
391 $reader->read_file($fdtfiletext);
392
393 my $amongstdefinitions = 0;
394 foreach my $fdtfileline (split(/\n/, $$fdtfiletext)) {
395 $fdtfileline =~ s/(\s*)$//; # Remove any nasty spaces at the end of the lines
396
397 if ($amongstdefinitions) {
398 my $fieldname = &unicode::substr($fdtfileline, 0, 30);
399 my $fieldsubfields = &unicode::substr($fdtfileline, 30, 20);
400 my $fieldspecs = &unicode::substr($fdtfileline, 50, 50);
401
402 # Remove extra spaces
403 $fieldname =~ s/(\s*)$//;
404 $fieldsubfields =~ s/(\s*)$//;
405 $fieldspecs =~ s/(\s*)$//;
406
407 # Map from tag number to metadata field title, subfields, and repeatability
408 my $fieldtag = (split(/ /, $fieldspecs))[0];
409 my $fieldrepeatable = (split(/ /, $fieldspecs))[3];
410 $fdtmapping{$fieldtag} = { 'name' => $fieldname,
411 'subfields' => $fieldsubfields,
412 'repeatable' => $fieldrepeatable };
413 }
414 elsif ($fdtfileline eq "***") {
415 $amongstdefinitions = 1;
416 }
417 }
418
419 close(FDT_FILE);
420
421 return %fdtmapping;
422}
423
424
425sub escape_metadata_value
426{
427 my $value = shift(@_);
428 $value =~ s/\</&lt;/g;
429 $value =~ s/\>/&gt;/g;
430 $value =~ s/\\/\\\\/g;
431 return $value;
432}
433
434
435sub clean_up_after_exploding
436{
437 my $self = shift(@_);
438
439 # Delete the FDT and XRF files too
440 &util::rm($self->{'fdt_file_path'});
441 &util::rm($self->{'xrf_file_path'});
442}
443
444
4451;
Note: See TracBrowser for help on using the repository browser.