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

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

Jefferey's new download modules

  • 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;
35
36# suppress the annoying "subroutine redefined" warning that various
37# gets cause under perl 5.6
38$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
39
40my $arguments = [];
41
42my $options = { 'name' => "BasDownload",
43 'desc' => "{BasDownload.desc}",
44 'abstract' => "yes",
45 'inherits' => "no" };
46
47sub new
48{
49 my $class = shift (@_);
50 my ($downloadlist,$args,$hashArgOptLists) = @_;
51 push(@$downloadlist, $class);
52 my $strDownloadName = (defined $downloadlist->[0]) ? $downloadlist->[0] : $class;
53
54 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
55 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
56
57 my $self = {};
58 $self->{'download_type'} = $strDownloadName;
59 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
60 $self->{"info_only"} = 0;
61
62 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
63 # the args, just return the object.
64 foreach my $strArg (@{$args})
65 {
66 if($strArg eq "-gsdlinfo")
67 {
68 $self->{"info_only"} = 1;
69 return bless $self, $class;
70 }
71 }
72
73 delete $self->{"info_only"};
74
75 if(!parse2::parse($args,$hashArgOptLists->{"ArgList"},$self))
76 {
77 my $classTempClass = bless $self, $class;
78 print STDERR "<BadDownload d=$self->{'download_name'}>\n";
79 &gsprintf(STDERR, "\n{BasDownload.bad_general_option}\n", $self->{'download_name'});
80 $classTempClass->print_txt_usage(""); # Use default resource bundle
81 die "\n";
82 }
83
84 return bless $self, $class;
85
86}
87
88sub download
89{
90 my ($self) = shift (@_);
91 my ($hashGeneralOptions) = @_;
92 &error("download","No download specified for $hashGeneralOptions->{download_mode}.\n");
93}
94
95
96sub print_xml_usage
97{
98 my $self = shift(@_);
99 my $header = shift(@_);
100
101 # XML output is always in UTF-8
102 gsprintf::output_strings_in_UTF8;
103
104 if ($header) {
105 &PrintUsage::print_xml_header("download");
106 }
107 $self->print_xml();
108}
109
110
111sub print_xml
112{
113 my $self = shift(@_);
114 my $optionlistref = $self->{'option_list'};
115 my @optionlist = @$optionlistref;
116 my $downloadoptions = shift(@$optionlistref);
117 return if (!defined($downloadoptions));
118
119 gsprintf(STDERR, "<DownloadInfo>\n");
120 gsprintf(STDERR, " <Name>$downloadoptions->{'name'}</Name>\n");
121 my $desc = gsprintf::lookup_string($downloadoptions->{'desc'});
122 $desc =~ s/</&amp;lt;/g; # doubly escaped
123 $desc =~ s/>/&amp;gt;/g;
124 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
125 gsprintf(STDERR, " <Abstract>$downloadoptions->{'abstract'}</Abstract>\n");
126 gsprintf(STDERR, " <Inherits>$downloadoptions->{'inherits'}</Inherits>\n");
127 gsprintf(STDERR, " <Arguments>\n");
128 if (defined($downloadoptions->{'args'})) {
129 &PrintUsage::print_options_xml($downloadoptions->{'args'});
130 }
131 gsprintf(STDERR, " </Arguments>\n");
132 # Recurse up the download hierarchy
133 $self->print_xml();
134 gsprintf(STDERR, "</DownloadInfo>\n");
135}
136
137
138sub print_txt_usage
139{
140 my $self = shift(@_);
141
142 # Print the usage message for a download (recursively)
143 my $descoffset = $self->determine_description_offset(0);
144 $self->print_download_usage($descoffset, 1);
145}
146
147sub determine_description_offset
148{
149 my $self = shift(@_);
150 my $maxoffset = shift(@_);
151
152 my $optionlistref = $self->{'option_list'};
153 my @optionlist = @$optionlistref;
154 my $downloadoptions = pop(@$optionlistref);
155 return $maxoffset if (!defined($downloadoptions));
156
157 # Find the length of the longest option string of this download
158 my $downloadargs = $downloadoptions->{'args'};
159 if (defined($downloadargs)) {
160 my $longest = &PrintUsage::find_longest_option_string($downloadargs);
161 if ($longest > $maxoffset) {
162 $maxoffset = $longest;
163 }
164 }
165
166 # Recurse up the download hierarchy
167 $maxoffset = $self->determine_description_offset($maxoffset);
168 $self->{'option_list'} = \@optionlist;
169 return $maxoffset;
170}
171
172
173sub print_download_usage
174{
175 my $self = shift(@_);
176 my $descoffset = shift(@_);
177 my $isleafclass = shift(@_);
178
179 my $optionlistref = $self->{'option_list'};
180 my @optionlist = @$optionlistref;
181 my $downloadoptions = shift(@$optionlistref);
182 return if (!defined($downloadoptions));
183
184 my $downloadname = $downloadoptions->{'name'};
185 my $downloadargs = $downloadoptions->{'args'};
186 my $downloaddesc = $downloadoptions->{'desc'};
187
188 # Produce the usage information using the data structure above
189 if ($isleafclass) {
190 if (defined($downloaddesc)) {
191 gsprintf(STDERR, "$downloaddesc\n\n");
192 }
193 gsprintf(STDERR, " {common.usage}: download $downloadname [{common.options}]\n\n");
194 }
195
196 # Display the download options, if there are some
197 if (defined($downloadargs)) {
198 # Calculate the column offset of the option descriptions
199 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
200
201 if ($isleafclass) {
202 gsprintf(STDERR, " {common.specific_options}:\n");
203 }
204 else {
205 gsprintf(STDERR, " {common.general_options}:\n", $downloadname);
206 }
207
208 # Display the download options
209 &PrintUsage::print_options_txt($downloadargs, $optiondescoffset);
210 }
211
212 # Recurse up the download hierarchy
213 $self->print_download_usage($descoffset, 0);
214 $self->{'option_list'} = \@optionlist;
215}
216
217sub url_information
218{
219 my ($self) = @_;
220 print STDERR "There is no extra information provided for this Download.\n";
221 return "";
222}
223
224sub error
225{
226 my ($strFunctionName,$strError) = @_;
227 {
228 print "Error occoured in BasDownload.pm\n".
229 "In Function: ".$strFunctionName."\n".
230 "Error Message: ".$strError."\n";
231 exit(-1);
232 }
233}
234
2351;
Note: See TracBrowser for help on using the repository browser.