source: main/trunk/greenstone2/perllib/plugins/OpenDocumentPlugin.pm@ 21764

Last change on this file since 21764 was 21764, checked in by kjdon, 14 years ago

fixed up all my copy and paste errors. doh.

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