--- trunk/lib/WebPAC/Output/MARC.pm 2006/08/25 18:06:42 621 +++ trunk/lib/WebPAC/Output/MARC.pm 2006/09/04 16:15:07 626 @@ -8,6 +8,7 @@ use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net use MARC::Lint; use Data::Dump qw/dump/; +use Encode qw/from_to decode/; =head1 NAME @@ -15,11 +16,11 @@ =head1 VERSION -Version 0.01 +Version 0.02 =cut -our $VERSION = '0.01'; +our $VERSION = '0.02'; =head1 SYNOPSIS @@ -33,7 +34,8 @@ my $marc = new WebPAC::Output::MARC( path => '/path/to/output.marc', - encoding => 'utf-8', + native_encoding => 'iso-8859-2', + marc_encoding => 'utf-8', lint => 1, dump => 0, ) @@ -61,7 +63,8 @@ $log->logconfess("new called without path"); } - $self->{encoding} ||= 'utf-8'; + $self->{native_encoding} ||= 'iso-8859-2'; + $self->{marc_encoding} ||= 'utf-8'; $self ? return $self : return undef; } @@ -91,13 +94,28 @@ unless ($arg->{fields} && defined $arg->{id}); my $marc = new MARC::Record; - $marc->encoding( $self->{encoding} ); + $marc->encoding( $self->{marc_encoding} ); my $id = $arg->{id}; $log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY'); - $marc->add_fields( @{ $arg->{fields} } ); + my $fields = $arg->{fields}; + + $log->debug("original fields = ", sub { dump( $fields ) }); + + # recode fields to marc_encoding + foreach my $j ( 0 .. $#$fields ) { + foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) { + my $f = $fields->[$j]->[ ($i * 2) + 4 ]; + $f = decode( $self->{native_encoding}, $f ); + $fields->[$j]->[ ($i * 2) + 4 ] = $f; + } + } + + $log->debug("recode fields = ", sub { dump( $fields ) }); + + $marc->add_fields( @$fields ); # tweak leader if (my $new_l = $arg->{leader}) { @@ -132,7 +150,10 @@ ); } - print {$self->{fh}} $marc->as_usmarc; + { + use bytes; + print {$self->{fh}} $marc->as_usmarc; + } }