source: main/tags/2.35c/gsdl/perllib/plugins/GMLPlug.pm@ 25632

Last change on this file since 25632 was 2363, checked in by jrm21, 23 years ago

fixed nasty bug where </srclink></a><srclink> was being matched
incorrectly, creating a new tag that was swallowing next line, which
happens to be <Identifier>... anyway, fixed regexp and we can now actually
get away with not escaping the text.

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