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