[12330] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[17743] | 3 | # GreenstoneXMLPlugout.pm -- the plugout module for Greenstone Archives
|
---|
[12330] | 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 |
|
---|
[17743] | 26 | package GreenstoneXMLPlugout;
|
---|
[12330] | 27 |
|
---|
| 28 | use strict;
|
---|
| 29 | no strict 'refs';
|
---|
[13172] | 30 | no strict 'subs';
|
---|
[12330] | 31 |
|
---|
| 32 | eval {require bytes};
|
---|
| 33 | use util;
|
---|
[27306] | 34 | use FileUtils;
|
---|
[17203] | 35 | use BasePlugout;
|
---|
[13172] | 36 | use docprint;
|
---|
[12330] | 37 |
|
---|
| 38 | sub BEGIN {
|
---|
[17743] | 39 | @GreenstoneXMLPlugout::ISA = ('BasePlugout');
|
---|
[12330] | 40 | }
|
---|
| 41 |
|
---|
[28642] | 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 | ];
|
---|
[17743] | 50 | my $options = { 'name' => "GreenstoneXMLPlugout",
|
---|
| 51 | 'desc' => "{GreenstoneXMLPlugout.desc}",
|
---|
[12330] | 52 | 'abstract' => "no",
|
---|
[28642] | 53 | 'inherits' => "yes",
|
---|
| 54 | 'args' => $arguments };
|
---|
[12330] | 55 |
|
---|
| 56 | sub new {
|
---|
| 57 | my ($class) = shift (@_);
|
---|
| 58 | my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
|
---|
| 59 | push(@$plugoutlist, $class);
|
---|
| 60 |
|
---|
[17203] | 61 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 62 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[12330] | 63 |
|
---|
[17203] | 64 | my $self = new BasePlugout($plugoutlist,$inputargs,$hashArgOptLists);
|
---|
[32511] | 65 |
|
---|
| 66 | if ($self->{'info_only'}) {
|
---|
| 67 | # don't worry about any options etc
|
---|
| 68 | return bless $self, $class;
|
---|
| 69 | }
|
---|
[17203] | 70 | return bless $self, $class;
|
---|
[12330] | 71 | }
|
---|
| 72 |
|
---|
[28642] | 73 | sub is_group {
|
---|
| 74 | my $self = shift (@_);
|
---|
| 75 | return ($self->{'group_size'} > 1);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[32512] | 78 | sub old_unused_saveas {
|
---|
[12330] | 79 | my $self = shift (@_);
|
---|
[28642] | 80 | my ($doc_obj, $doc_dir) = @_;
|
---|
[13172] | 81 | my $outhandler;
|
---|
[27522] | 82 | my $output_file;
|
---|
[13172] | 83 | if ($self->{'debug'}) {
|
---|
| 84 | $outhandler = STDOUT;
|
---|
[27517] | 85 | }
|
---|
[13172] | 86 | else {
|
---|
[28642] | 87 |
|
---|
| 88 | $self->process_assoc_files($doc_obj, $doc_dir, '');
|
---|
[19494] | 89 | $self->process_metafiles_metadata ($doc_obj);
|
---|
[28642] | 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 {
|
---|
[32534] | 96 | $output_file = $self->get_output_file($doc_dir); #&FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
|
---|
[28642] | 97 | # open the new handle
|
---|
| 98 | $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
|
---|
[19494] | 99 |
|
---|
[28642] | 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");
|
---|
[27517] | 113 |
|
---|
[28642] | 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 | }
|
---|
[27517] | 118 |
|
---|
[32512] | 119 | my $section_text = &docprint::get_section_xml($doc_obj);
|
---|
[28642] | 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);
|
---|
[12330] | 125 | }
|
---|
[27517] | 126 |
|
---|
[28642] | 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'}) {
|
---|
[13172] | 129 | if (defined $self->{'xslt_writer'}){
|
---|
| 130 | $self->close_xslt_pipe();
|
---|
| 131 | }
|
---|
| 132 | else {
|
---|
[27522] | 133 | &FileUtils::closeFileHandle($output_file, \$outhandler) if defined $output_file;
|
---|
[13172] | 134 | }
|
---|
[12330] | 135 | }
|
---|
[32533] | 136 | $self->{'short_doc_file'} = $self->get_short_doc_file($doc_dir); #&FileUtils::filenameConcatenate($doc_dir, "doc.xml");
|
---|
[28642] | 137 |
|
---|
| 138 | $self->store_output_info_reference($doc_obj);
|
---|
| 139 |
|
---|
[12330] | 140 | }
|
---|
| 141 |
|
---|
[32533] | 142 | # can be overridden in subclasses, for instance by GreenstoneSQLPlugout, to produce a different filename
|
---|
| 143 | # like docsql.xml
|
---|
| 144 | sub get_short_doc_file {
|
---|
| 145 | my $self = shift (@_);
|
---|
| 146 | my ($doc_dir) = @_;
|
---|
| 147 | return &FileUtils::filenameConcatenate($doc_dir, "doc.xml");
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | # can be overridden in subclasses, for instance by GreenstoneSQLPlugout, to produce a different filename
|
---|
| 151 | # like docsql.xml
|
---|
| 152 | sub get_output_file {
|
---|
| 153 | my $self = shift (@_);
|
---|
| 154 | my ($doc_dir) = @_;
|
---|
| 155 | return &FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[32512] | 158 | sub pre_saveas {
|
---|
| 159 | my $self = shift (@_);
|
---|
| 160 | my ($doc_obj, $doc_dir) = @_;
|
---|
| 161 | my $outhandler;
|
---|
| 162 | my $output_file;
|
---|
[32513] | 163 |
|
---|
| 164 | $self->process_assoc_files($doc_obj, $doc_dir, '');
|
---|
| 165 | $self->process_metafiles_metadata ($doc_obj);
|
---|
| 166 |
|
---|
[32512] | 167 | if ($self->{'debug'}) {
|
---|
| 168 | $outhandler = STDOUT;
|
---|
| 169 | }
|
---|
| 170 | else {
|
---|
| 171 |
|
---|
| 172 | # open up the outhandler
|
---|
| 173 | if ($self->is_group() && !$self->{'new_doc_dir'}) {
|
---|
| 174 | # we already have a handle open ??
|
---|
| 175 | $outhandler = $self->{'group_outhandler'};
|
---|
| 176 | } else {
|
---|
[32533] | 177 | $output_file = $self->get_output_file($doc_dir); #&FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
|
---|
[32512] | 178 | # open the new handle
|
---|
| 179 | $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
|
---|
| 180 |
|
---|
| 181 | if (defined $self->{'xslt_writer'}){
|
---|
| 182 | $outhandler = $self->{'xslt_writer'};
|
---|
| 183 | }
|
---|
| 184 | else{
|
---|
| 185 | $outhandler = $self->get_output_handler($output_file);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if ($self->is_group()) {
|
---|
| 189 | $self->{'group_outhandler'} = $outhandler;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | } # else not debug
|
---|
| 193 | binmode($outhandler,":utf8");
|
---|
| 194 |
|
---|
| 195 | # only output the header if we have started a new doc
|
---|
| 196 | if (!$self->is_group() || $self->{'new_doc_dir'}) {
|
---|
| 197 | $self->output_xml_header($outhandler);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | return ($outhandler, $output_file);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | sub saveas {
|
---|
| 204 | my $self = shift (@_);
|
---|
| 205 | my ($doc_obj, $doc_dir) = @_;
|
---|
| 206 |
|
---|
| 207 | # pre
|
---|
| 208 | my ($outhandler, $output_file) = $self->pre_saveas(@_);
|
---|
| 209 | push(@_, $outhandler, $output_file);
|
---|
| 210 |
|
---|
| 211 | # write out the doc xml file for the current document
|
---|
| 212 | my $section_text = &docprint::get_section_xml($doc_obj);
|
---|
| 213 | print $outhandler $section_text;
|
---|
| 214 |
|
---|
| 215 | # post
|
---|
| 216 | $self->post_saveas(@_);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | sub post_saveas {
|
---|
| 220 | my $self = shift (@_);
|
---|
| 221 | my ($doc_obj, $doc_dir, $outhandler, $output_file) = @_;
|
---|
| 222 |
|
---|
| 223 | # only output the footer if we are not doing group stuff. The group file will be finished in close_group_output
|
---|
| 224 | if (!$self->is_group()) {
|
---|
| 225 | $self->output_xml_footer($outhandler);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | # close off the output - in a group process situation, this will be done by close_group_output
|
---|
| 229 | if (!$self->is_group() && !$self->{'debug'}) {
|
---|
| 230 | if (defined $self->{'xslt_writer'}){
|
---|
| 231 | $self->close_xslt_pipe();
|
---|
| 232 | }
|
---|
| 233 | else {
|
---|
| 234 | &FileUtils::closeFileHandle($output_file, \$outhandler) if defined $output_file;
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
[32534] | 237 | $self->{'short_doc_file'} = $self->get_short_doc_file($doc_dir); #&FileUtils::filenameConcatenate($doc_dir, "doc.xml");
|
---|
[32512] | 238 |
|
---|
| 239 | $self->store_output_info_reference($doc_obj);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[28642] | 242 | sub output_xml_header {
|
---|
| 243 | my $self = shift (@_);
|
---|
| 244 | my ($outhandle) = @_;
|
---|
[13172] | 245 |
|
---|
[28642] | 246 | print $outhandle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
|
---|
| 247 | print $outhandle "<!DOCTYPE Archive SYSTEM \"http://greenstone.org/dtd/Archive/1.0/Archive.dtd\">\n";
|
---|
| 248 | print $outhandle "<Archive>\n";
|
---|
| 249 | }
|
---|
[13172] | 250 |
|
---|
[28642] | 251 | sub output_xml_footer {
|
---|
| 252 | my $self = shift (@_);
|
---|
| 253 | my ($outhandle) = @_;
|
---|
| 254 |
|
---|
| 255 | print $outhandle "</Archive>\n";
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | sub close_group_output
|
---|
| 259 | {
|
---|
| 260 | my $self = shift(@_);
|
---|
| 261 |
|
---|
| 262 | # make sure that the handle has been opened - it won't be if we failed
|
---|
| 263 | # to import any documents...
|
---|
| 264 | my $outhandle = $self->{'group_outhandler'};
|
---|
| 265 | if (defined(fileno($outhandle))) {
|
---|
| 266 | $self->output_xml_footer($outhandle);
|
---|
| 267 | &FileUtils::closeFileHandle("", \$outhandle);
|
---|
| 268 | undef $self->{'group_outhandler'}
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | my $OID = $self->{'gs_OID'};
|
---|
| 272 | my $short_doc_file = $self->{'short_doc_file'};
|
---|
| 273 |
|
---|
| 274 | ### TODO - from here is old code. check that it is still valid.
|
---|
| 275 | if ($self->{'gzip'}) {
|
---|
| 276 | my $doc_file = $self->{'gs_filename'};
|
---|
| 277 | `gzip $doc_file`;
|
---|
| 278 | $doc_file .= ".gz";
|
---|
| 279 | $short_doc_file .= ".gz";
|
---|
| 280 | if (!&FileUtils::fileExists($doc_file)) {
|
---|
| 281 | my $outhandle = $self->{'output_handle'};
|
---|
| 282 | print $outhandle "error while gzipping: $doc_file doesn't exist\n";
|
---|
| 283 | return 0;
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return 1;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 |
|
---|
[12330] | 291 | 1;
|
---|
| 292 |
|
---|