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

Last change on this file since 15182 was 15182, checked in by kjdon, 16 years ago

removed an unnecessary comment

  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 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
32package METSPlug;
33
34use ghtml;
35
36use strict;
37no strict 'refs'; # allow filehandles to be variables and viceversa
38
39use XMLPlug;
40use XML::XPath;
41use XML::XPath::XMLParser;
42
43sub BEGIN {
44 @METSPlug::ISA = ('XMLPlug');
45}
46
47my $arguments = [
48 ];
49my $options = { 'name' => "METSPlug",
50 'desc' => "{METSPlug.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 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
67 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
68
69 my $self = new XMLPlug($pluginlist, $inputargs, $hashArgOptLists);
70
71 $self->{'section'} = "";
72 $self->{'section_level'} = 0;
73 $self->{'metadata_name'} = "";
74 $self->{'metadata_value'} = "";
75 $self->{'content'} = "";
76
77 return bless $self, $class;
78}
79
80sub xml_start_document {
81 my $self = shift (@_);
82 my ($expat, $element) = @_;
83
84 $self->{'section'} = "";
85 $self->{'section_level'} = 0;
86 $self->{'metadata_name'} = "";
87 $self->{'metadata_value'} = "";
88 $self->{'content'} = "";
89
90 #**defined a dmdSection Table
91 $self->{'dmdSec_table'}={};
92
93 #**defined a fileSection Table
94 $self->{'fileSec_table'}={};
95
96 #***open doctxt.xml and read the data in
97 my $filename = $self->{'filename'};
98
99 $filename =~ s/docmets.xml$/doctxt.xml/;
100
101 if (!open (FILEIN,"<$filename")){
102 print STDERR "Warning: unable to open the $filename\n";
103 $self->{'xmltxt'} = "";
104 }
105 else {
106 my $xml_text = "";
107 while (defined (my $line = <FILEIN>)) {
108 if ($line !~ m/^<!DOCTYPE.*>/) {
109 $xml_text .= $line;
110 }
111 }
112 my $xml_parser = XML::XPath->new (xml=> $xml_text);
113 #my $xml_tree = $xml_parser->parse ($xml_text);
114
115 #eval {$self->{'parser_text'}->parse};
116 $self->{'parsed_xml'} = $xml_parser;
117 }
118 my $outhandle = $self->{'outhandle'};
119 print $outhandle "METSPlug: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
120 print STDERR "<Processing n='$self->{'file'}' p='METSPlug'>\n" if ($self->{'gli'});
121
122}
123
124sub xml_end_document {
125}
126
127sub xml_doctype {
128}
129
130sub xml_start_tag {
131 my $self = shift(@_);
132 my ($expat, $element) = @_;
133
134 $self->{'element'} = $element;
135
136 #**deal with dmdSection
137 if ($element eq "mets:dmdSec" || $element eq "gsdl3:Metadata"){
138 $self->xml_dmd_start_tag (@_);
139 } elsif ($element eq "mets:file") {
140 $_{'ID'} =~ m/FILE(.*)/;
141 $self->{'file_Id'} = $1;
142 } elsif ($element eq "mets:FLocat"){
143 #***deal with fileSection
144 $self->xml_fileloc_start_tag (@_);
145 } elsif ($element eq "mets:div"){
146 #***deal with StrucMap Section
147 $self->xml_strucMap_start_tag (@_);
148 }
149}
150
151sub xml_dmd_start_tag {
152 my $self = shift (@_);
153 my ($expat, $element) = @_;
154
155 if ($element eq "mets:dmdSec"){
156 my ($section_num) = ($_{'ID'} =~ m/DM(.*)/);
157 $self->{'dmdSec_table'}->{"$section_num"}=[];
158 $self->{'dmdSec_table'}->{'section_num'}=$section_num;
159 } elsif ($element eq "gsdl3:Metadata") {
160 $self->{'metadata_name'} = $_{'name'};
161 }
162}
163
164sub xml_fileloc_start_tag {
165 my $self = shift (@_);
166 my ($expat, $element) = @_;
167
168 my $xlink = $_{'xlink:href'};
169 #my ($section_num) = ($_{'ID'} =~ m/^FLOCAT(.*)$/);
170 my $section_num = $self->{'file_Id'};
171
172 return if (!defined $section_num);
173 #**return if the section_num is not defined or not deal with the whole section (ID="default.*")
174
175 $self->{'fileSec_table'}->{"$section_num"}=[];
176 $self->{'fileSec_table'}->{'section_num'}=$section_num;
177
178 my ($filename,$xpath_expr)=($xlink =~ m/^file:(.*)\#xpointer\((.*)\)$/);
179
180 my $nodeset = $self->{'parsed_xml'}->findnodes ($xpath_expr);
181 my $node_size= $nodeset->size;
182
183 if ($node_size==0) {
184 print STDERR "Warning: no text associated with XPATH $xpath_expr\n";
185 }
186 else {
187 foreach my $node ($nodeset->get_nodelist) {
188 my $xml_content = XML::XPath::XMLParser::as_string($node);
189 my $unescaped_xml_content = &ghtml::unescape_html($xml_content);
190
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 eq "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 eq "mets:file"){
260 $self->{'file_id'} = "";
261 }
262
263
264 #*** StrucMap Section
265 if ($element eq "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'} eq "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 repository browser.