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

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

renamed old html module ghtml -- it clashed with builtin html module
on windows

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