1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # XMLPlug.pm -- base class for XML plugins
|
---|
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) 2001 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 | package XMLPlug;
|
---|
27 |
|
---|
28 | use BasPlug;
|
---|
29 | use doc;
|
---|
30 |
|
---|
31 | sub BEGIN {
|
---|
32 | @ISA = ('BasPlug');
|
---|
33 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
34 | }
|
---|
35 |
|
---|
36 | use XML::Parser;
|
---|
37 |
|
---|
38 | my ($self);
|
---|
39 | sub new {
|
---|
40 | my $class = shift (@_);
|
---|
41 |
|
---|
42 | # $self is global for use within subroutines called by XML::Parser
|
---|
43 | $self = new BasPlug ($class, @_);
|
---|
44 |
|
---|
45 | my $parser = new XML::Parser('Style' => 'Stream',
|
---|
46 | 'Handlers' => {'Char' => \&Char,
|
---|
47 | 'XMLDecl' => \&XMLDecl,
|
---|
48 | 'Entity' => \&Entity,
|
---|
49 | 'Doctype' => \&Doctype,
|
---|
50 | 'Default' => \&Default
|
---|
51 | }
|
---|
52 | );
|
---|
53 | $self->{'parser'} = $parser;
|
---|
54 |
|
---|
55 | return bless $self, $class;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | sub read {
|
---|
60 | # this must be global!
|
---|
61 | $self = shift (@_);
|
---|
62 |
|
---|
63 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
64 |
|
---|
65 | my $filename = $file;
|
---|
66 | $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
|
---|
67 |
|
---|
68 | if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
|
---|
69 | $self->{'num_blocked'} ++;
|
---|
70 | return 0;
|
---|
71 | }
|
---|
72 | if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
|
---|
73 | return undef;
|
---|
74 | }
|
---|
75 | $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
|
---|
76 | $self->{'file'} = $file;
|
---|
77 | $self->{'filename'} = $filename;
|
---|
78 | $self->{'processor'} = $processor;
|
---|
79 | $self->{'metadata'} = $metadata;
|
---|
80 |
|
---|
81 | eval {
|
---|
82 | $self->{'parser'}->parsefile($filename);
|
---|
83 | };
|
---|
84 | if ($@) {
|
---|
85 |
|
---|
86 | # parsefile may either croak somewhere in XML::Parser (e.g. because
|
---|
87 | # the document is not well formed) or die somewhere in XMLPlug or a
|
---|
88 | # derived plugin (e.g. because we're attempting to process a
|
---|
89 | # document whose DOCTYPE is not meant for this plugin). For the
|
---|
90 | # first case we'll print a warning and continue, for the second
|
---|
91 | # we'll just continue quietly
|
---|
92 |
|
---|
93 | my ($msg) = $@ =~ /Carp::croak\(\'(.*?)\'\)/;
|
---|
94 | if (defined $msg) {
|
---|
95 | my $outhandle = $self->{'outhandle'};
|
---|
96 | my $plugin_name = ref ($self);
|
---|
97 | print $outhandle "$plugin_name failed to process $file ($msg)\n";
|
---|
98 | }
|
---|
99 | return undef;
|
---|
100 | }
|
---|
101 |
|
---|
102 | return 1; # processed the file
|
---|
103 | }
|
---|
104 |
|
---|
105 | sub get_default_process_exp {
|
---|
106 | my $self = shift (@_);
|
---|
107 |
|
---|
108 | return q^(?i)\.xml$^;
|
---|
109 | }
|
---|
110 |
|
---|
111 | sub StartDocument {$self->xml_start_document(@_);}
|
---|
112 | sub XMLDecl {$self->xml_xmldecl(@_);}
|
---|
113 | sub Entity {$self->xml_entity(@_);}
|
---|
114 | sub Doctype {$self->xml_doctype(@_);}
|
---|
115 | sub StartTag {$self->xml_start_tag(@_);}
|
---|
116 | sub EndTag {$self->xml_end_tag(@_);}
|
---|
117 | sub Text {$self->xml_text(@_);}
|
---|
118 | sub PI {$self->xml_pi(@_);}
|
---|
119 | sub EndDocument {$self->xml_end_document(@_);}
|
---|
120 | sub Default {$self->xml_default(@_);}
|
---|
121 |
|
---|
122 | # This Char function overrides the one in XML::Parser::Stream to overcome a
|
---|
123 | # problem where $expat->{Text} is treated as the return value, slowing
|
---|
124 | # things down significantly in some cases.
|
---|
125 | sub Char {
|
---|
126 | $_[0]->{'Text'} .= $_[1];
|
---|
127 | return undef;
|
---|
128 | }
|
---|
129 |
|
---|
130 | # Called at the beginning of the XML document.
|
---|
131 | sub xml_start_document {
|
---|
132 | my $self = shift(@_);
|
---|
133 | my ($expat) = @_;
|
---|
134 |
|
---|
135 | $self->open_document();
|
---|
136 | }
|
---|
137 |
|
---|
138 | # Called for XML declarations
|
---|
139 | sub xml_xmldecl {
|
---|
140 | my $self = shift(@_);
|
---|
141 | my ($expat, $version, $encoding, $standalone) = @_;
|
---|
142 | }
|
---|
143 |
|
---|
144 | # Called for XML entities
|
---|
145 | sub xml_entity {
|
---|
146 | my $self = shift(@_);
|
---|
147 | my ($expat, $name, $val, $sysid, $pubid, $ndata) = @_;
|
---|
148 | }
|
---|
149 |
|
---|
150 | # Called for DOCTYPE declarations - use die to bail out if this doctype
|
---|
151 | # is not meant for this plugin
|
---|
152 | sub xml_doctype {
|
---|
153 | my $self = shift(@_);
|
---|
154 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
155 | die "XMLPlug Cannot process XML document with DOCTYPE of $name";
|
---|
156 | }
|
---|
157 |
|
---|
158 | # Called for every start tag. The $_ variable will contain a copy of the
|
---|
159 | # tag and the %_ variable will contain the element's attributes.
|
---|
160 | sub xml_start_tag {
|
---|
161 | my $self = shift(@_);
|
---|
162 | my ($expat, $element) = @_;
|
---|
163 | }
|
---|
164 |
|
---|
165 | # Called for every end tag. The $_ variable will contain a copy of the tag.
|
---|
166 | sub xml_end_tag {
|
---|
167 | my $self = shift(@_);
|
---|
168 | my ($expat, $element) = @_;
|
---|
169 | }
|
---|
170 |
|
---|
171 | # Called just before start or end tags with accumulated non-markup text in
|
---|
172 | # the $_ variable.
|
---|
173 | sub xml_text {
|
---|
174 | my $self = shift(@_);
|
---|
175 | my ($expat) = @_;
|
---|
176 | }
|
---|
177 |
|
---|
178 | # Called for processing instructions. The $_ variable will contain a copy
|
---|
179 | # of the pi.
|
---|
180 | sub xml_pi {
|
---|
181 | my $self = shift(@_);
|
---|
182 | my ($expat, $target, $data) = @_;
|
---|
183 | }
|
---|
184 |
|
---|
185 | # Called at the end of the XML document.
|
---|
186 | sub xml_end_document {
|
---|
187 | my $self = shift(@_);
|
---|
188 | my ($expat) = @_;
|
---|
189 |
|
---|
190 | $self->close_document();
|
---|
191 | }
|
---|
192 |
|
---|
193 | # Called for any characters not handled by the above functions.
|
---|
194 | sub xml_default {
|
---|
195 | my $self = shift(@_);
|
---|
196 | my ($expat, $text) = @_;
|
---|
197 | }
|
---|
198 |
|
---|
199 | sub open_document {
|
---|
200 | my $self = shift(@_);
|
---|
201 |
|
---|
202 | # create a new document
|
---|
203 | $self->{'doc_obj'} = new doc ($self->{'filename'}, "indexed_doc");
|
---|
204 | $self->{'doc_obj'}->set_OIDtype ($self->{'processor'}->{'OIDtype'});
|
---|
205 | }
|
---|
206 |
|
---|
207 | sub close_document {
|
---|
208 | my $self = shift(@_);
|
---|
209 |
|
---|
210 | # include any metadata passed in from previous plugins
|
---|
211 | # note that this metadata is associated with the top level section
|
---|
212 | $self->extra_metadata ($self->{'doc_obj'},
|
---|
213 | $self->{'doc_obj'}->get_top_section(),
|
---|
214 | $self->{'metadata'});
|
---|
215 |
|
---|
216 | # do any automatic metadata extraction
|
---|
217 | $self->auto_extract_metadata ($self->{'doc_obj'});
|
---|
218 |
|
---|
219 | # add an OID
|
---|
220 | $self->{'doc_obj'}->set_OID();
|
---|
221 |
|
---|
222 | # process the document
|
---|
223 | $self->{'processor'}->process($self->{'doc_obj'});
|
---|
224 |
|
---|
225 | $self->{'num_processed'} ++;
|
---|
226 | }
|
---|
227 |
|
---|
228 | 1;
|
---|
229 |
|
---|