source: trunk/gsdl/perllib/plugins/GAPlug.pm@ 10218

Last change on this file since 10218 was 10218, checked in by kjdon, 19 years ago

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1###########################################################################
2#
3# GAPlug.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) 2001 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# Processes GreenstoneArchive XML documents. Note that this plugin does no
27# syntax checking (though the XML::Parser module tests for
28# well-formedness). It's assumed that the GreenstoneArchive files conform
29# to their DTD.
30
31package GAPlug;
32
33use XMLPlug;
34#$%^
35use parse2;
36
37sub BEGIN {
38 @GAPlug::ISA = ('XMLPlug');
39}
40
41
42sub get_default_process_exp {
43 my $self = shift (@_);
44
45 return q^(?i)doc\.xml$^;
46}
47
48my $options = { 'name' => "GAPlug",
49 'desc' => "{GAPlug.desc}",
50 'abstract' => "no",
51 'inherits' => "yes" };
52
53sub new {
54 my ($class) = shift (@_);
55 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
56 push(@$pluginlist, $class);
57
58 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
59 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
60
61 my $self = (defined $hashArgOptLists)? new XMLPlug($pluginlist,$inputargs,$hashArgOptLists): new XMLPlug($pluginlist,$inputargs);
62
63 $self->{'section'} = "";
64 $self->{'section_level'} = 0;
65 $self->{'metadata_name'} = "";
66 $self->{'metadata_value'} = "";
67 $self->{'content'} = "";
68
69 return bless $self, $class;
70}
71
72sub xml_start_document {
73}
74
75sub xml_end_document {
76}
77
78sub xml_doctype {
79 my $self = shift(@_);
80
81 my ($expat, $name, $sysid, $pubid, $internal) = @_;
82
83 # allow the short-lived and badly named "GreenstoneArchive" files to be processed
84 # as well as the "Archive" files which should now be created by import.pl
85 die "" if ($name !~ /^(Greenstone)?Archive$/);
86
87 my $outhandle = $self->{'outhandle'};
88 print $outhandle "GAPlug: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
89 print STDERR "<Processing n='$self->{'file'}' p='GAPlug'>\n" if $self->{'gli'};
90
91}
92
93
94sub xml_start_tag {
95 my $self = shift(@_);
96 my ($expat, $element) = @_;
97
98 $self->{'element'} = $element;
99 if ($element eq "Section") {
100 if ($self->{'section_level'} == 0) {
101 $self->open_document();
102 } else {
103 my $doc_obj = $self->{'doc_obj'};
104 $self->{'section'} =
105 $doc_obj->insert_section($doc_obj->get_end_child($self->{'section'}));
106 }
107
108 $self->{'section_level'} ++;
109 }
110 elsif ($element eq "Metadata") {
111 $self->{'metadata_name'} = $_{'name'};
112 }
113}
114
115sub xml_end_tag {
116 my $self = shift(@_);
117 my ($expat, $element) = @_;
118
119 if ($element eq "Section") {
120 $self->{'section_level'} --;
121 $self->{'section'} = $self->{'doc_obj'}->get_parent_section ($self->{'section'});
122 $self->close_document() if $self->{'section_level'} == 0;
123 }
124 elsif ($element eq "Metadata") {
125 $self->{'doc_obj'}->add_utf8_metadata($self->{'section'}, $self->{'metadata_name'},$self->{'metadata_value'});
126 $self->{'metadata_name'} = "";
127 $self->{'metadata_value'} = "";
128 }
129 elsif ($element eq "Content" && $self->{'content'} ne "") {
130 $self->{'doc_obj'}->add_utf8_text($self->{'section'}, $self->{'content'});
131 $self->{'content'} = "";
132 }
133
134 $self->{'element'} = "";
135}
136
137sub xml_text {
138 my $self = shift(@_);
139 my ($expat) = @_;
140
141 if ($self->{'element'} eq "Metadata") {
142 $self->{'metadata_value'} .= $_;
143 }
144 elsif ($self->{'element'} eq "Content") {
145 $self->{'content'} .= $_;
146 }
147}
148
149sub open_document {
150 my $self = shift(@_);
151
152 # create a new document
153 $self->{'doc_obj'} = new doc ();
154 $self->{'section'} = "";
155}
156
157sub close_document {
158 my $self = shift(@_);
159
160 # add the associated files
161 my $assoc_files =
162 $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
163
164 # for when "assocfilepath" isn't the same directory that doc.xml is in...
165 my $assoc_filepath_list= $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "assocfilepath");
166
167 my $assoc_filepath=shift (@$assoc_filepath_list);
168 if (defined ($assoc_filepath)) {
169 # make absolute rather than relative...
170 $self->{'filename'} =~ m@^(.*[\\/]archives)@;
171 $assoc_filepath = "$1/$assoc_filepath/";
172 } else {
173 $assoc_filepath = $self->{'filename'};
174 $assoc_filepath =~ s/[^\\\/]*$//;
175 }
176
177 foreach my $assoc_file_info (@$assoc_files) {
178 my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
179 my $real_dir = &util::filename_cat($assoc_filepath, $assoc_file),
180 my $assoc_dir = (defined $dir && $dir ne "")
181 ? &util::filename_cat($dir, $assoc_file) : $assoc_file;
182 $self->{'doc_obj'}->associate_file($real_dir, $assoc_dir, $mime_type);
183 }
184 $self->{'doc_obj'}->delete_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
185
186 # process the document
187 $self->{'processor'}->process($self->{'doc_obj'}, $self->{'file'});
188}
189
190
1911;
192
193
Note: See TracBrowser for help on using the repository browser.