########################################################################### # # MARCXMLPlugout.pm -- the plugout module for MARC xml recored # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 2006 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### package MARCXMLPlugout; use strict; no strict 'refs'; eval {require bytes}; use util; use BasPlugout; use docprint; # for escape_text sub BEGIN { @MARCXMLPlugout::ISA = ('BasPlugout'); } my $arguments = [ { 'name' => "group", 'desc' => "{MARCXMLPlugout.group}", 'type' => "flag", 'deft' => "0", 'reqd' => "no", 'hiddengli' => "no"}, { 'name' => "mapping_file", 'desc' => "{MARCXMLPlugout.mapping_file}", 'type' => "string", 'deft' => "$ENV{'GSDLHOME'}/etc/dc2marc-mapping.xml", 'reqd' => "yes", 'hiddengli' => "no"} ]; my $options = { 'name' => "MARCXMLPlugout", 'desc' => "{MARCXMLPlugout.desc}", 'abstract' => "no", 'inherits' => "yes", 'args' => $arguments }; sub new { my ($class) = shift (@_); my ($plugoutlist, $inputargs,$hashArgOptLists) = @_; push(@$plugoutlist, $class); if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});} if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)}; my $self = (defined $hashArgOptLists)? new BasPlugout($plugoutlist,$inputargs,$hashArgOptLists): new BasPlugout($plugoutlist,$inputargs); $self->{'buffered_output'} =""; if (!defined $self->{'mapping_file'} || (defined $self->{'mapping_file'} && $self->{'mapping_file'} eq "")){ $self->{'mapping_file'} = "$ENV{'GSDLHOME'}/etc/dc2marc-mapping.xml"; } if (!defined $self->{'xslt_file'} || (defined $self->{'xslt_file'} && $self->{'xslt_file'} eq "")){ $self->{'xslt_file'} = "$ENV{'GSDLHOME'}/etc/dc2marc.xsl"; } if(defined $self->{'mapping_file'} && $self->{'mapping_file'} ne "") { print STDERR "Can not find $self->{'mapping_file'}, please make sure you have supplied the correct file path\n" and die "\n" unless (-e $self->{'mapping_file'}); } if(defined $self->{'xslt_file'} && $self->{'xslt_file'} ne "") { print STDERR "Can not find $self->{'xslt_file'}, please make sure you have supplied the correct file path\n" and die "\n" unless (-e $self->{'xslt_file'}); } return bless $self, $class; } sub saveas { my $self = shift (@_); my ($doc_obj,$doc_dir) = @_; if ($self->{'group'}){ $self->{buffered_output} .= $self->get_top_metadata_list($doc_obj)."\n"; return; } my $output_dir = $self->get_output_dir(); &util::mk_all_dir ($output_dir) unless -e $output_dir; my $working_dir = &util::filename_cat ($output_dir, $doc_dir); &util::mk_all_dir ($working_dir) unless -e $working_dir; my $output_file = util::filename_cat ($working_dir, "marc.xml"); $self->open_xslt_pipe($output_file,$self->{'xslt_file'}); my $outhandler = $self->{'xslt_writer'}; $self->output_xml_header($outhandler,"MARCXML"); print $outhandler $self->get_top_metadata_list($doc_obj); $self->output_xml_footer($outhandler,"MARCXML"); $self->close_xslt_pipe(); } # returns a xml element of the form metadata_value... sub get_top_metadata_list { my $self = shift (@_); my ($doc_obj) = @_; my @topmetadata =$doc_obj->get_all_metadata($doc_obj->get_top_section()); my $metadatalist =''; foreach my $i (@topmetadata){ foreach my $j (@$i){ my %metaMap = @$j; foreach my $key (keys %metaMap){ $metadatalist .=''.&docprint::escape_text($metaMap{$key}).''."\n"; } } } $metadatalist .=''; return $metadatalist; } sub close_group_output{ my $self = shift (@_); return unless $self->{'group'} and $self->{buffered_output}; my $output_dir = $self->get_output_dir(); &util::mk_all_dir ($output_dir) unless -e $output_dir; my $output_file = util::filename_cat($output_dir, "marc.xml"); $self->open_xslt_pipe($output_file,$self->{'xslt_file'}); my $outhandler = $self->{'xslt_writer'}; $self->output_xml_header($outhandler,"MARCXML"); print $outhandler $self->{buffered_output}; $self->output_xml_footer($outhandler,"MARCXML"); $self->close_xslt_pipe(); } sub is_group{ my $self = shift (@_); return $self->{'group'}; } 1;