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

Last change on this file since 12970 was 12169, checked in by mdewsnip, 18 years ago

Tidied up that horrible long line in the new() function of every plugin.

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