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

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

new OpenDocument plugin written by Reuben Evans as a 517 project

  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 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 my $filename = $file;
151 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
152
153 if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
154 $self->{'num_blocked'} ++;
155 return 0;
156 }
157 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
158 return undef;
159 }
160
161 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
162 $self->{'file'} = $file;
163 $self->{'filename'} = $filename;
164 $self->{'processor'} = $processor;
165 $self->{'metadata'} = $metadata;
166 $self->{'gli'} = $gli;
167
168
169 eval{
170 my ($file_only) = $file =~ /([^\\\/]*)$/;
171 my $tmpdir = &util::get_tmp_filename ();
172 &util::mk_all_dir ($tmpdir);
173
174 $self->open_document();
175
176 # save current working directory
177 my $cwd = getcwd();
178 chdir ($tmpdir) || die "Unable to change to $tmpdir";
179 &util::cp ($filename, $tmpdir);
180
181 $self->unzip ("\"$file_only\"");
182
183 foreach my $xmlFile (@OpenDocumentPlug::filesProcess) {
184 print getcwd();
185 print $xmlFile;
186 $self->parse_file($xmlFile);
187 }
188
189 $self->close_document($filename,$file_only);
190
191 chdir ($cwd) || die "Unable to change back to $cwd";
192 &util::rm_r ($tmpdir);
193
194 };
195
196 if ($@) {
197
198 # parsefile may either croak somewhere in XML::Parser (e.g. because
199 # the document is not well formed) or die somewhere in XMLPlug or a
200 # derived plugin (e.g. because we're attempting to process a
201 # document whose DOCTYPE is not meant for this plugin). For the
202 # first case we'll print a warning and continue, for the second
203 # we'll just continue quietly
204
205 print STDERR "**** Error is: $@\n";
206
207 my ($msg) = $@ =~ /Carp::croak\(\'(.*?)\'\)/;
208 if (defined $msg) {
209 my $outhandle = $self->{'outhandle'};
210 my $plugin_name = ref ($self);
211 print $outhandle "$plugin_name failed to process $file ($msg)\n";
212 }
213
214 # reset ourself for the next document
215 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
216 return -1; # error during processing
217 }
218
219 return 1;
220}
221
222sub unzip {
223 my $self = shift (@_);
224 my ($file) = @_;
225
226 system ("unzip $file");
227 &util::rm ($file) if -e $file;
228}
229
230sub close_document() {
231 my $self = shift(@_);
232 my ($filename,$file_only) = @_;
233
234 my $doc_obj = $self->{'doc_obj'};
235
236 my $mimetype = $self->get_mimetype();
237
238 $doc_obj->associate_file($filename, $file_only, $mimetype, "");
239 $doc_obj->associate_file("Thumbnails/thumbnail.png", "thumbnail.png", "image/png", "");
240 my $doc_ext = $filename;
241 $doc_ext =~ s/.*\.od(.)/od$1/;
242
243 # We use set instead of add here because we only want one value
244 $doc_obj->set_utf8_metadata_element("", "FileFormat", "Open Document");
245
246 #setup to doclink thingi
247 my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/$file_only\">";
248 $doc_obj->add_utf8_metadata ("", "srclink", $doclink);
249 $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\">");
250 $doc_obj->add_utf8_metadata ("", "/srclink", "</a>");
251 $doc_obj->add_utf8_metadata ("", "Source", &ghtml::dmsafe($file_only));
252 $doc_obj->set_utf8_metadata_element("", "FileSize", (-s $filename));
253
254 # add a Title if none has been found yet
255 $self->title_fallback($doc_obj,"",$file_only);
256
257 # add an OID
258 $doc_obj->set_OID();
259
260 $doc_obj->add_utf8_metadata("", "Plugin", "$self->{'plugin_type'}");
261
262 # process the document
263 $self->{'processor'}->process($doc_obj);
264
265 $self->{'num_processed'} ++;
266 return 1;
267}
268
269sub get_mimetype(){
270 my $filename = "mimetype";
271 if (!open (FILEIN,"<$filename")){
272 print STDERR "Warning: unable to open the $filename\n";
273 return "Unknown OpenDocument Format";
274 }
275 else {
276 my $text = "";
277 while (defined (my $line = <FILEIN>)) {
278 $text .= $line;
279 }
280 return $text;
281 }
282}
2831;
284
285
286
287
Note: See TracBrowser for help on using the repository browser.