File Coverage

blib/lib/XML/Liberal/Remedy/InvalidEncoding.pm
Criterion Covered Total %
statement 9 22 40.9
branch 0 10 0.0
condition n/a
subroutine 3 4 75.0
pod 0 1 0.0
total 12 37 32.4


line stmt bran cond sub pod time code
1             package XML::Liberal::Remedy::InvalidEncoding;
2 2     2   1000 use strict;
  2         2  
  2         62  
3              
4 2     2   1072 use Encode;
  2         16580  
  2         137  
5 2     2   1019 use Encode::Guess;
  2         7175  
  2         10  
6              
7             my $RX = qr/^(<\?xml\s+version\s*=\s*["']1\.[01]["']\s+encoding\s*=\s*["'])([^"']+)/;
8              
9             sub apply {
10 0     0 0   my $class = shift;
11 0           my($driver, $error, $xml_ref) = @_;
12              
13 0 0         return 0 if $error->message !~
14             /^(?:(?:i18n |encoding )?error : )?input conversion failed due to input error/;
15              
16 0           my (undef, $encoding) = $$xml_ref =~ $RX;
17 0 0         unless ($encoding) {
18 0 0         my @suspects = @{ $driver->guess_encodings || [ qw(euc-jp shift_jis utf-8) ] };
  0            
19 0           my $enc = guess_encoding($$xml_ref, @suspects);
20 0           $encoding = $enc->name;
21             }
22              
23 0 0         if ($encoding) {
24 0 0         Encode::from_to($$xml_ref, $encoding, "UTF-8"), return 1
25             if $$xml_ref =~ s/$RX/$1utf-8/;
26             }
27              
28 0           Carp::carp("Can't find encoding from XML declaration: ", substr($$xml_ref, 0, 128));
29 0           return 0;
30             }
31              
32             1;