root/main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm @ 32512

Revision 32512, 7.7 KB (checked in by ak19, 22 months ago)

Refactoring some existing perl code as per Dr Bainbridge's ideas on this, so that MySQLPlugout, which inherits from GreenstoneXMLPlugout, can be structured better for reading.

  • Property svn:keywords set to Author Date Id Revision
Line 
1###########################################################################
2#
3# GreenstoneXMLPlugout.pm -- the plugout module for Greenstone Archives
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 GreenstoneXMLPlugout;
27
28use strict;
29no strict 'refs';
30no strict 'subs';
31
32eval {require bytes};
33use util;
34use FileUtils;
35use BasePlugout;
36use docprint;
37
38sub BEGIN {
39    @GreenstoneXMLPlugout::ISA = ('BasePlugout');
40}
41
42my $arguments = [
43       { 'name' => "group_size",
44    'desc' => "{BasePlugout.group_size}",
45    'type' => "int",
46        'deft' =>  "1",
47    'reqd' => "no",
48    'hiddengli' => "no"}
49    ];
50my $options = { 'name'     => "GreenstoneXMLPlugout",
51        'desc'     => "{GreenstoneXMLPlugout.desc}",
52        'abstract' => "no",
53        'inherits' => "yes",
54        'args'     => $arguments };
55
56sub new {
57    my ($class) = shift (@_);
58    my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
59    push(@$plugoutlist, $class);
60
61    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
62    push(@{$hashArgOptLists->{"OptList"}},$options);
63
64    my $self = new BasePlugout($plugoutlist,$inputargs,$hashArgOptLists);
65   
66    if ($self->{'info_only'}) {
67        # don't worry about any options etc
68        return bless $self, $class;
69    }
70    return bless $self, $class;
71}
72
73sub is_group {
74    my $self = shift (@_);
75    return ($self->{'group_size'} > 1);
76}
77
78sub old_unused_saveas {
79    my $self = shift (@_);
80    my ($doc_obj, $doc_dir) = @_;
81    my $outhandler;
82    my $output_file;
83    if ($self->{'debug'}) {
84    $outhandler = STDOUT;
85    }
86    else {
87       
88    $self->process_assoc_files($doc_obj, $doc_dir, '');
89    $self->process_metafiles_metadata ($doc_obj);
90   
91    # open up the outhandler   
92    if ($self->is_group() && !$self->{'new_doc_dir'}) {
93        # we already have a handle open ??
94        $outhandler = $self->{'group_outhandler'};
95    } else {
96        $output_file = &FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
97        # open the new handle
98        $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
99
100        if (defined $self->{'xslt_writer'}){
101        $outhandler = $self->{'xslt_writer'};
102        }
103        else{
104        $outhandler = $self->get_output_handler($output_file);
105        }
106       
107        if ($self->is_group()) {
108        $self->{'group_outhandler'} = $outhandler;
109        }
110    }
111    } # else not debug
112    binmode($outhandler,":utf8");
113
114    # only output the header if we have started a new doc
115    if (!$self->is_group() || $self->{'new_doc_dir'}) {
116    $self->output_xml_header($outhandler);
117    }
118
119    my $section_text = &docprint::get_section_xml($doc_obj);
120    print $outhandler $section_text;
121 
122    # only output the footer if we are not doing group stuff. The group file will be finished in close_group_output
123    if (!$self->is_group()) {
124    $self->output_xml_footer($outhandler);
125    }
126
127    # close off the output - in a group process situation, this will be done by close_group_output
128    if (!$self->is_group() && !$self->{'debug'}) {
129    if (defined $self->{'xslt_writer'}){     
130        $self->close_xslt_pipe();
131    }
132    else {
133        &FileUtils::closeFileHandle($output_file, \$outhandler) if defined $output_file;
134    }
135    }
136    $self->{'short_doc_file'} = &FileUtils::filenameConcatenate($doc_dir, "doc.xml"); 
137   
138    $self->store_output_info_reference($doc_obj);
139   
140}
141
142sub pre_saveas {
143    my $self = shift (@_);
144    my ($doc_obj, $doc_dir) = @_;
145    my $outhandler;
146    my $output_file;
147    if ($self->{'debug'}) {
148    $outhandler = STDOUT;
149    }
150    else {
151       
152    $self->process_assoc_files($doc_obj, $doc_dir, '');
153    $self->process_metafiles_metadata ($doc_obj);
154   
155    # open up the outhandler   
156    if ($self->is_group() && !$self->{'new_doc_dir'}) {
157        # we already have a handle open ??
158        $outhandler = $self->{'group_outhandler'};
159    } else {
160        $output_file = &FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
161        # open the new handle
162        $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
163
164        if (defined $self->{'xslt_writer'}){
165        $outhandler = $self->{'xslt_writer'};
166        }
167        else{
168        $outhandler = $self->get_output_handler($output_file);
169        }
170       
171        if ($self->is_group()) {
172        $self->{'group_outhandler'} = $outhandler;
173        }
174    }
175    } # else not debug
176    binmode($outhandler,":utf8");
177
178    # only output the header if we have started a new doc
179    if (!$self->is_group() || $self->{'new_doc_dir'}) {
180    $self->output_xml_header($outhandler);
181    }
182 
183    return ($outhandler, $output_file);
184}
185
186sub saveas {
187    my $self = shift (@_);
188    my ($doc_obj, $doc_dir) = @_;
189
190    # pre
191    my ($outhandler, $output_file) = $self->pre_saveas(@_);
192    push(@_, $outhandler, $output_file);
193
194    # write out the doc xml file for the current document
195    my $section_text = &docprint::get_section_xml($doc_obj);
196    print $outhandler $section_text;
197
198    # post
199    $self->post_saveas(@_);
200}
201
202sub post_saveas {
203    my $self = shift (@_);   
204    my ($doc_obj, $doc_dir, $outhandler, $output_file) = @_;
205   
206    # only output the footer if we are not doing group stuff. The group file will be finished in close_group_output
207    if (!$self->is_group()) {
208    $self->output_xml_footer($outhandler);
209    }
210
211    # close off the output - in a group process situation, this will be done by close_group_output
212    if (!$self->is_group() && !$self->{'debug'}) {
213    if (defined $self->{'xslt_writer'}){     
214        $self->close_xslt_pipe();
215    }
216    else {
217        &FileUtils::closeFileHandle($output_file, \$outhandler) if defined $output_file;
218    }
219    }
220    $self->{'short_doc_file'} = &FileUtils::filenameConcatenate($doc_dir, "doc.xml"); 
221   
222    $self->store_output_info_reference($doc_obj);   
223}
224
225sub output_xml_header {
226    my $self = shift (@_);
227    my ($outhandle) = @_;
228
229    print $outhandle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
230    print $outhandle "<!DOCTYPE Archive SYSTEM \"http://greenstone.org/dtd/Archive/1.0/Archive.dtd\">\n";
231    print $outhandle "<Archive>\n";
232}
233
234sub output_xml_footer {
235    my $self = shift (@_);
236    my ($outhandle) = @_;
237
238    print $outhandle "</Archive>\n";
239}
240
241sub close_group_output
242{
243    my $self = shift(@_);
244 
245    # make sure that the handle has been opened - it won't be if we failed
246    # to import any documents...
247    my $outhandle = $self->{'group_outhandler'};
248    if (defined(fileno($outhandle))) {
249    $self->output_xml_footer($outhandle);   
250    &FileUtils::closeFileHandle("", \$outhandle);
251    undef $self->{'group_outhandler'}
252    }
253
254    my $OID = $self->{'gs_OID'};
255    my $short_doc_file = $self->{'short_doc_file'};
256   
257    ### TODO - from here is old code. check that it is still valid.
258    if ($self->{'gzip'}) {
259    my $doc_file = $self->{'gs_filename'};
260    `gzip $doc_file`;
261    $doc_file .= ".gz";
262    $short_doc_file .= ".gz";
263    if (!&FileUtils::fileExists($doc_file)) {
264         my $outhandle = $self->{'output_handle'};
265        print $outhandle "error while gzipping: $doc_file doesn't exist\n";
266        return 0;
267    }
268    }
269
270    return 1;
271}
272
273
2741;
275
Note: See TracBrowser for help on using the browser.