1package XML::Liberal::Remedy::NotUTF8; 2use strict; 3 4use Encode; 5use Encode::Guess; 6 7sub apply { 8 my $class = shift; 9 my($driver, $error, $xml_ref) = @_; 10 11 return 0 if $error->message !~ 12 /^parser error : Input is not proper UTF-8, indicate encoding !/; 13 14 my @suspects = @{ $driver->guess_encodings || [ qw(euc-jp shift_jis utf-8) ] }; 15 my $enc = guess_encoding($$xml_ref, @suspects); 16 if (ref($enc)) { 17 Encode::from_to($$xml_ref, $enc->name, "UTF-8"); 18 return 1; 19 } else { 20 # fallback to UTF-8 and do roundtrip conversion 21 my $old = $$xml_ref; 22 my $xml = Encode::encode("utf-8", Encode::decode("utf-8", $$xml_ref)); 23 $$xml_ref = $xml; 24 return $$xml_ref ne $old; 25 } 26} 27 281; 29