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 |
|
---|
26 | package GreenstoneXMLPlugout;
|
---|
27 |
|
---|
28 | use strict;
|
---|
29 | no strict 'refs';
|
---|
30 | no strict 'subs';
|
---|
31 |
|
---|
32 | eval {require bytes};
|
---|
33 | use util;
|
---|
34 | use FileUtils;
|
---|
35 | use BasePlugout;
|
---|
36 | use docprint;
|
---|
37 |
|
---|
38 | sub BEGIN {
|
---|
39 | @GreenstoneXMLPlugout::ISA = ('BasePlugout');
|
---|
40 | }
|
---|
41 |
|
---|
42 | my $arguments = [
|
---|
43 | { 'name' => "group_size",
|
---|
44 | 'desc' => "{BasePlugout.group_size}",
|
---|
45 | 'type' => "int",
|
---|
46 | 'deft' => "1",
|
---|
47 | 'reqd' => "no",
|
---|
48 | 'hiddengli' => "no"}
|
---|
49 | ];
|
---|
50 | my $options = { 'name' => "GreenstoneXMLPlugout",
|
---|
51 | 'desc' => "{GreenstoneXMLPlugout.desc}",
|
---|
52 | 'abstract' => "no",
|
---|
53 | 'inherits' => "yes",
|
---|
54 | 'args' => $arguments };
|
---|
55 |
|
---|
56 | sub 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 |
|
---|
73 | sub is_group {
|
---|
74 | my $self = shift (@_);
|
---|
75 | return ($self->{'group_size'} > 1);
|
---|
76 | }
|
---|
77 |
|
---|
78 | # can be overridden in subclasses, for instance by GreenstoneSQLPlugout, to produce a different filename
|
---|
79 | # like docsql.xml
|
---|
80 | sub get_doc_xml_filename {
|
---|
81 | my $self = shift (@_);
|
---|
82 | my ($doc_obj) = @_;
|
---|
83 | return "doc.xml";
|
---|
84 | }
|
---|
85 |
|
---|
86 | sub pre_saveas {
|
---|
87 | my $self = shift (@_);
|
---|
88 | my ($doc_obj, $doc_dir) = @_;
|
---|
89 | my $outhandler;
|
---|
90 | my $output_file;
|
---|
91 |
|
---|
92 | $self->process_assoc_files($doc_obj, $doc_dir, '');
|
---|
93 | $self->process_metafiles_metadata ($doc_obj);
|
---|
94 |
|
---|
95 | if ($self->{'debug'}) {
|
---|
96 | $outhandler = STDOUT;
|
---|
97 | }
|
---|
98 | else {
|
---|
99 |
|
---|
100 | # open up the outhandler
|
---|
101 | if ($self->is_group() && !$self->{'new_doc_dir'}) {
|
---|
102 | # we already have a handle open ??
|
---|
103 | $outhandler = $self->{'group_outhandler'};
|
---|
104 | } else {
|
---|
105 | $output_file = $output_file = &FileUtils::filenameConcatenate(
|
---|
106 | $self->{'output_dir'}, $doc_dir, $self->get_doc_xml_filename($doc_obj));
|
---|
107 | # open the new handle
|
---|
108 | $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
|
---|
109 |
|
---|
110 | if (defined $self->{'xslt_writer'}){
|
---|
111 | $outhandler = $self->{'xslt_writer'};
|
---|
112 | }
|
---|
113 | else{
|
---|
114 | $outhandler = $self->get_output_handler($output_file);
|
---|
115 | }
|
---|
116 |
|
---|
117 | if ($self->is_group()) {
|
---|
118 | $self->{'group_outhandler'} = $outhandler;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | } # else not debug
|
---|
122 | binmode($outhandler,":utf8");
|
---|
123 |
|
---|
124 | # only output the header if we have started a new doc
|
---|
125 | if (!$self->is_group() || $self->{'new_doc_dir'}) {
|
---|
126 | $self->output_xml_header($outhandler, $doc_obj->get_OID());
|
---|
127 | }
|
---|
128 |
|
---|
129 | return ($outhandler, $output_file);
|
---|
130 | }
|
---|
131 |
|
---|
132 | sub saveas {
|
---|
133 | my $self = shift (@_);
|
---|
134 | my ($doc_obj, $doc_dir) = @_;
|
---|
135 |
|
---|
136 | # pre
|
---|
137 | my ($outhandler, $output_file) = $self->pre_saveas(@_);
|
---|
138 | push(@_, $outhandler, $output_file);
|
---|
139 |
|
---|
140 | # write out the doc xml file for the current document
|
---|
141 | my $section_text = &docprint::get_section_xml($doc_obj);
|
---|
142 | print $outhandler $section_text;
|
---|
143 |
|
---|
144 | # post
|
---|
145 | $self->post_saveas(@_);
|
---|
146 | }
|
---|
147 |
|
---|
148 | sub post_saveas {
|
---|
149 | my $self = shift (@_);
|
---|
150 | my ($doc_obj, $doc_dir, $outhandler, $output_file) = @_;
|
---|
151 |
|
---|
152 | # only output the footer if we are not doing group stuff. The group file will be finished in close_group_output
|
---|
153 | if (!$self->is_group()) {
|
---|
154 | $self->output_xml_footer($outhandler);
|
---|
155 | }
|
---|
156 |
|
---|
157 | # close off the output - in a group process situation, this will be done by close_group_output
|
---|
158 | if (!$self->is_group() && !$self->{'debug'}) {
|
---|
159 | if (defined $self->{'xslt_writer'}){
|
---|
160 | $self->close_xslt_pipe();
|
---|
161 | }
|
---|
162 | else {
|
---|
163 | &FileUtils::closeFileHandle($output_file, \$outhandler) if defined $output_file;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | $self->{'short_doc_file'} = &FileUtils::filenameConcatenate(
|
---|
167 | $doc_dir, $self->get_doc_xml_filename($doc_obj));
|
---|
168 |
|
---|
169 | $self->store_output_info_reference($doc_obj);
|
---|
170 | }
|
---|
171 |
|
---|
172 | sub output_xml_header {
|
---|
173 | my $self = shift (@_);
|
---|
174 | my ($outhandle, $doc_oid) = @_;
|
---|
175 |
|
---|
176 | print $outhandle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
|
---|
177 | print $outhandle "<!DOCTYPE Archive SYSTEM \"http://greenstone.org/dtd/Archive/1.0/Archive.dtd\">\n";
|
---|
178 | print $outhandle "<Archive>\n";
|
---|
179 | }
|
---|
180 |
|
---|
181 | sub output_xml_footer {
|
---|
182 | my $self = shift (@_);
|
---|
183 | my ($outhandle) = @_;
|
---|
184 |
|
---|
185 | print $outhandle "</Archive>\n";
|
---|
186 | }
|
---|
187 |
|
---|
188 | sub close_group_output
|
---|
189 | {
|
---|
190 | my $self = shift(@_);
|
---|
191 |
|
---|
192 | # make sure that the handle has been opened - it won't be if we failed
|
---|
193 | # to import any documents...
|
---|
194 | my $outhandle = $self->{'group_outhandler'};
|
---|
195 | if (defined(fileno($outhandle))) {
|
---|
196 | $self->output_xml_footer($outhandle);
|
---|
197 | &FileUtils::closeFileHandle("", \$outhandle);
|
---|
198 | undef $self->{'group_outhandler'}
|
---|
199 | }
|
---|
200 |
|
---|
201 | my $OID = $self->{'gs_OID'};
|
---|
202 | my $short_doc_file = $self->{'short_doc_file'};
|
---|
203 |
|
---|
204 | ### TODO - from here is old code. check that it is still valid.
|
---|
205 | if ($self->{'gzip'}) {
|
---|
206 | my $doc_file = $self->{'gs_filename'};
|
---|
207 | `gzip $doc_file`;
|
---|
208 | $doc_file .= ".gz";
|
---|
209 | $short_doc_file .= ".gz";
|
---|
210 | if (!&FileUtils::fileExists($doc_file)) {
|
---|
211 | my $outhandle = $self->{'output_handle'};
|
---|
212 | print $outhandle "error while gzipping: $doc_file doesn't exist\n";
|
---|
213 | return 0;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | return 1;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | 1;
|
---|
222 |
|
---|