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

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

Added a -out option to most of the perl building scripts to allow output
debug information to be directed to a file.

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