/[webpac]/trunk2/lib/WebPAC.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk2/lib/WebPAC.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 363 by dpavlin, Wed Jun 16 20:05:19 2004 UTC revision 366 by dpavlin, Thu Jun 17 01:44:25 2004 UTC
# Line 77  sub new { Line 77  sub new {
77    
78          $self->{indexer_config_file} = new Config::IniFiles( -file => $self->{config_file} ) || croak "can't open '$self->{config_file}'";          $self->{indexer_config_file} = new Config::IniFiles( -file => $self->{config_file} ) || croak "can't open '$self->{config_file}'";
79    
80            $self->{'utf2cp'} = Text::Iconv->new('UTF-8' ,$self->{'code_page'});
81          return $self;          return $self;
82  }  }
83    
# Line 209  sub open_import_xml { Line 210  sub open_import_xml {
210          my $arg = {@_};          my $arg = {@_};
211          confess "need type to load file from import_xml/" if (! $arg->{'type'});          confess "need type to load file from import_xml/" if (! $arg->{'type'});
212    
213          my $type = $arg->{'type'};          $self->{'type'} = $arg->{'type'};
214    
215          my $type_base = $type;          my $type_base = $arg->{'type'};
216          $type_base =~ s/_.*$//g;          $type_base =~ s/_.*$//g;
217    
218          my $f = "./import_xml/$type.xml";          $self->{'tag'} = $type2tag{$type_base};
219    
220            print STDERR "using type ",$self->{'type'}," tag ",$self->{'tag'},"\n" if ($self->{'debug'});
221    
222            my $f = "./import_xml/".$self->{'type'}.".xml";
223          confess "import_xml file '$f' doesn't exist!" if (! -e "$f");          confess "import_xml file '$f' doesn't exist!" if (! -e "$f");
224    
225          print STDERR "reading '$f'\n" if ($self->{'debug'});          print STDERR "reading '$f'\n" if ($self->{'debug'});
226    
227          $self->{'import_xml'} = XMLin($f,          $self->{'import_xml'} = XMLin($f,
228                  ForceArray => [ $type2tag{$type_base}, 'config', 'format' ],                  ForceArray => [ $self->{'tag'}, 'config', 'format' ],
229                  ForceContent => 1                  ForceContent => 1
230          );          );
231    
# Line 287  sub get_data { Line 292  sub get_data {
292                          return $$rec->{$f}->[$i]->{$sf};                          return $$rec->{$f}->[$i]->{$sf};
293                  } elsif ($$rec->{$f}->[$i]) {                  } elsif ($$rec->{$f}->[$i]) {
294                          $$found++ if (defined($$found));                          $$found++ if (defined($$found));
295                          return $$rec->{$f}->[$i];                          # it still might have subfield, just
296                            # not specified, so we'll dump all
297                            if ($$rec->{$f}->[$i] =~ /HASH/o) {
298                                    my $out;
299                                    foreach my $k (keys %{$$rec->{$f}->[$i]}) {
300                                            $out .= $$rec->{$f}->[$i]->{$k}." ";
301                                    }
302                                    return $out;
303                            } else {
304                                    return $$rec->{$f}->[$i];
305                            }
306                  }                  }
307          } else {          } else {
308                  return '';                  return '';
# Line 407  return output or nothing depending on ev Line 422  return output or nothing depending on ev
422  sub parse {  sub parse {
423          my $self = shift;          my $self = shift;
424    
425          my ($rec, $format, $i) = @_;          my ($rec, $format_utf8, $i) = @_;
426    
427            return if (! $format_utf8);
428    
429          confess("need HASH as first argument!") if ($rec !~ /HASH/o);          confess("need HASH as first argument!") if ($rec !~ /HASH/o);
430            confess("need utf2cp Text::Iconv object!") if (! $self->{'utf2cp'});
431    
432          $i = 0 if (! $i);          $i = 0 if (! $i);
433    
434            my $format = $self->{'utf2cp'}->convert($format_utf8) || confess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'});
435    
436          my @out;          my @out;
437    
438          my $eval_code;          my $eval_code;
# Line 454  sub parse { Line 474  sub parse {
474          return $out;          return $out;
475  }  }
476    
477    =head2 data_structure
478    
479    Create in-memory data structure which represents layout from C<import_xml>.
480    It is used later to produce output.
481    
482     my $ds = $webpac->data_structure($rec);
483    
484    =cut
485    
486    # private method _sort_by_order
487    # sort subrouting using order="" attribute
488    sub _sort_by_order {
489            my $self = shift;
490    
491            my $va = $self->{'import_xml'}->{'indexer'}->{$a}->{'order'} ||
492                    $self->{'import_xml'}->{'indexer'}->{$a};
493            my $vb = $self->{'import_xml'}->{'indexer'}->{$b}->{'order'} ||
494                    $self->{'import_xml'}->{'indexer'}->{$b};
495    
496            return $va <=> $vb;
497    }
498    
499    sub data_structure {
500            my $self = shift;
501    
502            my $rec = shift;
503            confess("need HASH as first argument!") if ($rec !~ /HASH/o);
504    
505            my @sorted_tags;
506            if ($self->{tags_by_order}) {
507                    @sorted_tags = @{$self->{tags_by_order}};
508            } else {
509                    @sorted_tags = sort { $self->_sort_by_order } keys %{$self->{'import_xml'}->{'indexer'}};
510                    $self->{tags_by_order} = \@sorted_tags;
511            }
512    
513            my $ds;
514    
515            foreach my $field (@sorted_tags) {
516    
517                    my $row;
518                    my $i = 0;
519    
520    #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}});
521    
522                    foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {
523    
524                            my $v = $self->parse($rec,$tag->{'content'},$i);
525    print "## $i:",$tag->{'content'}," = ",($v || 'null'),"\n";
526    
527                            next if (!$v || $v && $v eq '');
528    
529                            # does tag have type?
530                            if ($tag->{'type'}) {
531                                    push @{$row->{$tag->{'type'}}}, $v;
532                            } else {
533                                    push @{$row->{'display'}}, $v;
534                                    push @{$row->{'swish'}}, $v;
535                            }
536                    }
537    
538                    push @{$ds->{$field}}, $row if ($row);
539    
540            }
541    
542            print Dumper($ds);
543    
544    }
545    
546  1;  1;

Legend:
Removed from v.363  
changed lines
  Added in v.366

  ViewVC Help
Powered by ViewVC 1.1.26