source: trunk/gsdl/perllib/downloaders/BasDownload.pm@ 12546

Last change on this file since 12546 was 12546, checked in by kjdon, 18 years ago

changed parse2::parse so that it returns -1 on error, 0 on success, or if allow_extra_options is specified, then on success returns the number of args left over.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1###########################################################################
2#
3# BasDownload.pm -- base class for all the Download modules
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 2006 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package BasDownload;
27
28eval {require bytes};
29
30use strict;
31no strict 'subs';
32
33use gsprintf 'gsprintf';
34use printusage;
35use parse2;
36
37# suppress the annoying "subroutine redefined" warning that various
38# gets cause under perl 5.6
39$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
40
41my $arguments = [];
42
43my $options = { 'name' => "BasDownload",
44 'desc' => "{BasDownload.desc}",
45 'abstract' => "yes",
46 'inherits' => "no" };
47
48sub new
49{
50 my $class = shift (@_);
51 my ($downloadlist,$args,$hashArgOptLists) = @_;
52 push(@$downloadlist, $class);
53 my $strDownloadName = (defined $downloadlist->[0]) ? $downloadlist->[0] : $class;
54
55 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
56 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
57
58 my $self = {};
59 $self->{'download_type'} = $strDownloadName;
60 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
61 $self->{"info_only"} = 0;
62
63 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
64 # the args, just return the object.
65 foreach my $strArg (@{$args})
66 {
67 if($strArg eq "-gsdlinfo")
68 {
69 $self->{"info_only"} = 1;
70 return bless $self, $class;
71 }
72 }
73
74 delete $self->{"info_only"};
75
76 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
77 {
78 my $classTempClass = bless $self, $class;
79 print STDERR "<BadDownload d=$self->{'download_name'}>\n";
80 &gsprintf(STDERR, "\n{BasDownload.bad_general_option}\n", $self->{'download_name'});
81 $classTempClass->print_txt_usage(""); # Use default resource bundle
82 die "\n";
83 }
84
85 return bless $self, $class;
86
87}
88
89sub download
90{
91 my ($self) = shift (@_);
92 my ($hashGeneralOptions) = @_;
93 &error("download","No download specified for $hashGeneralOptions->{download_mode}.\n");
94}
95
96
97sub print_xml_usage
98{
99 my $self = shift(@_);
100 my $header = shift(@_);
101
102 # XML output is always in UTF-8
103 gsprintf::output_strings_in_UTF8;
104
105 if ($header) {
106 &PrintUsage::print_xml_header("download");
107 }
108 $self->print_xml();
109}
110
111
112sub print_xml
113{
114 my $self = shift(@_);
115 my $optionlistref = $self->{'option_list'};
116 my @optionlist = @$optionlistref;
117 my $downloadoptions = shift(@$optionlistref);
118 return if (!defined($downloadoptions));
119
120 gsprintf(STDERR, "<DownloadInfo>\n");
121 gsprintf(STDERR, " <Name>$downloadoptions->{'name'}</Name>\n");
122 my $desc = gsprintf::lookup_string($downloadoptions->{'desc'});
123 $desc =~ s/</&amp;lt;/g; # doubly escaped
124 $desc =~ s/>/&amp;gt;/g;
125 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
126 gsprintf(STDERR, " <Abstract>$downloadoptions->{'abstract'}</Abstract>\n");
127 gsprintf(STDERR, " <Inherits>$downloadoptions->{'inherits'}</Inherits>\n");
128 gsprintf(STDERR, " <Arguments>\n");
129 if (defined($downloadoptions->{'args'})) {
130 &PrintUsage::print_options_xml($downloadoptions->{'args'});
131 }
132 gsprintf(STDERR, " </Arguments>\n");
133 # Recurse up the download hierarchy
134 $self->print_xml();
135 gsprintf(STDERR, "</DownloadInfo>\n");
136}
137
138
139sub print_txt_usage
140{
141 my $self = shift(@_);
142
143 # Print the usage message for a download (recursively)
144 my $descoffset = $self->determine_description_offset(0);
145 $self->print_download_usage($descoffset, 1);
146}
147
148sub determine_description_offset
149{
150 my $self = shift(@_);
151 my $maxoffset = shift(@_);
152
153 my $optionlistref = $self->{'option_list'};
154 my @optionlist = @$optionlistref;
155 my $downloadoptions = pop(@$optionlistref);
156 return $maxoffset if (!defined($downloadoptions));
157
158 # Find the length of the longest option string of this download
159 my $downloadargs = $downloadoptions->{'args'};
160 if (defined($downloadargs)) {
161 my $longest = &PrintUsage::find_longest_option_string($downloadargs);
162 if ($longest > $maxoffset) {
163 $maxoffset = $longest;
164 }
165 }
166
167 # Recurse up the download hierarchy
168 $maxoffset = $self->determine_description_offset($maxoffset);
169 $self->{'option_list'} = \@optionlist;
170 return $maxoffset;
171}
172
173
174sub print_download_usage
175{
176 my $self = shift(@_);
177 my $descoffset = shift(@_);
178 my $isleafclass = shift(@_);
179
180 my $optionlistref = $self->{'option_list'};
181 my @optionlist = @$optionlistref;
182 my $downloadoptions = shift(@$optionlistref);
183 return if (!defined($downloadoptions));
184
185 my $downloadname = $downloadoptions->{'name'};
186 my $downloadargs = $downloadoptions->{'args'};
187 my $downloaddesc = $downloadoptions->{'desc'};
188
189 # Produce the usage information using the data structure above
190 if ($isleafclass) {
191 if (defined($downloaddesc)) {
192 gsprintf(STDERR, "$downloaddesc\n\n");
193 }
194 gsprintf(STDERR, " {common.usage}: download $downloadname [{common.options}]\n\n");
195 }
196
197 # Display the download options, if there are some
198 if (defined($downloadargs)) {
199 # Calculate the column offset of the option descriptions
200 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
201
202 if ($isleafclass) {
203 gsprintf(STDERR, " {common.specific_options}:\n");
204 }
205 else {
206 gsprintf(STDERR, " {common.general_options}:\n", $downloadname);
207 }
208
209 # Display the download options
210 &PrintUsage::print_options_txt($downloadargs, $optiondescoffset);
211 }
212
213 # Recurse up the download hierarchy
214 $self->print_download_usage($descoffset, 0);
215 $self->{'option_list'} = \@optionlist;
216}
217
218sub url_information
219{
220 my ($self) = @_;
221 print STDERR "There is no extra information provided for this Download.\n";
222 return "";
223}
224
225sub error
226{
227 my ($strFunctionName,$strError) = @_;
228 {
229 print "Error occoured in BasDownload.pm\n".
230 "In Function: ".$strFunctionName."\n".
231 "Error Message: ".$strError."\n";
232 exit(-1);
233 }
234}
235
2361;
Note: See TracBrowser for help on using the repository browser.