source: trunk/gsdl/perllib/plugins/METSPlug.pm@ 9144

Last change on this file since 9144 was 8740, checked in by chi, 20 years ago

Modifications for validated METS format.

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