root/main/trunk/greenstone2/perllib/plugins/GreenstoneMETSPlugin.pm @ 21803

Revision 21803, 9.3 KB (checked in by kjdon, 10 years ago)

set file_id to null if ID doesn't match FILE.* (previously it was ending up with mets: from the previous match

  • 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    # only store the file_id for sections with text. Not for default ids (assoc files)
140    if ($_{'ID'} =~ m/FILE(.*)/) {
141        $self->{'file_Id'} = $1;
142    }
143    else {
144        undef $self->{'file_Id'};
145    }
146    } elsif ($element =~ /^(mets:)?FLocat$/){
147    #***deal with fileSection
148    $self->xml_fileloc_start_tag (@_);
149    } elsif ($element =~ /^(mets:)?div$/){
150    #***deal with StrucMap Section
151    $self->xml_strucMap_start_tag (@_);
152    }
153}
154
155sub xml_dmd_start_tag {
156    my $self = shift (@_);
157    my ($expat, $element) = @_;
158
159    if ($element =~ /^(mets:)?dmdSec$/){
160    my ($section_num) = ($_{'ID'} =~ m/DM(.*)/);
161    $self->{'dmdSec_table'}->{"$section_num"}=[];
162    $self->{'dmdSec_table'}->{'section_num'}=$section_num;
163    } elsif ($element =~ /^(gsdl3:)?Metadata$/) {
164    $self->{'metadata_name'} = $_{'name'};
165    }
166}
167
168sub xml_fileloc_start_tag {
169    my $self = shift (@_);
170    my ($expat, $element) = @_;
171
172    my $xlink = $_{'xlink:href'};
173    if (!defined $xlink) {
174    # try without namespace
175    $xlink = $_{'href'};
176    }
177    #my ($section_num) = ($_{'ID'} =~ m/^FLOCAT(.*)$/);
178    my $section_num = $self->{'file_Id'};
179    return if (!defined $section_num);
180
181    $self->{'fileSec_table'}->{"$section_num"}=[];
182    $self->{'fileSec_table'}->{'section_num'}=$section_num;
183   
184    my ($filename,$xpath_expr)=($xlink =~ m/^file:(.*)\#xpointer\((.*)\)$/);
185    my $nodeset = $self->{'parsed_xml'}->findnodes ($xpath_expr);
186    my $node_size= $nodeset->size;
187   
188    if ($node_size==0) {
189    print STDERR "Warning: no text associated with XPATH $xpath_expr\n";
190    }
191    else {
192    foreach my $node ($nodeset->get_nodelist) {
193        my $xml_content = XML::XPath::XMLParser::as_string($node);
194        my $unescaped_xml_content = &ghtml::unescape_html($xml_content);
195        my $section_content={'section_content'=> $unescaped_xml_content};
196       
197        my $content_list = $self->{'fileSec_table'}->{"$section_num"};
198        push (@$content_list, $section_content);
199    }
200    }
201}
202
203sub xml_strucMap_start_tag {
204    my $self = shift (@_);
205    my ($expat, $element) = @_;
206
207
208    my ($section_num) = ($_{'ID'} =~ m/DS(.*)/);
209
210    if ($_{'ID'} ne "DSAll"){
211    if ($self->{'section_level'}==0) {
212        $self->open_document();
213    } else {
214        my $doc_obj = $self->{'doc_obj'};
215        $self->{'section'}=
216        $doc_obj->insert_section($doc_obj->get_end_child($self->{'section'}));
217    }
218    $self->{'section_level'}++;
219   
220    #***Add metadata from dmdSection
221    my $md_list = $self->{'dmdSec_table'}->{"$section_num"};
222   
223    foreach my $md_pair (@$md_list){
224        my $metadata_name = $md_pair->{'metadata_name'};
225        my $metadata_value = $md_pair->{'metadata_value'};
226        $self->{'doc_obj'}->add_utf8_metadata($self->{'section'}, $metadata_name, $metadata_value);
227    }
228   
229    #*** Add content from fileSection
230    my $content_list = $self->{'fileSec_table'}->{"$section_num"};
231   
232    foreach my $section_content (@$content_list){
233        my $content = $section_content->{'section_content'};
234        $self->{'doc_obj'}->add_utf8_text($self->{'section'},$content);
235    }
236    }
237}
238
239sub get_doctype {
240    my $self = shift(@_);
241   
242    return "mets:mets";
243}
244
245sub xml_end_tag {
246    my $self = shift(@_);
247    my ($expat, $element) = @_;
248
249    if ($element =~ /^(gsdl3:)?Metadata$/) {
250    my $section_num = $self->{'dmdSec_table'}->{'section_num'};
251    my $metadata_name=$self->{'metadata_name'};
252    my $metadata_value=$self->{'metadata_value'};
253
254    my $md_pair={'metadata_name' => $metadata_name,
255             'metadata_value'=> $metadata_value};
256
257    my $md_list = $self->{'dmdSec_table'}->{"$section_num"};
258
259    push(@$md_list,$md_pair);
260     
261    $self->{'metadata_name'} = "";
262    $self->{'metadata_value'} = "";
263    } elsif ($element =~ /^(mets:)?file$/){
264    $self->{'file_id'} = "";
265    }
266   
267   
268    #*** StrucMap Section
269    if ($element =~ /^(mets:)?div$/) {
270    $self->{'section_level'}--;
271    $self->{'section'} = $self->{'doc_obj'}->get_parent_section($self->{'section'});
272    $self->close_document() if $self->{'section_level'}==0;
273    }
274    $self->{'element'} = "";
275}
276
277sub xml_text {
278    my $self = shift(@_);
279    my ($expat) = @_;
280
281    if ($self->{'element'} =~ /^(gsdl3:)?Metadata$/) {
282    $self->{'metadata_value'} .= $_;
283    }
284}
285
286sub open_document {
287    my $self = shift(@_);
288
289    # create a new document
290    $self->{'doc_obj'} = new doc ();
291    $self->{'section'} = "";
292}
293
294sub close_document {
295    my $self = shift(@_);
296   
297    # add the associated files
298    my $assoc_files =
299    $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
300
301    # for when "assocfilepath" isn't the same directory that doc.xml is in...
302    my $assoc_filepath_list= $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "assocfilepath");
303
304    my $assoc_filepath=shift (@$assoc_filepath_list);
305    if (defined ($assoc_filepath)) {
306    # make absolute rather than relative...
307    $self->{'filename'} =~ m@^(.*[\\/]archives)@;
308    $assoc_filepath = "$1/$assoc_filepath/";
309    } else {
310    $assoc_filepath = $self->{'filename'};
311    $assoc_filepath =~ s/[^\\\/]*$//;
312    }
313
314    foreach my $assoc_file_info (@$assoc_files) {
315    my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
316    my $real_dir = &util::filename_cat($assoc_filepath, $assoc_file),
317    my $assoc_dir = (defined $dir && $dir ne "")
318        ? &util::filename_cat($dir, $assoc_file) : $assoc_file;
319    $self->{'doc_obj'}->associate_file($real_dir, $assoc_dir, $mime_type);
320    }
321    $self->{'doc_obj'}->delete_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
322
323    # process the document
324    $self->{'processor'}->process($self->{'doc_obj'}, $self->{'file'});
325}
326
327
3281;
329
Note: See TracBrowser for help on using the browser.