source: trunk/gsdl/perllib/plugins/RogPlug.pm@ 5845

Last change on this file since 5845 was 5680, checked in by mdewsnip, 21 years ago

Moved plugin descriptions into the resource bundle (perllib/strings.rb), for translation by UNESCO.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1###########################################################################
2#
3# RogPlug.pm -- simple text plugin
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) 1999 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
26# creates simple single-level document from .rog or .mdb files
27
28package RogPlug;
29
30use BasPlug;
31use sorttools;
32use doc;
33
34sub BEGIN {
35 @ISA = ('BasPlug');
36}
37
38my $options = { 'name' => "RogPlug",
39 'desc' => "{RogPlug.desc}",
40 'inherits' => "Yes" };
41
42sub new {
43 my ($class) = @_;
44 $self = new BasPlug ();
45
46 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
47 my $option_list = $self->{'option_list'};
48 push( @{$option_list}, $options );
49
50 return bless $self, $class;
51}
52
53sub is_recursive {
54 my $self = shift (@_);
55
56 return 0; # this is not a recursive plugin
57}
58
59
60# This plugin processes files with the suffix ".mdb" or ".rog"
61sub get_default_process_exp {
62 return q^(?i)\.(mdb|rog)$^;
63}
64
65sub read_rog_record
66{
67 my ($self,$file_buffer, $seclevel) = @_;
68
69 my $next_line = $file_buffer->{'next_line'};
70
71 return 0 if (!defined $next_line);
72
73 if ($next_line eq "")
74 {
75 my $line;
76 while(defined($line=<FILE>))
77 {
78 $line =~ s/\r$//;
79 $file_buffer->{'line_no'}++;
80 next if ($line =~ m/^\#/);
81 $next_line = $line;
82 last;
83 }
84 }
85
86 if ($next_line !~ m/^song( +)\"([^\"]*)\"( +)\"([^\"]*)\"( +)(\d+)( *)$/)
87 {
88 print STDERR "Error: Malformed Rog file: $next_line";
89 return 0;
90 }
91 else
92 {
93 # init default values
94 $file_buffer->{'song'}->{'tempo'} = 120;
95 $file_buffer->{'song'}->{'ks_type'} = 0;
96 $file_buffer->{'song'}->{'ks_num'} = 0;
97 $file_buffer->{'song'}->{'metadata'} = [];
98 $file_buffer->{'song'}->{'content'} = "";
99
100 $file_buffer->{'song'}->{'subcol'} = $2;
101 $file_buffer->{'song'}->{'title'} = $4;
102 $file_buffer->{'song'}->{'tval'} = $6;
103
104 chomp($next_line);
105 my $content = $next_line;
106 if (defined $seclevel)
107 {
108 $content.= " $seclevel";
109 }
110 $content .= "\n";
111
112 $file_buffer->{'song'}->{'content'} = $content;
113
114
115 my $line;
116 while(defined($line=<FILE>))
117 {
118 $line =~ s/\r$//;
119
120 $file_buffer->{'line_no'}++;
121 next if ($line =~ m/^\#/);
122
123 if ($line =~ m/^song/)
124 {
125 $file_buffer->{'next_line'} = $line;
126 return 1;
127 }
128 elsif ($line =~ m/^tempo( +)(\d+)( *)$/)
129 {
130 $file_buffer->{'song'}->{'tempo'} = $2;
131 $file_buffer->{'song'}->{'content'} .= $line;
132 }
133 elsif ($line =~ m/^keysig( +)(\d+)( +)(\d+)( *)$/)
134 {
135 $file_buffer->{'song'}->{'ks_type'} = $2;
136 $file_buffer->{'song'}->{'ks_num'} = $4;
137 $file_buffer->{'song'}->{'content'} .= $line;
138 }
139 elsif ($line =~ m/^timesig( +)(\d+)( +)(\d+)( *)$/)
140 {
141 $file_buffer->{'song'}->{'ts_numer'} = $2;
142 $file_buffer->{'song'}->{'ts_denom'} = $4;
143 $file_buffer->{'song'}->{'content'} .= $line;
144 }
145 elsif ($line =~ m/^metadata ([^:]*): (.*)/)
146 {
147 push(@{$file_buffer->{'song'}->{'metadata'}},[$1,$2]);
148 $file_buffer->{'song'}->{'content'} .= $line;
149 }
150 else
151 {
152 $file_buffer->{'song'}->{'content'} .= $line;
153 }
154 }
155
156 $file_buffer->{'next_line'} = undef;
157 }
158
159 return 1;
160}
161
162sub process_rog_record
163{
164 my ($self,$file,$metadata,$song,$processor) = @_;
165
166 # create a new document
167 my $doc_obj = new doc ($file, "indexed_doc");
168 my $cursection = $doc_obj->get_top_section();
169
170 my $title = $song->{'title'};
171 my $title_safe = $title;
172 $title_safe =~ s/\'/\\\\&apos;/g;
173
174 # add metadata
175 $doc_obj->add_metadata($cursection, "Tempo", $song->{'tempo'});
176 $doc_obj->add_metadata($cursection, "KeySigType", $song->{'ks_type'});
177 $doc_obj->add_metadata($cursection, "KeySigNum", $song->{'ks_num'});
178 $doc_obj->add_metadata($cursection, "SubCollection", $song->{'subcol'});
179 $doc_obj->add_metadata($cursection, "Title", $title);
180 $doc_obj->add_metadata($cursection, "TitleSafe", $title_safe);
181 $doc_obj->add_metadata($cursection, "TVal", $song->{'tval'});
182
183 foreach $md ( @{$song->{'metadata'}} )
184 {
185 $doc_obj->add_metadata($cursection, $md->[0], $md->[1]);
186 }
187
188 # add contents as text
189 $doc_obj->add_text($cursection,$song->{'content'});
190
191 $self->extra_metadata($doc_obj,$cursection, $metadata);
192
193 # add OID
194 $doc_obj->set_OID ();
195
196 my $oid = $doc_obj->get_OID();
197 my $appletlink = "<a href=\"javascript:meldexout(\'$oid\','[TitleSafe]')\">";
198
199 $doc_obj->add_utf8_metadata ($cursection, "audiolink", $appletlink);
200 $doc_obj->add_utf8_metadata ($cursection, "audioicon", "_iconaudio_");
201 $doc_obj->add_utf8_metadata ($cursection, "/audiolink", "</a>");
202
203 # process the document
204 $processor->process($doc_obj);
205}
206
207# return number of files processed, undef if can't process
208# Note that $base_dir might be "" and that $file might
209# include directories
210sub read {
211 my $self = shift (@_);
212 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
213
214 my $filename = &util::filename_cat($base_dir, $file);
215
216 return undef unless ($filename =~ /\.((rog|mdb)(\.gz)?)$/i && (-e $filename));
217
218 my $gz = (defined $3) ? 1: 0;
219
220 print STDERR "RogPlug: processing $filename\n" if $processor->{'verbosity'};
221
222 if ($gz) {
223 open (FILE, "zcat $filename |")
224 || die "RogPlug::read - zcat can't open $filename\n";
225 } else {
226 open (FILE, $filename)
227 || die "RogPlug::read - can't open $filename\n";
228 }
229
230 my $doc_count = 0;
231 my $dot_count = 0;
232 my $file_buffer = { line_no => 0, next_line => "", song => {} };
233
234 while ($self->read_rog_record($file_buffer))
235 {
236 $self->process_rog_record($file,$metadata,$file_buffer->{'song'},$processor);
237 $doc_count++;
238
239 if (defined $maxdocs && $maxdocs =~ /\d/)
240 {
241 last if ($maxdocs >=0 && $doc_count >= $maxdocs);
242 }
243
244 if (($doc_count % 10) == 0)
245 {
246 print STDERR ".";
247 $dot_count++;
248 print STDERR "\n" if (($dot_count % 80) == 0);
249 }
250 }
251
252 close FILE;
253
254 print STDERR "\n";
255
256 $self->{'num_processed'} = $doc_count;
257
258 return 1; # processed the file
259}
260
2611;
262
263
264
265
266
267
268
269
270
271
272
Note: See TracBrowser for help on using the repository browser.