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

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

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 5.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 return 0 if $filename =~ /\.(pdf|html?|jpe?g)$/i;
61 return undef unless $filename =~ /meta\.xml$/;
62
63 # create a new document
64 my $doc_obj = new doc ($filename, "indexed_doc");
65 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
66 my $topsection = $doc_obj->get_top_section();
67
68 # process the meta.xml file and set top level metadata
69 my $parser = new XML::Parser('Style' => 'Stream');
70 $self->{'section_metadata'} = {};
71 $self->{'Page'} = "TopLevel";
72 $parser->parsefile($filename);
73 foreach my $key (keys %{$self->{'section_metadata'}->{'TopLevel'}}) {
74 $doc_obj->add_utf8_metadata ($topsection, $key, $self->{'section_metadata'}->{'TopLevel'}->{$key});
75 }
76
77 # read in directory and process individual files
78 my $dir = File::Basename::dirname($filename);
79
80 opendir(DIR, $dir) || die;
81 my @files = readdir DIR;
82 closedir DIR;
83
84
85 # we rely on the files being named in such a way that they'll be read
86 # in the correct order
87 my $count = 1;
88 foreach my $thisfile (@files) {
89 if ($thisfile =~ /^(.*?)\.html?$/i) {
90 my $filesuf = $1;
91 $thisfile = &util::filename_cat($dir, $thisfile);
92 my ($language, $encoding) = $self->textcat_get_language_encoding ($thisfile);
93 # read in file ($text will be in utf8)
94 my $text = "";
95 $self->read_file ($thisfile, $encoding, $language, \$text);
96 if (!length ($text)) {
97 die "$thisfile has no text\n";
98 }
99
100 my $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
101
102 # process HTML file with HTMLPlug
103 $self->process_section (\$text, '', $thisfile, $doc_obj, $cursection);
104
105 # associate PDF file with doc object
106 my $pdffile = $thisfile;
107 $pdffile =~ s/\.html?$/\.pdf/;
108 die "no PDF file for $thisfile" unless -e $pdffile;
109 $doc_obj->associate_file($pdffile, "doc$count.pdf", undef, $cursection);
110 my $doclink = "<a href=\"_httpcollection_/index/assoc/[parent(Top):archivedir]/doc$count.pdf\">";
111# $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
112 $doc_obj->add_utf8_metadata ($cursection, "Title", $count);
113
114 # add any section level metadata we have for this page (set from within the meta.xml file)
115# if (defined ($self->{'section_metadata'}->{$filesuf})) {
116# foreach my $key (keys %{$self->{'section_metadata'}->{$filesuf}}) {
117# $doc_obj->add_utf8_metadata ($cursection, $key, $self->{'section_metadata'}->{$filesuf}->{$key});
118# }
119# }
120
121 $count ++;
122 }
123 }
124
125 # add an OID
126 $doc_obj->set_OID();
127
128 # process the document
129 $processor->process($doc_obj);
130
131 $self->{'num_processed'} ++;
132
133 return 1; # processed the file
134}
135
136
137sub StartTag {
138 my ($expat, $element) = @_;
139
140 if ($element eq "Page") {
141 $self->{'Page'} = $_{'filename'};
142
143 } elsif ($element eq "Metadata") {
144 if (!defined $self->{'section_metadata'}->{$self->{'Page'}}) {
145 $self->{'section_metadata'}->{$self->{'Page'}} = {};
146 }
147 $self->{'metadata_name'} = $_{'name'};
148 $self->{'section_metadata'}->{$self->{'Page'}}->{$_{'name'}} = "";
149 }
150}
151
152sub EndTag {
153 my ($expat, $element) = @_;
154
155 if ($element eq "Page") {
156 $self->{'Page'} eq "TopLevel";
157
158 } elsif ($element eq "Metadata") {
159 $self->{'metadata_name'} = "";
160 }
161}
162
163sub Text {
164 if ($self->{'metadata_name'} ne "") {
165 $self->{'section_metadata'}->{$self->{'Page'}}->{$self->{'metadata_name'}} .= $_;
166 }
167}
168
1691;
Note: See TracBrowser for help on using the repository browser.