root/main/trunk/greenstone2/perllib/plugins/AutoloadConverterScripting.pm @ 22705

Revision 22705, 5.4 KB (checked in by davidb, 9 years ago)

User of AutoloadConverterScripting? expanded to encompass PowerPoint? and Excel. No longer need OOConvertBinaryFile and PBConvertBinaryFile

Line 
1package AutoloadConverterScripting;
2
3use strict;
4no strict 'refs';
5no strict 'subs';
6
7use ConvertBinaryFile;
8
9use gsprintf 'gsprintf';
10
11# This plugin inherits from ConvertBinaryFile as a base line (so by default
12# behaviour is no different to ConvertBinaryFile).  If it can find the
13# plugin named in '::autoConverterScripting' then it loads this in as well
14# ,setting variables visible to the outside world that indicate whether:
15#    1. it found the named plugin, and
16#    2. if the named plugin itself could find the necessary programs it
17#       needs to run
18
19
20sub _dynamicISA
21{
22    my ($autoPackage) = @_;
23
24    my ($autoName)  = ($autoPackage =~ m/(^.*)Converter$/);
25    my $autoVar     = lc($autoName);
26
27    my $self = {};
28
29    $self->{'_autoPackage'} = $autoPackage;
30    $self->{'_autoName'}    = $autoName;
31    $self->{'_autoVar'}     = $autoVar;
32
33    # Always want this
34    @AutoloadConverterScripting::ISA = ('ConvertBinaryFile' );
35
36    # Now see if requested Converter plugin is around
37    eval("require $autoPackage");
38   
39    if ($@) {
40    # Useful debugging statement if there is a syntax error in the plugin
41    print STDERR "$@\n";
42    $self->{'scripting_ext_installed'} = 0;
43    $self->{'scripting_ext_working'} = 0;
44    }
45    else {
46    # Successfully found
47    $self->{'scripting_ext_installed'} = 1;
48   
49    # now check whether it can run soffice
50    if (eval "\$${autoPackage}::${autoVar}_conversion_available") {
51        $self->{'scripting_ext_working'} = 1;
52       
53    } else {
54        $self->{'scripting_ext_working'} = 0;
55    }
56    }
57
58    if ($self->{'scripting_ext_working'}) {
59    push(@AutoloadConverterScripting::ISA, $autoPackage);
60    }
61
62    return $self;
63}
64
65
66my $arguments = [];
67
68my $options = { 'name'     => "ConverterScripting",
69        'desc'     => "{ConverterScripting.desc}",
70        'abstract' => "yes",
71        'inherits' => "yes",
72        'args'     => $arguments };
73
74####
75# This plugin takes an extra initial parameter in its constructor (compared
76#   with the norm).  The extra parameter specifies which plugin it should
77#   try to dynamically load
78#####
79
80sub new {
81    my ($class) = shift (@_);
82    my ($autoPackage, $pluginlist,$inputargs,$hashArgOptLists) = @_;
83    push(@$pluginlist, $class);
84
85    my $dynamic_self = _dynamicISA($autoPackage);
86    my $autoName = $dynamic_self->{'_autoName'};
87    my $autoVar  = $dynamic_self->{'_autoVar'};
88
89    if ($dynamic_self->{'scripting_ext_installed'}) {
90    my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(0);
91
92    print STDERR "$class: $autoName Extension to Greenstone detected for $cpackage\n";
93    if ($dynamic_self->{'scripting_ext_working'}) {
94        # print STDERR "... and it appears to be working\n";
95    } else {
96        print STDERR "... but it appears to be broken\n";
97        &gsprintf(STDERR, "AutoloadConverter: {AutoloadConverter.noconversionavailable}");
98
99        my $dictentry_reason
100        = eval "$autoPackage\.\$${autoPackage}::no_${autoVar}_conversion_reason";
101
102        &gsprintf(STDERR, " ({$dictentry_reason})\n");
103    }
104    }
105
106    if ($dynamic_self->{'scripting_ext_working'}) {
107
108    my $opt_scripting_args =
109        [ { 'name' => "$autoVar\_scripting",
110        'desc' => "{$autoPackage.$autoVar\_scripting}",
111        'type' => "flag",
112        'reqd' => "no" } ];
113
114    push(@$arguments,@$opt_scripting_args);
115    }
116
117    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
118    push(@{$hashArgOptLists->{"OptList"}},$options);
119
120    my $self;
121    if ($dynamic_self->{'scripting_ext_working'}) {
122   
123    my $autoc_self = eval "new $autoPackage(\$pluginlist, \$inputargs, \$hashArgOptLists,1);";
124    my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
125    $self = BasePlugin::merge_inheritance($dynamic_self,$autoc_self, $cbf_self);
126
127    $self->{"{$autoVar}_available"} = 1;
128    }
129    else {
130    $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
131    $self->{"{$autoVar}_available"} = 0;
132   }
133
134    return bless $self, $class;
135}
136
137sub init {
138    my $self = shift (@_);
139    my ($verbosity, $outhandle, $failhandle) = @_;
140    my $autoPackage = $self->{'_autoPackage'};
141
142    $self->SUPER::init($verbosity,$outhandle,$failhandle);
143    if ($self->{'scripting_ext_working'}) {
144    eval "\$self->${autoPackage}::init();";
145    }
146}
147
148sub begin {
149    my $self = shift (@_);
150    my $autoPackage = $self->{'_autoPackage'};
151
152    if ($self->{'scripting_ext_working'}) {
153    eval "\$self->${autoPackage}::begin(\@_);";
154    }
155    $self->SUPER::begin(@_);
156}
157
158sub deinit {
159    # called only once, after all plugin passes have been done
160    my ($self) = @_;
161    my $autoPackage = $self->{'_autoPackage'};
162
163    if ($self->{'scripting_ext_working'}) {
164    eval "\$self->${autoPackage}::deinit()";
165    }
166    $self->SUPER::deinit();
167}
168
169# if the dynamically loaded Converter plugins is working, and scripting is on,, then call its convert
170# method, otherwise fall back to ConvertBinaryFile method.
171
172sub tmp_area_convert_file {
173    my $self = shift (@_);
174    my ($output_ext, $input_filename, $textref) = @_;
175    my $autoPackage = $self->{'_autoPackage'};
176    my $autoVar = $self->{'_autoVar'};
177    my $autoName = $self->{'_autoName'};
178
179    if ($self->{'scripting_ext_working'} && $self->{"${autoVar}_scripting"}) {
180    my ($result, $result_str, $new_filename)
181        = eval "\$self->${autoPackage}::convert(\$input_filename, \$output_ext);";
182    if ($result != 0) {
183        return $new_filename;
184    }
185    my $outhandle=$self->{'outhandle'};
186    print $outhandle "$autoName Conversion error\n";
187    print $outhandle $result_str;
188    return "";
189    }
190    else {
191    return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
192    }
193}
Note: See TracBrowser for help on using the browser.