blib/lib/XML/DoubleEncodedEntities.pm

filter: Statementstmt 100% 17 / 17 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 1 totaltotal 87.8% 29 / 33 SCAR 19.5
CC
7
Cov
90%
decode
7.3
BEGIN
1.0
BEGIN
1.0
CRAP 7.1
↑ Summary
1 package XML::DoubleEncodedEntities;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 require Exporter;
6  
7 1 ▶use vars qw($VERSION @EXPORT_OK @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION = '1.0';
10 1 @EXPORT_OK = qw(decode);
11 1 @ISA = qw(Exporter);
12  
13 # localising prevents the warningness leaking out of this module
14 1 local $^W = 1; # can't use warnings as that's a 5.6-ism
15  
16 =head1 NAME
17  
18 XML::DoubleEncodedEntities - unbreak XML with doubly-encoded entities
19  
20 =head1 DESCRIPTION
21  
22 Occasionally, XML files escape into the wild with their entities encoded
23 twice so instead of this:
24  
25 <chocolate>Green &amp; Blacks</chocolate>
26  
27 you get:
28  
29 &lt;chocolate&gt;Green &amp;amp; Blacks&lt;/chocolate&gt;
30  
31 A real-world example of this problem can be seen in this failing test
32 for a module which queries an online XML datasource:
33  
34 http://www.nntp.perl.org/group/perl.cpan.testers/2007/02/msg414642.html
35  
36 (search for the text 'Arcturus' in that page).
37  
38 This module tries to fix that.
39  
40 =head1 SYNOPSIS
41  
42 use XML::DoubleEncodedEntities;
43
44 my $xmlfile = XML::DoubleEncodedEntities::decode($xmlfile);
45  
46 =head1 The C<decode> function
47  
48 This function is not exported, but can be if you wish. It takes one
49 scalar parameter and returns a corresponding scalar, decoded if necessary.
50  
51 The parameter is assumed to be a string. If its first non-whitespace
52 characters are C<&lt;>, or if it contains the sequence C<&amp;amp;> the
53 string is assumed to be a doubly-encoded XML document, in which case the
54 following entities, if present, are decoded:
55 &amp;
56 &lt;
57 &gt;
58 &quot;
59 &apos;
60  
61 No other parameters are decoded. After all, if the input document has been
62 *doubly* encoded then something like C<æ>, which should be the entity C<&aelig;>
63 will be represented by the character sequence C<&amp;aelig;>. Once the
64 C<&amp;> has been corrected by this module, you'll be able to decode the
65 resulting C<&aelig;> in the normal way.
66  
67 =cut
68  
69 # ripped off (and simplified) from XML::Tiny
70 sub decode {
71 6 ▶ my $thingy = shift;
Subroutine decode: called CC=7 SCAR=19.8
Pod undocumented
72 6 ▶ return $thingy unless($thingy =~ /(^\s*&lt;|&amp;amp;)/);
Branch: 1 branch
BranchTrueFalseTotal
unless $thingy =~ /(^\s*&lt;|&amp;amp;)/ 2 4 6
73  
74 4 $thingy =~ s/&(lt;|gt;|quot;|apos;|amp;|.*)/
75 8 ▶ $1 eq 'lt;' ? '<' :
Branch: 5 branches
BranchTrueFalseTotal
$1 eq 'lt;' ? : 3 5 8
$1 eq 'gt;' ? : 0 5 5
$1 eq 'apos;' ? : 0 5 5
$1 eq 'quot;' ? : 0 5 5
$1 eq 'amp;' ? : 4 1 5
76 $1 eq 'gt;' ? '>' :
77 $1 eq 'apos;' ? "'" :
78 $1 eq 'quot;' ? '"' :
79 $1 eq 'amp;' ? '&' :
80 die("Illegal ampersand or entity\n\tat &$1\n")
81 /ge;
82 3 $thingy;
83 }
84  
85 =head1 BUGS and FEEDBACK
86  
87 I welcome feedback about my code, including constructive criticism.
88 Bug reports should be made using L<http://rt.cpan.org/> or by email,
89 and should include the smallest possible chunk of code, along with
90 any necessary data, which demonstrates the bug. Ideally, this
91 will be in the form of a file which I can drop in to the module's
92 test suite. Ideally such files will work in perl 5.004.
93  
94 If you are feeling particularly generous you can encourage me in my
95 open source endeavours by buying me something from my wishlist:
96 L<http://www.cantrell.org.uk/david/wishlist/>
97  
98 =head1 SEE ALSO
99  
100 L<Encode::DoubleEncodedUTF8>, which does the same job for broken UTF-8.
101  
102 L<Test::DoubleEncodedEntities>, which is HTMLish.
103  
104 =head1 AUTHOR
105  
106 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
107  
108 =head1 COPYRIGHT and LICENCE
109  
110 Copyright 2007 David Cantrell
111  
112 This module is free-as-in-speech software, and may be used, distributed,
113 and modified under the same terms as Perl itself.
114  
115 =head1 CONSPIRACY
116  
117 This module is also free-as-in-mason software.
118  
119 =cut
120  
121 1 '&amp;amp;#49'
↑ Summary