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 | package GMLPlug;
|
---|
30 |
|
---|
31 | use html;
|
---|
32 | use BasPlug;
|
---|
33 | use util;
|
---|
34 | use doc;
|
---|
35 |
|
---|
36 | sub BEGIN {
|
---|
37 | @ISA = ('BasPlug');
|
---|
38 | }
|
---|
39 |
|
---|
40 | sub new {
|
---|
41 | my ($class) = @_;
|
---|
42 | $self = new BasPlug ();
|
---|
43 |
|
---|
44 | return bless $self, $class;
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | sub is_recursive {
|
---|
49 | my $self = shift (@_);
|
---|
50 |
|
---|
51 | return 0; # this is not a recursive plugin
|
---|
52 | }
|
---|
53 |
|
---|
54 | sub _unescape_text {
|
---|
55 | my ($text) = @_;
|
---|
56 |
|
---|
57 | # special characters in the gml encoding
|
---|
58 | $text =~ s/</</g;
|
---|
59 | $text =~ s/>/>/g;
|
---|
60 | $text =~ s/"/\"/g;
|
---|
61 | $text =~ s/&/&/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
|
---|
69 | sub 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*(\w+)=\"([^\"]*)\"//o)) {
|
---|
155 | $doc_obj->add_utf8_metadata($section, $1, &GMLPlug::_unescape_text($2))
|
---|
156 | if (defined $1 and defined $2);
|
---|
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 | $section = $doc_obj->get_parent_section ($section);
|
---|
167 | last if $section eq ""; # back to top level again (more than one document in gml file)
|
---|
168 | }
|
---|
169 |
|
---|
170 | # add the associated files
|
---|
171 | $assoc_files = $doc_obj->get_metadata($doc_obj->get_top_section(), "gsdlassocfile");
|
---|
172 | my ($assoc_file_info);
|
---|
173 | foreach $assoc_file_info (@$assoc_files) {
|
---|
174 | my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
|
---|
175 | $dir = "" unless defined $dir;
|
---|
176 | $doc_obj->associate_file(&util::filename_cat($parent_dir, $assoc_file),
|
---|
177 | &util::filename_cat($dir, $assoc_file), $mime_type);
|
---|
178 | }
|
---|
179 | $doc_obj->delete_metadata($doc_obj->get_top_section(), "gsdlassocfile");
|
---|
180 |
|
---|
181 | # add metadata
|
---|
182 | foreach $field (keys(%$metadata)) {
|
---|
183 | # $metadata->{$field} may be an array reference
|
---|
184 | if (ref ($metadata->{$field}) eq "ARRAY") {
|
---|
185 | map {
|
---|
186 | $doc_obj->add_metadata ($doc_obj->get_top_section(), $field, $_);
|
---|
187 | } @{$metadata->{$field}};
|
---|
188 | } else {
|
---|
189 | $doc_obj->add_metadata ($doc_obj->get_top_section(), $field, $metadata->{$field});
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | # assume the document has an OID
|
---|
194 |
|
---|
195 | # process the document
|
---|
196 | $processor->process($doc_obj, $file);
|
---|
197 |
|
---|
198 | $no_docs++;
|
---|
199 | last if ($maxdocs > -1 && $no_docs >= $maxdocs);
|
---|
200 | last unless defined $gml;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return $no_docs; # no of docs processed
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | 1;
|
---|