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

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

openoffice_scripting arg has been moved to top level plugin. It should control whether we use this converter plugin or not. Once we ARE using the plugin, then this cinversion should always be on

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