source: gsdl/trunk/perllib/plugins/PrintInfo.pm@ 20682

Last change on this file since 20682 was 20682, checked in by kjdon, 13 years ago

in set_incremental, we want incremental to be 0 if incremental_mode is onlyadd

  • Property svn:executable set to *
File size: 8.3 KB
Line 
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
29package PrintInfo;
30
31BEGIN {
32 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
33}
34
35eval {require bytes};
36eval "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
42use strict;
43no strict 'subs';
44
45use gsprintf 'gsprintf';
46use parse2;
47use printusage;
48
49my $arguments = [];
50
51my $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.
58sub 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
110sub 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 "all") {
119 $self->{'incremental'} = 1;
120 $self->{'incremental_mode'} = "all";
121 }
122 else {
123 # none, onlyadd
124 $self->{'incremental'} = 0;
125 $self->{'incremental_mode'} = $incremental_mode;
126 }
127}
128
129sub get_arguments
130{
131 my $self = shift(@_);
132 my $optionlistref = $self->{'option_list'};
133 my @optionlist = @$optionlistref;
134 my $pluginoptions = pop(@$optionlistref);
135 my $pluginarguments = $pluginoptions->{'args'};
136 return $pluginarguments;
137}
138
139
140sub print_xml_usage
141{
142 my $self = shift(@_);
143 my $header = shift(@_);
144 my $high_level_information_only = shift(@_);
145
146 # XML output is always in UTF-8
147 gsprintf::output_strings_in_UTF8;
148
149 if ($header) {
150 &PrintUsage::print_xml_header("plugin");
151 }
152 $self->print_xml($high_level_information_only);
153}
154
155
156sub print_xml
157{
158 my $self = shift(@_);
159 my $high_level_information_only = shift(@_);
160
161 my $optionlistref = $self->{'option_list'};
162 my @optionlist = @$optionlistref;
163 my $pluginoptions = shift(@$optionlistref);
164 return if (!defined($pluginoptions));
165
166 # Find the process and block default expressions in the plugin arguments
167 my $process_exp = "";
168 my $block_exp = "";
169 if (defined($pluginoptions->{'args'})) {
170 foreach my $option (@{$pluginoptions->{'args'}}) {
171 if ($option->{'name'} eq "process_exp") {
172 $process_exp = $option->{'deft'};
173 }
174 if ($option->{'name'} eq "block_exp") {
175 $block_exp = $option->{'deft'};
176 }
177 }
178 }
179
180 gsprintf(STDERR, "<PlugInfo>\n");
181 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
182 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
183 $desc =~ s/</&amp;lt;/g; # doubly escaped
184 $desc =~ s/>/&amp;gt;/g;
185 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
186 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
187 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
188 gsprintf(STDERR, " <Processes>$process_exp</Processes>\n");
189 gsprintf(STDERR, " <Blocks>$block_exp</Blocks>\n");
190 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
191 # adding new option that works with replace_srcdoc_with_html.pl
192 gsprintf(STDERR, " <SourceReplaceable>" . ($pluginoptions->{'srcreplaceable'} || "no") . "</SourceReplaceable>\n");
193 unless (defined($high_level_information_only)) {
194 gsprintf(STDERR, " <Arguments>\n");
195 if (defined($pluginoptions->{'args'})) {
196 &PrintUsage::print_options_xml($pluginoptions->{'args'});
197 }
198 gsprintf(STDERR, " </Arguments>\n");
199
200 # Recurse up the plugin hierarchy
201 $self->print_xml();
202 }
203 gsprintf(STDERR, "</PlugInfo>\n");
204}
205
206
207sub print_txt_usage
208{
209 my $self = shift(@_);
210 # Print the usage message for a plugin (recursively)
211 my $descoffset = $self->determine_description_offset(0);
212 $self->print_plugin_usage($descoffset, 1);
213}
214
215
216sub determine_description_offset
217{
218 my $self = shift(@_);
219 my $maxoffset = shift(@_);
220
221 my $optionlistref = $self->{'option_list'};
222 my @optionlist = @$optionlistref;
223 my $pluginoptions = shift(@$optionlistref);
224 return $maxoffset if (!defined($pluginoptions));
225
226 # Find the length of the longest option string of this plugin
227 my $pluginargs = $pluginoptions->{'args'};
228 if (defined($pluginargs)) {
229 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
230 if ($longest > $maxoffset) {
231 $maxoffset = $longest;
232 }
233 }
234
235 # Recurse up the plugin hierarchy
236 $maxoffset = $self->determine_description_offset($maxoffset);
237 $self->{'option_list'} = \@optionlist;
238 return $maxoffset;
239}
240
241
242sub print_plugin_usage
243{
244 my $self = shift(@_);
245 my $descoffset = shift(@_);
246 my $isleafclass = shift(@_);
247
248 my $optionlistref = $self->{'option_list'};
249 my @optionlist = @$optionlistref;
250 my $pluginoptions = shift(@$optionlistref);
251 return if (!defined($pluginoptions));
252
253 my $pluginname = $pluginoptions->{'name'};
254 my $pluginargs = $pluginoptions->{'args'};
255 my $plugindesc = $pluginoptions->{'desc'};
256
257 # Produce the usage information using the data structure above
258 if ($isleafclass) {
259 if (defined($plugindesc)) {
260 gsprintf(STDERR, "$plugindesc\n\n");
261 }
262 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
263 }
264
265 # Display the plugin options, if there are some
266 if (defined($pluginargs)) {
267 # Calculate the column offset of the option descriptions
268 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
269
270 if ($isleafclass) {
271 gsprintf(STDERR, " {common.specific_options}:\n");
272 }
273 else {
274 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
275 }
276
277 # Display the plugin options
278 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
279 }
280
281 # Recurse up the plugin hierarchy
282 $self->print_plugin_usage($descoffset, 0);
283 $self->{'option_list'} = \@optionlist;
284}
285
286
2871;
288
289
Note: See TracBrowser for help on using the repository browser.