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

Last change on this file since 7243 was 6408, checked in by jmt12, 20 years ago

Added two new attributes for script arguments. HiddenGLI controls whether the argument will be visible at all in GLI, while ModeGLI defines the lowest detail mode under which the argument will be visible (only really for import and buildcol). Also ensured that the scripts were reporting their correct default process expressions, and further refined argument types by adding the catagory regexp for any regular expression (which can then be hidden under lower detail modes in GLI)

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