source: main/trunk/greenstone2/perllib/plugins/ExcelPlugin.pm@ 22597

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

code tidy up. rearranged how convertbinaryfile plugins set up their secondary plugins - now only set up the options for the one they are using. all subclass specific code moved out of convertbinaryfile.new into the appropriate plugin file.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1###########################################################################
2#
3# ExcelPlugin.pm -- plugin for importing Microsoft Excel files.
4# (currently only versions 95 and 97)
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) 2002 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###########################################################################
27
28package ExcelPlugin;
29
30use ConvertBinaryFile;
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33no strict 'subs';
34use gsprintf 'gsprintf';
35
36# @ISA dynamically configured to be either OpenOfficeConverter or ConvertBinaryFile
37
38# do not initialise these variables
39my $openoffice_ext_installed;
40my $openoffice_ext_working;
41sub BEGIN {
42 eval("require OpenOfficeConverter");
43 if ($@) {
44 # Useful debugging statement if there is a syntax error in OpenOfficeConverter
45 #print STDERR "$@\n";
46 @ExcelPlugin::ISA = ('ConvertBinaryFile');
47 $openoffice_ext_installed = 0;
48 $openoffice_ext_working = 0;
49 }
50 else {
51 # Successfully found
52 $openoffice_ext_installed = 1;
53 # now check whether it can run soffice
54 if ($OpenOfficeConverter::openoffice_conversion_available) {
55 @ExcelPlugin::ISA = ('OpenOfficeConverter');
56 $openoffice_ext_working = 1;
57
58 } else {
59 @ExcelPlugin::ISA = ('ConvertBinaryFile');
60 $openoffice_ext_working = 0;
61 }
62 }
63}
64
65my $arguments =
66 [ { 'name' => "process_exp",
67 'desc' => "{BasePlugin.process_exp}",
68 'type' => "regexp",
69 'reqd' => "no",
70 'deft' => &get_default_process_exp() }
71 ];
72
73my $opt_openoffice_args =
74 [ { 'name' => "openoffice_scripting",
75 'desc' => "{OpenOfficeConverter.openoffice_scripting}",
76 'type' => "flag",
77 'reqd' => "no" } ];
78
79
80my $options = { 'name' => "ExcelPlugin",
81 'desc' => "{ExcelPlugin.desc}",
82 'abstract' => "no",
83 'inherits' => "yes",
84 'srcreplaceable' => "yes", # Source docs in Excel format can be replaced with GS-generated html
85 'args' => $arguments };
86
87sub new {
88 my ($class) = shift (@_);
89 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
90 push(@$pluginlist, $class);
91
92 if ($openoffice_ext_installed) {
93 print STDERR "ExcelPlugin: OpenOffice Extension to Greenstone detected\n";
94 if ($openoffice_ext_working) {
95 print STDERR "... and it appears to be working\n";
96 } else {
97 print STDERR "... but it appears to be broken\n";
98 &gsprintf(STDERR, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$OpenOfficeConverter::no_openoffice_conversion_reason})\n");
99 }
100 }
101
102 if ($openoffice_ext_working) {
103 push(@$arguments,@$opt_openoffice_args);
104 }
105
106 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
107 push(@{$hashArgOptLists->{"OptList"}},$options);
108
109 my $self = {};
110
111 if ($openoffice_ext_working) {
112 $self = new OpenOfficeConverter($pluginlist, $inputargs, $hashArgOptLists);
113 }
114 else {
115 $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
116 }
117
118 if ($self->{'info_only'}) {
119 # don't worry about any options etc
120 return bless $self, $class;
121 }
122
123 $self->{'filename_extension'} = "xls";
124 $self->{'file_type'} = "Excel";
125
126 my $outhandle = $self->{'outhandle'};
127
128 # check convert_to
129 if ($self->{'convert_to'} eq "auto") {
130 $self->{'convert_to'} = "html";
131 }
132
133 $self->{'convert_options'} = "-openoffice_scripting" if $self->{'openoffice_scripting'};
134
135 # set convert_to_plugin and convert_to_ext
136 $self->ConvertBinaryFile::set_standard_convert_settings();
137 print STDERR "final convert-to $self->{'convert_to'}\n";
138
139 my $secondary_plugin_name = $self->{'convert_to_plugin'};
140 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
141
142 if (!defined $secondary_plugin_options->{$secondary_plugin_name}) {
143 $secondary_plugin_options->{$secondary_plugin_name} = [];
144 }
145 my $specific_options = $secondary_plugin_options->{$secondary_plugin_name};
146
147 push(@$specific_options,"-extract_language") if $self->{'extract_language'};
148 push(@$specific_options, "-file_rename_method", "none");
149
150 if ($secondary_plugin_name eq "HTMLPlugin") {
151 push(@$specific_options, "-processing_tmp_files");
152 }
153
154 $self = bless $self, $class;
155
156 $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
157 return bless $self;
158}
159
160sub convert_post_process_old
161{
162 my $self = shift (@_);
163 my ($conv_filename) = @_;
164
165 my $outhandle=$self->{'outhandle'};
166
167 my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
168
169 # read in file ($text will be in utf8)
170 my $text = "";
171 $self->read_file ($conv_filename, $encoding, $language, \$text);
172
173 # turn any high bytes that aren't valid utf-8 into utf-8.
174 #unicode::ensure_utf8(\$text);
175
176 # Write it out again!
177 #$self->utf8_write_file (\$text, $conv_filename);
178}
179
180sub get_default_process_exp {
181 my $self = shift (@_);
182 return q^(?i)\.xls$^;
183}
184
185
1861;
Note: See TracBrowser for help on using the repository browser.