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

Last change on this file since 10606 was 10254, checked in by kjdon, 19 years ago

added 'use strict' to all plugins, and made modifications (mostly adding 'my') to make them compile

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