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

Last change on this file since 10218 was 10218, checked in by kjdon, 19 years ago

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

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