source: main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm

Last change on this file was 38851, checked in by davidb, 5 weeks ago

General upgrade to support exporting files suitable for inclusion in OpenAI's Assistants API

  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
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
38use IO::Handle;
39
40sub BEGIN {
41 @GreenstoneXMLPlugout::ISA = ('BasePlugout');
42}
43
44my $arguments = [
45 { 'name' => "group_size",
46 'desc' => "{BasPlugout.group_size}",
47 'type' => "int",
48 'deft' => "1",
49 'reqd' => "no",
50 'hiddengli' => "no"}
51 ];
52my $options = { 'name' => "GreenstoneXMLPlugout",
53 'desc' => "{GreenstoneXMLPlugout.desc}",
54 'abstract' => "no",
55 'inherits' => "yes",
56 'args' => $arguments };
57
58sub new {
59 my ($class) = shift (@_);
60 my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
61 push(@$plugoutlist, $class);
62
63 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
64 push(@{$hashArgOptLists->{"OptList"}},$options);
65
66 my $self = new BasePlugout($plugoutlist,$inputargs,$hashArgOptLists);
67
68 if ($self->{'info_only'}) {
69 # don't worry about any options etc
70 return bless $self, $class;
71 }
72 return bless $self, $class;
73}
74
75sub is_group {
76 my $self = shift (@_);
77 return ($self->{'group_size'} > 1);
78}
79
80# can be overridden in subclasses, for instance by GreenstoneSQLPlugout, to produce a different filename
81# like docsql.xml
82sub get_doc_xml_filename {
83 my $self = shift (@_);
84 my ($doc_obj) = @_;
85 return "doc.xml";
86}
87
88sub pre_saveas {
89 my $self = shift (@_);
90 my ($doc_obj, $doc_dir) = @_;
91 my $outhandler;
92 my $output_file;
93
94 $self->process_assoc_files($doc_obj, $doc_dir, '');
95 $self->process_metafiles_metadata ($doc_obj);
96
97 if ($self->{'debug'}) {
98 $outhandler = STDOUT;
99 }
100 else {
101
102 # open up the outhandler
103 if ($self->is_group() && !$self->{'new_doc_dir'}) {
104 # we already have a handle open ??
105 $outhandler = $self->{'group_outhandler'};
106 } else {
107 # Here if is_group() and looking to start a new doc
108 # Or, not running with is_group() at all (i.e. every doc separately output)
109 $output_file = $output_file = &FileUtils::filenameConcatenate(
110 $self->{'output_dir'}, $doc_dir, $self->get_doc_xml_filename($doc_obj));
111 # open the new handle
112 $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
113
114 if (defined $self->{'xslt_writer'}){
115 $outhandler = $self->{'xslt_writer'};
116 }
117 else{
118 $outhandler = $self->get_output_handler($output_file);
119 }
120
121 if ($self->is_group()) {
122 $self->{'group_outhandler'} = $outhandler;
123 }
124 }
125 } # else not debug
126
127 # As currently written, when using group_size > 1 the binmode gets re-applied
128 # $outhandler, when it has already been set to utf8. However, this doesn't
129 # appear to cause any issues.
130 binmode($outhandler,":utf8");
131
132 # only output the header if we have started a new doc
133 if (!$self->is_group() || $self->{'new_doc_dir'}) {
134 $self->output_xml_header($outhandler, $doc_obj->get_OID());
135 }
136
137 return ($outhandler, $output_file);
138}
139
140sub saveas {
141 my $self = shift (@_);
142 my ($doc_obj, $doc_dir) = @_;
143
144 # pre
145 my ($outhandler, $output_file) = $self->pre_saveas(@_);
146 push(@_, $outhandler, $output_file);
147
148 # write out the doc xml file for the current document
149 my $section_text = &docprint::get_section_xml($doc_obj);
150
151# print STDERR "====\n";
152# print STDERR "$section_text\n";
153# print STDERR "====\n";
154
155 print $outhandler $section_text;
156
157 # post
158 $self->post_saveas(@_);
159}
160
161sub post_saveas {
162 my $self = shift (@_);
163 my ($doc_obj, $doc_dir, $outhandler, $output_file) = @_;
164
165 # only output the footer if we are not doing group stuff. The group file will be finished in close_group_output
166 if (!$self->is_group()) {
167 $self->output_xml_footer($outhandler);
168 }
169
170 # close off the output - in a group process situation, this will be done by close_group_output
171 if (!$self->is_group() && !$self->{'debug'}) {
172 if (defined $self->{'xslt_writer'}){
173 $self->close_xslt_pipe();
174 }
175 else {
176 &FileUtils::closeFileHandle($output_file, \$outhandler) if defined $output_file;
177 }
178 }
179 $self->{'short_doc_file'} = &FileUtils::filenameConcatenate(
180 $doc_dir, $self->get_doc_xml_filename($doc_obj));
181
182 $self->store_output_info_reference($doc_obj);
183}
184
185sub output_xml_header {
186 my $self = shift (@_);
187 my ($outhandle, $doc_oid) = @_;
188
189 print $outhandle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
190 print $outhandle "<!DOCTYPE Archive SYSTEM \"https://greenstone.org/dtd/Archive/1.0/Archive.dtd\">\n";
191 print $outhandle "<Archive>\n";
192}
193
194sub output_xml_footer {
195 my $self = shift (@_);
196 my ($outhandle) = @_;
197
198 print $outhandle "</Archive>\n";
199}
200
201sub close_group_output
202{
203 my $self = shift(@_);
204
205 # make sure that the handle has been opened - it won't be if we failed
206 # to import any documents...
207 my $outhandle = $self->{'group_outhandler'};
208 if (defined(fileno($outhandle))) {
209 $self->output_xml_footer($outhandle);
210
211 if (defined $self->{'xslt_writer'}){
212 $self->close_xslt_pipe();
213 }
214 else {
215 &FileUtils::closeFileHandle("", \$outhandle);
216 }
217 undef $self->{'group_outhandler'}
218 }
219
220 my $OID = $self->{'gs_OID'};
221 my $short_doc_file = $self->{'short_doc_file'};
222
223 ### TODO - from here is old code. check that it is still valid.
224 if ($self->{'gzip'}) {
225 my $doc_file = $self->{'gs_filename'};
226 `gzip $doc_file`;
227 $doc_file .= ".gz";
228 $short_doc_file .= ".gz";
229 if (!&FileUtils::fileExists($doc_file)) {
230 my $outhandle = $self->{'output_handle'};
231 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
232 return 0;
233 }
234 }
235
236 return 1;
237}
238
239
2401;
241
Note: See TracBrowser for help on using the repository browser.