source: main/trunk/greenstone2/perllib/plugins/ReadXMLFile.pm@ 23484

Last change on this file since 23484 was 23352, checked in by davidb, 14 years ago

Modifications to code to support filename encoding issues when tested under Windows

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
RevLine 
[2810]1###########################################################################
2#
[15871]3# ReadXMLFile.pm -- base class for XML plugins
[2810]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
[15871]26package ReadXMLFile;
[2810]27
[15871]28use BasePlugin;
[2810]29use doc;
[10254]30use strict;
31no strict 'refs'; # allow filehandles to be variables and viceversa
[2810]32
33sub BEGIN {
[15871]34 @ReadXMLFile::ISA = ('BasePlugin');
[2810]35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
36}
37
[8069]38use XMLParser;
[2810]39
[4744]40my $arguments =
41 [ { 'name' => "process_exp",
[15871]42 'desc' => "{BasePlugin.process_exp}",
[6408]43 'type' => "regexp",
[4744]44 'deft' => &get_default_process_exp(),
[9957]45 'reqd' => "no" },
46 { 'name' => "xslt",
[15871]47 'desc' => "{ReadXMLFile.xslt}",
[9957]48 'type' => "string",
49 'deft' => "",
[4744]50 'reqd' => "no" } ];
51
[15871]52my $options = { 'name' => "ReadXMLFile",
53 'desc' => "{ReadXMLFile.desc}",
[7244]54 'abstract' => "yes",
[4744]55 'inherits' => "yes",
56 'args' => $arguments };
[3540]57
[2810]58sub new {
[10218]59 my ($class) = shift (@_);
60 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
61 push(@$pluginlist, $class);
[2810]62
[15871]63 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
64 push(@{$hashArgOptLists->{"OptList"}},$options);
[10218]65
[15871]66 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
[9957]67
[11090]68 if ($self->{'info_only'}) {
[15871]69 # don't worry about creating the XML parser as all we want is the
70 # list of plugin options
[11090]71 return bless $self, $class;
72 }
73
[2810]74 my $parser = new XML::Parser('Style' => 'Stream',
[15871]75 'Pkg' => 'ReadXMLFile',
[14103]76 'PluginObj' => $self,
[2810]77 'Handlers' => {'Char' => \&Char,
78 'XMLDecl' => \&XMLDecl,
[16822]79 'Entity' => \&Entity,
[2810]80 'Doctype' => \&Doctype,
[16822]81 'Default' => \&Default
[14103]82 });
[13148]83
[2810]84 $self->{'parser'} = $parser;
85
86 return bless $self, $class;
87}
88
[13192]89# the inheriting class must implement this method to tell whether to parse this doc type
90sub get_doctype {
91 my $self = shift(@_);
[13221]92 die "$self The inheriting class must implement get_doctype method";
[13192]93}
94
95
[9957]96sub apply_xslt
97{
98 my $self = shift @_;
99 my ($xslt,$filename) = @_;
100
101 my $outhandle = $self->{'outhandle'};
[2810]102
[9957]103 my $xslt_filename = $xslt;
104
105 if (! -e $xslt_filename) {
106 # Look in main site directory
107 my $gsdlhome = $ENV{'GSDLHOME'};
108 $xslt_filename = &util::filename_cat($gsdlhome,$xslt);
109 }
110
111 if (! -e $xslt_filename) {
112 # Look in collection directory
[11661]113 my $coldir = $ENV{'GSDLCOLLECTDIR'};
[9957]114 $xslt_filename = &util::filename_cat($coldir,$xslt);
115 }
116
117 if (! -e $xslt_filename) {
118 print $outhandle "Warning: Unable to find XSLT $xslt\n";
119 if (open(XMLIN,"<$filename")) {
120
[10254]121 my $untransformed_xml = "";
[9957]122 while (defined (my $line = <XMLIN>)) {
123
124 $untransformed_xml .= $line;
125 }
126 close(XMLIN);
127
128 return $untransformed_xml;
129 }
130 else {
131 print $outhandle "Error: Unable to open file $filename\n";
132 print $outhandle " $!\n";
133 return "";
134 }
135
136 }
137
138 my $bin_java = &util::filename_cat($ENV{'GSDLHOME'},"bin","java");
139 my $jar_filename = &util::filename_cat($bin_java,"xalan.jar");
140 my $xslt_base_cmd = "java -jar $jar_filename";
141 my $xslt_cmd = "$xslt_base_cmd -IN \"$filename\" -XSL \"$xslt_filename\"";
142
143 my $transformed_xml = "";
144
145 if (open(XSLT_IN,"$xslt_cmd |")) {
146 while (defined (my $line = <XSLT_IN>)) {
147
148 $transformed_xml .= $line;
149 }
150 close(XSLT_IN);
151 }
152 else {
[10254]153 print $outhandle "Error: Unable to run command $xslt_cmd\n";
[9957]154 print $outhandle " $!\n";
155 }
156
157 return $transformed_xml;
158
159}
160
[16392]161sub can_process_this_file {
162 my $self = shift(@_);
163 my ($filename) = @_;
164
[17322]165 if (-f $filename && $self->SUPER::can_process_this_file($filename) && $self->check_doctype($filename)) {
[16392]166 return 1; # its a file for us
167 }
168 return 0;
169}
170
[13192]171sub check_doctype {
[14103]172 my $self = shift (@_);
[13192]173
174 my ($filename) = @_;
[14103]175
[13192]176 if (open(XMLIN,"<$filename")) {
177 my $doctype = $self->get_doctype();
178 ## check whether the doctype has the same name as the root element tag
179 while (defined (my $line = <XMLIN>)) {
180 ## find the root element
[13226]181 if ($line =~ /<([\w\d:]+)[\s>]/){
[13192]182 my $root = $1;
183 if ($root !~ $doctype){
184 close(XMLIN);
185 return 0;
186 }
187 else {
188 close(XMLIN);
189 return 1;
190 }
191 }
192 }
193 close(XMLIN);
194 }
195
196 return undef; # haven't found a valid line
197
198}
[9957]199
[2810]200sub read {
[14103]201 my $self = shift (@_);
[2810]202
[16392]203 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
[2810]204
[16392]205 # can we process this file??
206 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
207 return undef unless $self->can_process_this_file($filename_full_path);
208
[2810]209 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
[12844]210 $self->{'base_dir'} = $base_dir;
[2810]211 $self->{'file'} = $file;
[15871]212 $self->{'filename'} = $filename_full_path;
[20830]213 $self->{'filename_no_path'} = $filename_no_path;
[2810]214 $self->{'processor'} = $processor;
[23349]215
[17289]216 # this contains metadata passed in from running metadata_read with other plugins (eg from MetadataXMLPlugin)
217 # we are also using it to store up any metadata found during parsing the XML, so that it can be added to the doc obj.
[2810]218 $self->{'metadata'} = $metadata;
[9957]219
[17293]220 if ($self->parse_file($filename_full_path)) {
[17289]221 return 1; # processed the file
222 }
223 return -1;
224}
225
226
227sub parse_file {
228 my $self = shift (@_);
229 my ($filename_full_path, $file, $gli) = @_;
[2810]230 eval {
[9957]231 my $xslt = $self->{'xslt'};
232 if (defined $xslt && ($xslt ne "")) {
233 # perform xslt
[15871]234 my $transformed_xml = $self->apply_xslt($xslt,$filename_full_path);
[9957]235
236 # feed transformed file (now in memory as string) into XML parser
237 $self->{'parser'}->parse($transformed_xml);
238 }
239 else {
[15871]240 $self->{'parser'}->parsefile($filename_full_path);
[9957]241 }
[2810]242 };
[7900]243
[2810]244 if ($@) {
245
246 # parsefile may either croak somewhere in XML::Parser (e.g. because
[15871]247 # the document is not well formed) or die somewhere in ReadXMLFile or a
[2810]248 # derived plugin (e.g. because we're attempting to process a
249 # document whose DOCTYPE is not meant for this plugin). For the
250 # first case we'll print a warning and continue, for the second
251 # we'll just continue quietly
252
[10170]253 print STDERR "**** Error is: $@\n";
[7900]254
[2810]255 my ($msg) = $@ =~ /Carp::croak\(\'(.*?)\'\)/;
256 if (defined $msg) {
257 my $outhandle = $self->{'outhandle'};
258 my $plugin_name = ref ($self);
259 print $outhandle "$plugin_name failed to process $file ($msg)\n";
260 }
[7900]261
[3107]262 # reset ourself for the next document
263 $self->{'section_level'}=0;
[9584]264 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
[7362]265 return -1; # error during processing
[2810]266 }
[17289]267 return 1; # parsing was successful
[2810]268}
269
270sub get_default_process_exp {
271 my $self = shift (@_);
272
273 return q^(?i)\.xml$^;
274}
275
[14103]276sub StartDocument {$_[0]->{'PluginObj'}->xml_start_document(@_);}
277sub XMLDecl {$_[0]->{'PluginObj'}->xml_xmldecl(@_);}
278sub Entity {$_[0]->{'PluginObj'}->xml_entity(@_);}
279sub Doctype {$_[0]->{'PluginObj'}->xml_doctype(@_);}
280sub StartTag {$_[0]->{'PluginObj'}->xml_start_tag(@_);}
281sub EndTag {$_[0]->{'PluginObj'}->xml_end_tag(@_);}
282sub Text {$_[0]->{'PluginObj'}->xml_text(@_);}
283sub PI {$_[0]->{'PluginObj'}->xml_pi(@_);}
284sub EndDocument {$_[0]->{'PluginObj'}->xml_end_document(@_);}
285sub Default {$_[0]->{'PluginObj'}->xml_default(@_);}
[2810]286
287# This Char function overrides the one in XML::Parser::Stream to overcome a
288# problem where $expat->{Text} is treated as the return value, slowing
289# things down significantly in some cases.
290sub Char {
[9462]291 use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+
292 $_[0]->{'Text'} .= $_[1];
293 return undef;
[2810]294}
295
[18528]296
[2810]297# Called at the beginning of the XML document.
298sub xml_start_document {
299 my $self = shift(@_);
300 my ($expat) = @_;
301
302 $self->open_document();
303}
304
305# Called for XML declarations
306sub xml_xmldecl {
307 my $self = shift(@_);
308 my ($expat, $version, $encoding, $standalone) = @_;
309}
310
[2890]311# Called for XML entities
312sub xml_entity {
313 my $self = shift(@_);
314 my ($expat, $name, $val, $sysid, $pubid, $ndata) = @_;
315}
316
[2810]317# Called for DOCTYPE declarations - use die to bail out if this doctype
318# is not meant for this plugin
319sub xml_doctype {
320 my $self = shift(@_);
[13148]321
[2810]322 my ($expat, $name, $sysid, $pubid, $internal) = @_;
[15871]323 die "ReadXMLFile Cannot process XML document with DOCTYPE of $name";
[2810]324}
325
[13148]326
[2810]327# Called for every start tag. The $_ variable will contain a copy of the
328# tag and the %_ variable will contain the element's attributes.
329sub xml_start_tag {
330 my $self = shift(@_);
331 my ($expat, $element) = @_;
332}
333
334# Called for every end tag. The $_ variable will contain a copy of the tag.
335sub xml_end_tag {
336 my $self = shift(@_);
337 my ($expat, $element) = @_;
338}
339
340# Called just before start or end tags with accumulated non-markup text in
341# the $_ variable.
342sub xml_text {
343 my $self = shift(@_);
344 my ($expat) = @_;
345}
346
347# Called for processing instructions. The $_ variable will contain a copy
348# of the pi.
349sub xml_pi {
350 my $self = shift(@_);
351 my ($expat, $target, $data) = @_;
352}
353
354# Called at the end of the XML document.
355sub xml_end_document {
356 my $self = shift(@_);
357 my ($expat) = @_;
358
359 $self->close_document();
360}
361
362# Called for any characters not handled by the above functions.
363sub xml_default {
364 my $self = shift(@_);
365 my ($expat, $text) = @_;
366}
367
368sub open_document {
369 my $self = shift(@_);
370
[23352]371 my $metadata = $self->{'metadata'};
372 my $filename_full_path = $self->{'filename'};
[23349]373
[2810]374 # create a new document
[23352]375 my $doc_obj = $self->{'doc_obj'} = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
[23349]376
377 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
378
[23352]379 my $filename_no_path = $self->{'filename_no_path'};
380 my $plugin_filename_encoding = $self->{'filename_encoding'};
[23349]381 my $filename_encoding = $self->deduce_filename_encoding($filename_no_path,$metadata,$plugin_filename_encoding);
382
[23352]383 $self->set_Source_metadata($doc_obj, $filename_full_path, $filename_encoding);
[20830]384
[15871]385 # do we want other auto metadata here (see BasePlugin.read_into_doc_obj)
[2810]386}
387
388sub close_document {
389 my $self = shift(@_);
[8716]390 my $doc_obj = $self->{'doc_obj'};
[15871]391
392 # do we want other auto stuff here, see BasePlugin.read_into_doc_obj
393
[2810]394 # include any metadata passed in from previous plugins
395 # note that this metadata is associated with the top level section
[8716]396 $self->extra_metadata ($doc_obj,
397 $doc_obj->get_top_section(),
[2810]398 $self->{'metadata'});
399
400 # do any automatic metadata extraction
[8716]401 $self->auto_extract_metadata ($doc_obj);
[2810]402
403 # add an OID
[17026]404 $self->add_OID($doc_obj);
[2810]405
[7508]406 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
[8121]407 $doc_obj->add_metadata($doc_obj->get_top_section(), "FileFormat", "XML");
[5919]408
[2810]409 # process the document
[8716]410 $self->{'processor'}->process($doc_obj);
[2810]411
412 $self->{'num_processed'} ++;
[15871]413 undef $self->{'doc_obj'};
414 undef $doc_obj; # is this the same as above??
[2810]415}
416
4171;
418
[7900]419
420
421
Note: See TracBrowser for help on using the repository browser.