source: gs2-extensions/open-office/trunk/perllib/plugins/OpenOfficeConverter.pm@ 22502

Last change on this file since 22502 was 22502, checked in by kjdon, 14 years ago

added 2>&1 to soffice -headless command - otherwise it hangs under linux

  • Property svn:executable set to *
File size: 7.5 KB
Line 
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###########################################################################
27package OpenOfficeConverter;
28
29use ConvertBinaryFile;
30use BaseMediaConverter;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
35use gsprintf 'gsprintf';
36
37BEGIN {
38 @OpenOfficeConverter::ISA = ('ConvertBinaryFile', 'BaseMediaConverter');
39}
40
41my $arguments = [ ];
42
43my $opt_arguments = [
44 { 'name' => "openoffice_scripting",
45 'desc' => "{OpenOfficeConverter.openoffice_scripting}",
46 'type' => "flag",
47 'reqd' => "no" },
48 { 'name' => "openoffice_port",
49 'desc' => "{OpenOfficeConverter.openoffice_port}",
50 'type' => "int",
51 'deft' => "8100",
52 'range' => "81,",
53 'reqd' => "no" },
54 ];
55
56my $options = { 'name' => "OpenOfficeConverter",
57 'desc' => "{OpenOfficeConverter.desc}",
58 'abstract' => "yes",
59 'inherits' => "yes",
60 'args' => $arguments };
61
62sub new {
63 my ($class) = shift (@_);
64 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
65 push(@$pluginlist, $class);
66
67
68 # Check that OpenOffice and jodconverter are installed and available on
69 # the path
70
71 my $openoffice_conversion_available = 1;
72 my $no_openoffice_conversion_reason = "";
73
74 if (! defined $ENV{'GEXT_OPENOFFICE'}) {
75 $openoffice_conversion_available = 0;
76 $no_openoffice_conversion_reason = "gextopenofficenotinstalled";
77 }
78 else {
79 my $gextoo_home = $ENV{'GEXT_OPENOFFICE'};
80 my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverter.jar");
81
82 if (!-e $jodjar) {
83 print STDERR "Failed to find $jodjar\n";
84 $openoffice_conversion_available = 0;
85 $no_openoffice_conversion_reason = "gextjodconverternotinstalled";
86 }
87 else {
88 # test to see if soffice in in path
89 if ($ENV{'GSDLOS'} =~ m/^windows$/) {
90 my $ooffice_dir_guess =
91 &util::filename_cat($ENV{'ProgramFiles'},"OpenOffice.org 3",
92 "program");
93 if (-d $ooffice_dir_guess) {
94 &util::envvar_append("PATH",$ooffice_dir_guess);
95 }
96 }
97
98 my $cmd = "soffice -headless 2>&1";
99
100 my $status = system($cmd);
101 if ($status != 0) {
102 print STDERR "Failed to run: $cmd\n";
103 print STDERR "$!\n";
104 $openoffice_conversion_available = 0;
105 $no_openoffice_conversion_reason = "openofficenotinstalled";
106 }
107 }
108 }
109
110 if ($openoffice_conversion_available) {
111 push(@$arguments,@$opt_arguments);
112 }
113
114 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
115 push(@{$hashArgOptLists->{"OptList"}},$options);
116
117 my $bmc_self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists,1);
118 my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
119
120 my $self = BasePlugin::merge_inheritance($bmc_self,$cbf_self);
121
122 if ($openoffice_conversion_available) {
123 if ($self->{'openoffice_scripting'}) {
124 my $oo_port = $self->{'openoffice_port'};
125 my $launch_cmd = "soffice";
126 $launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
127 $launch_cmd .= " -headless";
128
129 $self->{'openoffice_launch_cmd'} = $launch_cmd;
130 }
131 }
132 else {
133 $self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
134
135 my $outhandle = $self->{'outhandle'};
136 &gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
137 }
138
139 $self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
140
141 return bless $self, $class;
142
143}
144
145sub init {
146 my $self = shift(@_);
147 my ($verbosity, $outhandle, $failhandle) = @_;
148
149 $self->ConvertBinaryFile::init($verbosity,$outhandle,$failhandle);
150
151 $self->{'ootmp_file_paths'} = ();
152}
153
154sub deinit {
155 my $self = shift(@_);
156
157 $self->ConvertBinaryFile::deinit(@_);
158
159 $self->clean_up_temporary_files();
160}
161
162
163
164
165sub convert {
166 my $self = shift(@_);
167 my $source_file_path = shift(@_);
168 my $target_file_type = shift(@_);
169 my $convert_options = shift(@_) || "";
170 my $convert_id = shift(@_) || "";
171 my $cache_mode = shift(@_) || "";
172
173 my $outhandle = $self->{'outhandle'};
174 my $verbosity = $self->{'verbosity'};
175
176 my $source_file_no_path = &File::Basename::basename($source_file_path);
177
178 # Determine the full name and path of the output file
179 my $target_file_path;
180 if ($self->{'enable_cache'}) {
181 my $cached_image_dir = $self->{'cached_dir'};
182 my $image_root = $self->{'cached_file_root'};
183 $image_root .= "_$convert_id" if ($convert_id ne "");
184 my $image_file = "$image_root.$target_file_type";
185 $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
186 }
187 else {
188 $target_file_path = &util::get_tmp_filename($target_file_type);
189 push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
190 }
191
192 # Generate and run the convert command
193 my $convert_command = "oo2html $convert_options \"$source_file_path\" \"$target_file_path\"";
194
195 my $print_info = { 'message_prefix' => $convert_id,
196 'message' => "Converting $source_file_no_path to: $convert_id $target_file_type" };
197 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
198
199 my ($regenerated,$result,$had_error)
200 = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
201
202 return ($result,$target_file_path);
203}
204
205sub convert_without_result {
206 my $self = shift(@_);
207
208 my $source_file_path = shift(@_);
209 my $target_file_type = shift(@_);
210 my $convert_options = shift(@_) || "";
211 my $convert_id = shift(@_) || "";
212
213 return $self->convert($source_file_path,$target_file_type,
214 $convert_options,$convert_id,"without_result");
215}
216
217
218sub tmp_area_convert_fileXX {
219 my $self = shift (@_);
220 my ($output_ext, $input_filename, $textref) = @_;
221
222 my $outhandle = $self->{'outhandle'};
223 my $convert_to = $self->{'convert_to'};
224 my $failhandle = $self->{'failhandle'};
225 my $convert_to_ext = $self->{'convert_to_ext'};
226
227 # derive tmp filename from input filename
228 my ($tailname, $dirname, $suffix)
229 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
230}
231
232
233sub clean_up_temporary_files {
234 my $self = shift(@_);
235
236 foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
237 if (-e $ootmp_file_path) {
238 &util::rm($ootmp_file_path);
239 }
240 }
241
242 $self->{'ootmp_file_paths'} = ();
243}
244
245
246
2471;
Note: See TracBrowser for help on using the repository browser.