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

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

convert the files in the collection's tmp folder, not the main gsdl one. and use a timestamped subfolder as may end up with files having the same names

  • Property svn:executable set to *
File size: 8.1 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 my $cmd = "soffice -headless 2>&1";
66
67
68 if ($ENV{'GSDLOS'} =~ m/^windows$/) {
69 my $ooffice_dir_guess =
70 &util::filename_cat($ENV{'ProgramFiles'},"OpenOffice.org 3",
71 "program");
72 if (-d $ooffice_dir_guess) {
73 &util::envvar_append("PATH",$ooffice_dir_guess);
74 }
75 $cmd .= " >nul";
76 }
77 else {
78 # Windows seems to launch OpenOffice as a service (i.e.
79 # automatically puts it in the background).
80 # For Unix putting it in the background needs to be done
81 # explicitly
82
83 $cmd .= " >/dev/null &";
84 }
85
86 my $status = system($cmd);
87 if ($status != 0) {
88 print STDERR "Failed to run: $cmd\n";
89 print STDERR "$!\n";
90 $openoffice_conversion_available = 0;
91 $no_openoffice_conversion_reason = "openofficenotinstalled";
92 }
93 }
94 }
95}
96
97my $arguments = [
98 { 'name' => "openoffice_port",
99 'desc' => "{OpenOfficeConverter.openoffice_port}",
100 'type' => "int",
101 'deft' => "8100",
102 'range' => "81,",
103 'reqd' => "no" },
104 ];
105
106
107my $options = { 'name' => "OpenOfficeConverter",
108 'desc' => "{OpenOfficeConverter.desc}",
109 'abstract' => "yes",
110 'inherits' => "yes",
111 'args' => $arguments };
112
113sub new {
114 my ($class) = shift (@_);
115 my ($pluginlist,$inputargs,$hashArgOptLists,$auxilary) = @_;
116 push(@$pluginlist, $class);
117
118 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
119 push(@{$hashArgOptLists->{"OptList"}},$options);
120
121 my $self = new BaseMediaConverter($pluginlist, $inputargs,
122 $hashArgOptLists, $auxilary);
123
124 if ($self->{'info_only'}) {
125 # don't worry about any options etc
126 return bless $self, $class;
127 }
128 if (!$openoffice_conversion_available) {
129 $self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
130
131 my $outhandle = $self->{'outhandle'};
132 &gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
133 }
134
135 $self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
136
137 return bless $self, $class;
138
139}
140
141sub init {
142 my $self = shift(@_);
143 my ($verbosity, $outhandle, $failhandle) = @_;
144
145 if ($openoffice_conversion_available) {
146 my $oo_port = $self->{'openoffice_port'};
147
148 my $launch_cmd = "soffice";
149 $launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
150 $launch_cmd .= " -headless";
151 $self->{'openoffice_launch_cmd'} = $launch_cmd;
152 }
153
154 $self->{'ootmp_file_paths'} = ();
155}
156
157sub deinit {
158 my $self = shift(@_);
159
160 $self->clean_up_temporary_files();
161}
162
163
164sub convert {
165 my $self = shift(@_);
166 my $source_file_full_path = shift(@_);
167 my $target_file_type = shift(@_);
168 my $convert_options = shift(@_) || "";
169 my $convert_id = shift(@_) || "";
170 my $cache_mode = shift(@_) || "";
171
172 return (0,undef,undef) unless $openoffice_conversion_available;
173 # check the filename
174 return (0,undef,undef) if ( !-f $source_file_full_path);
175
176 my $outhandle = $self->{'outhandle'};
177 my $verbosity = $self->{'verbosity'};
178
179 my $source_file_no_path = &File::Basename::basename($source_file_full_path);
180 # Determine the full name and path of the output file
181 my $target_file_path;
182 if ($self->{'enable_cache'}) {
183 $self->init_cache_for_file($source_file_full_path);
184 my $cache_dir = $self->{'cached_dir'};
185 my $file_root = $self->{'cached_file_root'};
186 $file_root .= "_$convert_id" if ($convert_id ne "");
187 my $target_file = "$file_root.$target_file_type";
188 $target_file_path = &util::filename_cat($cache_dir,$target_file);
189 }
190 else {
191 $target_file_path = &util::get_timestamped_tmp_filename_in_collection($source_file_full_path, $target_file_type);
192 push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
193 }
194
195 # Generate and run the convert command
196
197 my $gextoo_home = $ENV{'GEXT_OPENOFFICE'};
198 my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverter.jar");
199
200 my $convert_cmd = "java -jar \"$jodjar\"";
201 $convert_cmd .= " \"$source_file_full_path\" \"$target_file_path\"";
202
203 if ($verbosity>2) {
204 print $outhandle "Convert command: $convert_cmd\n";
205 }
206
207 my $print_info = { 'message_prefix' => "OpenOffice Conversion",
208 'message' => "Converting $source_file_no_path to: $target_file_type" };
209 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
210
211 my ($regenerated,$result,$had_error)
212 = $self->autorun_general_cmd($convert_cmd,$source_file_full_path, $target_file_path,$print_info);
213 if ($had_error) {
214 return (0, $result,$target_file_path);
215 }
216 return (1, $result,$target_file_path);
217}
218
219
220sub convert_without_result {
221 my $self = shift(@_);
222
223 my $source_file_path = shift(@_);
224 my $target_file_type = shift(@_);
225 my $convert_options = shift(@_) || "";
226 my $convert_id = shift(@_) || "";
227
228 return $self->convert($source_file_path,$target_file_type,
229 $convert_options,$convert_id,"without_result");
230}
231
232
233sub tmp_area_convert_fileXX {
234 my $self = shift (@_);
235 my ($output_ext, $input_filename, $textref) = @_;
236
237 my $outhandle = $self->{'outhandle'};
238 my $convert_to = $self->{'convert_to'};
239 my $failhandle = $self->{'failhandle'};
240 my $convert_to_ext = $self->{'convert_to_ext'};
241
242 # derive tmp filename from input filename
243 my ($tailname, $dirname, $suffix)
244 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
245}
246
247
248sub clean_up_temporary_files {
249 my $self = shift(@_);
250
251 foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
252 if (-e $ootmp_file_path) {
253 &util::rm($ootmp_file_path);
254 }
255 }
256
257 $self->{'ootmp_file_paths'} = ();
258}
259
260
261
2621;
Note: See TracBrowser for help on using the repository browser.