source: trunk/gsdl/perllib/plugins/XMLPlug.pm@ 2890

Last change on this file since 2890 was 2890, checked in by sjboddie, 22 years ago

Added xml_entity function to XMLPlug

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