source: main/trunk/greenstone2/perllib/plugins/PrintInfo.pm@ 32280

Last change on this file since 32280 was 32280, checked in by ak19, 6 years ago

Implementing PDFv2paged_text (with pdfbox)

  • Property svn:executable set to *
File size: 9.0 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 { 'name' => "gs_version",
51 'desc' => "{PrintInfo.gs_version}",
52 'type' => "string",
53 'reqd' => "no",
54 'hiddengli' => "yes" }
55
56];
57
58my $options = { 'name' => "PrintInfo",
59 'desc' => "{PrintInfo.desc}",
60 'abstract' => "yes",
61 'inherits' => "no",
62 'args' => $arguments };
63
64# $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.
65sub new
66{
67 my $class = shift (@_);
68 my ($pluginlist,$args,$hashArgOptLists, $auxiliary) = @_;
69 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
70
71 if ($plugin_name eq $class) {
72 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
73 push(@{$hashArgOptLists->{"OptList"}},$options);
74 }
75 my $self = {};
76 $self->{'outhandle'} = STDERR;
77 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
78 $self->{"info_only"} = 0;
79 $self->{'gs_version'} = "2";
80 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
81 # the args, just return the object.
82 # gsdlinfo must come before gs_version. both are set by plugin.pm
83 my $v=0;
84 foreach my $strArg (@{$args})
85 {
86 if ($v) {
87 $self->{'gs_version'} = $strArg;
88 last;
89 }
90 elsif($strArg eq "-gsdlinfo")
91 {
92 $self->{"info_only"} = 1;
93 #return bless $self, $class;
94 }
95 elsif ($strArg eq "-gs_version") {
96 $v = 1;
97 }
98 }
99
100 if ($self->{"info_only"}) {
101 return bless $self, $class;
102 }
103 if (defined $auxiliary) { # don't parse the args here
104 return bless $self, $class;
105 }
106
107 # now that we are passed printing out info, we do need to add in this class's options so that they are available for parsing.
108 if ($plugin_name ne $class) {
109 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
110 push(@{$hashArgOptLists->{"OptList"}},$options);
111 }
112
113 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
114 {
115 my $classTempClass = bless $self, $class;
116 print STDERR "<BadPlugin p=$plugin_name>\n";
117 &gsprintf(STDERR, "\n{PrintInfo.bad_general_option}\n", $plugin_name);
118 $classTempClass->print_txt_usage(""); # Use default resource bundle
119 die "\n";
120 }
121
122 delete $self->{"info_only"};
123 # else parsing was successful.
124
125 $self->{'plugin_type'} = $plugin_name;
126
127 return bless $self, $class;
128
129}
130
131#sub init {
132#}
133
134sub set_incremental {
135 my $self = shift(@_);
136 my ($incremental_mode) = @_;
137
138 if (!defined $incremental_mode) {
139 $self->{'incremental'} = 0;
140 $self->{'incremental_mode'} = "none";
141 }
142 elsif ($incremental_mode eq "all") {
143 $self->{'incremental'} = 1;
144 $self->{'incremental_mode'} = "all";
145 }
146 else {
147 # none, onlyadd
148 $self->{'incremental'} = 0;
149 $self->{'incremental_mode'} = $incremental_mode;
150 }
151}
152
153sub get_arguments
154{
155 my $self = shift(@_);
156 my $optionlistref = $self->{'option_list'};
157 my @optionlist = @$optionlistref;
158 my $pluginoptions = pop(@$optionlistref);
159 my $pluginarguments = $pluginoptions->{'args'};
160 return $pluginarguments;
161}
162
163
164sub print_xml_usage
165{
166 my $self = shift(@_);
167 my $header = shift(@_);
168 my $high_level_information_only = shift(@_);
169
170 # XML output is always in UTF-8
171 gsprintf::output_strings_in_UTF8;
172
173 if ($header) {
174 &PrintUsage::print_xml_header("plugin");
175 }
176 $self->print_xml($high_level_information_only);
177# print STDERR "\n@@@ BLA\n\n";
178}
179
180
181sub print_xml
182{
183 my $self = shift(@_);
184 my $high_level_information_only = shift(@_);
185
186 my $optionlistref = $self->{'option_list'};
187 my @optionlist = @$optionlistref;
188 my $pluginoptions = shift(@$optionlistref);
189 return if (!defined($pluginoptions));
190
191 # Find the process and block default expressions in the plugin arguments
192 my $process_exp = "";
193 my $block_exp = "";
194 if (defined($pluginoptions->{'args'})) {
195 foreach my $option (@{$pluginoptions->{'args'}}) {
196 if ($option->{'name'} eq "process_exp") {
197 $process_exp = $option->{'deft'};
198 }
199 if ($option->{'name'} eq "block_exp") {
200 $block_exp = $option->{'deft'};
201 }
202 }
203 }
204
205 gsprintf(STDERR, "<PlugInfo>\n");
206 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
207 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
208 $desc =~ s/</&amp;lt;/g; # doubly escaped
209 $desc =~ s/>/&amp;gt;/g;
210 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
211 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
212 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
213 gsprintf(STDERR, " <Processes>$process_exp</Processes>\n");
214 gsprintf(STDERR, " <Blocks>$block_exp</Blocks>\n");
215 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
216 # adding new option that works with replace_srcdoc_with_html.pl
217 gsprintf(STDERR, " <SourceReplaceable>" . ($pluginoptions->{'srcreplaceable'} || "no") . "</SourceReplaceable>\n");
218 unless (defined($high_level_information_only)) {
219 gsprintf(STDERR, " <Arguments>\n");
220 if (defined($pluginoptions->{'args'})) {
221 &PrintUsage::print_options_xml($pluginoptions->{'args'});
222 }
223 gsprintf(STDERR, " </Arguments>\n");
224
225 # Recurse up the plugin hierarchy
226 $self->print_xml();
227 }
228 gsprintf(STDERR, "</PlugInfo>\n");
229}
230
231
232sub print_txt_usage
233{
234 my $self = shift(@_);
235 # Print the usage message for a plugin (recursively)
236 my $descoffset = $self->determine_description_offset(0);
237 $self->print_plugin_usage($descoffset, 1);
238}
239
240
241sub determine_description_offset
242{
243 my $self = shift(@_);
244 my $maxoffset = shift(@_);
245
246 my $optionlistref = $self->{'option_list'};
247 my @optionlist = @$optionlistref;
248 my $pluginoptions = shift(@$optionlistref);
249 return $maxoffset if (!defined($pluginoptions));
250
251 # Find the length of the longest option string of this plugin
252 my $pluginargs = $pluginoptions->{'args'};
253 if (defined($pluginargs)) {
254 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
255 if ($longest > $maxoffset) {
256 $maxoffset = $longest;
257 }
258 }
259
260 # Recurse up the plugin hierarchy
261 $maxoffset = $self->determine_description_offset($maxoffset);
262 $self->{'option_list'} = \@optionlist;
263 return $maxoffset;
264}
265
266
267sub print_plugin_usage
268{
269 my $self = shift(@_);
270 my $descoffset = shift(@_);
271 my $isleafclass = shift(@_);
272
273 my $optionlistref = $self->{'option_list'};
274 my @optionlist = @$optionlistref;
275 my $pluginoptions = shift(@$optionlistref);
276 return if (!defined($pluginoptions));
277
278 my $pluginname = $pluginoptions->{'name'};
279 my $pluginargs = $pluginoptions->{'args'};
280 my $plugindesc = $pluginoptions->{'desc'};
281
282 # Produce the usage information using the data structure above
283 if ($isleafclass) {
284 if (defined($plugindesc)) {
285 gsprintf(STDERR, "$plugindesc\n\n");
286 }
287 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
288 }
289
290 # Display the plugin options, if there are some
291 if (defined($pluginargs)) {
292 # Calculate the column offset of the option descriptions
293 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
294
295 if ($isleafclass) {
296 gsprintf(STDERR, " {common.specific_options}:\n");
297 }
298 else {
299 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
300 }
301
302 # Display the plugin options
303 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
304 }
305
306 # Recurse up the plugin hierarchy
307 $self->print_plugin_usage($descoffset, 0);
308 $self->{'option_list'} = \@optionlist;
309}
310
311
3121;
313
314
Note: See TracBrowser for help on using the repository browser.