source: gsdl/trunk/perllib/plugins/OpenDocumentPlugin.pm@ 16955

Last change on this file since 16955 was 16955, checked in by ak19, 12 years ago

Associated file names are url-encoded

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