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

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

* empty log message *

  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 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 die "$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 die "no PDF file for $thisfile" unless -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
126 # add any section level metadata we have for this page (set from within the meta.xml file)
127
128# currently commented out as we're not using Class1, Class2, and Class3 metadata yet
129# if (defined ($self->{'section_metadata'}->{$filesuf})) {
130# foreach my $key (keys %{$self->{'section_metadata'}->{$filesuf}}) {
131# $doc_obj->add_utf8_metadata ($cursection, $key, $self->{'section_metadata'}->{$filesuf}->{$key});
132# }
133# }
134
135 $count ++;
136 }
137 }
138
139 # add an OID
140 $doc_obj->set_OID();
141
142 # process the document
143 $processor->process($doc_obj);
144
145 $self->{'num_processed'} ++;
146
147 return 1; # processed the file
148}
149
150
151sub StartTag {
152 my ($expat, $element) = @_;
153
154 if ($element eq "Page") {
155 $self->{'Page'} = $_{'filename'};
156
157 } elsif ($element eq "Metadata") {
158 if (!defined $self->{'section_metadata'}->{$self->{'Page'}}) {
159 $self->{'section_metadata'}->{$self->{'Page'}} = {};
160 }
161 $self->{'metadata_name'} = $_{'name'};
162 $self->{'section_metadata'}->{$self->{'Page'}}->{$_{'name'}} = "";
163 }
164}
165
166sub EndTag {
167 my ($expat, $element) = @_;
168
169 if ($element eq "Page") {
170 $self->{'Page'} eq "TopLevel";
171
172 } elsif ($element eq "Metadata") {
173 $self->{'metadata_name'} = "";
174 }
175}
176
177sub Text {
178 if ($self->{'metadata_name'} ne "") {
179 $self->{'section_metadata'}->{$self->{'Page'}}->{$self->{'metadata_name'}} .= $_;
180 }
181}
182
183# don't want to convert character entities for now as mgpp appears to be broken
184sub read_file {
185 my ($self, $filename, $encoding, $language, $textref) = @_;
186
187 &BasPlug::read_file($self, $filename, $encoding, $language, $textref);
188
189 # Convert entities to their UTF8 equivalents
190# $$textref =~ s/&(lt|gt|amp|quot);/&z$1;/go;
191# $$textref =~ s/&([^;]+);/&ghtml::getcharequiv($1,1)/gseo;
192# $$textref =~ s/&z(lt|gt|amp|quot);/&$1;/go;
193}
194
195
1961;
Note: See TracBrowser for help on using the repository browser.