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