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