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

Last change on this file since 22704 was 22704, checked in by davidb, 14 years ago

Tidied up printing of command run to conditionally displayed based on verbosity level

  • Property svn:executable set to *
File size: 9.0 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
86my $arguments = [
87 { 'name' => "openoffice_port",
88 'desc' => "{OpenOfficeConverter.openoffice_port}",
89 'type' => "int",
90 'deft' => "8100",
91 'range' => "81,",
92 'reqd' => "no" },
93 ];
94
95
96my $options = { 'name' => "OpenOfficeConverter",
97 'desc' => "{OpenOfficeConverter.desc}",
98 'abstract' => "yes",
99 'inherits' => "yes",
100 'args' => $arguments };
101
102sub new {
103 my ($class) = shift (@_);
104 my ($pluginlist,$inputargs,$hashArgOptLists,$auxilary) = @_;
105 push(@$pluginlist, $class);
106
107 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
108 push(@{$hashArgOptLists->{"OptList"}},$options);
109
110 my $self = new BaseMediaConverter($pluginlist, $inputargs,
111 $hashArgOptLists, $auxilary);
112
113 if ($self->{'info_only'}) {
114 # don't worry about any options etc
115 return bless $self, $class;
116 }
117 if (!$openoffice_conversion_available) {
118 $self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
119
120 my $outhandle = $self->{'outhandle'};
121 &gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
122 }
123
124 $self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
125
126 return bless $self, $class;
127
128}
129
130sub init {
131 my $self = shift(@_);
132 my ($verbosity, $outhandle, $failhandle) = @_;
133
134 if ($openoffice_conversion_available) {
135 my $oo_port = $self->{'openoffice_port'};
136
137 my $launch_cmd = "soffice";
138 $launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
139 $launch_cmd .= " -headless";
140 $self->{'openoffice_launch_cmd'} = $launch_cmd;
141 }
142
143 $self->{'ootmp_file_paths'} = ();
144}
145
146sub deinit {
147 my $self = shift(@_);
148
149 $self->clean_up_temporary_files();
150}
151
152
153sub convert {
154 my $self = shift(@_);
155 my ($source_file_full_path, $target_file_type) = @_;
156
157 return 0 unless $openoffice_conversion_available;
158 # check the filename
159 return 0 if ( !-f $source_file_full_path);
160
161 my $outhandle = $self->{'outhandle'};
162 my $verbosity = $self->{'verbosity'};
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 if ($verbosity>2) {
184 print $outhandle "Convert command: $convert_command\n";
185 }
186
187 my $print_info = { 'message_prefix' => "OpenOffice Conversion",
188 'message' => "Converting $source_file_no_path to: $target_file_type" };
189 #$print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
190
191 my ($regenerated,$result,$had_error)
192 = $self->autorun_general_cmd($convert_command,$source_file_full_path, $target_file_path,$print_info);
193 if ($had_error) {
194 return (0, $result,$target_file_path);
195 }
196 return (1, $result,$target_file_path);
197}
198
199
200
201sub convert_orig {
202 my $self = shift(@_);
203 my $source_file_path = shift(@_);
204 my $target_file_type = shift(@_);
205 my $convert_options = shift(@_) || "";
206 my $convert_id = shift(@_) || "";
207 my $cache_mode = shift(@_) || "";
208
209 my $outhandle = $self->{'outhandle'};
210 my $verbosity = $self->{'verbosity'};
211
212 my $source_file_no_path = &File::Basename::basename($source_file_path);
213
214 # Determine the full name and path of the output file
215 my $target_file_path;
216 if ($self->{'enable_cache'}) {
217 my $cached_image_dir = $self->{'cached_dir'};
218 my $image_root = $self->{'cached_file_root'};
219 $image_root .= "_$convert_id" if ($convert_id ne "");
220 my $image_file = "$image_root.$target_file_type";
221 $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
222 }
223 else {
224 $target_file_path = &util::get_tmp_filename($target_file_type);
225 push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
226 }
227
228 # Generate and run the convert command
229 my $convert_command = "oo2html $convert_options \"$source_file_path\" \"$target_file_path\"";
230
231 my $print_info = { 'message_prefix' => $convert_id,
232 'message' => "Converting $source_file_no_path to: $convert_id $target_file_type" };
233 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
234
235 my ($regenerated,$result,$had_error)
236 = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
237
238 return ($result,$target_file_path);
239}
240
241sub convert_without_result {
242 my $self = shift(@_);
243
244 my $source_file_path = shift(@_);
245 my $target_file_type = shift(@_);
246 my $convert_options = shift(@_) || "";
247 my $convert_id = shift(@_) || "";
248
249 return $self->convert($source_file_path,$target_file_type,
250 $convert_options,$convert_id,"without_result");
251}
252
253
254sub tmp_area_convert_fileXX {
255 my $self = shift (@_);
256 my ($output_ext, $input_filename, $textref) = @_;
257
258 my $outhandle = $self->{'outhandle'};
259 my $convert_to = $self->{'convert_to'};
260 my $failhandle = $self->{'failhandle'};
261 my $convert_to_ext = $self->{'convert_to_ext'};
262
263 # derive tmp filename from input filename
264 my ($tailname, $dirname, $suffix)
265 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
266}
267
268
269sub clean_up_temporary_files {
270 my $self = shift(@_);
271
272 foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
273 if (-e $ootmp_file_path) {
274 &util::rm($ootmp_file_path);
275 }
276 }
277
278 $self->{'ootmp_file_paths'} = ();
279}
280
281
282
2831;
Note: See TracBrowser for help on using the repository browser.