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

Revision 22702, 5.5 KB (checked in by davidb, 9 years ago)

Introduction of new plugin AutoloadConverterScripting? to replace duplicated code that turns up in plugins like 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 with
76#   the norm).  The extra parameter specifies which plugin it should try to
77#   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#    print STDERR "**** autoload NEW autoVar = $dynamic_self->{'_autoVar'}\n";
90#    print STDERR "**** working ", $dynamic_self->{'scripting_ext_working} , "\n";
91
92    if ($dynamic_self->{'scripting_ext_installed'}) {
93    print STDERR "$class: $autoName Extension to Greenstone detected\n";
94    if ($dynamic_self->{'scripting_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, "AutoloadConverter: {AutoloadConverter.noconversionavailable}");
99
100        my $dictentry_reason
101        = eval "$autoPackage\.\$${autoPackage}::no_${autoVar}_conversion_reason";
102
103        &gsprintf(STDERR, " ({$dictentry_reason})\n");
104    }
105    }
106
107    if ($dynamic_self->{'scripting_ext_working'}) {
108
109    my $opt_scripting_args =
110        [ { 'name' => "$autoVar\_scripting",
111        'desc' => "{$autoPackage.$autoVar\_scripting}",
112        'type' => "flag",
113        'reqd' => "no" } ];
114
115    push(@$arguments,@$opt_scripting_args);
116    }
117
118    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
119    push(@{$hashArgOptLists->{"OptList"}},$options);
120
121    my $self;
122    if ($dynamic_self->{'scripting_ext_working'}) {
123   
124    my $autoc_self = eval "new $autoPackage(\$pluginlist, \$inputargs, \$hashArgOptLists,1);";
125    my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
126    $self = BasePlugin::merge_inheritance($dynamic_self,$autoc_self, $cbf_self);
127
128    $self->{"{$autoVar}_available"} = 1;
129    }
130    else {
131    $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
132    $self->{"{$autoVar}_available"} = 0;
133   }
134
135    return bless $self, $class;
136}
137
138sub init {
139    my $self = shift (@_);
140    my ($verbosity, $outhandle, $failhandle) = @_;
141    my $autoPackage = $self->{'_autoPackage'};
142
143    $self->SUPER::init($verbosity,$outhandle,$failhandle);
144    if ($self->{'scripting_ext_working'}) {
145    eval "\$self->${autoPackage}::init();";
146    }
147}
148
149sub begin {
150    my $self = shift (@_);
151    my $autoPackage = $self->{'_autoPackage'};
152
153    if ($self->{'scripting_ext_working'}) {
154    eval "\$self->${autoPackage}::begin(\@_);";
155    }
156    $self->SUPER::begin(@_);
157}
158
159sub deinit {
160    # called only once, after all plugin passes have been done
161    my ($self) = @_;
162    my $autoPackage = $self->{'_autoPackage'};
163
164    if ($self->{'scripting_ext_working'}) {
165    eval "\$self->${autoPackage}::deinit()";
166    }
167    $self->SUPER::deinit();
168}
169
170# if the dynamically loaded Converter plugins is working, and scripting is on,, then call its convert
171# method, otherwise fall back to ConvertBinaryFile method.
172
173sub tmp_area_convert_file {
174    my $self = shift (@_);
175    my ($output_ext, $input_filename, $textref) = @_;
176    my $autoPackage = $self->{'_autoPackage'};
177    my $autoVar = $self->{'_autoVar'};
178    my $autoName = $self->{'_autoName'};
179
180    if ($self->{'scripting_ext_working'} && $self->{"${autoVar}_scripting"}) {
181    my ($result, $result_str, $new_filename)
182        = eval "\$self->${autoPackage}::convert(\$input_filename, \$output_ext);";
183    if ($result != 0) {
184        return $new_filename;
185    }
186    my $outhandle=$self->{'outhandle'};
187    print $outhandle "$autoName Conversion error\n";
188    print $outhandle $result_str;
189    return "";
190    }
191    else {
192    return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
193    }
194}
Note: See TracBrowser for help on using the browser.