root/trunk/gsdl/perllib/plugouts/BasPlugout.pm @ 13225

Revision 13225, 23.5 KB (checked in by shaoqun, 14 years ago)

always use xalan package

  • Property svn:keywords set to Author Date Id Revision
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    # this OID already has an assigned directory, use the
521    # same one.
522    $doc_dir = $doc_info->[0];
523    $doc_dir =~ s/\/?((doc(mets)?)|(dublin_core))\.xml(\.gz)?$//;
524    } elsif ($self->{'keep_import_structure'}) {
525    $source_filename = &File::Basename::dirname($source_filename);
526    $source_filename =~ s/[\\\/]+/\//g;
527    $source_filename =~ s/\/$//;
528
529        $doc_dir = substr($source_filename, length($ENV{'GSDLIMPORTDIR'}) + 1);
530
531    }
532
533    # have to get a new document directory
534    $doc_dir = $self->get_new_doc_dir($working_info,$working_dir,$OID) unless $doc_dir ne "";
535
536    $doc_dir .= ".dir";
537    if (!defined $self->{'group'} || !$self->{'group'}){
538    &util::mk_all_dir (&util::filename_cat ($working_dir, $doc_dir));
539    }
540    return $doc_dir;
541}
542
543sub get_new_doc_dir{
544   my $self = shift (@_); 
545   my($working_info,$working_dir,$OID) = @_;     
546   
547   my $doc_dir = "";
548   my $doc_dir_rest = $OID;
549   my $doc_dir_num = 0;
550
551   do {
552       $doc_dir .= "/" if $doc_dir_num > 0;
553       if ($doc_dir_rest =~ s/^(.{1,8})//) {
554       $doc_dir .= $1;
555       $doc_dir_num++;
556       }
557   } while ($doc_dir_rest ne "" &&
558        ((-d &util::filename_cat ($working_dir, "$doc_dir.dir")) ||
559         ($working_info->size() >= 1024 && $doc_dir_num < 2)));
560   
561   
562   return $doc_dir;
563}
564
565sub process_assoc_files {
566    my $self = shift (@_);
567    my ($doc_obj, $doc_dir, $handle) = @_;
568
569    my $outhandle = $self->{'output_handle'};
570   
571    my $output_dir = $self->get_output_dir();
572    return if (!defined $output_dir);
573
574    &util::mk_all_dir ($output_dir) unless -e $output_dir;
575     
576    my $working_dir = &util::filename_cat($output_dir, $doc_dir);
577    &util::mk_all_dir ($working_dir) unless -e $working_dir;
578
579    my @assoc_files = ();
580    my $filename;;
581
582    my $source_filename = $doc_obj->get_source_filename();
583
584    my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
585
586    if (defined $collect_dir) {
587    my $dirsep_regexp = &util::get_os_dirsep();
588
589    if ($collect_dir !~ /$dirsep_regexp$/) {
590        $collect_dir .= &util::get_dirsep(); # ensure there is a slash at the end
591    }
592
593    # This test is never going to fail on Windows -- is this a problem?
594     
595    if ($source_filename !~ /^$dirsep_regexp/) {
596        $source_filename = &util::filename_cat($collect_dir, $source_filename);
597    }
598    }
599
600
601    # set the assocfile path (even if we have no assoc files - need this for lucene)
602    $doc_obj->set_utf8_metadata_element ($doc_obj->get_top_section(),
603                     "assocfilepath",
604                     "$doc_dir");
605    foreach my $assoc_file_rec (@{$doc_obj->get_assoc_files()}) {
606    my ($dir, $afile) = $assoc_file_rec->[1] =~ /^(.*?)([^\/\\]+)$/;
607    $dir = "" unless defined $dir;
608       
609   
610    my $real_filename = $assoc_file_rec->[0];
611    # for some reasons the image associate file has / before the full path
612    $real_filename =~ s/^\\(.*)/$1/i;
613    if (-e $real_filename) {
614
615        $filename = &util::filename_cat($working_dir, $afile);
616
617        &util::hard_link ($real_filename, $filename);
618       
619        $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
620                     "gsdlassocfile",
621                     "$afile:$assoc_file_rec->[2]:$dir");
622    } elsif ($self->{'verbosity'} > 2) {
623        print $outhandle "BasPlugout::process couldn't copy the associated file " .
624        "$real_filename to $afile\n";
625    }
626    }
627}
628
629sub set_sortmeta {
630    my $self = shift (@_);
631    my ($sortmeta, $removeprefix, $removesuffix) = @_;
632   
633    $self->{'sortmeta'} = $sortmeta;
634    if (defined ($removeprefix) && $removeprefix ) {
635    $removeprefix =~ s/^\^//; # don't need a leading ^
636    $self->{'removeprefix'} = $removeprefix;
637    }
638    if (defined ($removesuffix) && $removesuffix) {
639    $removesuffix =~ s/\$$//; # don't need a trailing $
640    $self->{'removesuffix'} = $removesuffix;
641    }
642}
643
644sub open_xslt_pipe
645{
646    my $self = shift @_;
647    my ($output_file_name, $xslt_file)=@_;
648
649   return unless defined $xslt_file and $xslt_file ne "" and -e $xslt_file;
650
651    my $java_class_path =  &util::filename_cat ($ENV{'GSDLHOME'},"bin","java");
652
653    if ($ENV{'GSDLOS'} eq "windows"){
654    $java_class_path .=";".&util::filename_cat ($ENV{'GSDLHOME'},"bin","java","xalan.jar");
655    }
656    else{
657    $java_class_path .=":".&util::filename_cat ($ENV{'GSDLHOME'},"bin","java","xalan.jar");
658    }
659
660
661    $java_class_path = "\"".$java_class_path."\"";
662
663    $xslt_file = "\"".$xslt_file."\"";
664
665    my $cmd = "| java -cp $java_class_path org.nzdl.gsdl.ApplyXSLT $xslt_file ";
666
667    if (defined $self->{'mapping_file'} and $self->{'mapping_file'} ne ""){
668    my $mapping_file_path = "\"".$self->{'mapping_file'}."\"";
669    $cmd .= $mapping_file_path;
670    }
671 
672    open(*XMLWRITER, $cmd)
673    or die "can't open pipe to xslt: $!";
674
675   
676    $self->{'xslt_writer'} = *XMLWRITER;
677
678    print XMLWRITER "<?DocStart?>\n";       
679    print XMLWRITER "$output_file_name\n";
680 
681  }
682 
683
684sub close_xslt_pipe
685{
686  my $self = shift @_;
687
688 
689  return unless defined $self->{'xslt_writer'} ;
690   
691  my $xsltwriter = $self->{'xslt_writer'};
692 
693  print $xsltwriter "<?DocEnd?>\n";
694  close($xsltwriter);
695
696  undef $self->{'xslt_writer'};
697
698}
699
700sub close_file_output
701{
702    my ($self) = @_;
703 
704    # make sure that the handle has been opened - it won't be if we failed
705    # to import any documents...
706    if (defined(fileno(GROUPPROCESS))) {
707    $self->output_xml_footer('GROUPPROCESS','Archive');   
708    close GROUPPROCESS;
709    }
710
711    my $OID = $self->{'gs_OID'};
712    my $short_doc_file = $self->{'short_doc_file'};
713   
714    if ($self->{'gzip'}) {
715    my $doc_file = $self->{'gs_filename'};
716    `gzip $doc_file`;
717    $doc_file .= ".gz";
718    $short_doc_file .= ".gz";
719    if (!-e $doc_file) {
720         my $outhandle = $self->{'output_handle'};
721        print $outhandle "error while gzipping: $doc_file doesn't exist\n";
722        return 0;
723    }
724    }
725
726    # store reference in output_info
727    my $output_info = $self->{'output_info'};
728    return 0 if (!defined $output_info);
729    $output_info->add_info($OID, $short_doc_file, undef, undef);
730    return 1;
731}
732
733
734#the subclass should implement this method if is_group method could return 1.
735sub close_group_output{
736   my $self = shift (@_);       
737}
738
739sub is_group {
740    my $self = shift (@_);
741    return 0;       
742}
743
744my $dc_set = { Title => 1,       
745           Creator => 1,
746           Subject => 1,
747           Description => 1,
748           Publisher => 1,
749           Contributor => 1,
750           Date => 1,
751           Type => 1,
752           Format => 1,
753           Identifier => 1,
754           Source => 1,
755           Language => 1,
756           Relation => 1,
757           Coverage => 1,
758           Rights => 1};
759
760
761# returns an XML representation of the dublin core metadata
762# if dc meta is not found, try ex mete
763sub get_dc_metadata {
764    my $self = shift(@_);
765    my ($doc_obj, $section, $version) = @_;
766   
767    # build up string of dublin core metadata
768    $section="" unless defined $section;
769   
770    my $section_ptr = $doc_obj->_lookup_section($section);
771    return "" unless defined $section_ptr;
772
773
774    my $explicit_dc = {};
775    my $explicit_ex = {};
776
777    my $all_text="";
778    foreach my $data (@{$section_ptr->{'metadata'}}){
779    my $escaped_value = &docprint::escape_text($data->[1]);
780    if ($data->[0]=~ m/^dc\./) {
781        $data->[0] =~ tr/[A-Z]/[a-z]/;
782
783        $data->[0] =~ m/^dc\.(.*)/;
784        my $dc_element =  $1;
785
786        if (!defined $explicit_dc->{$dc_element}) {
787        $explicit_dc->{$dc_element} = [];
788        }
789        push(@{$explicit_dc->{$dc_element}},$escaped_value);
790
791        if (defined $version && ($version eq "oai_dc")) {
792        $all_text .= "   <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
793        }
794        else {
795        # qualifier???
796        $all_text .= '   <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
797        }
798
799    }
800    elsif (($data->[0] =~ m/^ex\./) || ($data->[0] !~ m/\./)) {
801        $data->[0] =~ m/^(ex\.)?(.*)/;
802        my $ex_element =  $2;
803        my $lc_ex_element = lc($ex_element);
804
805        if (defined $dc_set->{$ex_element}) {
806        if (!defined $explicit_ex->{$lc_ex_element}) {
807            $explicit_ex->{$lc_ex_element} = [];
808        }
809        push(@{$explicit_ex->{$lc_ex_element}},$escaped_value);
810        }
811    }
812    }
813
814    # go through dc_set and for any element *not* defined in explicit_dc
815    # that do exist in explicit_ex, add it in as metadata
816    foreach my $k ( keys %$dc_set ) {
817    my $lc_k = lc($k);
818
819    if (!defined $explicit_dc->{$lc_k}) {
820        if (defined $explicit_ex->{$lc_k}) {
821
822        foreach my $v (@{$explicit_ex->{$lc_k}}) {
823            my $dc_element    = $lc_k;
824            my $escaped_value = $v;
825
826            if (defined $version && ($version eq "oai_dc")) {
827            $all_text .= "   <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
828            }
829            else {
830            $all_text .= '   <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
831            }
832           
833        }
834        }
835    }
836    }
837
838    if ($all_text eq "") {
839    $all_text .= "   There is no Dublin Core metatdata in this document\n";
840    }   
841    $all_text =~ s/[\x00-\x09\x0B\x0C\x0E-\x1F]//g;
842
843    return $all_text;
844}
845
846# Build up dublin_core metadata.  Priority given to dc.* over ex.*
847# This method was apparently added by Jeffrey and committed by Shaoqun.
848# But we don't know why it was added, so not using it anymore.
849sub new_get_dc_metadata {
850   
851    my $self = shift(@_);
852    my ($doc_obj, $section, $version) = @_;
853
854    # build up string of dublin core metadata
855    $section="" unless defined $section;
856   
857    my $section_ptr=$doc_obj->_lookup_section($section);
858    return "" unless defined $section_ptr;
859
860    my $all_text = "";
861    foreach my $data (@{$section_ptr->{'metadata'}}){
862    my $escaped_value = &docprint::escape_text($data->[1]);
863    my $dc_element =  $data->[0];
864   
865    my @array = split('\.',$dc_element);
866    my ($type,$name);
867
868    if(defined $array[1])
869    {
870        $type = $array[0];
871        $name = $array[1];
872    }
873    else
874    {
875        $type = "ex";
876        $name = $array[0];
877    }
878   
879    $all_text .= '   <Metadata Type="'. $type.'" Name="'.$name.'">'. $escaped_value. "</Metadata>\n";
880    }
881    return $all_text;
882}
883
884
8851;
Note: See TracBrowser for help on using the browser.