root/gsdl/trunk/perllib/plugins/GreenstoneMETSPlugin.pm @ 17054

Revision 17054, 9.2 KB (checked in by kjdon, 11 years ago)

a previous change to ReadXMLFile meant that all namespaces were stripped out of the parsed XML. Have modified all the element name tests to use regular expressions with the namespace optional

  • Property svn:keywords set to Author Date Id Revision
Line 
1###########################################################################
2#
3# GreenstoneMETSPlugin.pm
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
32package GreenstoneMETSPlugin;
33
34use ghtml;
35
36use strict;
37no strict 'refs'; # allow filehandles to be variables and viceversa
38
39use ReadXMLFile;
40use XML::XPath;
41use XML::XPath::XMLParser;
42
43sub BEGIN {
44    @GreenstoneMETSPlugin::ISA = ('ReadXMLFile');
45}
46
47my $arguments = [
48         ];
49my $options = { 'name'     => "GreenstoneMETSPlugin",
50        'desc'     => "{GreenstoneMETSPlugin.desc}",
51        'abstract' => "no",
52        'inherits' => "yes" };
53
54
55sub get_default_process_exp {
56    my $self = shift (@_);
57
58    return q^(?i)docmets\.xml$^;
59}
60
61sub new {
62    my ($class) = shift (@_);
63    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
64    push(@$pluginlist, $class);
65
66    # have no args - do we still want this?
67    #push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
68    push(@{$hashArgOptLists->{"OptList"}},$options);
69
70    my $self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
71
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
81sub 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'} = "";
89    $self->{'content'} = "";
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};
117    $self->{'parsed_xml'} = $xml_parser;
118    }
119    my $outhandle = $self->{'outhandle'};
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'});
122
123}
124
125sub xml_end_document {
126}
127
128sub xml_doctype {
129}
130
131sub xml_start_tag {
132    my $self = shift(@_);
133    my ($expat, $element) = @_;
134    $self->{'element'} = $element;
135    #**deal with dmdSection
136    if ($element =~ /^(mets:)?dmdSec$/ || $element =~ /(gsdl3:)?Metadata$/){
137    $self->xml_dmd_start_tag (@_);
138    } elsif ($element =~ /^(mets:)?file$/) {
139    $_{'ID'} =~ m/FILE(.*)/;
140    $self->{'file_Id'} = $1;
141    } elsif ($element =~ /^(mets:)?FLocat$/){
142    #***deal with fileSection
143    $self->xml_fileloc_start_tag (@_);
144    } elsif ($element =~ /^(mets:)?div$/){
145    #***deal with StrucMap Section
146    $self->xml_strucMap_start_tag (@_);
147    }
148}
149
150sub xml_dmd_start_tag {
151    my $self = shift (@_);
152    my ($expat, $element) = @_;
153
154    if ($element =~ /^(mets:)?dmdSec$/){
155    my ($section_num) = ($_{'ID'} =~ m/DM(.*)/);
156    $self->{'dmdSec_table'}->{"$section_num"}=[];
157    $self->{'dmdSec_table'}->{'section_num'}=$section_num;
158    } elsif ($element =~ /^(gsdl3:)?Metadata$/) {
159    $self->{'metadata_name'} = $_{'name'};
160    }
161}
162
163sub xml_fileloc_start_tag {
164    my $self = shift (@_);
165    my ($expat, $element) = @_;
166
167    my $xlink = $_{'xlink:href'};
168    if (!defined $xlink) {
169    # try without namespace
170    $xlink = $_{'href'};
171    }
172    #my ($section_num) = ($_{'ID'} =~ m/^FLOCAT(.*)$/);
173    my $section_num = $self->{'file_Id'};
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
199sub xml_strucMap_start_tag {
200    my $self = shift (@_);
201    my ($expat, $element) = @_;
202
203
204    my ($section_num) = ($_{'ID'} =~ m/DS(.*)/);
205
206    if ($_{'ID'} ne "DSAll"){
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   
216    #***Add metadata from dmdSection
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   
225    #*** Add content from fileSection
226    my $content_list = $self->{'fileSec_table'}->{"$section_num"};
227   
228    foreach my $section_content (@$content_list){
229        my $content = $section_content->{'section_content'};
230        $self->{'doc_obj'}->add_utf8_text($self->{'section'},$content);
231    }
232    }
233}
234
235sub get_doctype {
236    my $self = shift(@_);
237   
238    return "mets:mets";
239}
240
241sub xml_end_tag {
242    my $self = shift(@_);
243    my ($expat, $element) = @_;
244
245    if ($element =~ /^(gsdl3:)?Metadata$/) {
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'} = "";
259    } elsif ($element =~ /^(mets:)?file$/){
260    $self->{'file_id'} = "";
261    }
262   
263   
264    #*** StrucMap Section
265    if ($element =~ /^(mets:)?div$/) {
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
273sub xml_text {
274    my $self = shift(@_);
275    my ($expat) = @_;
276
277    if ($self->{'element'} =~ /^(gsdl3:)?Metadata$/) {
278    $self->{'metadata_value'} .= $_;
279    }
280}
281
282sub open_document {
283    my $self = shift(@_);
284
285    # create a new document
286    $self->{'doc_obj'} = new doc ();
287    $self->{'section'} = "";
288}
289
290sub 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
3241;
325
Note: See TracBrowser for help on using the browser.