########################################################################### # # ProtemixPlug.pm -- # # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 2002 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### package ProtemixPlug; use HTMLPlug; use util; sub BEGIN { @ISA = ('HTMLPlug'); } use XML::Parser; sub new { my $class = shift (@_); # $self must be global for XML parser routines $self = new HTMLPlug ($class, @_); $self->{'no_metadata'} = 1; $self->{'nolinks'} = 1; $self->{'section_metadata'} = {}; $self->{'Page'} = "TopLevel"; $self->{'metadata_name'} = ""; return bless $self, $class; } sub read { my $self = shift (@_); my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_; my $filename = $file; $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/; return 0 if $filename =~ /\.(pdf|html?|jpe?g)$/i; return undef unless $filename =~ /meta\.xml$/; # create a new document my $doc_obj = new doc ($filename, "indexed_doc"); $doc_obj->set_OIDtype ($processor->{'OIDtype'}); my $topsection = $doc_obj->get_top_section(); # process the meta.xml file and set top level metadata my $parser = new XML::Parser('Style' => 'Stream'); $self->{'section_metadata'} = {}; $self->{'Page'} = "TopLevel"; $parser->parsefile($filename); foreach my $key (keys %{$self->{'section_metadata'}->{'TopLevel'}}) { $doc_obj->add_utf8_metadata ($topsection, $key, $self->{'section_metadata'}->{'TopLevel'}->{$key}); } # read in directory and process individual files my $dir = File::Basename::dirname($filename); opendir(DIR, $dir) || die; my @files = readdir DIR; closedir DIR; # we rely on the files being named in such a way that they'll be read # in the correct order my $count = 1; foreach my $thisfile (@files) { if ($thisfile =~ /^(.*?)\.html?$/i) { my $filesuf = $1; $thisfile = &util::filename_cat($dir, $thisfile); my ($language, $encoding) = $self->textcat_get_language_encoding ($thisfile); # read in file ($text will be in utf8) my $text = ""; $self->read_file ($thisfile, $encoding, $language, \$text); if (!length ($text)) { die "$thisfile has no text\n"; } my $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection)); # process HTML file with HTMLPlug $self->process_section (\$text, '', $thisfile, $doc_obj, $cursection); # associate PDF file with doc object my $pdffile = $thisfile; $pdffile =~ s/\.html?$/\.pdf/; die "no PDF file for $thisfile" unless -e $pdffile; $doc_obj->associate_file($pdffile, "doc$count.pdf", undef, $cursection); my $doclink = ""; # $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink); $doc_obj->add_utf8_metadata ($cursection, "Title", $count); # add any section level metadata we have for this page (set from within the meta.xml file) # if (defined ($self->{'section_metadata'}->{$filesuf})) { # foreach my $key (keys %{$self->{'section_metadata'}->{$filesuf}}) { # $doc_obj->add_utf8_metadata ($cursection, $key, $self->{'section_metadata'}->{$filesuf}->{$key}); # } # } $count ++; } } # add an OID $doc_obj->set_OID(); # process the document $processor->process($doc_obj); $self->{'num_processed'} ++; return 1; # processed the file } sub StartTag { my ($expat, $element) = @_; if ($element eq "Page") { $self->{'Page'} = $_{'filename'}; } elsif ($element eq "Metadata") { if (!defined $self->{'section_metadata'}->{$self->{'Page'}}) { $self->{'section_metadata'}->{$self->{'Page'}} = {}; } $self->{'metadata_name'} = $_{'name'}; $self->{'section_metadata'}->{$self->{'Page'}}->{$_{'name'}} = ""; } } sub EndTag { my ($expat, $element) = @_; if ($element eq "Page") { $self->{'Page'} eq "TopLevel"; } elsif ($element eq "Metadata") { $self->{'metadata_name'} = ""; } } sub Text { if ($self->{'metadata_name'} ne "") { $self->{'section_metadata'}->{$self->{'Page'}}->{$self->{'metadata_name'}} .= $_; } } 1;