--- trunk/lib/WebPAC/Normalize.pm 2005/07/17 22:48:25 22 +++ trunk/lib/WebPAC/Normalize.pm 2005/11/20 20:13:39 74 @@ -2,6 +2,7 @@ use warnings; use strict; +use base 'WebPAC::Common'; use Data::Dumper; =head1 NAME @@ -10,11 +11,11 @@ =head1 VERSION -Version 0.01 +Version 0.02 =cut -our $VERSION = '0.01'; +our $VERSION = '0.02'; =head1 SYNOPSIS @@ -78,15 +79,17 @@ return length($_); }, ... }, - db => $webpac_db_obj, + db => $db_obj, lookup_regex => $lookup->regex, + lookup => $lookup_obj, ); Parametar C defines user supplied snippets of perl code which can be use with C notation. Recommended parametar C is used to enable parsing of lookups -in structures. +in structures. If you pass this parametar, you must also pass C +which is C object. =cut @@ -95,6 +98,19 @@ my $self = {@_}; bless($self, $class); + my $r = $self->{'lookup_regex'} ? 1 : 0; + my $l = $self->{'lookup'} ? 1 : 0; + + my $log = $self->_get_logger(); + + # those two must be in pair + if ( ($r & $l) != ($r || $l) ) { + my $log = $self->_get_logger(); + $log->logdie("lookup_regex and lookup must be in pair"); + } + + $log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); + $self ? return $self : return undef; } @@ -106,7 +122,7 @@ This structures are used to produce output. - my @ds = $webpac->data_structure($rec); + my $ds = $webpac->data_structure($rec); B @@ -127,8 +143,10 @@ my $cache_file; if ($self->{'db'}) { - my @ds = $self->{'db'}->load_ds($rec); - return @ds if (@ds); + my $ds = $self->{'db'}->load_ds($rec); + $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) }); + return $ds if ($ds); + $log->debug("cache miss, creating"); } undef $self->{'currnet_filename'}; @@ -142,7 +160,7 @@ $self->{tags_by_order} = \@sorted_tags; } - my @ds; + my $ds; $log->debug("tags: ",sub { join(", ",@sorted_tags) }); @@ -153,7 +171,10 @@ #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { - my $format = $tag->{'value'} || $tag->{'content'}; + my $format; + + $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); + $format = $tag->{'value'} || $tag->{'content'}; $log->debug("format: $format"); @@ -189,7 +210,7 @@ } # default types - my @types = qw(display swish); + my @types = qw(display search); # override by type attribute @types = ( $tag->{'type'} ) if ($tag->{'type'}); @@ -222,27 +243,33 @@ # TODO: name_sigular, name_plural my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; - $row->{'name'} = $name ? $self->_x($name) : $field; + my $row_name = $name ? $self->_x($name) : $field; # post-sort all values in field if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { $log->warn("sort at field tag not implemented"); } - push @ds, $row; + $ds->{$row_name} = $row; $log->debug("row $field: ",sub { Dumper($row) }); } } + $log->logdie("there is no current_filename defined! Do you have filename tag in conf/normalize/?.xml") unless ($self->{'current_filename'}); + $self->{'db'}->save_ds( - ds => \@ds, + ds => $ds, current_filename => $self->{'current_filename'}, headline => $self->{'headline'}, ) if ($self->{'db'}); - return @ds; + $log->debug("ds: ", sub { Dumper($ds) }); + + $log->logconfess("data structure returned is not array any more!") if wantarray; + + return $ds; } @@ -438,7 +465,11 @@ } # do we have lookups? if ($self->{'lookup'}) { - return $self->lookup($format); + if ($self->{'lookup'}->can('lookup')) { + return $self->{'lookup'}->lookup($format); + } else { + $log->warn("Have lookup object but can't invoke lookup method"); + } } else { return $format; } @@ -511,7 +542,7 @@ if ($sf && $$rec->{$f}->[$i]->{$sf}) { $$found++ if (defined($$found)); return $$rec->{$f}->[$i]->{$sf}; - } elsif ($$rec->{$f}->[$i]) { + } elsif (! $sf && $$rec->{$f}->[$i]) { $$found++ if (defined($$found)); # it still might have subfield, just # not specified, so we'll dump all @@ -524,6 +555,8 @@ } else { return $$rec->{$f}->[$i]; } + } else { + return ''; } } else { return ''; @@ -564,7 +597,7 @@ $log->debug("using format $name [$format] on $data to produce: $out"); if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { - return $self->lookup($out); + return $self->{'lookup'}->lookup($out); } else { return $out; }