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

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

Switch to running java -jar command directly (rather than going through oo2html). Has the advantage of better quoting around filenames (compared to oo2html.bat which was known to be deficient).

  • Property svn:executable set to *
File size: 7.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
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 = shift(@_);
156 my $target_file_type = shift(@_);
157 my $convert_options = shift(@_) || "";
158 my $convert_id = shift(@_) || "";
159 my $cache_mode = shift(@_) || "";
160
161 return (0,undef,undef) unless $openoffice_conversion_available;
162 # check the filename
163 return (0,undef,undef) if ( !-f $source_file_full_path);
164
165 my $outhandle = $self->{'outhandle'};
166 my $verbosity = $self->{'verbosity'};
167
168 my $source_file_no_path = &File::Basename::basename($source_file_full_path);
169 # Determine the full name and path of the output file
170 my $target_file_path;
171 if ($self->{'enable_cache'}) {
172 $self->init_cache_for_file($source_file_full_path);
173 my $cache_dir = $self->{'cached_dir'};
174 my $file_root = $self->{'cached_file_root'};
175 $file_root .= "_$convert_id" if ($convert_id ne "");
176 my $target_file = "$file_root.$target_file_type";
177 $target_file_path = &util::filename_cat($cache_dir,$target_file);
178 }
179 else {
180 # this is in gsdl/tmp. get a tmp filename in collection instead???
181 $target_file_path = &util::get_tmp_filename($target_file_type);
182 push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
183 }
184
185 # Generate and run the convert command
186 my $jodjar;
187
188 my $gextoo_home = $ENV{'GEXT_OPENOFFICE'};
189 my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverter.jar");
190
191 my $convert_cmd = "java -jar \"$jodjar\"";
192 $convert_cmd .= " \"$source_file_full_path\" \"$target_file_path\"";
193
194 if ($verbosity>2) {
195 print $outhandle "Convert command: $convert_cmd\n";
196 }
197
198 my $print_info = { 'message_prefix' => "OpenOffice Conversion",
199 'message' => "Converting $source_file_no_path to: $target_file_type" };
200 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
201
202 my ($regenerated,$result,$had_error)
203 = $self->autorun_general_cmd($convert_cmd,$source_file_full_path, $target_file_path,$print_info);
204 if ($had_error) {
205 return (0, $result,$target_file_path);
206 }
207 return (1, $result,$target_file_path);
208}
209
210
211sub convert_without_result {
212 my $self = shift(@_);
213
214 my $source_file_path = shift(@_);
215 my $target_file_type = shift(@_);
216 my $convert_options = shift(@_) || "";
217 my $convert_id = shift(@_) || "";
218
219 return $self->convert($source_file_path,$target_file_type,
220 $convert_options,$convert_id,"without_result");
221}
222
223
224sub tmp_area_convert_fileXX {
225 my $self = shift (@_);
226 my ($output_ext, $input_filename, $textref) = @_;
227
228 my $outhandle = $self->{'outhandle'};
229 my $convert_to = $self->{'convert_to'};
230 my $failhandle = $self->{'failhandle'};
231 my $convert_to_ext = $self->{'convert_to_ext'};
232
233 # derive tmp filename from input filename
234 my ($tailname, $dirname, $suffix)
235 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
236}
237
238
239sub clean_up_temporary_files {
240 my $self = shift(@_);
241
242 foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
243 if (-e $ootmp_file_path) {
244 &util::rm($ootmp_file_path);
245 }
246 }
247
248 $self->{'ootmp_file_paths'} = ();
249}
250
251
252
2531;
Note: See TracBrowser for help on using the repository browser.