source: main/trunk/greenstone2/perllib/plugins/MARCXMLPlugin.pm@ 27503

Last change on this file since 27503 was 27503, checked in by kjdon, 11 years ago

modified to handle files with just a single record. So no collection tag, just a record tag

  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1###########################################################################
2#
3# MARCXMLPlugin.pm
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 2001 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# Processes MARCXML documents. Note that this plugin does no
27# syntax checking (though the XML::Parser module tests for
28# well-formedness).
29
30package MARCXMLPlugin;
31
32use ReadXMLFile;
33use ReadTextFile;
34use MetadataRead;
35use marcmapping;
36
37use strict;
38no strict 'refs'; # allow filehandles to be variables and viceversa
39
40# methods with identical signatures take precedence in the order given in the ISA list.
41sub BEGIN {
42 @MARCXMLPlugin::ISA = ('MetadataRead', 'ReadXMLFile', 'ReadTextFile');
43}
44
45my $arguments = [{'name' => "metadata_mapping_file",
46 'desc' => "{MARCXMLPlugin.metadata_mapping_file}",
47 'type' => "string",
48 'deft' => "marc2dc.txt",
49 'reqd' => "no" },
50 { 'name' => "process_exp",
51 'desc' => "{BasePlugin.process_exp}",
52 'type' => "regexp",
53 'deft' => &get_default_process_exp(),
54 'reqd' => "no" }];
55
56my $options = { 'name' => "MARCXMLPlugin",
57 'desc' => "{MARCXMLPlugin.desc}",
58 'abstract' => "no",
59 'inherits' => "yes",
60 'args' => $arguments
61 };
62
63
64sub new {
65 my ($class) = shift (@_);
66 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
67 push(@$pluginlist, $class);
68
69 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
70 push(@{$hashArgOptLists->{"OptList"}},$options);
71
72 # we want to be able to use the textcat methods from ReadTextFile
73 # to get the language and encoding
74 new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists, 1);
75
76 my $self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
77
78 # we want to strip namespaces, so have to create a new XML parser
79 my $parser = new XML::Parser('Style' => 'Stream',
80 'Pkg' => 'ReadXMLFile',
81 'PluginObj' => $self,
82 'Namespaces' => 1, # strip out namespaces
83 'Handlers' => {'Char' => \&ReadXMLFile::Char,
84 'XMLDecl' => \&ReadXMLFile::XMLDecl,
85 'Entity' => \&ReadXMLFile::Entity,
86 'Doctype' => \&ReadXMLFile::Doctype,
87 'Default' => \&ReadXMLFile::Default
88 });
89
90 $self->{'parser'} = $parser;
91
92 $self->{'content'} = "";
93 $self->{'xmlcontent'} = "";
94 $self->{'record_count'} = 1;
95 $self->{'language'} = "";
96 $self->{'encoding'} = "";
97 $self->{'marc_mapping'} = {};
98 $self->{'current_code'} = "";
99 $self->{'current_tag'} = "";
100 $self->{'current_element'} = "";
101 $self->{'metadata_mapping'} = undef;
102 $self->{'num_processed'} = 0;
103 $self->{'indent'} = 0;
104
105 # in case we have individual records without a collection tag
106 $self->{'xmlcollectiontag'} = "<collection>";
107 return bless $self, $class;
108}
109
110
111sub get_default_process_exp {
112 my $self = shift (@_);
113
114 return q^(?i)\.xml$^;
115}
116
117sub get_doctype {
118 my $self = shift(@_);
119
120 return "(collection|record)";
121}
122
123
124sub init {
125 my $self = shift (@_);
126 my ($verbosity, $outhandle, $failhandle) = @_;
127
128 ## the mapping file has already been loaded
129 if (defined $self->{'metadata_mapping'} ){
130 $self->SUPER::init(@_);
131 return;
132 }
133
134 # read in the metadata mapping file
135 my $mm_file = &util::locate_config_file($self->{'metadata_mapping_file'});
136
137 if (! defined $mm_file)
138 {
139 my $msg = "MARCXMLPlugin ERROR: Can't locate mapping file \"" .
140 $self->{'metadata_mapping_file'} . "\".\n " .
141 " No metadata will be extracted from MARCXML files.\n";
142
143 print $outhandle $msg;
144 print $failhandle $msg;
145 $self->{'metadata_mapping'} = undef;
146 # We pick up the error in process() if there is no $mm_file
147 # If we exit here, then pluginfo.pl will exit too!
148 }
149 else {
150 $self->{'metadata_mapping'} = &marcmapping::parse_marc_metadata_mapping($mm_file, $outhandle);
151 }
152
153
154 ##map { print STDERR $_."=>".$self->{'metadata_mapping'}->{$_}."\n"; } keys %{$self->{'metadata_mapping'}};
155
156 $self->SUPER::init(@_);
157}
158
159# Called for DOCTYPE declarations - use die to bail out if this doctype
160# is not meant for this plugin
161sub xml_doctype {
162 my $self = shift(@_);
163
164 my ($expat, $name, $sysid, $pubid, $internal) = @_;
165 return;
166
167}
168
169
170sub xml_start_document {
171 my $self = shift(@_);
172
173 my ($expat, $name, $sysid, $pubid, $internal) = @_;
174
175
176 my $file = $self->{'file'};
177 my $filename = $self->{'filename'};
178
179 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
180
181 $self->{'language'} = $language;
182 $self->{'encoding'} = $encoding;
183 $self->{'element_count'} = 1;
184 $self->{'indent'} = 0;
185 my $outhandle = $self->{'outhandle'};
186 print $outhandle "MARCXMLPlugin: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
187 print STDERR "<Processing n='$self->{'file'}' p='MARCXMLPlugin'>\n" if $self->{'gli'};
188
189 # reset the base id
190 $self->{'base_oid'} = undef;
191
192}
193
194sub xml_end_document {
195
196}
197
198sub xml_start_tag {
199 my $self = shift;
200 my $expat = shift;
201 my $element = shift;
202
203 my $text = $_;
204 my $escaped_text = $self->escape_text($_);
205
206 $self->{'current_element'} = $element;
207
208 ##get all atributes of this element and store it in a map name=>value
209 my %attr_map = ();
210 my $attrstring = $_;
211 while ($attrstring =~ /(\w+)=\"(\w+)\"/){
212 $attr_map{$1}=$2;
213 $attrstring = $'; #'
214 }
215
216
217 my $processor = $self->{'processor'};
218 my $metadata = $self->{'metadata'};
219
220 ##create a new document for each record
221 if ($element eq "record") {
222 my $filename = $self->{'filename'};
223 my $language = $self->{'language'};
224 my $encoding = $self->{'encoding'};
225 my $file = $self->{'file'};
226 my $doc_obj = new doc($filename, undef, $self->{'file_rename_method'});
227 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
228 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
229
230 my ($filemeta) = $file =~ /([^\\\/]+)$/;
231 my $plugin_filename_encoding = $self->{'filename_encoding'};
232 my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
233 $self->set_Source_metadata($doc_obj, $filename, $filename_encoding);
234
235 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "SourceSegment", "$self->{'record_count'}");
236 if ($self->{'cover_image'}) {
237 $self->associate_cover_image($doc_obj, $filename);
238 }
239 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
240 $doc_obj->add_metadata($doc_obj->get_top_section(), "FileFormat", "MARCXML");
241
242 my $outhandle = $self->{'outhandle'};
243 print $outhandle "Record $self->{'record_count'}\n" if $self->{'verbosity'} > 1;
244
245 $self->{'record_count'}++;
246 $self->{'doc_obj'} = $doc_obj;
247 $self->{'num_processed'}++;
248 if (!defined $self->{'base_oid'}) {
249 $self->SUPER::add_OID($doc_obj);
250 $self->{'base_oid'} = $doc_obj->get_OID();
251 }
252
253
254 }
255
256 ## get the marc code, for example 520
257 if ($element eq "datafield") {
258 if (defined $attr_map{'tag'} and $attr_map{'tag'} ne ""){
259 $self->{'current_tag'} = $attr_map{tag};
260 }
261 }
262
263
264 ## append the subcode to the marc code for example 520a or 520b
265 if ($element eq "subfield"){
266 if (defined $attr_map{'code'} and $attr_map{'code'} ne "" and $self->{'current_tag'} ne ""){
267 $self->{'current_code'} = $attr_map{'code'};
268 }
269 }
270
271 if ($element eq "record"){
272 $self->{'indent'} = 0;
273 $self->{'content'} = "";
274 $self->{'xmlcontent'} = "";
275 }
276 else {
277 if ($element ne "subfield"){
278 $self->{'indent'} = 1;
279 }
280 else{
281 $self->{'indent'} = 2;
282 }
283 }
284
285
286 if ($element eq "collection") {
287 # remember the full start tag for <collection ...>
288 # This is needed to wrap around each <record> when generating its associate MARCXML file
289
290 $self->{'xmlcollectiontag'} = $text;
291 }
292 else {
293 $self->{'content'} .= "<br/>" if ($element ne "record");
294 $self->{'content'} .= $self->calculate_indent($self->{'indent'}).$escaped_text;
295 $self->{'xmlcontent'} .= $text;
296 }
297
298}
299
300
301
302sub xml_end_tag {
303 my $self = shift(@_);
304 my ($expat, $element) = @_;
305
306 my $text = $_;
307 my $escaped_text = $self->escape_text($_);
308
309 if ($element eq "record" and defined $self->{'doc_obj'}) {
310 # process the document
311 my $processor = $self->{'processor'};
312 my $doc_obj = $self->{'doc_obj'};
313 $self->{'content'} .= "<br/>".$escaped_text;
314 $self->{'xmlcontent'} .= $text;
315
316
317 my $top_section = $doc_obj->get_top_section();
318
319 my $tmp_marcxml_filename = &util::get_tmp_filename("xml");
320 if (open (XMLOUT,">$tmp_marcxml_filename")) {
321
322 print XMLOUT "<?xml-stylesheet type=\"text/xsl\" href=\"MARC21slim2English.xsl\"?>\n";
323 my $xml_content = $self->{'xmlcontent'};
324
325 $xml_content = $self->{'xmlcollectiontag'}.$xml_content."</collection>";
326
327 print XMLOUT $xml_content;
328
329 close(XMLOUT);
330
331 $doc_obj->associate_file($tmp_marcxml_filename,"marcxml.xml","text/xml", $top_section);
332
333 # assicate xsl style file for presentation as HTML
334 my $xsl_filename = &util::filename_cat($ENV{'GSDLHOME'},"etc","MARC21slim2English.xsl");
335 $doc_obj->associate_file($xsl_filename,"MARC21slim2English.xsl","text/xml", $top_section);
336
337 }
338 else {
339 my $outhandle = $self->{'outhandle'};
340 print $outhandle "Warning: Unable for write out associated MARCXML file $tmp_marcxml_filename\n";
341 }
342
343 # include any metadata passed in from previous plugins
344 # note that this metadata is associated with the top level section
345
346 $self->extra_metadata ($doc_obj,
347 $doc_obj->get_top_section(),
348 $self->{'metadata'});
349
350
351 $self->add_OID($doc_obj, $self->{'base_oid'}, $self->{'record_count'});
352
353 $doc_obj->add_utf8_text($doc_obj->get_top_section(),$self->{'content'});
354 $processor->process($doc_obj);
355
356 ##clean up
357 $self->{'content'} = "";
358 $self->{'xmlcontent'} = "";
359 $self->{'doc_obj'} = undef;
360 return;
361 }
362
363 ## map the xmlmarc to gsdl metadata
364 if ($element eq "datafield" and defined $self->{'doc_obj'} and defined $self->{'marc_mapping'} and defined $self->{'metadata_mapping'}){
365 my $metadata_mapping = $self->{'metadata_mapping'};
366 my $marc_mapping = $self->{'marc_mapping'};
367 my $doc_obj = $self->{'doc_obj'};
368
369 ##print STDERR "**** Marc Record\n";
370 ##map { print STDERR $_."=>".$marc_mapping->{$_}."\n"; } keys %$marc_mapping;
371 ##print STDERR "**** Metadata Mapping\n";
372 ##map { print STDERR $_."=>".$metadata_mapping->{$_}."\n"; } keys %$metadata_mapping;
373
374
375 foreach my $marc_field (keys %$metadata_mapping){
376
377 ## test whether this field has subfield
378 my $subfield = undef;
379 if ($marc_field =~ /(\d\d\d)(?:\$|\^)?(\w)/){
380 $marc_field = $1;
381 $subfield = $2;
382 }
383
384 my $matched_field = $marc_mapping->{$marc_field};
385
386 if (defined $matched_field) {
387
388 my $meta_name = undef;
389 my $meta_value = undef;
390
391 if (defined $subfield){
392 $meta_name = $metadata_mapping->{$marc_field."\$".$subfield};
393
394 $meta_value = $matched_field->{$subfield};
395
396 if (!defined $meta_value) {
397 # record read in does not have the specified subfield
398 next;
399 }
400 }
401 else {
402 $meta_name = $metadata_mapping->{$marc_field};
403
404 # no subfield => get all the values
405 my $first = 1;
406 foreach my $value (sort keys %{$matched_field}) {
407 if ($first) {
408 $meta_value = $matched_field->{$value};
409 $first = 0;
410 } else {
411 $meta_value .= " " . $matched_field->{$value};
412 }
413 }
414
415 }
416
417 ## escape [ and ]
418 $meta_value =~ s/\[/\\\[/g;
419 $meta_value =~ s/\]/\\\]/g;
420 ##print STDERR "$meta_name=$meta_value\n";
421 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(),$meta_name, $meta_value);
422
423 }
424
425 }
426
427 ##clean up
428 $self->{'marc_mapping'} = undef;
429 $self->{'current_tag'} = "";
430 }
431
432 if ($element eq "datafield"){
433 $self->{'indent'} = 1;
434 $self->{'content'} .= "<br/>".$self->calculate_indent($self->{'indent'}).$escaped_text;
435 $self->{'xmlcontent'} .= $text;
436 }
437 else{
438 $self->{'content'} .= $escaped_text;
439 $self->{'xmlcontent'} .= $text;
440 }
441
442}
443
444sub add_OID {
445 my $self = shift (@_);
446 my ($doc_obj, $id, $record_number) = @_;
447
448 my $full_id = $id . "r" . $record_number;
449 if ($self->{'OIDtype'} eq "assigned") {
450 my $identifier = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'OIDmetadata'});
451 if (defined $identifier && $identifier ne "") {
452 $full_id = $identifier;
453 $full_id = &util::tidy_up_oid($full_id);
454 }
455 }
456 $doc_obj->set_OID($full_id);
457}
458
459sub xml_text {
460 my $self = shift(@_);
461 my ($expat) = @_;
462
463 my $text = $_;
464 my $escaped_text = $self->escape_text($_);
465
466 # protect against & in raw text file
467 $text =~ s/&/&amp;/g; # can't have & in raw form, even in 'raw' xml text
468
469 ## store the text of a marc code, for exapmle 520a=>A poem about....
470 if ($self->{'current_element'} eq "subfield" and $self->{'current_code'} ne "" and $_ ne "" ){
471 ##stored it in the marc_mapping
472
473 my $current_tag = $self->{'current_tag'};
474 my $current_code = $self->{'current_code'};
475
476 $self->{'marc_mapping'}->{$current_tag}->{$current_code} .= $_;
477
478 $self->{'current_code'} = "";
479 }
480
481 $self->{'content'} .= $escaped_text;
482 $self->{'xmlcontent'} .= $text;
483
484}
485
486sub calculate_indent{
487 my ($self,$num) = @_;
488
489 my $indent ="";
490
491 for (my $i=0; $i<$num;$i++){
492 $indent .= "&nbsp;&nbsp;&nbsp;&nbsp;";
493 }
494
495 return $indent;
496
497}
498
499sub escape_text {
500 my ($self,$text) = @_;
501 # special characters in the xml encoding
502 $text =~ s/&/&amp;/g; # this has to be first...
503 $text =~ s/</&lt;/g;
504 $text =~ s/>/&gt;/g;
505 $text =~ s/\"/&quot;/g;
506
507 return $text;
508}
509
510
511sub unescape_text {
512 my ($self,$text) = @_;
513 # special characters in the xml encoding
514 $text =~ s/&lt;/</g;
515 $text =~ s/&gt;/>/g;
516 $text =~ s/&quot;/\"/g;
517
518 $text =~ s/&/&amp;/g; # can't have & in raw form, even in unescaped xml!
519
520 return $text;
521}
522
523
5241;
525
526
Note: See TracBrowser for help on using the repository browser.