source: trunk/gsdl/perllib/plugins/GMLPlug.pm@ 5096

Last change on this file since 5096 was 3540, checked in by kjdon, 22 years ago

added John T's changes into CVS - added info to enable retrieval of usage info in xml

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1###########################################################################
2#
3# GMLPlug.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) 1999 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# plugin which processes a GML format document
27# assumes that gml tags are all in lower-case.
28
29# 12/05/02 Added usage datastructure - John Thompson
30
31package GMLPlug;
32
33use BasPlug;
34use util;
35use doc;
36
37sub BEGIN {
38 @ISA = ('BasPlug');
39}
40
41my $options = { 'name' => "GMLPlug",
42 'desc' => "Plugin which processes a GML format document assumes that gml tags are all in lower-case.",
43 'inherits' => "yes" };
44
45sub new {
46 my ($class) = @_;
47 my $self = new BasPlug ("GMLPlug", @_);
48
49 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
50 my $option_list = $self->{'option_list'};
51 push( @{$option_list}, $options );
52
53 return bless $self, $class;}
54
55sub get_default_process_exp {
56 my $self = shift (@_);
57
58 return q^(?i)\.gml?$^;
59}
60
61# return number of files processed, undef if can't process
62# Note that $base_dir might be "" and that $file might
63# include directories
64sub read {
65 my $self = shift (@_);
66 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
67 my $outhandle = $self->{'outhandle'};
68
69 my $filename = $file;
70 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
71
72 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
73 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
74 return undef;
75 }
76 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
77
78 print $outhandle "GMLPlug: processing $file\n";
79
80 my $parent_dir = $file;
81 $parent_dir =~ s/[^\\\/]*$//;
82 $parent_dir = &util::filename_cat ($base_dir, $parent_dir);
83
84 if (!open (INFILE, $filename)) {
85 print $outhandle "GMLPlug::read - couldn't read $filename\n";
86 return 0;
87 }
88
89 undef $/;
90 my $gml = <INFILE>;
91 $/ = "\n";
92 close (INFILE);
93
94 my @gml_sections = split("</gsdlsection>",$gml);
95 $gml = shift(@gml_sections);
96
97 my $no_docs = 0;
98
99 while (1) {
100 # create a new document
101 my $doc_obj = new doc ();
102 my $section = $doc_obj->get_top_section();
103
104 # process the document
105 my $firstsection = 1;
106 while (1) {
107 my ($tags, $text) = ("", "");
108
109 my @indenting_sections = split("<gsdlsection", $gml);
110 shift(@indenting_sections); # skips over xml header if present
111
112 foreach $gml (@indenting_sections) {
113
114 if ($gml =~ /^\s*([^>]*)>(.*)$/so) {
115 $tags = $1 if defined $1;
116 $text = &GMLPlug::_unescape_text($2);
117
118 } else {
119 print $outhandle "GMLPlug::read - error in file $filename\n";
120 print $outhandle "text: \"$gml\"\n";
121 last;
122 }
123
124 # create the section (unless this is the first section)
125 if ($firstsection) {
126 $firstsection = 0;
127# $tags =~ /gsdlsourcefilename\s*=\s*(?:\"([^\"]*)\")/o;
128# $src_filename = $2 || $3;
129
130 } else {
131
132 $tags =~ s/gsdlnum\s*=\s*\"?(\d+)\"?//o;
133 if (defined $1) {
134 $section .= ".$1";
135 $doc_obj->create_named_section($section);
136 } else {
137 $section = $doc_obj->insert_section($doc_obj->get_end_child($section));
138 }
139 }
140
141 # add the metadata
142 # could be stored as either attributes or ....
143 while ((defined $tags) && ($tags =~ s/^\s*(\S+)=\"([^\"]*)\"//o)) {
144 $doc_obj->add_utf8_metadata($section, $1, &GMLPlug::_unescape_text($2))
145 if (defined $1 and defined $2);
146
147 }
148
149 # ... or tags (xml compliant)
150 if ($text =~ m/^\s*<metadata>/)
151 {
152 my ($metadata, $tagname, $tagvalue);
153 ($metadata,$text)
154 = ($text =~ m/\s*<metadata>\s*(<.*)\s*<\/metadata>(.*)$/s);
155
156 # note: \1 refers to 1st match within regexp, so we can
157 # handle the unescaped text here...
158 while ((defined $metadata)
159 && ($metadata =~ s/<(.*?)>(.*?)<\/\1>//s))
160 {
161 if (defined $1 && defined $2)
162 {
163 $tagname = $1;
164 $tagvalue = $2;
165
166 # if tagname begins with '/' it will be escaped
167 $tagname =~ s/^&\#47;/\//;
168
169 $doc_obj->add_utf8_metadata($section, $tagname, &GMLPlug::_unescape_text($tagvalue));
170 }
171 }
172 }
173
174 # add the text
175
176 $doc_obj->add_utf8_text($section, $text)
177 if ((defined $text) && ($text ne ""));
178 }
179
180 $gml = shift(@gml_sections); # get next bit of data
181 last unless defined $gml;
182 last if $section eq ""; # back to top level again (more than one document in gml file)
183 $section = $doc_obj->get_parent_section ($section);
184 } # while (1) section level
185
186 # add the associated files
187 my $assoc_files = $doc_obj->get_metadata($doc_obj->get_top_section(), "gsdlassocfile");
188 my ($assoc_file_info);
189
190 foreach $assoc_file_info (@$assoc_files)
191 {
192 my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
193 my $real_dir = &util::filename_cat($parent_dir, $assoc_file),
194 my $assoc_dir = (defined $dir && $dir ne "")
195 ? &util::filename_cat($dir, $assoc_file) : $assoc_file;
196 $doc_obj->associate_file($real_dir, $assoc_dir, $mime_type);
197
198 }
199 $doc_obj->delete_metadata($doc_obj->get_top_section(), "gsdlassocfile");
200
201 # add metadata passed in from elsewhere
202 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
203
204 # do any automatic metadata extraction
205 $self->auto_extract_metadata ($doc_obj);
206
207 # assume the document has an OID already
208
209 # process the document
210 $processor->process($doc_obj, $file);
211
212 $no_docs++;
213 last if ($maxdocs > -1 && $no_docs >= $maxdocs);
214 last unless defined $gml && $gml =~ /\w/;
215 } # while(1) document level
216
217 return $no_docs; # no of docs processed
218}
219
220sub _unescape_text {
221 my ($text) = @_;
222
223 # special characters in the gml encoding
224 $text =~ s/&lt;/</g;
225 $text =~ s/&gt;/>/g;
226 $text =~ s/&quot;/\"/g;
227 $text =~ s/&amp;/&/g; # this has to be last...
228
229 return $text;
230}
231
2321;
Note: See TracBrowser for help on using the repository browser.