source: gsdl/trunk/perllib/plugouts/MARCXMLPlugout.pm@ 17203

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

BasPlugout renamed to BasePlugout. And tidied up the constructors

  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1###########################################################################
2#
3# MARCXMLPlugout.pm -- the plugout module for MARC xml recored
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) 2006 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
26package MARCXMLPlugout;
27
28use strict;
29no strict 'refs';
30
31eval {require bytes};
32use util;
33use BasePlugout;
34use docprint; # for escape_text
35
36
37sub BEGIN {
38 @MARCXMLPlugout::ISA = ('BasePlugout');
39}
40
41my $arguments = [
42 { 'name' => "group",
43 'desc' => "{MARCXMLPlugout.group}",
44 'type' => "flag",
45 'deft' => "0",
46 'reqd' => "no",
47 'hiddengli' => "no"},
48 { 'name' => "mapping_file",
49 'desc' => "{MARCXMLPlugout.mapping_file}",
50 'type' => "string",
51 'deft' => "$ENV{'GSDLHOME'}/etc/dc2marc-mapping.xml",
52 'reqd' => "yes",
53 'hiddengli' => "no"}
54 ];
55
56my $options = { 'name' => "MARCXMLPlugout",
57 'desc' => "{MARCXMLPlugout.desc}",
58 'abstract' => "no",
59 'inherits' => "yes",
60 'args' => $arguments
61 };
62
63sub new {
64 my ($class) = shift (@_);
65 my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
66 push(@$plugoutlist, $class);
67
68 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
69 push(@{$hashArgOptLists->{"OptList"}},$options);
70
71 my $self = new BasePlugout($plugoutlist,$inputargs,$hashArgOptLists);
72
73 $self->{'buffered_output'} ="";
74
75 if (!defined $self->{'mapping_file'} || (defined $self->{'mapping_file'} && $self->{'mapping_file'} eq "")){
76 $self->{'mapping_file'} = "$ENV{'GSDLHOME'}/etc/dc2marc-mapping.xml";
77 }
78
79 if (!defined $self->{'xslt_file'} || (defined $self->{'xslt_file'} && $self->{'xslt_file'} eq "")){
80 $self->{'xslt_file'} = "$ENV{'GSDLHOME'}/etc/dc2marc.xsl";
81 }
82
83 if(defined $self->{'mapping_file'} && $self->{'mapping_file'} ne "")
84 {
85 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'});
86 }
87
88 if(defined $self->{'xslt_file'} && $self->{'xslt_file'} ne "")
89 {
90 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'});
91 }
92
93 return bless $self, $class;
94}
95
96sub saveas {
97 my $self = shift (@_);
98 my ($doc_obj,$doc_dir) = @_;
99
100
101 if ($self->{'group'}){
102 $self->{buffered_output} .= $self->get_top_metadata_list($doc_obj)."\n";
103 return;
104 }
105
106
107 my $output_dir = $self->get_output_dir();
108 &util::mk_all_dir ($output_dir) unless -e $output_dir;
109
110 my $working_dir = &util::filename_cat ($output_dir, $doc_dir);
111 &util::mk_all_dir ($working_dir) unless -e $working_dir;
112
113 my $output_file = util::filename_cat ($working_dir, "marc.xml");
114
115 $self->open_xslt_pipe($output_file,$self->{'xslt_file'});
116
117 my $outhandler = $self->{'xslt_writer'};
118
119 $self->output_xml_header($outhandler,"MARCXML");
120 print $outhandler $self->get_top_metadata_list($doc_obj);
121 $self->output_xml_footer($outhandler,"MARCXML");
122 $self->close_xslt_pipe();
123
124}
125
126# returns a xml element of the form <MetadataList><Metadata name="metadata-name">metadata_value</Metadata>...</MetadataList>
127
128sub get_top_metadata_list {
129
130 my $self = shift (@_);
131 my ($doc_obj) = @_;
132
133 my @topmetadata =$doc_obj->get_all_metadata($doc_obj->get_top_section());
134 my $metadatalist ='<MetadataList>';
135
136 foreach my $i (@topmetadata){
137 foreach my $j (@$i){
138 my %metaMap = @$j;
139 foreach my $key (keys %metaMap){
140 $metadatalist .='<Metadata name='."\"$key\"".'>'.&docprint::escape_text($metaMap{$key}).'</Metadata>'."\n";
141 }
142 }
143 }
144
145 $metadatalist .='</MetadataList>';
146 return $metadatalist;
147}
148
149
150sub close_group_output{
151 my $self = shift (@_);
152
153 return unless $self->{'group'} and $self->{buffered_output};
154
155 my $output_dir = $self->get_output_dir();
156 &util::mk_all_dir ($output_dir) unless -e $output_dir;
157
158 my $output_file = util::filename_cat($output_dir, "marc.xml");
159
160 $self->open_xslt_pipe($output_file,$self->{'xslt_file'});
161
162 my $outhandler = $self->{'xslt_writer'};
163
164 $self->output_xml_header($outhandler,"MARCXML");
165 print $outhandler $self->{buffered_output};
166 $self->output_xml_footer($outhandler,"MARCXML");
167 $self->close_xslt_pipe();
168}
169
170
171sub is_group{
172 my $self = shift (@_);
173 return $self->{'group'};
174}
175
1761;
Note: See TracBrowser for help on using the repository browser.