source: trunk/protemix/perllib/plugins/ProtemixPlug.pm@ 3189

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

* empty log message *

  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1###########################################################################
2#
3# ProtemixPlug.pm --
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2002 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27package ProtemixPlug;
28
29use HTMLPlug;
30use util;
31
32sub BEGIN {
33 @ISA = ('HTMLPlug');
34}
35
36use XML::Parser;
37
38sub new {
39 my $class = shift (@_);
40
41 # $self must be global for XML parser routines
42 $self = new HTMLPlug ($class, @_);
43
44 $self->{'no_metadata'} = 1;
45 $self->{'nolinks'} = 1;
46 $self->{'section_metadata'} = {};
47 $self->{'Page'} = "TopLevel";
48 $self->{'metadata_name'} = "";
49
50 return bless $self, $class;
51}
52
53sub read {
54 my $self = shift (@_);
55 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
56
57 my $filename = $file;
58 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
59
60 my $basename = File::Basename::basename($filename);
61 return 0 if $basename =~ /^\d+(-(all|\d+))?\.(html?|pdf)/;
62 return undef unless $filename =~ /meta\.xml$/;
63
64 # create a new document
65 my $doc_obj = new doc ($filename, "indexed_doc");
66 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
67 my $topsection = $doc_obj->get_top_section();
68
69 # process the meta.xml file and set top level metadata
70 my $parser = new XML::Parser('Style' => 'Stream');
71 $self->{'section_metadata'} = {};
72 $self->{'Page'} = "TopLevel";
73 $parser->parsefile($filename);
74 foreach my $key (keys %{$self->{'section_metadata'}->{'TopLevel'}}) {
75 $doc_obj->add_utf8_metadata ($topsection, $key, $self->{'section_metadata'}->{'TopLevel'}->{$key});
76 }
77
78 my $dir = File::Basename::dirname($filename);
79
80 my $outhandle = $self->{'outhandle'};
81 print $outhandle "ProtemixPlug: processing $dir\n";
82
83
84 # associate article level pdf file
85 my ($pdffile) = $dir =~ /([^\/\\]+)$/;
86 $pdffile = &util::filename_cat($dir, $pdffile);
87 $pdffile .= "-all.pdf";
88 die "$pdffile does not exist" unless -e $pdffile;
89 $doc_obj->associate_file($pdffile, "article.pdf", undef, $topsection);
90 $doc_obj->add_utf8_metadata ($cursection, "pdf", "article.pdf");
91
92 # read in directory and process individual files
93 opendir(DIR, $dir) || die;
94 my @files = readdir DIR;
95 closedir DIR;
96
97
98 # we rely on the files being named in such a way that they'll be read
99 # in the correct order
100 my $count = 1;
101 foreach my $thisfile (@files) {
102 if ($thisfile =~ /^(.*?)\.html?$/i) {
103 my $filesuf = $1;
104 $thisfile = &util::filename_cat($dir, $thisfile);
105 my ($language, $encoding) = $self->textcat_get_language_encoding ($thisfile);
106 # read in file ($text will be in utf8)
107 my $text = "";
108 $self->read_file ($thisfile, $encoding, $language, \$text);
109 if (!length ($text)) {
110 print STDERR "ProtemixPlug: Warning: $thisfile has no text\n";
111 }
112
113 my $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
114
115 # process HTML file with HTMLPlug
116 $self->process_section (\$text, '', $thisfile, $doc_obj, $cursection);
117
118 # associate PDF page level pdf file
119 my $pdffile = $thisfile;
120 $pdffile =~ s/\.html?$/\.pdf/;
121 if (-e $pdffile) {
122 $doc_obj->associate_file($pdffile, "page$count.pdf", undef, $cursection);
123 $doc_obj->add_utf8_metadata ($cursection, "pdf", "page$count.pdf");
124 $doc_obj->add_utf8_metadata ($cursection, "Title", $count);
125 } else {
126 print STDERR "ProtemixPlug: Warning: no pdf file for $thisfile\n";
127 }
128
129 # add any section level metadata we have for this page (set from within the meta.xml file)
130
131# currently commented out as we're not using Class1, Class2, and Class3 metadata yet
132# if (defined ($self->{'section_metadata'}->{$filesuf})) {
133# foreach my $key (keys %{$self->{'section_metadata'}->{$filesuf}}) {
134# $doc_obj->add_utf8_metadata ($cursection, $key, $self->{'section_metadata'}->{$filesuf}->{$key});
135# }
136# }
137
138 $count ++;
139 }
140 }
141
142 # add an OID
143 $doc_obj->set_OID();
144
145 # process the document
146 $processor->process($doc_obj);
147
148 $self->{'num_processed'} ++;
149
150 return 1; # processed the file
151}
152
153
154sub StartTag {
155 my ($expat, $element) = @_;
156
157 if ($element eq "Page") {
158 $self->{'Page'} = $_{'filename'};
159
160 } elsif ($element eq "Metadata") {
161 if (!defined $self->{'section_metadata'}->{$self->{'Page'}}) {
162 $self->{'section_metadata'}->{$self->{'Page'}} = {};
163 }
164 $self->{'metadata_name'} = $_{'name'};
165 $self->{'section_metadata'}->{$self->{'Page'}}->{$_{'name'}} = "";
166 }
167}
168
169sub EndTag {
170 my ($expat, $element) = @_;
171
172 if ($element eq "Page") {
173 $self->{'Page'} eq "TopLevel";
174
175 } elsif ($element eq "Metadata") {
176 $self->{'metadata_name'} = "";
177 }
178}
179
180sub Text {
181 if ($self->{'metadata_name'} ne "") {
182 $self->{'section_metadata'}->{$self->{'Page'}}->{$self->{'metadata_name'}} .= $_;
183 }
184}
185
186# don't want to convert character entities for now as mgpp appears to be broken
187sub read_file {
188 my ($self, $filename, $encoding, $language, $textref) = @_;
189
190 &BasPlug::read_file($self, $filename, $encoding, $language, $textref);
191
192 # Convert entities to their UTF8 equivalents
193# $$textref =~ s/&(lt|gt|amp|quot);/&z$1;/go;
194# $$textref =~ s/&([^;]+);/&ghtml::getcharequiv($1,1)/gseo;
195# $$textref =~ s/&z(lt|gt|amp|quot);/&$1;/go;
196}
197
198
1991;
Note: See TracBrowser for help on using the repository browser.