source: gsdl/trunk/perllib/plugouts/BasPlugout.pm@ 16694

Last change on this file since 16694 was 16252, checked in by mdewsnip, 16 years ago

Fixes to get_doc_dir() and get_new_doc_dir() so if you are importing the same document multiple times it doesn't generate a new archives directory with another ".dir" at the end every time.

  • Property svn:keywords set to Author Date Id Revision
File size: 23.6 KB
Line 
1###########################################################################
2#
3# BasPlugout.pm -- base class for all the plugout modules
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) 2006 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
26package BasPlugout;
27
28eval {require bytes};
29
30use strict;
31no strict 'subs';
32no strict 'refs';
33
34use gsprintf 'gsprintf';
35use printusage;
36use parse2;
37
38# suppress the annoying "subroutine redefined" warning that various
39# gets cause under perl 5.6
40$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
41
42my $arguments = [
43 { 'name' => "group_size",
44 'desc' => "{BasPlugout.group_size}",
45 'type' => "int",
46 'deft' => "1",
47 'reqd' => "no",
48 'hiddengli' => "no"},
49 { 'name' => "output_info",
50 'desc' => "{BasPlugout.output_info}",
51 'type' => "string",
52 'reqd' => "yes",
53 'hiddengli' => "yes"},
54 { 'name' => "xslt_file",
55 'desc' => "{BasPlugout.xslt_file}",
56 'type' => "string",
57 'reqd' => "no",
58 'hiddengli' => "no"},
59 { 'name' => "output_handle",
60 'desc' => "{BasPlugout.output_handle}",
61 'type' => "string",
62 'deft' => 'STDERR',
63 'reqd' => "no",
64 'hiddengli' => "yes"},
65 { 'name' => "verbosity",
66 'desc' => "{BasPlugout.verbosity}",
67 'type' => "int",
68 'deft' => "0",
69 'reqd' => "no",
70 'hiddengli' => "no"},
71 { 'name' => "gzip_output",
72 'desc' => "{BasPlugout.gzip_output}",
73 'type' => "flag",
74 'reqd' => "no",
75 'hiddengli' => "no"},
76 { 'name' => "debug",
77 'desc' => "{BasPlugout.debug}",
78 'type' => "flag",
79 'reqd' => "no",
80 'hiddengli' => "yes"}
81];
82
83my $options = { 'name' => "BasPlugout",
84 'desc' => "{BasPlugout.desc}",
85 'abstract' => "yes",
86 'inherits' => "no",
87 'args' => $arguments};
88
89sub new
90{
91 my $class = shift (@_);
92
93 my ($plugoutlist,$args,$hashArgOptLists) = @_;
94 push(@$plugoutlist, $class);
95
96 my $strPlugoutName = (defined $plugoutlist->[0]) ? $plugoutlist->[0] : $class;
97
98 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
99 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
100
101 my $self = {};
102 $self->{'plugout_type'} = $class;
103 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
104 $self->{"info_only"} = 0;
105
106 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
107 # the args, just return the object.
108 foreach my $strArg (@{$args})
109 {
110 if(defined $strArg && $strArg eq "-gsdlinfo")
111 {
112 $self->{"info_only"} = 1;
113 return bless $self, $class;
114 }
115 }
116
117 delete $self->{"info_only"};
118
119 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
120 {
121 my $classTempClass = bless $self, $class;
122 print STDERR "<BadPlugout d=$self->{'plugout_name'}>\n";
123 &gsprintf(STDERR, "\n{BasPlugout.bad_general_option}\n", $self->{'plugout_name'});
124 $classTempClass->print_txt_usage(""); # Use default resource bundle
125 die "\n";
126 }
127
128
129 if(defined $self->{'xslt_file'} && $self->{'xslt_file'} ne "")
130 {
131 ##$self->{'xslt_file'} =~ s/\"//g;##working on Windows???
132 print STDERR "Can not find $self->{'xslt_file'}, please make sure you have supplied the correct file path\n" and die "\n" unless (-e $self->{'xslt_file'});
133 }
134
135 $self->{'gs_count'} = 0;
136
137 $self->{'keep_import_structure'} = 0;
138
139 return bless $self, $class;
140
141}
142
143sub print_xml_usage
144{
145 my $self = shift(@_);
146 my $header = shift(@_);
147 my $high_level_information_only = shift(@_);
148
149 # XML output is always in UTF-8
150 gsprintf::output_strings_in_UTF8;
151
152 if ($header) {
153 &PrintUsage::print_xml_header("plugout");
154 }
155 $self->print_xml($high_level_information_only);
156}
157
158
159sub print_xml
160{
161 my $self = shift(@_);
162 my $high_level_information_only = shift(@_);
163
164 my $optionlistref = $self->{'option_list'};
165 my @optionlist = @$optionlistref;
166 my $plugoutoptions = shift(@$optionlistref);
167 return if (!defined($plugoutoptions));
168
169 gsprintf(STDERR, "<PlugoutInfo>\n");
170 gsprintf(STDERR, " <Name>$plugoutoptions->{'name'}</Name>\n");
171 my $desc = gsprintf::lookup_string($plugoutoptions->{'desc'});
172 $desc =~ s/</&amp;lt;/g; # doubly escaped
173 $desc =~ s/>/&amp;gt;/g;
174 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
175 gsprintf(STDERR, " <Abstract>$plugoutoptions->{'abstract'}</Abstract>\n");
176 gsprintf(STDERR, " <Inherits>$plugoutoptions->{'inherits'}</Inherits>\n");
177 unless (defined($high_level_information_only)) {
178 gsprintf(STDERR, " <Arguments>\n");
179 if (defined($plugoutoptions->{'args'})) {
180 &PrintUsage::print_options_xml($plugoutoptions->{'args'});
181 }
182 gsprintf(STDERR, " </Arguments>\n");
183
184 # Recurse up the plugout hierarchy
185 $self->print_xml();
186 }
187 gsprintf(STDERR, "</PlugoutInfo>\n");
188}
189
190
191sub print_txt_usage
192{
193 my $self = shift(@_);
194
195 # Print the usage message for a plugout (recursively)
196 my $descoffset = $self->determine_description_offset(0);
197 $self->print_plugout_usage($descoffset, 1);
198}
199
200sub determine_description_offset
201{
202 my $self = shift(@_);
203 my $maxoffset = shift(@_);
204
205 my $optionlistref = $self->{'option_list'};
206 my @optionlist = @$optionlistref;
207 my $plugoutoptions = pop(@$optionlistref);
208 return $maxoffset if (!defined($plugoutoptions));
209
210 # Find the length of the longest option string of this download
211 my $plugoutargs = $plugoutoptions->{'args'};
212 if (defined($plugoutargs)) {
213 my $longest = &PrintUsage::find_longest_option_string($plugoutargs);
214 if ($longest > $maxoffset) {
215 $maxoffset = $longest;
216 }
217 }
218
219 # Recurse up the download hierarchy
220 $maxoffset = $self->determine_description_offset($maxoffset);
221 $self->{'option_list'} = \@optionlist;
222 return $maxoffset;
223}
224
225
226sub print_plugout_usage
227{
228 my $self = shift(@_);
229 my $descoffset = shift(@_);
230 my $isleafclass = shift(@_);
231
232 my $optionlistref = $self->{'option_list'};
233 my @optionlist = @$optionlistref;
234 my $plugoutoptions = shift(@$optionlistref);
235 return if (!defined($plugoutoptions));
236
237 my $plugoutname = $plugoutoptions->{'name'};
238 my $plugoutargs = $plugoutoptions->{'args'};
239 my $plugoutdesc = $plugoutoptions->{'desc'};
240
241 # Produce the usage information using the data structure above
242 if ($isleafclass) {
243 if (defined($plugoutdesc)) {
244 gsprintf(STDERR, "$plugoutdesc\n\n");
245 }
246 gsprintf(STDERR, " {common.usage}: plugout $plugoutname [{common.options}]\n\n");
247 }
248
249 # Display the download options, if there are some
250 if (defined($plugoutargs)) {
251 # Calculate the column offset of the option descriptions
252 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
253
254 if ($isleafclass) {
255 gsprintf(STDERR, " {common.specific_options}:\n");
256 }
257 else {
258 gsprintf(STDERR, " {common.general_options}:\n", $plugoutname);
259 }
260
261 # Display the download options
262 &PrintUsage::print_options_txt($plugoutargs, $optiondescoffset);
263 }
264
265 # Recurse up the download hierarchy
266 $self->print_plugout_usage($descoffset, 0);
267 $self->{'option_list'} = \@optionlist;
268}
269
270
271sub error
272{
273 my ($strFunctionName,$strError) = @_;
274 {
275 print "Error occoured in BasPlugout.pm\n".
276 "In Function: ".$strFunctionName."\n".
277 "Error Message: ".$strError."\n";
278 exit(-1);
279 }
280}
281
282# OIDtype may be "hash" or "incremental" or "dirname" or "assigned"
283sub set_OIDtype {
284 my $self = shift (@_);
285 my ($type, $metadata) = @_;
286
287 if ($type =~ /^(hash|incremental|dirname|assigned)$/) {
288 $self->{'OIDtype'} = $type;
289 } else {
290 $self->{'OIDtype'} = "hash";
291 }
292 if ($type =~ /^assigned$/) {
293 if (defined $metadata) {
294 $self->{'OIDmetadata'} = $metadata;
295 } else {
296 $self->{'OIDmetadata'} = "dc.Identifier";
297 }
298 }
299}
300
301sub set_output_dir
302{
303 my $self = shift @_;
304 my ($output_dir) = @_;
305
306 $self->{'output_dir'} = $output_dir;
307}
308
309sub setoutputdir
310{
311 my $self = shift @_;
312 my ($output_dir) = @_;
313
314 $self->{'output_dir'} = $output_dir;
315}
316
317sub get_output_dir
318{
319 my $self = shift (@_);
320
321 return $self->{'output_dir'};
322}
323
324sub getoutputdir
325{
326 my $self = shift (@_);
327
328 return $self->{'output_dir'};
329}
330
331sub getoutputinfo
332{
333 my $self = shift (@_);
334
335 return $self->{'output_info'};
336}
337
338
339sub get_output_handler
340{
341 my $self = shift (@_);
342
343 my ($output_file_name) = @_;
344
345 open(*OUTPUT, ">$output_file_name") or die "Can not open a file handler for $output_file_name\n";
346
347 return *OUTPUT;
348}
349
350sub release_output_handler
351{
352 my $self = shift (@_);
353 my ($outhandler) = @_;
354
355 close($outhandler);
356
357}
358
359sub output_xml_header {
360 my $self = shift (@_);
361 my ($handle,$docroot,$nondoctype) = @_;
362
363 print $handle '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
364
365 if (!defined $nondoctype){
366 print $handle '<!DOCTYPE Archive SYSTEM "http://greenstone.org/dtd/Archive/1.0/Archive.dtd">' . "\n";
367 }
368
369 print $handle "<$docroot>\n" if defined $docroot;
370}
371
372sub output_xml_footer {
373 my $self = shift (@_);
374 my ($handle,$docroot) = @_;
375 print $handle "</$docroot>\n" if defined $docroot;
376}
377
378sub process {
379 my $self = shift (@_);
380 my ($doc_obj) = @_;
381
382 $doc_obj->set_lastmodified();
383
384 if ($self->{'group_size'} > 1) {
385 $self->group_process ($doc_obj);
386 return;
387 }
388
389 my $OID = $doc_obj->get_OID();
390 $OID = "NULL" unless defined $OID;
391
392 my $top_section = $doc_obj->get_top_section();
393
394 #get document's directory
395 my $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
396
397 my $output_info = $self->{'output_info'};
398 return if (!defined $output_info);
399
400 ##############################
401 # call subclass' saveas method
402 ##############################
403 $self->saveas($doc_obj,$doc_dir);
404
405}
406
407sub store_output_info_reference {
408 my $self = shift (@_);
409 my ($doc_obj) = @_;
410
411 my $output_info = $self->{'output_info'};
412 my $metaname = $self->{'sortmeta'};
413 if (!defined $metaname || $metaname !~ /\S/) {
414 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, "");
415 return;
416 }
417
418 my $metadata = "";
419 my $top_section = $doc_obj->get_top_section();
420
421 my @commameta_list = split(/,/, $metaname);
422 foreach my $cmn (@commameta_list) {
423 my $meta = $doc_obj->get_metadata_element($top_section, $cmn);
424 if ($meta) {
425 # do remove prefix/suffix - this will apply to all values
426 $meta =~ s/^$self->{'removeprefix'}// if defined $self->{'removeprefix'};
427 $meta =~ s/$self->{'removesuffix'}$// if defined $self->{'removesuffix'};
428 $meta = &sorttools::format_metadata_for_sorting($cmn, $meta, $doc_obj);
429 $metadata .= $meta if ($meta);
430 }
431 }
432
433 # store reference in the output_info
434 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, $metadata);
435
436}
437
438sub group_process {
439
440 my $self = shift (@_);
441 my ($doc_obj) = @_;
442
443 my $OID = $doc_obj->get_OID();
444 $OID = "NULL" unless defined $OID;
445
446 my $groupsize = $self->{'group_size'};
447 my $gs_count = $self->{'gs_count'};
448 my $open_new_file = (($gs_count % $groupsize)==0);
449 my $outhandle = $self->{'output_handle'};
450
451 # opening a new file, or document has assoicated files => directory needed
452 if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0)) {
453
454 # The directory the archive file (doc.xml) and all associated files
455 # should end up in
456 my $doc_dir;
457 # If we've determined its time for a new file, open it now
458 if ($open_new_file || !defined($self->{'gs_doc_dir'}))
459 {
460 $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
461 # only if opening new file
462 my $output_dir = $self->get_output_dir();
463 &util::mk_all_dir ($output_dir) unless -e $output_dir;
464 my $doc_file = &util::filename_cat ($output_dir, $doc_dir, "doc.xml");
465 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.xml");
466
467 if ($gs_count>0)
468 {
469 return if (!$self->close_file_output());
470 }
471
472 open (GROUPPROCESS, ">$doc_file") or (print $outhandle "BasPlugout::group_process could not write to file $doc_file\n" and return);
473
474
475 $self->{'gs_filename'} = $doc_file;
476 $self->{'short_doc_file'} = $short_doc_file;
477 $self->{'gs_OID'} = $OID;
478 $self->{'gs_doc_dir'} = $doc_dir;
479
480 $self->output_xml_header('BasPlugout::GROUPPROCESS','Archive');
481 }
482 # Otherwise load the same archive document directory used last time
483 else
484 {
485 $doc_dir = $self->{'gs_doc_dir'};
486 }
487
488 # copy all the associated files, add this information as metadata
489 # to the document
490 print $outhandle "Writing associated files to $doc_dir\n";
491 $self->process_assoc_files ($doc_obj, $doc_dir);
492 }
493
494 # save this document
495 my $section_text = &docprint::get_section_xml($doc_obj,$doc_obj->get_top_section());
496 print GROUPPROCESS $section_text;
497
498 $self->{'gs_count'}++;
499}
500
501
502sub saveas {
503 my $self = shift (@_);
504
505 die "Basplug::saveas function must be implemented in sub classes\n";
506}
507
508sub get_doc_dir {
509 my $self = shift (@_);
510 my ($OID, $source_filename) = @_;
511
512 my $working_dir = $self->get_output_dir();
513 my $working_info = $self->{output_info};
514 return if (!defined $working_info);
515
516 my $doc_info = $working_info->get_info($OID);
517 my $doc_dir = '';
518
519 if (defined $doc_info && scalar(@$doc_info) >= 1)
520 {
521 # This OID already has an archives directory, so use it again
522 $doc_dir = $doc_info->[0];
523 $doc_dir =~ s/\/?((doc(mets)?)|(dublin_core))\.xml(\.gz)?$//;
524 }
525 elsif ($self->{'keep_import_structure'})
526 {
527 $source_filename = &File::Basename::dirname($source_filename);
528 $source_filename =~ s/[\\\/]+/\//g;
529 $source_filename =~ s/\/$//;
530
531 $doc_dir = substr($source_filename, length($ENV{'GSDLIMPORTDIR'}) + 1);
532 }
533
534 # We have to use a new archives directory for this document
535 if ($doc_dir eq "")
536 {
537 $doc_dir = $self->get_new_doc_dir ($working_info, $working_dir, $OID);
538 }
539
540 if (!defined $self->{'group'} || !$self->{'group'}){
541 &util::mk_all_dir (&util::filename_cat ($working_dir, $doc_dir));
542 }
543
544 return $doc_dir;
545}
546
547sub get_new_doc_dir{
548 my $self = shift (@_);
549 my($working_info,$working_dir,$OID) = @_;
550
551 my $doc_dir = "";
552 my $doc_dir_rest = $OID;
553 my $doc_dir_num = 0;
554
555 do {
556 $doc_dir .= "/" if $doc_dir_num > 0;
557 if ($doc_dir_rest =~ s/^(.{1,8})//) {
558 $doc_dir .= $1;
559 $doc_dir_num++;
560 }
561 } while ($doc_dir_rest ne "" &&
562 ((-d &util::filename_cat ($working_dir, "$doc_dir.dir")) ||
563 ($working_info->size() >= 1024 && $doc_dir_num < 2)));
564
565 return "$doc_dir.dir";
566}
567
568sub process_assoc_files {
569 my $self = shift (@_);
570 my ($doc_obj, $doc_dir, $handle) = @_;
571
572 my $outhandle = $self->{'output_handle'};
573
574 my $output_dir = $self->get_output_dir();
575 return if (!defined $output_dir);
576
577 &util::mk_all_dir ($output_dir) unless -e $output_dir;
578
579 my $working_dir = &util::filename_cat($output_dir, $doc_dir);
580 &util::mk_all_dir ($working_dir) unless -e $working_dir;
581
582 my @assoc_files = ();
583 my $filename;;
584
585 my $source_filename = $doc_obj->get_source_filename();
586
587 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
588
589 if (defined $collect_dir) {
590 my $dirsep_regexp = &util::get_os_dirsep();
591
592 if ($collect_dir !~ /$dirsep_regexp$/) {
593 $collect_dir .= &util::get_dirsep(); # ensure there is a slash at the end
594 }
595
596 # This test is never going to fail on Windows -- is this a problem?
597
598 if ($source_filename !~ /^$dirsep_regexp/) {
599 $source_filename = &util::filename_cat($collect_dir, $source_filename);
600 }
601 }
602
603
604 # set the assocfile path (even if we have no assoc files - need this for lucene)
605 $doc_obj->set_utf8_metadata_element ($doc_obj->get_top_section(),
606 "assocfilepath",
607 "$doc_dir");
608 foreach my $assoc_file_rec (@{$doc_obj->get_assoc_files()}) {
609 my ($dir, $afile) = $assoc_file_rec->[1] =~ /^(.*?)([^\/\\]+)$/;
610 $dir = "" unless defined $dir;
611
612
613 my $real_filename = $assoc_file_rec->[0];
614 # for some reasons the image associate file has / before the full path
615 $real_filename =~ s/^\\(.*)/$1/i;
616 if (-e $real_filename) {
617
618 $filename = &util::filename_cat($working_dir, $afile);
619
620 &util::hard_link ($real_filename, $filename);
621
622 $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
623 "gsdlassocfile",
624 "$afile:$assoc_file_rec->[2]:$dir");
625 } elsif ($self->{'verbosity'} > 2) {
626 print $outhandle "BasPlugout::process couldn't copy the associated file " .
627 "$real_filename to $afile\n";
628 }
629 }
630}
631
632sub set_sortmeta {
633 my $self = shift (@_);
634 my ($sortmeta, $removeprefix, $removesuffix) = @_;
635
636 $self->{'sortmeta'} = $sortmeta;
637 if (defined ($removeprefix) && $removeprefix ) {
638 $removeprefix =~ s/^\^//; # don't need a leading ^
639 $self->{'removeprefix'} = $removeprefix;
640 }
641 if (defined ($removesuffix) && $removesuffix) {
642 $removesuffix =~ s/\$$//; # don't need a trailing $
643 $self->{'removesuffix'} = $removesuffix;
644 }
645}
646
647sub open_xslt_pipe
648{
649 my $self = shift @_;
650 my ($output_file_name, $xslt_file)=@_;
651
652 return unless defined $xslt_file and $xslt_file ne "" and -e $xslt_file;
653
654 my $java_class_path = &util::filename_cat ($ENV{'GSDLHOME'},"bin","java");
655
656 my $mapping_file_path = "";
657
658 if ($ENV{'GSDLOS'} eq "windows"){
659 $java_class_path .=";".&util::filename_cat ($ENV{'GSDLHOME'},"bin","java","xalan.jar");
660 $xslt_file = "\"file:///".$xslt_file."\"";
661 $mapping_file_path = "\"file:///";
662 }
663 else{
664 $java_class_path .=":".&util::filename_cat ($ENV{'GSDLHOME'},"bin","java","xalan.jar");
665 }
666
667
668 $java_class_path = "\"".$java_class_path."\"";
669
670 my $cmd = "| java -cp $java_class_path org.nzdl.gsdl.ApplyXSLT -t $xslt_file ";
671
672 if (defined $self->{'mapping_file'} and $self->{'mapping_file'} ne ""){
673 my $mapping_file_path = "\"".$self->{'mapping_file'}."\"";
674 $cmd .= "-m $mapping_file_path";
675 }
676
677 open(*XMLWRITER, $cmd)
678 or die "can't open pipe to xslt: $!";
679
680
681 $self->{'xslt_writer'} = *XMLWRITER;
682
683 print XMLWRITER "<?DocStart?>\n";
684 print XMLWRITER "$output_file_name\n";
685
686
687 }
688
689
690sub close_xslt_pipe
691{
692 my $self = shift @_;
693
694
695 return unless defined $self->{'xslt_writer'} ;
696
697 my $xsltwriter = $self->{'xslt_writer'};
698
699 print $xsltwriter "<?DocEnd?>\n";
700 close($xsltwriter);
701
702 undef $self->{'xslt_writer'};
703
704}
705
706sub close_file_output
707{
708 my ($self) = @_;
709
710 # make sure that the handle has been opened - it won't be if we failed
711 # to import any documents...
712 if (defined(fileno(GROUPPROCESS))) {
713 $self->output_xml_footer('GROUPPROCESS','Archive');
714 close GROUPPROCESS;
715 }
716
717 my $OID = $self->{'gs_OID'};
718 my $short_doc_file = $self->{'short_doc_file'};
719
720 if ($self->{'gzip'}) {
721 my $doc_file = $self->{'gs_filename'};
722 `gzip $doc_file`;
723 $doc_file .= ".gz";
724 $short_doc_file .= ".gz";
725 if (!-e $doc_file) {
726 my $outhandle = $self->{'output_handle'};
727 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
728 return 0;
729 }
730 }
731
732 # store reference in output_info
733 my $output_info = $self->{'output_info'};
734 return 0 if (!defined $output_info);
735 $output_info->add_info($OID, $short_doc_file, undef, undef);
736 return 1;
737}
738
739
740#the subclass should implement this method if is_group method could return 1.
741sub close_group_output{
742 my $self = shift (@_);
743}
744
745sub is_group {
746 my $self = shift (@_);
747 return 0;
748}
749
750my $dc_set = { Title => 1,
751 Creator => 1,
752 Subject => 1,
753 Description => 1,
754 Publisher => 1,
755 Contributor => 1,
756 Date => 1,
757 Type => 1,
758 Format => 1,
759 Identifier => 1,
760 Source => 1,
761 Language => 1,
762 Relation => 1,
763 Coverage => 1,
764 Rights => 1};
765
766
767# returns an XML representation of the dublin core metadata
768# if dc meta is not found, try ex mete
769sub get_dc_metadata {
770 my $self = shift(@_);
771 my ($doc_obj, $section, $version) = @_;
772
773 # build up string of dublin core metadata
774 $section="" unless defined $section;
775
776 my $section_ptr = $doc_obj->_lookup_section($section);
777 return "" unless defined $section_ptr;
778
779
780 my $explicit_dc = {};
781 my $explicit_ex = {};
782
783 my $all_text="";
784 foreach my $data (@{$section_ptr->{'metadata'}}){
785 my $escaped_value = &docprint::escape_text($data->[1]);
786 if ($data->[0]=~ m/^dc\./) {
787 $data->[0] =~ tr/[A-Z]/[a-z]/;
788
789 $data->[0] =~ m/^dc\.(.*)/;
790 my $dc_element = $1;
791
792 if (!defined $explicit_dc->{$dc_element}) {
793 $explicit_dc->{$dc_element} = [];
794 }
795 push(@{$explicit_dc->{$dc_element}},$escaped_value);
796
797 if (defined $version && ($version eq "oai_dc")) {
798 $all_text .= " <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
799 }
800 else {
801 # qualifier???
802 $all_text .= ' <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
803 }
804
805 }
806 elsif (($data->[0] =~ m/^ex\./) || ($data->[0] !~ m/\./)) {
807 $data->[0] =~ m/^(ex\.)?(.*)/;
808 my $ex_element = $2;
809 my $lc_ex_element = lc($ex_element);
810
811 if (defined $dc_set->{$ex_element}) {
812 if (!defined $explicit_ex->{$lc_ex_element}) {
813 $explicit_ex->{$lc_ex_element} = [];
814 }
815 push(@{$explicit_ex->{$lc_ex_element}},$escaped_value);
816 }
817 }
818 }
819
820 # go through dc_set and for any element *not* defined in explicit_dc
821 # that does exist in explicit_ex, add it in as metadata
822 foreach my $k ( keys %$dc_set ) {
823 my $lc_k = lc($k);
824
825 if (!defined $explicit_dc->{$lc_k}) {
826 if (defined $explicit_ex->{$lc_k}) {
827
828 foreach my $v (@{$explicit_ex->{$lc_k}}) {
829 my $dc_element = $lc_k;
830 my $escaped_value = $v;
831
832 if (defined $version && ($version eq "oai_dc")) {
833 $all_text .= " <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
834 }
835 else {
836 $all_text .= ' <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
837 }
838
839 }
840 }
841 }
842 }
843
844 if ($all_text eq "") {
845 $all_text .= " There is no Dublin Core metatdata in this document\n";
846 }
847 $all_text =~ s/[\x00-\x09\x0B\x0C\x0E-\x1F]//g;
848
849 return $all_text;
850}
851
852# Build up dublin_core metadata. Priority given to dc.* over ex.*
853# This method was apparently added by Jeffrey and committed by Shaoqun.
854# But we don't know why it was added, so not using it anymore.
855sub new_get_dc_metadata {
856
857 my $self = shift(@_);
858 my ($doc_obj, $section, $version) = @_;
859
860 # build up string of dublin core metadata
861 $section="" unless defined $section;
862
863 my $section_ptr=$doc_obj->_lookup_section($section);
864 return "" unless defined $section_ptr;
865
866 my $all_text = "";
867 foreach my $data (@{$section_ptr->{'metadata'}}){
868 my $escaped_value = &docprint::escape_text($data->[1]);
869 my $dc_element = $data->[0];
870
871 my @array = split('\.',$dc_element);
872 my ($type,$name);
873
874 if(defined $array[1])
875 {
876 $type = $array[0];
877 $name = $array[1];
878 }
879 else
880 {
881 $type = "ex";
882 $name = $array[0];
883 }
884
885 $all_text .= ' <Metadata Type="'. $type.'" Name="'.$name.'">'. $escaped_value. "</Metadata>\n";
886 }
887 return $all_text;
888}
889
890
8911;
Note: See TracBrowser for help on using the repository browser.