/[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 372 by dpavlin, Sat Jun 19 18:16:20 2004 UTC revision 373 by dpavlin, Sun Jun 20 15:49:09 2004 UTC
# Line 12  use Log::Log4perl qw(get_logger :levels) Line 12  use Log::Log4perl qw(get_logger :levels)
12    
13  use Data::Dumper;  use Data::Dumper;
14    
15    #my $LOOKUP_REGEX = '\[[^\[\]]+\]';
16    #my $LOOKUP_REGEX_SAVE = '\[([^\[\]]+)\]';
17    my $LOOKUP_REGEX = 'lookup{[^\{\}]+}';
18    my $LOOKUP_REGEX_SAVE = 'lookup{([^\{\}]+)}';
19    
20  =head1 NAME  =head1 NAME
21    
22  WebPAC - base class for WebPAC  WebPAC - base class for WebPAC
# Line 217  sub fetch_rec { Line 222  sub fetch_rec {
222    
223          if ($mfn > $self->{'max_mfn'}) {          if ($mfn > $self->{'max_mfn'}) {
224                  $self->{'current_mfn'} = $self->{'max_mfn'};                  $self->{'current_mfn'} = $self->{'max_mfn'};
225                    $log->debug("at EOF");
226                  return;                  return;
227          }          }
228    
# Line 255  sub open_import_xml { Line 261  sub open_import_xml {
261    
262          $self->{'import_xml'} = XMLin($f,          $self->{'import_xml'} = XMLin($f,
263                  ForceArray => [ $self->{'tag'}, 'config', 'format' ],                  ForceArray => [ $self->{'tag'}, 'config', 'format' ],
                 ForceContent => 1  
264          );          );
265    
266  }  }
# Line 284  sub create_lookup { Line 289  sub create_lookup {
289                          my $key = $self->fill_in($rec,$i->{'key'});                          my $key = $self->fill_in($rec,$i->{'key'});
290                          my @val = $self->fill_in($rec,$i->{'val'});                          my @val = $self->fill_in($rec,$i->{'val'});
291                          if ($key && @val && eval $eval) {                          if ($key && @val && eval $eval) {
292                                    $log->debug("stored $key = ",sub { join(" | ",@val) });
293                                  push @{$self->{'lookup'}->{$key}}, @val;                                  push @{$self->{'lookup'}->{$key}}, @val;
294                          }                          }
295                  } else {                  } else {
296                          my $key = $self->fill_in($rec,$i->{'key'});                          my $key = $self->fill_in($rec,$i->{'key'});
297                          my @val = $self->fill_in($rec,$i->{'val'});                          my @val = $self->fill_in($rec,$i->{'val'});
298                          if ($key && @val) {                          if ($key && @val) {
299                                    $log->debug("stored $key = ",sub { join(" | ",@val) });
300                                  push @{$self->{'lookup'}->{$key}}, @val;                                  push @{$self->{'lookup'}->{$key}}, @val;
301                          }                          }
302                  }                  }
# Line 384  sub fill_in { Line 391  sub fill_in {
391          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);
392    
393          # do actual replacement of placeholders          # do actual replacement of placeholders
394          $format =~ s/v(\d+)(?:\^(\w))*/$self->get_data(\$rec,$1,$2,$i,\$found)/ges;          $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges;
395    
396          if ($found) {          if ($found) {
397                    $log->debug("format: $format");
398                  if ($eval_code) {                  if ($eval_code) {
399                          my $eval = $self->fill_in($rec,$eval_code,$i);                          my $eval = $self->fill_in($rec,$eval_code,$i);
400                          return if (! $self->_eval($eval));                          return if (! $self->_eval($eval));
401                  }                  }
402                  # do we have lookups?                  # do we have lookups?
403                  $log->debug("test format '$format' for lookups");                  if ($format =~ /$LOOKUP_REGEX/o) {
404                  if ($format =~ /\[[^\[\]]+\]/o) {                          $log->debug("format '$format' has lookup");
405                          return $self->lookup($format);                          return $self->lookup($format);
406                  } else {                  } else {
407                          return $format;                          return $format;
# Line 420  sub lookup { Line 428  sub lookup {
428    
429          my $tmp = shift || $log->logconfess("need format");          my $tmp = shift || $log->logconfess("need format");
430    
431          if ($tmp =~ /\[[^\[\]]+\]/o) {          if ($tmp =~ /$LOOKUP_REGEX/o) {
432                  my @in = ( $tmp );                  my @in = ( $tmp );
433    
434                  $log->debug("lookup for: ",$tmp);                  $log->debug("lookup for: ",$tmp);
435    
436                  my @out;                  my @out;
437                  while (my $f = shift @in) {                  while (my $f = shift @in) {
438                          if ($f =~ /\[([^\[\]]+)\]/) {                          if ($f =~ /$LOOKUP_REGEX_SAVE/o) {
439                                  my $k = $1;                                  my $k = $1;
440                                  if ($self->{'lookup'}->{$k}) {                                  if ($self->{'lookup'}->{$k}) {
441                                          foreach my $nv (@{$self->{'lookup'}->{$k}}) {                                          foreach my $nv (@{$self->{'lookup'}->{$k}}) {
442                                                  my $tmp2 = $f;                                                  my $tmp2 = $f;
443                                                  $tmp2 =~ s/\[$k\]/$nv/g;                                                  $tmp2 =~ s/lookup{$k}/$nv/g;
444                                                  push @in, $tmp2;                                                  push @in, $tmp2;
445                                          }                                          }
446                                  } else {                                  } else {
# Line 442  sub lookup { Line 450  sub lookup {
450                                  push @out, $f;                                  push @out, $f;
451                          }                          }
452                  }                  }
453                    $log->logconfess("return is array and it's not expected!") unless wantarray;
454                  return @out;                  return @out;
455          } else {          } else {
456                  return $tmp;                  return $tmp;
# Line 476  sub parse { Line 485  sub parse {
485    
486          my @out;          my @out;
487    
488            $log->debug("format: $format");
489    
490          my $eval_code;          my $eval_code;
491          # remove eval{...} from beginning          # remove eval{...} from beginning
492          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);          $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);
# Line 483  sub parse { Line 494  sub parse {
494          my $prefix;          my $prefix;
495          my $all_found=0;          my $all_found=0;
496    
497          while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))*//s) {          while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))?//s) {
498    
499                  my $del = $1 || '';                  my $del = $1 || '';
500                  $prefix ||= $del if ($all_found == 0);                  $prefix ||= $del if ($all_found == 0);
# Line 500  sub parse { Line 511  sub parse {
511    
512          return if (! $all_found);          return if (! $all_found);
513    
514          my $out = join('',@out) . $format;          my $out = join('',@out);
515    
516            if ($out) {
517                    # add rest of format (suffix)
518                    $out .= $format;
519    
520          # add prefix if not there                  # add prefix if not there
521          $out = $prefix . $out if ($out !~ m/^\Q$prefix\E/);                  $out = $prefix . $out if ($out !~ m/^\Q$prefix\E/);
522    
523                    $log->debug("result: $out");
524            }
525    
526          if ($eval_code) {          if ($eval_code) {
527                  my $eval = $self->fill_in($rec,$eval_code,$i);                  my $eval = $self->fill_in($rec,$eval_code,$i);
528                  $log->debug("about to eval ",$eval," [$out]");                  $log->debug("about to eval{",$eval,"} format: $out");
529                  return if (! $self->_eval($eval));                  return if (! $self->_eval($eval));
530          }          }
531    
# Line 539  sub parse_to_arr { Line 557  sub parse_to_arr {
557                  push @arr, $v;                  push @arr, $v;
558          }          }
559    
560            $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr);
561    
562          return @arr;          return @arr;
563  }  }
564    
565    =head2 fill_in_to_arr
566    
567    Similar to C<fill_in>, but returns array of all repeatable fields. Usable
568    for fields which have lookups, so they shouldn't be parsed but rather
569    C<fill_id>ed.
570    
571     my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]');
572    
573    =cut
574    
575    sub fill_in_to_arr {
576            my $self = shift;
577    
578            my ($rec, $format_utf8) = @_;
579    
580            my $log = $self->_get_logger();
581    
582            $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
583            return if (! $format_utf8);
584    
585            my $i = 0;
586            my @arr;
587    
588            while (my @v = $self->fill_in($rec,$format_utf8,$i++)) {
589                    push @arr, @v;
590            }
591    
592            $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr);
593    
594            return @arr;
595    }
596    
597    
598  =head2 data_structure  =head2 data_structure
599    
600  Create in-memory data structure which represents layout from C<import_xml>.  Create in-memory data structure which represents layout from C<import_xml>.
# Line 569  sub data_structure { Line 622  sub data_structure {
622    
623          my @ds;          my @ds;
624    
625            $log->debug("tags: ",sub { join(", ",@sorted_tags) });
626    
627          foreach my $field (@sorted_tags) {          foreach my $field (@sorted_tags) {
628    
629                  my $row;                  my $row;
# Line 576  sub data_structure { Line 631  sub data_structure {
631  #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}});  #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}});
632    
633                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {                  foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {
634                          my @v = $self->parse_to_arr($rec,$tag->{'content'});                          my $format = $tag->{'value'} || $tag->{'content'};
635    
636                            $log->debug("format: $format");
637    
638                            my @v;
639                            if ($format =~ /$LOOKUP_REGEX/o) {
640                                    @v = $self->fill_in_to_arr($rec,$format);
641                            } else {
642                                    @v = $self->parse_to_arr($rec,$format);
643                            }
644                          next if (! @v);                          next if (! @v);
645    
646                          # does tag have type?                          # does tag have type?
# Line 587  sub data_structure { Line 650  sub data_structure {
650                                  push @{$row->{'display'}}, @v;                                  push @{$row->{'display'}}, @v;
651                                  push @{$row->{'swish'}}, @v;                                  push @{$row->{'swish'}}, @v;
652                          }                          }
653    
654                  }                  }
655    
656                  if ($row) {                  if ($row) {
657                          $row->{'tag'} = $field;                          $row->{'tag'} = $field;
658                          push @ds, $row;                          push @ds, $row;
659                            $log->debug("row $field: ",sub { Dumper($row) });
660                  }                  }
661    
662          }          }

Legend:
Removed from v.372  
changed lines
  Added in v.373

  ViewVC Help
Powered by ViewVC 1.1.26