source: trunk/gsdl/perllib/plugins/OpenDocumentPlug.pm@ 11090

Last change on this file since 11090 was 11090, checked in by kjdon, 18 years ago

made all plugins that implement read() call read_block to check process_exp, block_exp, smart blocking, cover image blocking etc

  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1###########################################################################
2#
3# OpenDocumentPlug.pm -- The Open Document plugin
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###########################################################################
25package OpenDocumentPlug;
26
27use strict;
28no strict 'refs'; # allow filehandles to be variables and viceversa
29
30use XMLPlug;
31use XML::XPath;
32use XML::XPath::XMLParser;
33use Cwd;
34use util;
35use ghtml;
36
37sub BEGIN {
38 @OpenDocumentPlug::ISA = ('XMLPlug');
39}
40
41
42#our @filesAssoc = ();
43our @filesProcess = ( "content.xml" , "meta.xml" );
44#XML plug has this so we need it too
45our ($self);
46
47my $arguments = [
48 { 'name' => "process_exp",
49 'desc' => "{BasPlug.process_exp}",
50 'type' => "regexp",
51 'deft' => &get_default_process_exp() }
52 ];
53my $options = { 'name' => "OpenDocumentPlug",
54 'desc' => "{OpenDocumentPlug.desc}",
55 'abstract' => "no",
56 'inherits' => "yes" };
57
58sub get_default_process_exp { return q^(?i)\.o(?:d|t)(?:t|s|p|g)$^; }
59
60sub new {
61 my ($class) = shift (@_);
62 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
63 push(@$pluginlist, $class);
64
65 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
66 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
67
68 my $self = (defined $hashArgOptLists)? new XMLPlug($pluginlist,$inputargs,$hashArgOptLists): new XMLPlug($pluginlist,$inputargs);
69
70 $self->{'section'} = "";
71 $self->{'office:meta'} = "";
72
73 return bless $self, $class;
74}
75
76
77sub xml_doctype {
78 my $self = shift(@_);
79 my ($expat, $name, $sysid, $pubid, $internal) = @_;
80 die "The only valid doctype is manifest, $name is not valid" if ($name ne "manifest:manifest");
81}
82
83# Called for every start tag. The $_ variable will contain a copy of the
84# tag and the %_ variable will contain the element's attributes.
85sub xml_start_tag {
86 my $self = shift(@_);
87 my ($expat, $element) = @_;
88 my %atts = %_;
89 $self->{'office:meta'} = $element if $self->{'office:meta'} eq "Start";
90 if($element eq 'office:text') {
91 $self->{'collectedText'} = "";
92 }elsif($element eq 'office:meta') {
93 $self->{'collectedText'} = "";
94 $self->{'office:meta'} = "Start";
95 }elsif($element eq 'meta:document-statistic'){
96 foreach my $att (keys %atts) {
97 $self->{'doc_obj'}->add_utf8_metadata("",$att,$atts{$att});
98 }
99
100 }
101}
102
103sub xml_end_tag {
104 my $self = shift(@_);
105 my ($expat, $element) = @_;
106
107 if($element eq 'office:text') {
108 $self->{'doc_obj'}->add_utf8_text("",$self->{'collectedText'});
109 $self->{'collectedText'} = "";
110 }elsif($element eq $self->{'office:meta'}) {
111 if( $self->{'collectedText'} ne "") {
112 $self->{'doc_obj'}->add_utf8_metadata("",$self->{'office:meta'},$self->{'collectedText'});
113 $self->{'doc_obj'}->add_utf8_metadata("","Title",$self->{'collectedText'}) if $self->{'office:meta'} =~ m/:title$/;
114 $self->{'doc_obj'}->add_utf8_metadata("","Language",$self->{'collectedText'}) if $self->{'office:meta'} =~ m/:language$/;
115 $self->{'doc_obj'}->add_utf8_metadata("","GENERATOR",$self->{'collectedText'}) if $self->{'office:meta'} =~ m/:generator$/;
116
117 }
118 $self->{'collectedText'} = "";
119 $self->{'office:meta'} = "Start";
120 }elsif($element eq 'office:meta'){
121 $self->{'office:meta'} = "";
122 }elsif($element eq 'office:body'){
123 #some documents have text in other places that should probably be indexed if we can't find any doc text
124 if( $self->{'collectedText'} ne "" && $self->{'doc_obj'}->get_text("") eq "") {
125 $self->{'doc_obj'}->add_utf8_text("",$self->{'collectedText'});
126 }
127 }
128}
129
130sub xml_text {
131 my $self = shift(@_);
132 my ($expat) = @_;
133
134 if($_ =~ m/\w/i) {
135 $self->{'collectedText'} .= "<br/>" if $self->{'collectedText'} ne "";
136 $self->{'collectedText'} .= "$_";
137 }
138}
139
140#trap start and end document so we do not get our doc_obj closed too soon
141sub xml_start_document {}
142sub xml_end_document {}
143
144sub read {
145 # $self must be global to work with XML callback routines.
146 $self = shift (@_);
147
148 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
149
150 # check process and block exps, smart block, associate_ext etc
151 my ($block_status,$filename) = $self->read_block(@_);
152 return $block_status if ((!defined $block_status) || ($block_status==0));
153
154 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
155 $self->{'file'} = $file;
156 $self->{'filename'} = $filename;
157 $self->{'processor'} = $processor;
158 $self->{'metadata'} = $metadata;
159 $self->{'gli'} = $gli;
160
161
162 eval{
163 my ($file_only) = $file =~ /([^\\\/]*)$/;
164 my $tmpdir = &util::get_tmp_filename ();
165 &util::mk_all_dir ($tmpdir);
166
167 $self->open_document();
168
169 # save current working directory
170 my $cwd = getcwd();
171 chdir ($tmpdir) || die "Unable to change to $tmpdir";
172 &util::cp ($filename, $tmpdir);
173
174 $self->unzip ("\"$file_only\"");
175
176 foreach my $xmlFile (@OpenDocumentPlug::filesProcess) {
177 print getcwd();
178 print $xmlFile;
179 $self->parse_file($xmlFile);
180 }
181
182 $self->close_document($filename,$file_only);
183
184 chdir ($cwd) || die "Unable to change back to $cwd";
185 &util::rm_r ($tmpdir);
186
187 };
188
189 if ($@) {
190
191 # parsefile may either croak somewhere in XML::Parser (e.g. because
192 # the document is not well formed) or die somewhere in XMLPlug or a
193 # derived plugin (e.g. because we're attempting to process a
194 # document whose DOCTYPE is not meant for this plugin). For the
195 # first case we'll print a warning and continue, for the second
196 # we'll just continue quietly
197
198 print STDERR "**** Error is: $@\n";
199
200 my ($msg) = $@ =~ /Carp::croak\(\'(.*?)\'\)/;
201 if (defined $msg) {
202 my $outhandle = $self->{'outhandle'};
203 my $plugin_name = ref ($self);
204 print $outhandle "$plugin_name failed to process $file ($msg)\n";
205 }
206
207 # reset ourself for the next document
208 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
209 return -1; # error during processing
210 }
211
212 return 1;
213}
214
215sub unzip {
216 my $self = shift (@_);
217 my ($file) = @_;
218
219 system ("unzip $file");
220 &util::rm ($file) if -e $file;
221}
222
223sub close_document() {
224 my $self = shift(@_);
225 my ($filename,$file_only) = @_;
226
227 my $doc_obj = $self->{'doc_obj'};
228
229 my $mimetype = $self->get_mimetype();
230
231 $doc_obj->associate_file($filename, $file_only, $mimetype, "");
232 $doc_obj->associate_file("Thumbnails/thumbnail.png", "thumbnail.png", "image/png", "");
233 my $doc_ext = $filename;
234 $doc_ext =~ s/.*\.od(.)/od$1/;
235
236 # We use set instead of add here because we only want one value
237 $doc_obj->set_utf8_metadata_element("", "FileFormat", "Open Document");
238
239 #setup to doclink thingi
240 my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/$file_only\">";
241 $doc_obj->add_utf8_metadata ("", "srclink", $doclink);
242 $doc_obj->add_utf8_metadata ("", "srcicon", "<img border=\"0\" align=\"absmiddle\" src=\"_httpcollection_/index/assoc/[archivedir]/thumbnail.png\" alt=\"View the Open document\" title=\"View the Open document\">");
243 $doc_obj->add_utf8_metadata ("", "/srclink", "</a>");
244 $doc_obj->add_utf8_metadata ("", "Source", &ghtml::dmsafe($file_only));
245 $doc_obj->set_utf8_metadata_element("", "FileSize", (-s $filename));
246
247 # add a Title if none has been found yet
248 $self->title_fallback($doc_obj,"",$file_only);
249
250 # add an OID
251 $doc_obj->set_OID();
252
253 $doc_obj->add_utf8_metadata("", "Plugin", "$self->{'plugin_type'}");
254
255 # process the document
256 $self->{'processor'}->process($doc_obj);
257
258 $self->{'num_processed'} ++;
259 return 1;
260}
261
262sub get_mimetype(){
263 my $filename = "mimetype";
264 if (!open (FILEIN,"<$filename")){
265 print STDERR "Warning: unable to open the $filename\n";
266 return "Unknown OpenDocument Format";
267 }
268 else {
269 my $text = "";
270 while (defined (my $line = <FILEIN>)) {
271 $text .= $line;
272 }
273 return $text;
274 }
275}
2761;
277
278
279
280
Note: See TracBrowser for help on using the repository browser.