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

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

some moving around and tidying up of code

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