1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # PrintInfo - most base plugin
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 2008 New Zealand Digital Library Project
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 | ## Most basic plugin, just handles parsing the arguments and printing out descriptions. Used for plugins and Extractor plugins
|
---|
28 |
|
---|
29 | package PrintInfo;
|
---|
30 |
|
---|
31 | BEGIN {
|
---|
32 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
33 | }
|
---|
34 |
|
---|
35 | eval {require bytes};
|
---|
36 | eval "require diagnostics"; # some perl distros (eg mac) don't have this
|
---|
37 |
|
---|
38 | # suppress the annoying "subroutine redefined" warning that various
|
---|
39 | # plugins cause under perl 5.6
|
---|
40 | $SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
|
---|
41 |
|
---|
42 | use strict;
|
---|
43 | no strict 'subs';
|
---|
44 |
|
---|
45 | use gsprintf 'gsprintf';
|
---|
46 | use parse2;
|
---|
47 | use printusage;
|
---|
48 |
|
---|
49 | my $arguments = [];
|
---|
50 |
|
---|
51 | my $options = { 'name' => "PrintInfo",
|
---|
52 | 'desc' => "{PrintInfo.desc}",
|
---|
53 | 'abstract' => "yes",
|
---|
54 | 'inherits' => "no",
|
---|
55 | 'args' => $arguments };
|
---|
56 |
|
---|
57 | # $auxiliary_plugin argument passed in by "on-the-side" plugin helpers such as Extractors and ImageConverter. We don't want parsing of args done by them.
|
---|
58 | sub new
|
---|
59 | {
|
---|
60 | my $class = shift (@_);
|
---|
61 | my ($pluginlist,$args,$hashArgOptLists, $auxiliary) = @_;
|
---|
62 | my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
|
---|
63 |
|
---|
64 | if ($plugin_name eq $class) {
|
---|
65 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
66 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
67 | }
|
---|
68 | my $self = {};
|
---|
69 | $self->{'outhandle'} = STDERR;
|
---|
70 | $self->{'option_list'} = $hashArgOptLists->{"OptList"};
|
---|
71 | $self->{"info_only"} = 0;
|
---|
72 |
|
---|
73 | # Check if gsdlinfo is in the argument list or not - if it is, don't parse
|
---|
74 | # the args, just return the object.
|
---|
75 | foreach my $strArg (@{$args})
|
---|
76 | {
|
---|
77 | if($strArg eq "-gsdlinfo")
|
---|
78 | {
|
---|
79 | $self->{"info_only"} = 1;
|
---|
80 | return bless $self, $class;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (defined $auxiliary) { # don't parse the args here
|
---|
85 | return bless $self, $class;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
|
---|
89 | {
|
---|
90 | my $classTempClass = bless $self, $class;
|
---|
91 | print STDERR "<BadPlugin p=$plugin_name>\n";
|
---|
92 | &gsprintf(STDERR, "\n{PrintInfo.bad_general_option}\n", $plugin_name);
|
---|
93 | $classTempClass->print_txt_usage(""); # Use default resource bundle
|
---|
94 | die "\n";
|
---|
95 | }
|
---|
96 |
|
---|
97 | delete $self->{"info_only"};
|
---|
98 | # else parsing was successful.
|
---|
99 |
|
---|
100 | $self->{'plugin_type'} = $plugin_name;
|
---|
101 |
|
---|
102 | return bless $self, $class;
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | #sub init {
|
---|
107 | #}
|
---|
108 |
|
---|
109 |
|
---|
110 | sub set_incremental {
|
---|
111 | my $self = shift(@_);
|
---|
112 | my ($incremental_mode) = @_;
|
---|
113 |
|
---|
114 | if (!defined $incremental_mode) {
|
---|
115 | $self->{'incremental'} = 0;
|
---|
116 | $self->{'incremental_mode'} = "none";
|
---|
117 | }
|
---|
118 | elsif ($incremental_mode eq "none") {
|
---|
119 | $self->{'incremental'} = 0;
|
---|
120 | $self->{'incremental_mode'} = "none";
|
---|
121 | }
|
---|
122 | else {
|
---|
123 | $self->{'incremental'} = 1;
|
---|
124 | $self->{'incremental_mode'} = $incremental_mode;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | sub get_arguments
|
---|
129 | {
|
---|
130 | my $self = shift(@_);
|
---|
131 | my $optionlistref = $self->{'option_list'};
|
---|
132 | my @optionlist = @$optionlistref;
|
---|
133 | my $pluginoptions = pop(@$optionlistref);
|
---|
134 | my $pluginarguments = $pluginoptions->{'args'};
|
---|
135 | return $pluginarguments;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | sub print_xml_usage
|
---|
140 | {
|
---|
141 | my $self = shift(@_);
|
---|
142 | my $header = shift(@_);
|
---|
143 | my $high_level_information_only = shift(@_);
|
---|
144 |
|
---|
145 | # XML output is always in UTF-8
|
---|
146 | gsprintf::output_strings_in_UTF8;
|
---|
147 |
|
---|
148 | if ($header) {
|
---|
149 | &PrintUsage::print_xml_header("plugin");
|
---|
150 | }
|
---|
151 | $self->print_xml($high_level_information_only);
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | sub print_xml
|
---|
156 | {
|
---|
157 | my $self = shift(@_);
|
---|
158 | my $high_level_information_only = shift(@_);
|
---|
159 |
|
---|
160 | my $optionlistref = $self->{'option_list'};
|
---|
161 | my @optionlist = @$optionlistref;
|
---|
162 | my $pluginoptions = shift(@$optionlistref);
|
---|
163 | return if (!defined($pluginoptions));
|
---|
164 |
|
---|
165 | # Find the process and block default expressions in the plugin arguments
|
---|
166 | my $process_exp = "";
|
---|
167 | my $block_exp = "";
|
---|
168 | if (defined($pluginoptions->{'args'})) {
|
---|
169 | foreach my $option (@{$pluginoptions->{'args'}}) {
|
---|
170 | if ($option->{'name'} eq "process_exp") {
|
---|
171 | $process_exp = $option->{'deft'};
|
---|
172 | }
|
---|
173 | if ($option->{'name'} eq "block_exp") {
|
---|
174 | $block_exp = $option->{'deft'};
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | gsprintf(STDERR, "<PlugInfo>\n");
|
---|
180 | gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
|
---|
181 | my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
|
---|
182 | $desc =~ s/</&lt;/g; # doubly escaped
|
---|
183 | $desc =~ s/>/&gt;/g;
|
---|
184 | gsprintf(STDERR, " <Desc>$desc</Desc>\n");
|
---|
185 | gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
|
---|
186 | gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
|
---|
187 | gsprintf(STDERR, " <Processes>$process_exp</Processes>\n");
|
---|
188 | gsprintf(STDERR, " <Blocks>$block_exp</Blocks>\n");
|
---|
189 | gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
|
---|
190 | # adding new option that works with replace_srcdoc_with_html.pl
|
---|
191 | gsprintf(STDERR, " <SourceReplaceable>" . ($pluginoptions->{'srcreplaceable'} || "no") . "</SourceReplaceable>\n");
|
---|
192 | unless (defined($high_level_information_only)) {
|
---|
193 | gsprintf(STDERR, " <Arguments>\n");
|
---|
194 | if (defined($pluginoptions->{'args'})) {
|
---|
195 | &PrintUsage::print_options_xml($pluginoptions->{'args'});
|
---|
196 | }
|
---|
197 | gsprintf(STDERR, " </Arguments>\n");
|
---|
198 |
|
---|
199 | # Recurse up the plugin hierarchy
|
---|
200 | $self->print_xml();
|
---|
201 | }
|
---|
202 | gsprintf(STDERR, "</PlugInfo>\n");
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | sub print_txt_usage
|
---|
207 | {
|
---|
208 | my $self = shift(@_);
|
---|
209 | # Print the usage message for a plugin (recursively)
|
---|
210 | my $descoffset = $self->determine_description_offset(0);
|
---|
211 | $self->print_plugin_usage($descoffset, 1);
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | sub determine_description_offset
|
---|
216 | {
|
---|
217 | my $self = shift(@_);
|
---|
218 | my $maxoffset = shift(@_);
|
---|
219 |
|
---|
220 | my $optionlistref = $self->{'option_list'};
|
---|
221 | my @optionlist = @$optionlistref;
|
---|
222 | my $pluginoptions = shift(@$optionlistref);
|
---|
223 | return $maxoffset if (!defined($pluginoptions));
|
---|
224 |
|
---|
225 | # Find the length of the longest option string of this plugin
|
---|
226 | my $pluginargs = $pluginoptions->{'args'};
|
---|
227 | if (defined($pluginargs)) {
|
---|
228 | my $longest = &PrintUsage::find_longest_option_string($pluginargs);
|
---|
229 | if ($longest > $maxoffset) {
|
---|
230 | $maxoffset = $longest;
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | # Recurse up the plugin hierarchy
|
---|
235 | $maxoffset = $self->determine_description_offset($maxoffset);
|
---|
236 | $self->{'option_list'} = \@optionlist;
|
---|
237 | return $maxoffset;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | sub print_plugin_usage
|
---|
242 | {
|
---|
243 | my $self = shift(@_);
|
---|
244 | my $descoffset = shift(@_);
|
---|
245 | my $isleafclass = shift(@_);
|
---|
246 |
|
---|
247 | my $optionlistref = $self->{'option_list'};
|
---|
248 | my @optionlist = @$optionlistref;
|
---|
249 | my $pluginoptions = shift(@$optionlistref);
|
---|
250 | return if (!defined($pluginoptions));
|
---|
251 |
|
---|
252 | my $pluginname = $pluginoptions->{'name'};
|
---|
253 | my $pluginargs = $pluginoptions->{'args'};
|
---|
254 | my $plugindesc = $pluginoptions->{'desc'};
|
---|
255 |
|
---|
256 | # Produce the usage information using the data structure above
|
---|
257 | if ($isleafclass) {
|
---|
258 | if (defined($plugindesc)) {
|
---|
259 | gsprintf(STDERR, "$plugindesc\n\n");
|
---|
260 | }
|
---|
261 | gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
|
---|
262 | }
|
---|
263 |
|
---|
264 | # Display the plugin options, if there are some
|
---|
265 | if (defined($pluginargs)) {
|
---|
266 | # Calculate the column offset of the option descriptions
|
---|
267 | my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
|
---|
268 |
|
---|
269 | if ($isleafclass) {
|
---|
270 | gsprintf(STDERR, " {common.specific_options}:\n");
|
---|
271 | }
|
---|
272 | else {
|
---|
273 | gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
|
---|
274 | }
|
---|
275 |
|
---|
276 | # Display the plugin options
|
---|
277 | &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
|
---|
278 | }
|
---|
279 |
|
---|
280 | # Recurse up the plugin hierarchy
|
---|
281 | $self->print_plugin_usage($descoffset, 0);
|
---|
282 | $self->{'option_list'} = \@optionlist;
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | 1;
|
---|
287 |
|
---|
288 |
|
---|