[22227] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # OpenOfficeConverter - helper plugin that does office document conversion
|
---|
| 4 | # using jodconverter combined with OpenOffice
|
---|
| 5 | #
|
---|
| 6 | # A component of the Greenstone digital library software
|
---|
| 7 | # from the New Zealand Digital Library Project at the
|
---|
| 8 | # University of Waikato, New Zealand.
|
---|
| 9 | #
|
---|
| 10 | # Copyright (C) 2010 New Zealand Digital Library Project
|
---|
| 11 | #
|
---|
| 12 | # This program is free software; you can redistribute it and/or modify
|
---|
| 13 | # it under the terms of the GNU General Public License as published by
|
---|
| 14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | # (at your option) any later version.
|
---|
| 16 | #
|
---|
| 17 | # This program is distributed in the hope that it will be useful,
|
---|
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | # GNU General Public License for more details.
|
---|
| 21 | #
|
---|
| 22 | # You should have received a copy of the GNU General Public License
|
---|
| 23 | # along with this program; if not, write to the Free Software
|
---|
| 24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | #
|
---|
| 26 | ###########################################################################
|
---|
| 27 | package OpenOfficeConverter;
|
---|
| 28 |
|
---|
[22494] | 29 | use ConvertBinaryFile;
|
---|
[22227] | 30 | use BaseMediaConverter;
|
---|
| 31 |
|
---|
| 32 | use strict;
|
---|
| 33 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 34 |
|
---|
| 35 | use gsprintf 'gsprintf';
|
---|
| 36 |
|
---|
| 37 | BEGIN {
|
---|
[22494] | 38 | @OpenOfficeConverter::ISA = ('ConvertBinaryFile', 'BaseMediaConverter');
|
---|
[22227] | 39 | }
|
---|
| 40 |
|
---|
| 41 | my $arguments = [ ];
|
---|
| 42 |
|
---|
| 43 | my $opt_arguments = [
|
---|
| 44 | { 'name' => "openoffice_port",
|
---|
[22494] | 45 | 'desc' => "{OpenOfficeConverter.openoffice_port}",
|
---|
[22227] | 46 | 'type' => "int",
|
---|
| 47 | 'deft' => "8100",
|
---|
| 48 | 'range' => "81,",
|
---|
| 49 | 'reqd' => "no" },
|
---|
| 50 | ];
|
---|
| 51 |
|
---|
| 52 | my $options = { 'name' => "OpenOfficeConverter",
|
---|
| 53 | 'desc' => "{OpenOfficeConverter.desc}",
|
---|
| 54 | 'abstract' => "yes",
|
---|
| 55 | 'inherits' => "yes",
|
---|
| 56 | 'args' => $arguments };
|
---|
| 57 |
|
---|
| 58 | sub new {
|
---|
| 59 | my ($class) = shift (@_);
|
---|
| 60 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 61 | push(@$pluginlist, $class);
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | # Check that OpenOffice and jodconverter are installed and available on
|
---|
| 65 | # the path
|
---|
| 66 |
|
---|
| 67 | my $openoffice_conversion_available = 1;
|
---|
| 68 | my $no_openoffice_conversion_reason = "";
|
---|
| 69 |
|
---|
[22375] | 70 | if (! defined $ENV{'GEXT_OPENOFFICE'}) {
|
---|
[22227] | 71 | $openoffice_conversion_available = 0;
|
---|
| 72 | $no_openoffice_conversion_reason = "gextopenofficenotinstalled";
|
---|
| 73 | }
|
---|
| 74 | else {
|
---|
[22375] | 75 | my $gextoo_home = $ENV{'GEXT_OPENOFFICE'};
|
---|
[22227] | 76 | my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverter.jar");
|
---|
| 77 |
|
---|
| 78 | if (!-e $jodjar) {
|
---|
| 79 | print STDERR "Failed to find $jodjar\n";
|
---|
| 80 | $openoffice_conversion_available = 0;
|
---|
| 81 | $no_openoffice_conversion_reason = "gextjodconverternotinstalled";
|
---|
| 82 | }
|
---|
| 83 | else {
|
---|
| 84 | # test to see if soffice in in path
|
---|
[22494] | 85 | if ($ENV{'GSDLOS'} =~ m/^windows$/) {
|
---|
| 86 | my $ooffice_dir_guess =
|
---|
| 87 | &util::filename_cat($ENV{'ProgramFiles'},"OpenOffice.org 3",
|
---|
| 88 | "program");
|
---|
| 89 | if (-d $ooffice_dir_guess) {
|
---|
| 90 | &util::envvar_append("PATH",$ooffice_dir_guess);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[22227] | 93 |
|
---|
[22502] | 94 | my $cmd = "soffice -headless 2>&1";
|
---|
[22494] | 95 |
|
---|
| 96 | my $status = system($cmd);
|
---|
| 97 | if ($status != 0) {
|
---|
[22227] | 98 | print STDERR "Failed to run: $cmd\n";
|
---|
| 99 | print STDERR "$!\n";
|
---|
| 100 | $openoffice_conversion_available = 0;
|
---|
| 101 | $no_openoffice_conversion_reason = "openofficenotinstalled";
|
---|
[22502] | 102 | }
|
---|
[22227] | 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | if ($openoffice_conversion_available) {
|
---|
[22494] | 107 | push(@$arguments,@$opt_arguments);
|
---|
[22227] | 108 | }
|
---|
| 109 |
|
---|
| 110 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 111 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
| 112 |
|
---|
[22494] | 113 | my $bmc_self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists,1);
|
---|
| 114 | my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[22227] | 115 |
|
---|
[22494] | 116 | my $self = BasePlugin::merge_inheritance($bmc_self,$cbf_self);
|
---|
| 117 |
|
---|
[22227] | 118 | if ($openoffice_conversion_available) {
|
---|
[22506] | 119 | my $oo_port = $self->{'openoffice_port'};
|
---|
| 120 | my $launch_cmd = "soffice";
|
---|
| 121 | $launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
|
---|
| 122 | $launch_cmd .= " -headless";
|
---|
| 123 |
|
---|
| 124 | $self->{'openoffice_launch_cmd'} = $launch_cmd;
|
---|
[22227] | 125 | }
|
---|
| 126 | else {
|
---|
| 127 | $self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
|
---|
| 128 |
|
---|
| 129 | my $outhandle = $self->{'outhandle'};
|
---|
| 130 | &gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
|
---|
| 131 | }
|
---|
[22494] | 132 |
|
---|
| 133 | $self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
|
---|
[22227] | 134 |
|
---|
| 135 | return bless $self, $class;
|
---|
| 136 |
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | sub init {
|
---|
| 140 | my $self = shift(@_);
|
---|
[22494] | 141 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
[22227] | 142 |
|
---|
[22494] | 143 | $self->ConvertBinaryFile::init($verbosity,$outhandle,$failhandle);
|
---|
| 144 |
|
---|
[22227] | 145 | $self->{'ootmp_file_paths'} = ();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | sub deinit {
|
---|
| 149 | my $self = shift(@_);
|
---|
| 150 |
|
---|
[22494] | 151 | $self->ConvertBinaryFile::deinit(@_);
|
---|
| 152 |
|
---|
[22227] | 153 | $self->clean_up_temporary_files();
|
---|
| 154 | }
|
---|
[22494] | 155 |
|
---|
| 156 |
|
---|
[22227] | 157 |
|
---|
| 158 |
|
---|
| 159 | sub convert {
|
---|
| 160 | my $self = shift(@_);
|
---|
| 161 | my $source_file_path = shift(@_);
|
---|
| 162 | my $target_file_type = shift(@_);
|
---|
| 163 | my $convert_options = shift(@_) || "";
|
---|
| 164 | my $convert_id = shift(@_) || "";
|
---|
| 165 | my $cache_mode = shift(@_) || "";
|
---|
| 166 |
|
---|
| 167 | my $outhandle = $self->{'outhandle'};
|
---|
| 168 | my $verbosity = $self->{'verbosity'};
|
---|
| 169 |
|
---|
| 170 | my $source_file_no_path = &File::Basename::basename($source_file_path);
|
---|
| 171 |
|
---|
| 172 | # Determine the full name and path of the output file
|
---|
| 173 | my $target_file_path;
|
---|
| 174 | if ($self->{'enable_cache'}) {
|
---|
| 175 | my $cached_image_dir = $self->{'cached_dir'};
|
---|
| 176 | my $image_root = $self->{'cached_file_root'};
|
---|
| 177 | $image_root .= "_$convert_id" if ($convert_id ne "");
|
---|
| 178 | my $image_file = "$image_root.$target_file_type";
|
---|
| 179 | $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
|
---|
| 180 | }
|
---|
| 181 | else {
|
---|
| 182 | $target_file_path = &util::get_tmp_filename($target_file_type);
|
---|
| 183 | push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | # Generate and run the convert command
|
---|
| 187 | my $convert_command = "oo2html $convert_options \"$source_file_path\" \"$target_file_path\"";
|
---|
| 188 |
|
---|
| 189 | my $print_info = { 'message_prefix' => $convert_id,
|
---|
| 190 | 'message' => "Converting $source_file_no_path to: $convert_id $target_file_type" };
|
---|
| 191 | $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
|
---|
| 192 |
|
---|
| 193 | my ($regenerated,$result,$had_error)
|
---|
| 194 | = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
|
---|
| 195 |
|
---|
| 196 | return ($result,$target_file_path);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | sub convert_without_result {
|
---|
| 200 | my $self = shift(@_);
|
---|
| 201 |
|
---|
| 202 | my $source_file_path = shift(@_);
|
---|
| 203 | my $target_file_type = shift(@_);
|
---|
| 204 | my $convert_options = shift(@_) || "";
|
---|
| 205 | my $convert_id = shift(@_) || "";
|
---|
| 206 |
|
---|
| 207 | return $self->convert($source_file_path,$target_file_type,
|
---|
| 208 | $convert_options,$convert_id,"without_result");
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 |
|
---|
[22494] | 212 | sub tmp_area_convert_fileXX {
|
---|
| 213 | my $self = shift (@_);
|
---|
| 214 | my ($output_ext, $input_filename, $textref) = @_;
|
---|
| 215 |
|
---|
| 216 | my $outhandle = $self->{'outhandle'};
|
---|
| 217 | my $convert_to = $self->{'convert_to'};
|
---|
| 218 | my $failhandle = $self->{'failhandle'};
|
---|
| 219 | my $convert_to_ext = $self->{'convert_to_ext'};
|
---|
| 220 |
|
---|
| 221 | # derive tmp filename from input filename
|
---|
| 222 | my ($tailname, $dirname, $suffix)
|
---|
| 223 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
| 224 | }
|
---|
[22227] | 225 |
|
---|
[22494] | 226 |
|
---|
[22227] | 227 | sub clean_up_temporary_files {
|
---|
| 228 | my $self = shift(@_);
|
---|
| 229 |
|
---|
| 230 | foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
|
---|
| 231 | if (-e $ootmp_file_path) {
|
---|
| 232 | &util::rm($ootmp_file_path);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | $self->{'ootmp_file_paths'} = ();
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | 1;
|
---|