1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # FOXPlug.pm
|
---|
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 | # plugin to process a Foxbase dbt file. This plugin provides the basic
|
---|
27 | # functionality to read in the dbt and dbf files and process each record.
|
---|
28 | # This general plugin should be overridden for a particular database to process
|
---|
29 | # the appropriate fields in the file.
|
---|
30 |
|
---|
31 | # 12/05/02 Added usage datastructure - John Thompson
|
---|
32 |
|
---|
33 | package FOXPlug;
|
---|
34 |
|
---|
35 | use BasPlug;
|
---|
36 | use util;
|
---|
37 | use doc;
|
---|
38 | use unicode;
|
---|
39 | use cnseg;
|
---|
40 | # use gb;
|
---|
41 |
|
---|
42 | use strict;
|
---|
43 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
44 |
|
---|
45 |
|
---|
46 | sub BEGIN {
|
---|
47 | @FOXPlug::ISA = ('BasPlug');
|
---|
48 | }
|
---|
49 |
|
---|
50 | my $arguments = [
|
---|
51 | ];
|
---|
52 |
|
---|
53 | my $options = { 'name' => "FOXPlug",
|
---|
54 | 'desc' => "{FOXPlug.desc}",
|
---|
55 | 'abstract' => "no",
|
---|
56 | 'inherits' => "yes" };
|
---|
57 |
|
---|
58 | sub new {
|
---|
59 | my ($class) = shift (@_);
|
---|
60 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
61 | push(@$pluginlist, $class);
|
---|
62 |
|
---|
63 | if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
|
---|
64 | if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
|
---|
65 |
|
---|
66 | my $self = (defined $hashArgOptLists)? new BasPlug($pluginlist,$inputargs,$hashArgOptLists): new BasPlug($pluginlist,$inputargs);
|
---|
67 |
|
---|
68 | return bless $self, $class;
|
---|
69 | }
|
---|
70 |
|
---|
71 | sub get_default_process_exp {
|
---|
72 | my $self = shift (@_);
|
---|
73 |
|
---|
74 | return q^(?i)\.dbf$^;
|
---|
75 | }
|
---|
76 |
|
---|
77 | #dbt files are processed at the same time as dbf files
|
---|
78 | sub get_default_block_exp {
|
---|
79 | my $self = shift (@_);
|
---|
80 |
|
---|
81 | return q^(?i)\.dbt$^;
|
---|
82 | }
|
---|
83 |
|
---|
84 | # return number of files processed, undef if can't process
|
---|
85 | # Note that $base_dir might be "" and that $file might
|
---|
86 | # include directories
|
---|
87 | sub read {
|
---|
88 | my $self = shift (@_);
|
---|
89 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
90 |
|
---|
91 | #check for associate_ext, blocking etc, are we processing this file?
|
---|
92 | my ($block_status,$fullname) = $self->read_block(@_);
|
---|
93 | return $block_status if ((!defined $block_status) || ($block_status==0));
|
---|
94 |
|
---|
95 | print STDERR "<Processing n='$file' p='FOXPlug'>\n" if ($gli);
|
---|
96 | print STDERR "FOXPlug: processing $file\n" if $self->{'verbosity'} > 1;
|
---|
97 |
|
---|
98 | my ($parent_dir) = $fullname =~ /^(.*)\/[^\/]+\.dbf$/i;
|
---|
99 |
|
---|
100 | # open the file
|
---|
101 | if (!open (FOXBASEIN, $fullname)) {
|
---|
102 | if ($gli) {
|
---|
103 | print STDERR "<ProcessingError n='$file' r='Could not read $fullname'>\n";
|
---|
104 | }
|
---|
105 | print STDERR "FOXPlug::read - couldn't read $fullname\n";
|
---|
106 | return -1; # error in processing
|
---|
107 | }
|
---|
108 |
|
---|
109 | # read in the database header
|
---|
110 | my ($temp, %dbf);
|
---|
111 |
|
---|
112 | # read in information about dbt file
|
---|
113 | if (read (FOXBASEIN, $temp, 32) < 32) {
|
---|
114 | if ($gli) {
|
---|
115 | print STDERR "<ProcessingError n='$file' r='EOF while reading database header'>\n";
|
---|
116 | }
|
---|
117 | print STDERR "FOXPlug::read - eof while reading database header\n";
|
---|
118 | close (FOXBASEIN);
|
---|
119 | return -1;
|
---|
120 | }
|
---|
121 |
|
---|
122 | # unpack the header
|
---|
123 | ($dbf{'hasdbt'},
|
---|
124 | $dbf{'modyear'}, $dbf{'modmonth'}, $dbf{'modday'},
|
---|
125 | $dbf{'numrecords'}, $dbf{'headerlen'},
|
---|
126 | $dbf{'recordlen'}) = unpack ("CCCCVvv", $temp);
|
---|
127 |
|
---|
128 | # process hasdbt
|
---|
129 | if ($dbf{'hasdbt'} == 131) {
|
---|
130 | $dbf{'hasdbt'} = 1;
|
---|
131 | } elsif ($dbf{'hasdbt'} == 3 || $dbf{'hasdbt'} == 48) {
|
---|
132 | $dbf{'hasdbt'} = 0;
|
---|
133 | } else {
|
---|
134 | if ($gli) {
|
---|
135 | print STDERR "<ProcessingError n='$file' r='Does not seem to be a Foxbase file'>\n";
|
---|
136 | }
|
---|
137 | print STDERR "FOXPlug:read - $fullname doesn't seem to be a Foxbase file\n";
|
---|
138 | return -1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | # read in the field description
|
---|
142 | $dbf{'numfields'} = 0;
|
---|
143 | $dbf{'fieldinfo'} = [];
|
---|
144 | while (read (FOXBASEIN, $temp, 1) > 0) {
|
---|
145 | last if ($temp eq "\x0d");
|
---|
146 | last if (read (FOXBASEIN, $temp, 31, 1) < 31);
|
---|
147 |
|
---|
148 | my %field = ();
|
---|
149 | $field{'name'} = $self->extracttext($temp, 11);
|
---|
150 | ($field{'type'}, $field{'pos'}, $field{'len'}, $field{'dp'})
|
---|
151 | = unpack ("x11a1VCC", $temp);
|
---|
152 |
|
---|
153 | push (@{$dbf{'fieldinfo'}}, \%field);
|
---|
154 |
|
---|
155 | $dbf{'numfields'} ++;
|
---|
156 | }
|
---|
157 |
|
---|
158 | # open the dbt file if we need to
|
---|
159 | my $dbtfullname = $fullname;
|
---|
160 | if ($fullname =~ /f$/) {
|
---|
161 | $dbtfullname =~ s/f$/t/;
|
---|
162 | } else {
|
---|
163 | $dbtfullname =~ s/F$/T/;
|
---|
164 | }
|
---|
165 | if ($dbf{'hasdbt'} && !open (DBTIN, $dbtfullname)) {
|
---|
166 | if ($gli) {
|
---|
167 | print STDERR "<ProcessingError n='$file' r='Could not read $dbtfullname'>\n";
|
---|
168 | }
|
---|
169 | print STDERR "FOXPlug::read - couldn't read $dbtfullname\n";
|
---|
170 | close (FOXBASEIN);
|
---|
171 | return -1;
|
---|
172 | }
|
---|
173 |
|
---|
174 | # read in and process each record in the database
|
---|
175 | my $numrecords = 0;
|
---|
176 | while (($numrecords < $dbf{'numrecords'}) &&
|
---|
177 | (read (FOXBASEIN, $temp, $dbf{'recordlen'}) == $dbf{'recordlen'})) {
|
---|
178 |
|
---|
179 | # create a new record
|
---|
180 | my $record = [];
|
---|
181 |
|
---|
182 | foreach my $field (@{$dbf{'fieldinfo'}}) {
|
---|
183 | my $fieldvalue = "";
|
---|
184 |
|
---|
185 | if ($field->{'type'} eq "M" && $dbf{'hasdbt'}) {
|
---|
186 | # a memo field, look up this field in the dbt file
|
---|
187 | my $seekpos = substr ($temp, $field->{'pos'}, $field->{'len'});
|
---|
188 |
|
---|
189 | $seekpos =~ s/^\s*//;
|
---|
190 | $seekpos = 0 unless $seekpos =~ /^\d+$/;
|
---|
191 |
|
---|
192 | $seekpos = $seekpos * 512;
|
---|
193 |
|
---|
194 | if ($seekpos == 0) {
|
---|
195 | # there is no memo field
|
---|
196 |
|
---|
197 | } elsif (seek (DBTIN, $seekpos, 0)) {
|
---|
198 | while (read (DBTIN, $fieldvalue, 512, length($fieldvalue)) > 0) {
|
---|
199 | last if ($fieldvalue =~ /\cZ/);
|
---|
200 | }
|
---|
201 |
|
---|
202 | # remove everything after the control-Z
|
---|
203 | substr($fieldvalue, index($fieldvalue, "\cZ")) = "";
|
---|
204 |
|
---|
205 | } else {
|
---|
206 | print STDERR "\nERROR - seek (to $seekpos) failed\n";
|
---|
207 | }
|
---|
208 |
|
---|
209 | } else {
|
---|
210 | # a normal field
|
---|
211 | $fieldvalue = substr ($temp, $field->{'pos'}, $field->{'len'});
|
---|
212 | }
|
---|
213 |
|
---|
214 | push (@$record, {%$field, 'value'=>$fieldvalue});
|
---|
215 | }
|
---|
216 |
|
---|
217 | # process this record
|
---|
218 | $self->process_record ($pluginfo, $base_dir, $file, $metadata, $processor,
|
---|
219 | $numrecords, $record);
|
---|
220 |
|
---|
221 | # finished another record...
|
---|
222 | $numrecords++;
|
---|
223 | }
|
---|
224 |
|
---|
225 | # close the dbt file if we need to
|
---|
226 | if ($dbf{'hasdbt'}) {
|
---|
227 | close (DBTIN);
|
---|
228 | }
|
---|
229 |
|
---|
230 | # close the dbf file
|
---|
231 | close (FOXBASEIN);
|
---|
232 |
|
---|
233 | # finished processing
|
---|
234 | return 1;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | # will extract a string from some larger string, making it
|
---|
239 | # conform to a number of constraints
|
---|
240 | sub extracttext {
|
---|
241 | my $self = shift (@_);
|
---|
242 | my ($text, $maxlen, $offset, $stopstr) = @_;
|
---|
243 | $offset = 0 unless defined $offset;
|
---|
244 | $stopstr = "\x00" unless defined $stopstr;
|
---|
245 |
|
---|
246 | # decide where the string finishes
|
---|
247 | my $end = index ($text, $stopstr, $offset);
|
---|
248 | $end = length ($text) if $end < 0;
|
---|
249 | $end = $offset+$maxlen if (defined $maxlen) && ($end-$offset > $maxlen);
|
---|
250 |
|
---|
251 | return "" if ($end <= $offset);
|
---|
252 | return substr ($text, $offset, $end-$offset);
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | # process_record should be overriden for a particular type
|
---|
257 | # of database. This default version outputs an html document
|
---|
258 | # containing all the fields in the record as a table.
|
---|
259 | # It also assumes that the text is in utf-8.
|
---|
260 | sub process_record {
|
---|
261 | my $self = shift (@_);
|
---|
262 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $numrecords, $record) = @_;
|
---|
263 |
|
---|
264 | # create a new document
|
---|
265 | my $doc_obj = new doc ($file, "indexed_doc");
|
---|
266 | $doc_obj->set_OIDtype ($processor->{'OIDtype'});
|
---|
267 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
|
---|
268 | my $section = $doc_obj->get_top_section();
|
---|
269 |
|
---|
270 | $doc_obj->add_metadata($section, "FileFormat", "FOX");
|
---|
271 | $doc_obj->add_metadata($section, "FileSize", (-s $file));
|
---|
272 |
|
---|
273 | # start of document
|
---|
274 | $doc_obj->add_utf8_text($section, "<table>\n");
|
---|
275 |
|
---|
276 | # add each field
|
---|
277 | foreach my $field (@$record) {
|
---|
278 | if (defined ($field->{'name'}) && defined ($field->{'value'})) {
|
---|
279 | $doc_obj->add_utf8_text($section, " <tr>\n");
|
---|
280 | $doc_obj->add_utf8_text($section, " <td>$field->{'name'}</td>\n");
|
---|
281 | $doc_obj->add_utf8_text($section, " <td>$field->{'value'}</td>\n");
|
---|
282 | $doc_obj->add_utf8_text($section, " </tr>\n");
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | # end of document
|
---|
287 | $doc_obj->add_utf8_text($section, "</table>\n");
|
---|
288 |
|
---|
289 | # add an object id
|
---|
290 | $doc_obj->set_OID();
|
---|
291 |
|
---|
292 | # process the document
|
---|
293 | $processor->process($doc_obj);
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | 1;
|
---|