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

Last change on this file since 15179 was 15179, checked in by kjdon, 16 years ago

needed to add extra_metadata() call in close_document so that can get metadata from metadata.xml files

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