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

Last change on this file since 17739 was 16639, checked in by kjdon, 16 years ago

moved the require diagnostics line to here from ReadTextFile

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