[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);
|
---|
[12330] | 65 |
|
---|
[17203] | 66 | return bless $self, $class;
|
---|
[12330] | 67 | }
|
---|
| 68 |
|
---|
[28642] | 69 | sub is_group {
|
---|
| 70 | my $self = shift (@_);
|
---|
| 71 | return ($self->{'group_size'} > 1);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[12330] | 74 | sub saveas {
|
---|
| 75 | my $self = shift (@_);
|
---|
[28642] | 76 | my ($doc_obj, $doc_dir) = @_;
|
---|
[13172] | 77 | my $outhandler;
|
---|
[27522] | 78 | my $output_file;
|
---|
[13172] | 79 | if ($self->{'debug'}) {
|
---|
| 80 | $outhandler = STDOUT;
|
---|
[27517] | 81 | }
|
---|
[13172] | 82 | else {
|
---|
[28642] | 83 |
|
---|
| 84 | $self->process_assoc_files($doc_obj, $doc_dir, '');
|
---|
[19494] | 85 | $self->process_metafiles_metadata ($doc_obj);
|
---|
[28642] | 86 |
|
---|
| 87 | # open up the outhandler
|
---|
| 88 | if ($self->is_group() && !$self->{'new_doc_dir'}) {
|
---|
| 89 | # we already have a handle open ??
|
---|
| 90 | $outhandler = $self->{'group_outhandler'};
|
---|
| 91 | } else {
|
---|
| 92 | $output_file = &FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
|
---|
| 93 | # open the new handle
|
---|
| 94 | $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
|
---|
[19494] | 95 |
|
---|
[28642] | 96 | if (defined $self->{'xslt_writer'}){
|
---|
| 97 | $outhandler = $self->{'xslt_writer'};
|
---|
| 98 | }
|
---|
| 99 | else{
|
---|
| 100 | $outhandler = $self->get_output_handler($output_file);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | if ($self->is_group()) {
|
---|
| 104 | $self->{'group_outhandler'} = $outhandler;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | } # else not debug
|
---|
| 108 | binmode($outhandler,":utf8");
|
---|
[27517] | 109 |
|
---|
[28642] | 110 | # only output the header if we have started a new doc
|
---|
| 111 | if (!$self->is_group() || $self->{'new_doc_dir'}) {
|
---|
| 112 | $self->output_xml_header($outhandler);
|
---|
| 113 | }
|
---|
[27517] | 114 |
|
---|
[28642] | 115 | my $section_text = &docprint::get_section_xml($doc_obj,$doc_obj->get_top_section());
|
---|
| 116 | print $outhandler $section_text;
|
---|
| 117 |
|
---|
| 118 | # only output the footer if we are not doing group stuff. The group file will be finished in close_group_output
|
---|
| 119 | if (!$self->is_group()) {
|
---|
| 120 | $self->output_xml_footer($outhandler);
|
---|
[12330] | 121 | }
|
---|
[27517] | 122 |
|
---|
[28642] | 123 | # close off the output - in a group process situation, this will be done by close_group_output
|
---|
| 124 | if (!$self->is_group() && !$self->{'debug'}) {
|
---|
[13172] | 125 | if (defined $self->{'xslt_writer'}){
|
---|
| 126 | $self->close_xslt_pipe();
|
---|
| 127 | }
|
---|
| 128 | else {
|
---|
[27522] | 129 | &FileUtils::closeFileHandle($output_file, \$outhandler) if defined $output_file;
|
---|
[13172] | 130 | }
|
---|
[12330] | 131 | }
|
---|
[28642] | 132 | $self->{'short_doc_file'} = &FileUtils::filenameConcatenate($doc_dir, "doc.xml");
|
---|
| 133 |
|
---|
| 134 | $self->store_output_info_reference($doc_obj);
|
---|
| 135 |
|
---|
[12330] | 136 | }
|
---|
| 137 |
|
---|
[28642] | 138 | sub output_xml_header {
|
---|
| 139 | my $self = shift (@_);
|
---|
| 140 | my ($outhandle) = @_;
|
---|
[13172] | 141 |
|
---|
[28642] | 142 | print $outhandle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
|
---|
| 143 | print $outhandle "<!DOCTYPE Archive SYSTEM \"http://greenstone.org/dtd/Archive/1.0/Archive.dtd\">\n";
|
---|
| 144 | print $outhandle "<Archive>\n";
|
---|
| 145 | }
|
---|
[13172] | 146 |
|
---|
[28642] | 147 | sub output_xml_footer {
|
---|
| 148 | my $self = shift (@_);
|
---|
| 149 | my ($outhandle) = @_;
|
---|
| 150 |
|
---|
| 151 | print $outhandle "</Archive>\n";
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | sub close_group_output
|
---|
| 155 | {
|
---|
| 156 | my $self = shift(@_);
|
---|
| 157 |
|
---|
| 158 | # make sure that the handle has been opened - it won't be if we failed
|
---|
| 159 | # to import any documents...
|
---|
| 160 | my $outhandle = $self->{'group_outhandler'};
|
---|
| 161 | if (defined(fileno($outhandle))) {
|
---|
| 162 | $self->output_xml_footer($outhandle);
|
---|
| 163 | &FileUtils::closeFileHandle("", \$outhandle);
|
---|
| 164 | undef $self->{'group_outhandler'}
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | my $OID = $self->{'gs_OID'};
|
---|
| 168 | my $short_doc_file = $self->{'short_doc_file'};
|
---|
| 169 |
|
---|
| 170 | ### TODO - from here is old code. check that it is still valid.
|
---|
| 171 | if ($self->{'gzip'}) {
|
---|
| 172 | my $doc_file = $self->{'gs_filename'};
|
---|
| 173 | `gzip $doc_file`;
|
---|
| 174 | $doc_file .= ".gz";
|
---|
| 175 | $short_doc_file .= ".gz";
|
---|
| 176 | if (!&FileUtils::fileExists($doc_file)) {
|
---|
| 177 | my $outhandle = $self->{'output_handle'};
|
---|
| 178 | print $outhandle "error while gzipping: $doc_file doesn't exist\n";
|
---|
| 179 | return 0;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | return 1;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
[12330] | 187 | 1;
|
---|
| 188 |
|
---|