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

Last change on this file since 15880 was 15868, checked in by kjdon, 16 years ago

plugin overhaul: BasPlug has been split into several base plugins: PrintInfo just does the printing for pluginfo.pl, and does the argument parsing in the constructor. All plugins and supporting extractors etc inherit directly or indirectly from this. AbstractPlugin adds a few methods to this, is used by Directory and ArchivesInf plugins. These are not really plugins so can we remove them? anyway, not sure if AbstractPlugin will live for very long. BasePlugin is a proper base plugin, has read and read_into_doc_obj methods. It does nothing with reading in the file or textcat stuff. Makes a basic doc obj and adds some metadata. It also handles all the blocking stuff, associate ext stuff etc. Binary plugins can implement the process method to do file specific stuff. AutoExtractMetadata inherits BasePlugin and adds automatic metadata extraction using hte new Extractor plugins. ReadTextFile is the equivalent in functionality to the old BasPlug - does lang and encoding extraction, and reading in the file. It inherits from AutoExtractMetadata. If your file type is binary and will have no text, then inherit from BasePlugin. If its binary but ends up with text (eg using convert_to) then inherit from AutoExtractMetadata. If your file is a text type file, then inherit from ReadTextFile.

  • Property svn:executable set to *
File size: 6.5 KB
Line 
1## Most basic plugin, just handles parsing the arguments and printing out descriptions. Used for plugins and Extractor plugins
2
3package PrintInfo;
4
5BEGIN {
6 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
7}
8
9eval {require bytes};
10
11# suppress the annoying "subroutine redefined" warning that various
12# plugins cause under perl 5.6
13$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
14
15use strict;
16no strict 'subs';
17
18use gsprintf 'gsprintf';
19use parse2;
20use printusage;
21
22my $arguments = [];
23
24my $options = { 'name' => "PrintInfo",
25 'desc' => "{PrintInfo.desc}",
26 'abstract' => "yes",
27 'inherits' => "no",
28 'args' => $arguments };
29
30
31sub new
32{
33 my $class = shift (@_);
34 my ($pluginlist,$args,$hashArgOptLists) = @_;
35 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
36
37 if ($plugin_name eq $class) {
38 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
39 push(@{$hashArgOptLists->{"OptList"}},$options);
40 }
41 my $self = {};
42 $self->{'outhandle'} = STDERR;
43 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
44 $self->{"info_only"} = 0;
45
46 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
47 # the args, just return the object.
48 foreach my $strArg (@{$args})
49 {
50 if($strArg eq "-gsdlinfo")
51 {
52 $self->{"info_only"} = 1;
53 return bless $self, $class;
54 }
55 }
56
57 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
58 {
59 my $classTempClass = bless $self, $class;
60 print STDERR "<BadPlugin p=$plugin_name>\n";
61 &gsprintf(STDERR, "\n{BasPlug.bad_general_option}\n", $plugin_name);
62 $classTempClass->print_txt_usage(""); # Use default resource bundle
63 die "\n";
64 }
65
66 delete $self->{"info_only"};
67 # else parsing was successful.
68
69 $self->{'plugin_type'} = $plugin_name;
70
71 return bless $self, $class;
72
73}
74
75#sub init {
76#}
77
78sub get_arguments
79{
80 my $self = shift(@_);
81 my $optionlistref = $self->{'option_list'};
82 my @optionlist = @$optionlistref;
83 my $pluginoptions = pop(@$optionlistref);
84 my $pluginarguments = $pluginoptions->{'args'};
85 return $pluginarguments;
86}
87
88
89sub print_xml_usage
90{
91 my $self = shift(@_);
92 my $header = shift(@_);
93 my $high_level_information_only = shift(@_);
94
95 # XML output is always in UTF-8
96 gsprintf::output_strings_in_UTF8;
97
98 if ($header) {
99 &PrintUsage::print_xml_header("plugin");
100 }
101 $self->print_xml($high_level_information_only);
102}
103
104
105sub print_xml
106{
107 my $self = shift(@_);
108 my $high_level_information_only = shift(@_);
109
110 my $optionlistref = $self->{'option_list'};
111 my @optionlist = @$optionlistref;
112 my $pluginoptions = shift(@$optionlistref);
113 return if (!defined($pluginoptions));
114
115 # Find the process and block default expressions in the plugin arguments
116 my $process_exp = "";
117 my $block_exp = "";
118 if (defined($pluginoptions->{'args'})) {
119 foreach my $option (@{$pluginoptions->{'args'}}) {
120 if ($option->{'name'} eq "process_exp") {
121 $process_exp = $option->{'deft'};
122 }
123 if ($option->{'name'} eq "block_exp") {
124 $block_exp = $option->{'deft'};
125 }
126 }
127 }
128
129 gsprintf(STDERR, "<PlugInfo>\n");
130 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
131 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
132 $desc =~ s/</&amp;lt;/g; # doubly escaped
133 $desc =~ s/>/&amp;gt;/g;
134 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
135 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
136 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
137 gsprintf(STDERR, " <Processes>$process_exp</Processes>\n");
138 gsprintf(STDERR, " <Blocks>$block_exp</Blocks>\n");
139 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
140 # adding new option that works with replace_srcdoc_with_html.pl
141 gsprintf(STDERR, " <SourceReplaceable>" . ($pluginoptions->{'srcreplaceable'} || "no") . "</SourceReplaceable>\n");
142 unless (defined($high_level_information_only)) {
143 gsprintf(STDERR, " <Arguments>\n");
144 if (defined($pluginoptions->{'args'})) {
145 &PrintUsage::print_options_xml($pluginoptions->{'args'});
146 }
147 gsprintf(STDERR, " </Arguments>\n");
148
149 # Recurse up the plugin hierarchy
150 $self->print_xml();
151 }
152 gsprintf(STDERR, "</PlugInfo>\n");
153}
154
155
156sub print_txt_usage
157{
158 my $self = shift(@_);
159 # Print the usage message for a plugin (recursively)
160 my $descoffset = $self->determine_description_offset(0);
161 $self->print_plugin_usage($descoffset, 1);
162}
163
164
165sub determine_description_offset
166{
167 my $self = shift(@_);
168 my $maxoffset = shift(@_);
169
170 my $optionlistref = $self->{'option_list'};
171 my @optionlist = @$optionlistref;
172 my $pluginoptions = shift(@$optionlistref);
173 return $maxoffset if (!defined($pluginoptions));
174
175 # Find the length of the longest option string of this plugin
176 my $pluginargs = $pluginoptions->{'args'};
177 if (defined($pluginargs)) {
178 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
179 if ($longest > $maxoffset) {
180 $maxoffset = $longest;
181 }
182 }
183
184 # Recurse up the plugin hierarchy
185 $maxoffset = $self->determine_description_offset($maxoffset);
186 $self->{'option_list'} = \@optionlist;
187 return $maxoffset;
188}
189
190
191sub print_plugin_usage
192{
193 my $self = shift(@_);
194 my $descoffset = shift(@_);
195 my $isleafclass = shift(@_);
196
197 my $optionlistref = $self->{'option_list'};
198 my @optionlist = @$optionlistref;
199 my $pluginoptions = shift(@$optionlistref);
200 return if (!defined($pluginoptions));
201
202 my $pluginname = $pluginoptions->{'name'};
203 my $pluginargs = $pluginoptions->{'args'};
204 my $plugindesc = $pluginoptions->{'desc'};
205
206 # Produce the usage information using the data structure above
207 if ($isleafclass) {
208 if (defined($plugindesc)) {
209 gsprintf(STDERR, "$plugindesc\n\n");
210 }
211 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
212 }
213
214 # Display the plugin options, if there are some
215 if (defined($pluginargs)) {
216 # Calculate the column offset of the option descriptions
217 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
218
219 if ($isleafclass) {
220 gsprintf(STDERR, " {common.specific_options}:\n");
221 }
222 else {
223 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
224 }
225
226 # Display the plugin options
227 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
228 }
229
230 # Recurse up the plugin hierarchy
231 $self->print_plugin_usage($descoffset, 0);
232 $self->{'option_list'} = \@optionlist;
233}
234
2351;
236
237
Note: See TracBrowser for help on using the repository browser.