root/main/trunk/greenstone2/perllib/plugins/AutoLoadConverters.pm @ 23752

Revision 23752, 4.8 KB (checked in by davidb, 9 years ago)

Missing @_ parameter needs to be passed on from AutoLoadConverter? methods to 'reflection' based methods called through 'eval' statements. This issue was noticed when enable-caching was used, as the appropriate part of the plugin did not have a valid 'base dir' for the cache directory.

Line 
1package AutoLoadConverters;
2
3use strict;
4no strict 'refs';
5no strict 'subs';
6
7use PrintInfo;
8
9use gsprintf 'gsprintf';
10
11sub BEGIN {
12    @AutoLoadConverters::ISA = ('PrintInfo');
13}
14
15# the dynamic conversion args will get added here
16my $arguments = [];
17
18my $options = { 'name'     => "AutoLoadConverters",
19        'desc'     => "{AutoLoadConverters.desc}",
20        'abstract' => "yes",
21        'inherits' => "yes",
22        'args'     => $arguments };
23
24sub dynamically_load_converter
25{
26    my ($autoPackage, $calling_class) = @_;
27
28    my $settings = {};
29   
30    my ($autoName)  = ($autoPackage =~ m/(^.*)Converter$/);
31    my $autoVar = lc($autoName);
32    $settings->{'converter_name'} = $autoName;
33    $settings->{'converter_var'} = $autoVar;
34   
35    eval("require $autoPackage");
36    if ($@) {
37    # Useful debugging statement if there is a syntax error in the plugin
38    #print STDERR "$@\n";
39    $settings->{'converter_installed'} = 0;
40    $settings->{'conversion_available'} = 0;
41    }
42    else {
43    # found the converter
44    $settings->{'conversion_installed'} = 1;
45    print STDERR "AutoLoadConverters: $autoName Extension to Greenstone detected for $calling_class\n";
46    # but can it run???
47    if (eval "\$${autoPackage}::${autoVar}_conversion_available") {
48        $settings->{'conversion_available'} = 1;
49        push(@AutoLoadConverters::ISA, $autoPackage);
50
51    } else {
52        $settings->{'conversion_available'} = 0;
53        print STDERR "... but it appears to be broken\n";
54        &gsprintf(STDERR, "AutoLoadConverters: {AutoloadConverter.noconversionavailable}");
55        my $dictentry_reason
56        = eval "$autoPackage\.\$${autoPackage}::no_${autoVar}_conversion_reason";
57
58        &gsprintf(STDERR, " ({$dictentry_reason})\n");
59
60    }
61    }
62
63    if ($settings->{'conversion_available'}) {
64
65    my $opt_conversion_args =
66        [ { 'name' => "$autoVar\_conversion",
67        'desc' => "{$autoPackage.$autoVar\_conversion}",
68        'type' => "flag",
69        'reqd' => "no" } ];
70
71    $settings->{'converter_arguments'} = $opt_conversion_args;
72
73    }
74    return $settings;
75
76
77}
78
79####
80# This plugin takes an extra initial parameter in its constructor (compared
81#   with the norm).  The extra parameter is an array of converters
82# it should  try to dynamically load
83#####
84
85sub new {
86
87    # Start the AutoExtractMetadata Constructor
88    my $class = shift (@_);
89    my ($pluginlist,$inputargs,$hashArgOptLists,$autoPackages, $auxiliary) = @_;
90   
91    push(@$pluginlist, $class);
92    my $classPluginName = $pluginlist->[0];
93
94    push(@{$hashArgOptLists->{"OptList"}},$options);
95
96    # this static list is just for pluginfo on AutoLoadConverters - we will try to explicitly load all packages
97    $autoPackages = ["OpenOfficeConverter", "PDFBoxConverter"] unless defined $autoPackages;
98
99    my @self_array = ();
100    my $temporary_self = {};
101    $temporary_self->{'converter_list'} = [];
102    push (@self_array, $temporary_self);
103    foreach my $packageName (@$autoPackages) {
104    my $package_settings = &dynamically_load_converter($packageName, $classPluginName);
105    my $available_var = $package_settings->{'converter_var'}."_available";
106    if ($package_settings->{'conversion_available'}) {
107        push(@$arguments, @{$package_settings->{'converter_arguments'}});
108        my $package_self = eval "new $packageName(\$pluginlist, \$inputargs, \$hashArgOptLists,1);";
109        push (@self_array, $package_self);
110        $temporary_self->{$available_var} = 1;
111        push(@{$temporary_self->{'converter_list'}}, $packageName);
112    } else {
113        $temporary_self->{$available_var} = 0;
114    }
115    }
116    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
117    my $self;
118    my $pi_self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
119    push (@self_array, $pi_self);
120    $self = BasePlugin::merge_inheritance(@self_array);
121
122    return bless $self, $class;
123}
124
125sub init {
126    my $self = shift (@_);
127
128    foreach my $converter (@{$self->{'converter_list'}}) {
129    eval "\$self->${converter}::init(\@_);";
130    }
131
132}
133
134sub begin {
135    my $self = shift (@_);
136
137    foreach my $converter(@{$self->{'converter_list'}}) {
138    eval "\$self->${converter}::begin(\@_);";
139    }
140}
141
142sub deinit {
143    my $self = shift (@_);
144
145    foreach my $converter (@{$self->{'converter_list'}}) {
146    eval "\$self->${converter}::deinit(\@_);";
147    }
148
149}
150
151sub tmp_area_convert_file {
152    my $self = shift (@_);
153    my ($output_ext, $input_filename, $textref) = @_;
154
155    foreach my $converter(@{$self->{'converter_list'}}) {
156    my ($var) = ($converter =~ m/(^.*)Converter$/);
157    $var = lc($var);
158    if ($self->{"${var}_conversion"}) {
159        my ($result, $result_str, $new_filename)
160        = eval "\$self->${converter}::convert(\$input_filename, \$output_ext);";
161        if ($result != 0) {
162        return $new_filename;
163        }
164        my $outhandle=$self->{'outhandle'};
165        print $outhandle "$converter Conversion error\n";
166        print $outhandle $result_str;
167        return "";
168    }
169    }
170   
171    # if got here, no converter was specified in plugin args
172    return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
173   
174}
Note: See TracBrowser for help on using the browser.