r9064@llin: dpavlin | 2005-11-23 01:15:24 +0100
[webpac2] / lib / WebPAC / Output / Estraier.pm
index 0fc5359..871237f 100644 (file)
@@ -91,11 +91,13 @@ sub new {
        my $encoding = $self->{'encoding'} || 'ISO-8859-2';
        $log->info("using encoding $encoding");
 
-       my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
+       $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or
+               $log->logdie("can't create conversion from $encoding to UTF-8");
 
        $self ? return $self : return undef;
 }
 
+
 =head2 add
 
 Adds one entry to database.
@@ -138,13 +140,13 @@ sub add {
        $log->debug("creating $uri");
 
        my $doc = HyperEstraier::Document->new;
-       $doc->add_attr('@uri', $uri);
+       $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );
 
        $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
 
        # filter all tags which have type defined
        my @tags = grep {
-               defined( $args->{'ds'}->{$_}->{$type} )
+               ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} )
        } keys %{ $args->{'ds'} };
 
        $log->debug("tags = ", join(",", @tags));
@@ -157,15 +159,22 @@ sub add {
 
                $log->logconfess("no values for $tag/$type") unless ($vals);
 
-               $doc->add_attr($tag, $vals);
-               $doc->add_hidden_text($vals);
+               $vals = $self->{'iconv'}->convert( $vals ) or
+                       $log->logdie("can't convert '$vals' to UTF-8");
+
+               $doc->add_attr( $tag, $vals );
+               $doc->add_hidden_text( $vals );
        }
 
        my $text = $args->{'text'};
-       $doc->add_text( $text ) if ( $text );
+       if ( $text ) {
+               $text = $self->{'iconv'}->convert( $text ) or
+                       $log->logdie("can't convert '$text' to UTF-8");
+               $doc->add_text( $text );
+       }
 
        $log->debug("adding ", sub { $doc->dump_draft } );
-       $self->{'db'}->put_doc($doc) || $log->die("can't add document $uri to index");
+       $self->{'db'}->put_doc($doc) || $log->logdie("can't add document $uri to index");
 
        return 1;
 }