[7901] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[17028] | 3 | # GreenstoneMETSPlugin.pm
|
---|
[7901] | 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 GreenstoneArchive METS documents. Note that this plugin does no
|
---|
| 27 | # syntax checking (though the XML::Parser module tests for
|
---|
| 28 | # well-formedness). It's assumed that the GreenstoneArchive files conform
|
---|
| 29 | # to their DTD.
|
---|
| 30 |
|
---|
| 31 |
|
---|
[17028] | 32 | package GreenstoneMETSPlugin;
|
---|
[7901] | 33 |
|
---|
[22840] | 34 | use Encode;
|
---|
[7901] | 35 | use ghtml;
|
---|
| 36 |
|
---|
[10254] | 37 | use strict;
|
---|
| 38 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 39 |
|
---|
[15872] | 40 | use ReadXMLFile;
|
---|
[7901] | 41 | use XML::XPath;
|
---|
| 42 | use XML::XPath::XMLParser;
|
---|
| 43 |
|
---|
| 44 | sub BEGIN {
|
---|
[17028] | 45 | @GreenstoneMETSPlugin::ISA = ('ReadXMLFile');
|
---|
[7901] | 46 | }
|
---|
| 47 |
|
---|
[22840] | 48 | my $arguments = [ { 'name' => "process_exp",
|
---|
[31492] | 49 | 'desc' => "{BaseImporter.process_exp}",
|
---|
[22840] | 50 | 'type' => "regexp",
|
---|
| 51 | 'reqd' => "no",
|
---|
| 52 | 'deft' => &get_default_process_exp()
|
---|
| 53 | }
|
---|
| 54 | ];
|
---|
| 55 |
|
---|
[17028] | 56 | my $options = { 'name' => "GreenstoneMETSPlugin",
|
---|
| 57 | 'desc' => "{GreenstoneMETSPlugin.desc}",
|
---|
[7901] | 58 | 'abstract' => "no",
|
---|
[22840] | 59 | 'inherits' => "yes",
|
---|
| 60 | 'args' => $arguments };
|
---|
| 61 |
|
---|
[7901] | 62 |
|
---|
| 63 |
|
---|
| 64 | sub get_default_process_exp {
|
---|
| 65 | my $self = shift (@_);
|
---|
| 66 |
|
---|
| 67 | return q^(?i)docmets\.xml$^;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | sub new {
|
---|
[10218] | 71 | my ($class) = shift (@_);
|
---|
| 72 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 73 | push(@$pluginlist, $class);
|
---|
[7901] | 74 |
|
---|
[15872] | 75 | # have no args - do we still want this?
|
---|
| 76 | #push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 77 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[7901] | 78 |
|
---|
[15872] | 79 | my $self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[10218] | 80 |
|
---|
[7901] | 81 | $self->{'section'} = "";
|
---|
| 82 | $self->{'section_level'} = 0;
|
---|
| 83 | $self->{'metadata_name'} = "";
|
---|
| 84 | $self->{'metadata_value'} = "";
|
---|
| 85 | $self->{'content'} = "";
|
---|
| 86 |
|
---|
| 87 | return bless $self, $class;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | sub xml_start_document {
|
---|
| 91 | my $self = shift (@_);
|
---|
| 92 | my ($expat, $element) = @_;
|
---|
| 93 |
|
---|
| 94 | $self->{'section'} = "";
|
---|
| 95 | $self->{'section_level'} = 0;
|
---|
| 96 | $self->{'metadata_name'} = "";
|
---|
| 97 | $self->{'metadata_value'} = "";
|
---|
[8514] | 98 | $self->{'content'} = "";
|
---|
[7901] | 99 |
|
---|
| 100 | #**defined a dmdSection Table
|
---|
| 101 | $self->{'dmdSec_table'}={};
|
---|
| 102 |
|
---|
| 103 | #**defined a fileSection Table
|
---|
| 104 | $self->{'fileSec_table'}={};
|
---|
| 105 |
|
---|
| 106 | #***open doctxt.xml and read the data in
|
---|
| 107 | my $filename = $self->{'filename'};
|
---|
| 108 |
|
---|
| 109 | $filename =~ s/docmets.xml$/doctxt.xml/;
|
---|
| 110 |
|
---|
[22840] | 111 | if (!open (FILEIN, "<:utf8", $filename)) {
|
---|
[7901] | 112 | print STDERR "Warning: unable to open the $filename\n";
|
---|
| 113 | $self->{'xmltxt'} = "";
|
---|
| 114 | }
|
---|
| 115 | else {
|
---|
| 116 | my $xml_text = "";
|
---|
| 117 | while (defined (my $line = <FILEIN>)) {
|
---|
| 118 | if ($line !~ m/^<!DOCTYPE.*>/) {
|
---|
| 119 | $xml_text .= $line;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
[22840] | 122 |
|
---|
[7901] | 123 | my $xml_parser = XML::XPath->new (xml=> $xml_text);
|
---|
| 124 | #my $xml_tree = $xml_parser->parse ($xml_text);
|
---|
| 125 |
|
---|
| 126 | #eval {$self->{'parser_text'}->parse};
|
---|
[8740] | 127 | $self->{'parsed_xml'} = $xml_parser;
|
---|
[7901] | 128 | }
|
---|
[8740] | 129 | my $outhandle = $self->{'outhandle'};
|
---|
[17028] | 130 | print $outhandle "GreenstoneMETSPlugin: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
|
---|
| 131 | print STDERR "<Processing n='$self->{'file'}' p='GreenstoneMETSPlugin'>\n" if ($self->{'gli'});
|
---|
[9468] | 132 |
|
---|
[7901] | 133 | }
|
---|
| 134 |
|
---|
| 135 | sub xml_end_document {
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | sub xml_doctype {
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | sub xml_start_tag {
|
---|
| 142 | my $self = shift(@_);
|
---|
| 143 | my ($expat, $element) = @_;
|
---|
| 144 | $self->{'element'} = $element;
|
---|
| 145 | #**deal with dmdSection
|
---|
[17054] | 146 | if ($element =~ /^(mets:)?dmdSec$/ || $element =~ /(gsdl3:)?Metadata$/){
|
---|
[7901] | 147 | $self->xml_dmd_start_tag (@_);
|
---|
[17054] | 148 | } elsif ($element =~ /^(mets:)?file$/) {
|
---|
[21803] | 149 | # only store the file_id for sections with text. Not for default ids (assoc files)
|
---|
| 150 | if ($_{'ID'} =~ m/FILE(.*)/) {
|
---|
| 151 | $self->{'file_Id'} = $1;
|
---|
| 152 | }
|
---|
| 153 | else {
|
---|
| 154 | undef $self->{'file_Id'};
|
---|
| 155 | }
|
---|
[17054] | 156 | } elsif ($element =~ /^(mets:)?FLocat$/){
|
---|
[7901] | 157 | #***deal with fileSection
|
---|
[8740] | 158 | $self->xml_fileloc_start_tag (@_);
|
---|
[17054] | 159 | } elsif ($element =~ /^(mets:)?div$/){
|
---|
[7901] | 160 | #***deal with StrucMap Section
|
---|
| 161 | $self->xml_strucMap_start_tag (@_);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | sub xml_dmd_start_tag {
|
---|
| 166 | my $self = shift (@_);
|
---|
| 167 | my ($expat, $element) = @_;
|
---|
| 168 |
|
---|
[17054] | 169 | if ($element =~ /^(mets:)?dmdSec$/){
|
---|
[7901] | 170 | my ($section_num) = ($_{'ID'} =~ m/DM(.*)/);
|
---|
| 171 | $self->{'dmdSec_table'}->{"$section_num"}=[];
|
---|
| 172 | $self->{'dmdSec_table'}->{'section_num'}=$section_num;
|
---|
[17054] | 173 | } elsif ($element =~ /^(gsdl3:)?Metadata$/) {
|
---|
[7901] | 174 | $self->{'metadata_name'} = $_{'name'};
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[8740] | 178 | sub xml_fileloc_start_tag {
|
---|
[7901] | 179 | my $self = shift (@_);
|
---|
| 180 | my ($expat, $element) = @_;
|
---|
| 181 |
|
---|
| 182 | my $xlink = $_{'xlink:href'};
|
---|
[17054] | 183 | if (!defined $xlink) {
|
---|
| 184 | # try without namespace
|
---|
| 185 | $xlink = $_{'href'};
|
---|
| 186 | }
|
---|
[8740] | 187 | #my ($section_num) = ($_{'ID'} =~ m/^FLOCAT(.*)$/);
|
---|
| 188 | my $section_num = $self->{'file_Id'};
|
---|
[7901] | 189 | return if (!defined $section_num);
|
---|
[21803] | 190 |
|
---|
[7901] | 191 | $self->{'fileSec_table'}->{"$section_num"}=[];
|
---|
| 192 | $self->{'fileSec_table'}->{'section_num'}=$section_num;
|
---|
| 193 |
|
---|
| 194 | my ($filename,$xpath_expr)=($xlink =~ m/^file:(.*)\#xpointer\((.*)\)$/);
|
---|
| 195 | my $nodeset = $self->{'parsed_xml'}->findnodes ($xpath_expr);
|
---|
| 196 | my $node_size= $nodeset->size;
|
---|
| 197 |
|
---|
| 198 | if ($node_size==0) {
|
---|
| 199 | print STDERR "Warning: no text associated with XPATH $xpath_expr\n";
|
---|
| 200 | }
|
---|
| 201 | else {
|
---|
| 202 | foreach my $node ($nodeset->get_nodelist) {
|
---|
| 203 | my $xml_content = XML::XPath::XMLParser::as_string($node);
|
---|
| 204 | my $unescaped_xml_content = &ghtml::unescape_html($xml_content);
|
---|
| 205 | my $section_content={'section_content'=> $unescaped_xml_content};
|
---|
| 206 |
|
---|
| 207 | my $content_list = $self->{'fileSec_table'}->{"$section_num"};
|
---|
| 208 | push (@$content_list, $section_content);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | sub xml_strucMap_start_tag {
|
---|
| 214 | my $self = shift (@_);
|
---|
| 215 | my ($expat, $element) = @_;
|
---|
| 216 |
|
---|
[8740] | 217 |
|
---|
| 218 | my ($section_num) = ($_{'ID'} =~ m/DS(.*)/);
|
---|
| 219 |
|
---|
| 220 | if ($_{'ID'} ne "DSAll"){
|
---|
[7901] | 221 | if ($self->{'section_level'}==0) {
|
---|
| 222 | $self->open_document();
|
---|
| 223 | } else {
|
---|
| 224 | my $doc_obj = $self->{'doc_obj'};
|
---|
| 225 | $self->{'section'}=
|
---|
| 226 | $doc_obj->insert_section($doc_obj->get_end_child($self->{'section'}));
|
---|
| 227 | }
|
---|
| 228 | $self->{'section_level'}++;
|
---|
| 229 |
|
---|
[8740] | 230 | #***Add metadata from dmdSection
|
---|
[7901] | 231 | my $md_list = $self->{'dmdSec_table'}->{"$section_num"};
|
---|
| 232 |
|
---|
| 233 | foreach my $md_pair (@$md_list){
|
---|
[22840] | 234 | # text read in by XML::Parser is in Perl's binary byte value
|
---|
| 235 | # form ... need to explicitly make it UTF-8
|
---|
| 236 |
|
---|
| 237 | my $metadata_name = decode("utf8",$md_pair->{'metadata_name'});
|
---|
| 238 | my $metadata_value = decode("utf8",$md_pair->{'metadata_value'});
|
---|
| 239 |
|
---|
| 240 | $self->{'doc_obj'}->add_utf8_metadata($self->{'section'},
|
---|
| 241 | $metadata_name, $metadata_value);
|
---|
[7901] | 242 | }
|
---|
| 243 |
|
---|
[8740] | 244 | #*** Add content from fileSection
|
---|
[7901] | 245 | my $content_list = $self->{'fileSec_table'}->{"$section_num"};
|
---|
[8740] | 246 |
|
---|
[7901] | 247 | foreach my $section_content (@$content_list){
|
---|
[22840] | 248 | # Don't need to decode $content as this has been readin in
|
---|
| 249 | # through XPath which (unlike XML::Parser) correctly sets
|
---|
| 250 | # the string to be UTF8 rather than a 'binary' string of bytes
|
---|
[7901] | 251 | my $content = $section_content->{'section_content'};
|
---|
[22840] | 252 |
|
---|
[7901] | 253 | $self->{'doc_obj'}->add_utf8_text($self->{'section'},$content);
|
---|
[8740] | 254 | }
|
---|
[7901] | 255 | }
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[13222] | 258 | sub get_doctype {
|
---|
| 259 | my $self = shift(@_);
|
---|
| 260 |
|
---|
| 261 | return "mets:mets";
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[7901] | 264 | sub xml_end_tag {
|
---|
| 265 | my $self = shift(@_);
|
---|
| 266 | my ($expat, $element) = @_;
|
---|
| 267 |
|
---|
[17054] | 268 | if ($element =~ /^(gsdl3:)?Metadata$/) {
|
---|
[7901] | 269 | my $section_num = $self->{'dmdSec_table'}->{'section_num'};
|
---|
| 270 | my $metadata_name=$self->{'metadata_name'};
|
---|
| 271 | my $metadata_value=$self->{'metadata_value'};
|
---|
| 272 |
|
---|
| 273 | my $md_pair={'metadata_name' => $metadata_name,
|
---|
| 274 | 'metadata_value'=> $metadata_value};
|
---|
| 275 |
|
---|
| 276 | my $md_list = $self->{'dmdSec_table'}->{"$section_num"};
|
---|
| 277 |
|
---|
| 278 | push(@$md_list,$md_pair);
|
---|
| 279 |
|
---|
| 280 | $self->{'metadata_name'} = "";
|
---|
| 281 | $self->{'metadata_value'} = "";
|
---|
[17054] | 282 | } elsif ($element =~ /^(mets:)?file$/){
|
---|
[8740] | 283 | $self->{'file_id'} = "";
|
---|
[7901] | 284 | }
|
---|
| 285 |
|
---|
[8740] | 286 |
|
---|
[7901] | 287 | #*** StrucMap Section
|
---|
[17054] | 288 | if ($element =~ /^(mets:)?div$/) {
|
---|
[7901] | 289 | $self->{'section_level'}--;
|
---|
| 290 | $self->{'section'} = $self->{'doc_obj'}->get_parent_section($self->{'section'});
|
---|
| 291 | $self->close_document() if $self->{'section_level'}==0;
|
---|
| 292 | }
|
---|
| 293 | $self->{'element'} = "";
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | sub xml_text {
|
---|
| 297 | my $self = shift(@_);
|
---|
| 298 | my ($expat) = @_;
|
---|
| 299 |
|
---|
[17054] | 300 | if ($self->{'element'} =~ /^(gsdl3:)?Metadata$/) {
|
---|
[7901] | 301 | $self->{'metadata_value'} .= $_;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | sub open_document {
|
---|
| 306 | my $self = shift(@_);
|
---|
| 307 |
|
---|
| 308 | # create a new document
|
---|
| 309 | $self->{'doc_obj'} = new doc ();
|
---|
| 310 | $self->{'section'} = "";
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | sub close_document {
|
---|
| 314 | my $self = shift(@_);
|
---|
| 315 |
|
---|
| 316 | # add the associated files
|
---|
| 317 | my $assoc_files =
|
---|
| 318 | $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
|
---|
| 319 |
|
---|
| 320 | # for when "assocfilepath" isn't the same directory that doc.xml is in...
|
---|
| 321 | my $assoc_filepath_list= $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "assocfilepath");
|
---|
| 322 |
|
---|
| 323 | my $assoc_filepath=shift (@$assoc_filepath_list);
|
---|
| 324 | if (defined ($assoc_filepath)) {
|
---|
| 325 | # make absolute rather than relative...
|
---|
| 326 | $self->{'filename'} =~ m@^(.*[\\/]archives)@;
|
---|
| 327 | $assoc_filepath = "$1/$assoc_filepath/";
|
---|
| 328 | } else {
|
---|
| 329 | $assoc_filepath = $self->{'filename'};
|
---|
| 330 | $assoc_filepath =~ s/[^\\\/]*$//;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | foreach my $assoc_file_info (@$assoc_files) {
|
---|
| 334 | my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
|
---|
| 335 | my $real_dir = &util::filename_cat($assoc_filepath, $assoc_file),
|
---|
| 336 | my $assoc_dir = (defined $dir && $dir ne "")
|
---|
| 337 | ? &util::filename_cat($dir, $assoc_file) : $assoc_file;
|
---|
| 338 | $self->{'doc_obj'}->associate_file($real_dir, $assoc_dir, $mime_type);
|
---|
| 339 | }
|
---|
| 340 | $self->{'doc_obj'}->delete_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
|
---|
| 341 |
|
---|
| 342 | # process the document
|
---|
| 343 | $self->{'processor'}->process($self->{'doc_obj'}, $self->{'file'});
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | 1;
|
---|
| 348 |
|
---|