[10997] | 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 | package OpenDocumentPlug;
|
---|
| 26 |
|
---|
| 27 | use strict;
|
---|
| 28 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 29 |
|
---|
| 30 | use XMLPlug;
|
---|
| 31 | use XML::XPath;
|
---|
| 32 | use XML::XPath::XMLParser;
|
---|
| 33 | use Cwd;
|
---|
| 34 | use util;
|
---|
| 35 | use ghtml;
|
---|
| 36 |
|
---|
| 37 | sub BEGIN {
|
---|
| 38 | @OpenDocumentPlug::ISA = ('XMLPlug');
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | #our @filesAssoc = ();
|
---|
| 43 | our @filesProcess = ( "content.xml" , "meta.xml" );
|
---|
| 44 | #XML plug has this so we need it too
|
---|
| 45 | our ($self);
|
---|
| 46 |
|
---|
| 47 | my $arguments = [
|
---|
| 48 | { 'name' => "process_exp",
|
---|
| 49 | 'desc' => "{BasPlug.process_exp}",
|
---|
| 50 | 'type' => "regexp",
|
---|
| 51 | 'deft' => &get_default_process_exp() }
|
---|
| 52 | ];
|
---|
| 53 | my $options = { 'name' => "OpenDocumentPlug",
|
---|
| 54 | 'desc' => "{OpenDocumentPlug.desc}",
|
---|
| 55 | 'abstract' => "no",
|
---|
| 56 | 'inherits' => "yes" };
|
---|
| 57 |
|
---|
| 58 | sub get_default_process_exp { return q^(?i)\.o(?:d|t)(?:t|s|p|g)$^; }
|
---|
| 59 |
|
---|
| 60 | sub 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 |
|
---|
| 77 | sub 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.
|
---|
| 85 | sub 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 |
|
---|
| 103 | sub 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 |
|
---|
| 130 | sub 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
|
---|
| 141 | sub xml_start_document {}
|
---|
| 142 | sub xml_end_document {}
|
---|
| 143 |
|
---|
| 144 | sub 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 |
|
---|
[11090] | 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));
|
---|
[10997] | 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 |
|
---|
| 213 | sub unzip {
|
---|
| 214 | my $self = shift (@_);
|
---|
| 215 | my ($file) = @_;
|
---|
| 216 |
|
---|
| 217 | system ("unzip $file");
|
---|
| 218 | &util::rm ($file) if -e $file;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | sub 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
|
---|
[11834] | 238 | my $doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[archivedir]/$file_only\">";
|
---|
[10997] | 239 | $doc_obj->add_utf8_metadata ("", "srclink", $doclink);
|
---|
[11834] | 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\">");
|
---|
[10997] | 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 |
|
---|
| 260 | sub 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 | }
|
---|
| 274 | 1;
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 |
|
---|