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

Last change on this file since 863 was 863, checked in by sjboddie, 24 years ago

fixed a couple of bugs that I introduced when including Davids stuff

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 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 html;
32use BasPlug;
33use util;
34use doc;
35
36sub BEGIN {
37 @ISA = ('BasPlug');
38}
39
40sub new {
41 my ($class) = @_;
42 $self = new BasPlug ();
43
44 return bless $self, $class;
45}
46
47
48sub is_recursive {
49 my $self = shift (@_);
50
51 return 0; # this is not a recursive plugin
52}
53
54sub _unescape_text {
55 my ($text) = @_;
56
57 # special characters in the gml encoding
58 $text =~ s/&lt;/</g;
59 $text =~ s/&gt;/>/g;
60 $text =~ s/&quot;/\"/g;
61 $text =~ s/&amp;/&/g; # this has to be last...
62
63 return $text;
64}
65
66# return number of files processed, undef if can't process
67# Note that $base_dir might be "" and that $file might
68# include directories
69sub read {
70 my $self = shift (@_);
71 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
72 my $fullname = &util::filename_cat ($base_dir, $file);
73
74 # see if this is a gml book
75 return undef unless (-f $fullname && $fullname =~ /\.gml(\.gz)?$/io);
76
77 my ($parent_dir, $gz) = $fullname =~ /^(.*?)[\/\\][^\/\\]+.gml(\.gz)?$/io;
78
79 if (defined $gz && $gz =~ /\.gz/io) {
80 $gz = 1;
81 } else {
82 $gz = 0;
83 }
84
85 print STDERR "GMLPlug: processing $file\n";
86
87 # read in the document
88 if ($gz) {
89 if (!open (INFILE, "zcat $fullname |")) {
90 print STDERR "GMLPlug::read - zcat couldn't read $fullname\n";
91 return undef;
92 }
93 } else {
94 if (!open (INFILE, $fullname)) {
95 print STDERR "GMLPlug::read - couldn't read $fullname\n";
96 return undef;
97 }
98 }
99
100 undef $/;
101 my $gml = <INFILE>;
102 $/ = "\n";
103 close (INFILE);
104
105 my @gml_sections = split("</gsdlsection>",$gml);
106 $gml = shift(@gml_sections);
107
108 my $no_docs = 0;
109# my $src_filename = ""; #### don't appear to use this anymore - not sure if that's right
110
111 while (1) {
112 # create a new document
113 my $doc_obj = new doc ();
114 my $section = $doc_obj->get_top_section();
115
116 # process the document
117 my $firstsection = 1;
118 while (1) {
119 my ($tags, $text) = ("", "");
120
121 my @indenting_sections = split("<gsdlsection", $gml);
122 shift(@indenting_sections); # first entry is trivially empty
123
124 foreach $gml (@indenting_sections) {
125
126 if ($gml =~ /^\s*([^>]*)>(.*)$/so) {
127 $tags = $1 if defined $1;
128 $text = &GMLPlug::_unescape_text($2);
129
130 } else {
131 print STDERR "GMLPlug::read - error in file $fullname\n";
132 print STDERR "text: \"$gml\"\n";
133 last;
134 }
135
136 # create the section (unless this is the first section)
137 if ($firstsection) {
138 $firstsection = 0;
139# $tags =~ /gsdlsourcefilename\s*=\s*(?:\"([^\"]*)\")/o;
140# $src_filename = $2 || $3;
141
142 } else {
143
144 $tags =~ s/gsdlnum\s*=\s*\"?(\d+)\"?//o;
145 if (defined $1) {
146 $section .= ".$1";
147 $doc_obj->create_named_section($section);
148 } else {
149 $section = $doc_obj->insert_section($doc_obj->get_end_child($section));
150 }
151 }
152
153 # add the tags
154 while ((defined $tags) && ($tags =~ s/^\s*(\S+)=\"([^\"]*)\"//o)) {
155 $doc_obj->add_utf8_metadata($section, $1, &GMLPlug::_unescape_text($2))
156 if (defined $1 and defined $2);
157
158 }
159
160 # add the text
161 $doc_obj->add_utf8_text($section, $text)
162 if ((defined $text) && ($text ne ""));
163 }
164
165 $gml = shift(@gml_sections); # get next bit of data
166 last unless defined $gml;
167 last if $section eq ""; # back to top level again (more than one document in gml file)
168 $section = $doc_obj->get_parent_section ($section);
169 }
170
171 # add the associated files
172 $assoc_files = $doc_obj->get_metadata($doc_obj->get_top_section(), "gsdlassocfile");
173 my ($assoc_file_info);
174 foreach $assoc_file_info (@$assoc_files) {
175 my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
176 $dir = "" unless defined $dir;
177 $doc_obj->associate_file(&util::filename_cat($parent_dir, $assoc_file),
178 &util::filename_cat($dir, $assoc_file), $mime_type);
179 }
180 $doc_obj->delete_metadata($doc_obj->get_top_section(), "gsdlassocfile");
181
182 # add metadata passed in from elsewhere
183 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
184
185 # assume the document has an OID
186
187 # process the document
188 $processor->process($doc_obj, $file);
189
190 $no_docs++;
191 last if ($maxdocs > -1 && $no_docs >= $maxdocs);
192 last unless defined $gml;
193 }
194
195 return $no_docs; # no of docs processed
196}
197
198
1991;
Note: See TracBrowser for help on using the repository browser.