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

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

Initial cut at supporting Converter plugin for OpenOffice docs

  • Property svn:executable set to *
File size: 6.5 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 BaseMediaConverter;
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33
34use gsprintf 'gsprintf';
35
36BEGIN {
37 @OpenOfficeConverter::ISA = ('BaseMediaConverter');
38}
39
40my $arguments = [ ];
41
42my $opt_arguments = [
43 { 'name' => "openoffice_scripting",
44 'desc' => "{OpenOfficeConverter.openoffice_scripting}",
45 'type' => "flag",
46 'reqd' => "no" },
47 { 'name' => "openoffice_port",
48 'desc' => "{OpenOfficeConverter.openoffice_scripting}",
49 'type' => "int",
50 'deft' => "8100",
51 'range' => "81,",
52 'reqd' => "no" },
53 ];
54
55my $options = { 'name' => "OpenOfficeConverter",
56 'desc' => "{OpenOfficeConverter.desc}",
57 'abstract' => "yes",
58 'inherits' => "yes",
59 'args' => $arguments };
60
61sub new {
62 my ($class) = shift (@_);
63 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
64 push(@$pluginlist, $class);
65
66
67 # Check that OpenOffice and jodconverter are installed and available on
68 # the path
69
70 my $openoffice_conversion_available = 1;
71 my $no_openoffice_conversion_reason = "";
72
73 if (! defined $ENV{'GEXTOPENOFFICE'}) {
74 $openoffice_conversion_available = 0;
75 $no_openoffice_conversion_reason = "gextopenofficenotinstalled";
76 }
77 else {
78 my $gextoo_home = $ENV{'GEXTOPENOFFICE'};
79 my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverter.jar");
80
81 if (!-e $jodjar) {
82 print STDERR "Failed to find $jodjar\n";
83 $openoffice_conversion_available = 0;
84 $no_openoffice_conversion_reason = "gextjodconverternotinstalled";
85 }
86 else {
87 # test to see if soffice in in path
88 my $cmd = "soffice -headless -help 2>&1";
89
90 if (system($cmd)!=0) {
91 print STDERR "Failed to run: $cmd\n";
92 print STDERR "$!\n";
93 $openoffice_conversion_available = 0;
94 $no_openoffice_conversion_reason = "openofficenotinstalled";
95 }
96 }
97 }
98
99 if ($openoffice_conversion_available) {
100 push(@$arguments,$opt_arguments);
101
102 # have necessary components => launch OpenOffice in headless mode
103 # listing to localhost port
104 }
105
106 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
107 push(@{$hashArgOptLists->{"OptList"}},$options);
108
109 my $self
110 = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
111
112 if ($openoffice_conversion_available) {
113
114 my $oo_port = $self->{'openoffice_port'};
115 my $launch_cmd = "soffice";
116 $launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
117 $launch_cmd .= " -headless";
118
119 $self->{'openoffice_launch_cmd'} = $launch_cmd;
120 }
121 else {
122 $self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
123 $self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
124
125 my $outhandle = $self->{'outhandle'};
126 &gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
127 }
128
129 return bless $self, $class;
130
131}
132
133sub init {
134 my $self = shift(@_);
135
136 $self->{'ootmp_file_paths'} = ();
137}
138
139sub deinit {
140 my $self = shift(@_);
141
142 $self->clean_up_temporary_files();
143}
144
145
146sub convert {
147 my $self = shift(@_);
148 my $source_file_path = shift(@_);
149 my $target_file_type = shift(@_);
150 my $convert_options = shift(@_) || "";
151 my $convert_id = shift(@_) || "";
152 my $cache_mode = shift(@_) || "";
153
154 my $outhandle = $self->{'outhandle'};
155 my $verbosity = $self->{'verbosity'};
156
157 my $source_file_no_path = &File::Basename::basename($source_file_path);
158
159 # Determine the full name and path of the output file
160 my $target_file_path;
161 if ($self->{'enable_cache'}) {
162 my $cached_image_dir = $self->{'cached_dir'};
163 my $image_root = $self->{'cached_file_root'};
164 $image_root .= "_$convert_id" if ($convert_id ne "");
165 my $image_file = "$image_root.$target_file_type";
166 $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
167 }
168 else {
169 $target_file_path = &util::get_tmp_filename($target_file_type);
170 push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
171 }
172
173 # Generate and run the convert command
174 my $convert_command = "oo2html $convert_options \"$source_file_path\" \"$target_file_path\"";
175
176 my $print_info = { 'message_prefix' => $convert_id,
177 'message' => "Converting $source_file_no_path to: $convert_id $target_file_type" };
178 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
179
180 my ($regenerated,$result,$had_error)
181 = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
182
183 return ($result,$target_file_path);
184}
185
186sub convert_without_result {
187 my $self = shift(@_);
188
189 my $source_file_path = shift(@_);
190 my $target_file_type = shift(@_);
191 my $convert_options = shift(@_) || "";
192 my $convert_id = shift(@_) || "";
193
194 return $self->convert($source_file_path,$target_file_type,
195 $convert_options,$convert_id,"without_result");
196}
197
198
199
200sub clean_up_temporary_files {
201 my $self = shift(@_);
202
203 foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
204 if (-e $ootmp_file_path) {
205 &util::rm($ootmp_file_path);
206 }
207 }
208
209 $self->{'ootmp_file_paths'} = ();
210}
211
212
213
2141;
Note: See TracBrowser for help on using the repository browser.