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

Last change on this file since 22701 was 22701, checked in by davidb, 10 years ago

Minor adjustments to make PDFBoxConverter fit in with new AutoloadConverterScripting Plugin

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