blib/lib/XML/RSSLite.pm

filter: Statementstmt 58.0% 97 / 167 Branchbran 43.0% 43 / 100 Conditioncond 30.4% 7 / 23 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 84.6% 11 / 13 Podpod 50.0% 2 / 4 totaltotal 50.3% 162 / 322 CC 66 SCAR 38.1
CC
66
Cov
51%
postprocess
128.5
_parseSN
30.0
_parseRDF
28.4
CRAP 45.2
↑ Summary
1 #!/usr/bin/perl
2 package XML::RSSLite;
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 $VERSION = 0.16;
7  
8 sub import{
9 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶ shift;
Subroutine import: called CC=2 SCAR=7.2
11 2 my $pkg = scalar caller();
12 2 *{"${pkg}::parseRSS"} = \&parseRSS;
13 2 ▶ *{"${pkg}::parseXML"} = \&parseXML if grep($_ eq 'parseXML', @_);
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ eq "parseXML"), @_) 0 2 2
14 }
15  
16  
17 sub parseRSS {
18 2 ▶ my($rr, $cref, $strip) = @_;
Subroutine parseRSS: called CC=8 SCAR=25.8
Pod documented
19  
20 2 ▶ die "$rr is not a hash reference" unless ref($rr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rr eq "HASH" 0 2 2
21 2 ▶ die "$cref is not a scalar reference" unless ref($cref) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cref eq "SCALAR" 0 2 2
22  
23 # Gotta have some content to parse
24 2 ▶ return unless $$cref;
Branch: 1 branch
BranchTrueFalseTotal
unless $$cref 0 2 2
25  
26 2 preprocess($cref, $strip);
27 {
28 2 ▶ _parseRSS($rr, $cref), last if index(${$cref}, '<rss')+1;
Branch: 1 branch
BranchTrueFalseTotal
if index(${$cref;}, "<rss") + 1 1 1 2
29 1 ▶ _parseRDF($rr, $cref), last if index(${$cref}, '<rdf:RDF')+1;
Branch: 1 branch
BranchTrueFalseTotal
if index(${$cref;}, "<rdf:RDF") + 1 1 0 1
30 0 ▶ _parseSN( $rr, $cref), last if index(${$cref}, '<scriptingnews')+1;
Branch: 1 branch
BranchTrueFalseTotal
if index(${$cref;}, "<scriptingnews") + 1 0 0 0
31 0 ▶ _parseWL( $rr, $cref), last if index(${$cref}, '<weblog')+1;
Branch: 1 branch
BranchTrueFalseTotal
if index(${$cref;}, "<weblog") + 1 0 0 0
32 0 die "Content must be RSS|RDF|ScriptingNews|Weblog|reasonably close";
33 }
34 2 postprocess($rr);
35 }
36  
37 sub preprocess {
38 2 ▶ my($cref, $strip) = @_;
Subroutine preprocess: called CC=3 SCAR=11.2
Pod undocumented
39 2 $$cref =~ y/\r\n/\n/s;
40  
41 2 ▶ if( ! defined($strip) ){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $strip) { } 1 1 2
elsif ($strip eq '1') { } 0 1 1
42 1 $$cref =~ y{\n\t ~0-9\-+!@#$%^&*()_=a-zA-Z[]\\;':",./<>?}{ }cs;
43 }
44 elsif($strip eq '1' ){
45 0 $$cref =~ s/[^:print:]]/ /g;
46 }
47  
48 #XXX $$cref =~ s/&(?!0[a-zA-Z0-9]+|#\d+);/amp/gs;
49 #XXX Do we wish to (re)allow escaped HTML?!
50 2 $$cref =~ s{(?:<|&lt;)/?(?:b|i|h[1-6]|p|center|quote|strong)(?:>|&gt;)}{}gsi;
51 }
52  
53 sub _parseRSS {
54 1 ▶ parseXML($_[0], $_[1], 'channel', 0);
Subroutine _parseRSS: called CC=1 SCAR=0.0
55 1 $_[0]->{'items'} = $_[0]->{'item'};
56 }
57  
58 sub _parseRDF {
59 1 ▶ my ($rr, $cref) = @_;
Subroutine _parseRDF: called CC=7 SCAR=33.5
60  
61 1 $rr->{'items'} = [];
62 1 my $item;
63  
64 1 parseXML($_[0], $_[1], 'rdf:RDF', 0);
65  
66 # Alias RDF to RSS
67 1 ▶ if( exists($rr->{'item'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rr->{'item'}) { } 1 0 1
68 1 $rr->{'items'} = $rr->{'item'};
69 }
70 else{
71 0 ▶ my $li = $_[0]->{'rdf:li'} || $_[0]->{'rdf:Seq'}->{'rdf:li'};
Condition0%0/3
$_[0]{'rdf:li'} || $_[0]{'rdf:Seq'}{'rdf:li'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{'rdf:li'} || $_[0]{'rdf:Seq'}{'rdf:li'}
$_[0]{'rdf:li'} $_[0]{'rdf:Seq'}{'rdf:li'}
Truth table0%0/3
A || B
A= $_[0]{'rdf:li'}B= $_[0]{'rdf:Seq'}{'rdf:li'}
ABresult
1X1
011
000
72 0 foreach $item ( @{$li} ){
73 0 my %ia;
74 0 ▶ if (exists $item->{'dc:description'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"dc:description"}) 0 0 0
75 0 $ia{'description'} = $item->{'dc:description'};
76 }
77 0 ▶ if (exists $item->{'dc:title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"dc:title"}) 0 0 0
78 0 $ia{'title'} = $item->{'dc:title'};
79 }
80 0 ▶ if (exists $item->{'dc:identifier'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"dc:identifier"}) 0 0 0
81 0 $ia{'link'} = delete($item->{'dc:identifier'});
82 }
83
84 0 push(@{$rr->{'items'}}, \%ia);
85 }
86 }
87 }
88  
89 sub _parseSN {
90 0 ▶ my ($rr, $cref) = @_;
Subroutine _parseSN: not called CC=5 SCAR=34.0
91
92 0 $rr->{'items'} = ();
93 0 my $item;
94  
95 0 parseXML($rr, $cref, 'channel', 0);
96
97 # Alias SN to RSS terms
98 0 foreach $item ( @{$_[0]->{'rdf:li'}} ){
99 0 my %ia;
100 0 ▶ if (exists $item->{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"text"}) 0 0 0
101 0 $ia{'description'} = $item->{'text'};
102 }
103 0 ▶ if (exists $item->{'linetext'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"linetext"}) 0 0 0
104 0 $ia{'title'} = $item->{'linetext'};
105 }
106 0 ▶ if (exists $item->{'url'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"url"}) 0 0 0
107 0 $ia{'link'} = $item->{'url'};
108 }
109  
110 0 push(@{$rr->{'items'}}, \%ia);
111 }
112 }
113  
114  
115 sub _parseWL {
116 0 ▶ my ($rr, $cref) = @_;
Subroutine _parseWL: not called CC=3 SCAR=24.8
117  
118 0 $rr->{'items'} = ();
119 0 my $item;
120  
121 #XXX is this the right tag to parse for?
122 0 parseXML($rr, $cref, 'channel', 0);
123
124 # Alias WL to RSS
125 0 foreach $item ( @{$_[0]->{'rdf:li'}} ){
126 0 my %ia;
127 0 ▶ if (exists $item->{'url'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"url"}) 0 0 0
128 0 $ia{'link'} = delete($item->{'url'});
129 }
130  
131 0 push(@{$rr->{'items'}}, \%ia);
132 }
133 }
134  
135  
136 sub postprocess {
137 2 ▶ my $rr = shift;
Subroutine postprocess: called CC=19 SCAR=48.6
Pod undocumented
138  
139 #XXX Not much to do, what about un-munging URL's in source, etc.?!
140 2 ▶ return unless defined($rr->{'items'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rr->{"items"} 0 2 2
141 2 ▶ $rr->{'items'} = [$rr->{'items'}] unless ref($rr->{'items'}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rr->{"items"} eq "ARRAY" 0 2 2
142  
143 2 foreach my $i (@{$rr->{'items'}}) {
144 26 ▶ $i->{description} = $i->{description}->{'<>'} if ref($i->{description});
Branch: 1 branch
BranchTrueFalseTotal
if ref $i->{"description"} 0 26 26
145  
146 # Put stuff into the right name if necessary
147 26 ▶ if( not $i->{'link'} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($i->{"link"}) 0 26 26
148 0 ▶ if( defined($i->{'url'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $i->{'url'}) { } 0 0 0
elsif (exists $i->{'title'}) { } 0 0 0
149 0 $i->{'link'} = delete($i->{'url'}); }
150 # See if you can use misplaced url in title for empty links
151 elsif( exists($i->{'title'}) ){
152 # The next case would trap this, but try to short-circuit the gathering
153 0 ▶ if ($i->{'title'} =~ /^(?:https?|ftp):/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i->{'title'} =~ /^(?:https?|ftp):/) { } 0 0 0
elsif ($i->{'title'} =~ /"((?:https?|ftp).*?)"/) { } 0 0 0
154 0 $i->{'link'} = $i->{'title'};
155 }
156 elsif ($i->{'title'} =~ /"((?:https?|ftp).*?)"/) {
157 0 $i->{'link'} = $1;
158 0 $i->{'title'} =~ s/<.*?>//;
159 }
160 else {
161 0 next;
162 }
163 }
164 }
165
166 # Trim whitespace
167 26 $i->{'link'} =~ s/\s+$//;
168 26 $i->{'link'} =~ s/^\s+//;
169  
170 # Make sure you've got an http/ftp link
171 26 ▶ if( exists( $i->{'link'}) && $i->{'link'} !~ m{^(https?|ftp)://}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i->{"link"} and not $i->{"link"} =~ m[^(https?|ftp)://]i) 0 26 26
Condition33%1/3
exists $i->{"link"} and not $i->{"link"} =~ m[^(https?|ftp)://]i
!ll&&!rl&&r
0260
MC/DC0%0/2
exists $i->{"link"} and not $i->{"link"} =~ m[^(https?|ftp)://]i
exists $i->{"link"} not $i->{"link"} =~ m[^(https?|ftp)://]i
Truth table33%1/3
A and not(B)
A= exists $i->{"link"}B= not $i->{"link"} =~ m[^(https?|ftp)://]i
ABresult
0X0
100
111
172 ## Rip link out of anchor tag
173 0 ▶ if( ref($i->{'link'}) && $i->{'link'}->{a}->{href} ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $i->{'link'} and $i->{'link'}{'a'}{'href'}) { } 0 0 0
elsif ($i->{'link'} =~ m[^[#/]] and $rr->{'link'} =~ m[^https?://]) { } 0 0 0
Condition0%0/3
ref $i->{"link"} and $i->{"link"}{"a"}{"href"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $i->{"link"} and $i->{"link"}{"a"}{"href"}
ref $i->{"link"} $i->{"link"}{"a"}{"href"}
Truth table0%0/3
A and B
A= ref $i->{"link"}B= $i->{"link"}{"a"}{"href"}
ABresult
0X0
100
111
174 0 $i->{'link'} = $i->{'link'}->{a}->{href} }
175 ## Smells like a relative url
176 ▶ elsif( $i->{'link'} =~ m{^[#/]} and $rr->{'link'} =~ m{^https?://} ){
Condition0%0/3
$i->{"link"} =~ m[^[#/]] and $rr->{"link"} =~ m[^https?://]
!ll&&!rl&&r
000
MC/DC0%0/2
$i->{"link"} =~ m[^[#/]] and $rr->{"link"} =~ m[^https?://]
$i->{"link"} =~ m[^[#/]] $rr->{"link"} =~ m[^https?://]
Truth table0%0/3
A and B
A= $i->{"link"} =~ m[^[#/]]B= $rr->{"link"} =~ m[^https?://]
ABresult
0X0
100
111
177 0 ▶ if (substr($i->{'link'}, 0, 1) ne '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($i->{"link"}, 0, 1) ne "/") 0 0 0
178 0 $i->{'link'} = '/' . $i->{'link'};
179 }
180 0 $i->{'link'} = $rr->{'link'} . $i->{'link'};
181 }
182 else {
183 0 next;
184 }
185 }
186
187 #If we don't have a title, use the link
188 26 ▶ unless( defined($i->{'title'}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $i->{"title"}) 0 26 26
189 0 $i->{'title'} = $i->{'link'};
190 }
191
192 26 ▶ if( exists($i->{'link'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i->{"link"}) 26 0 26
193 #XXX # Fix pre-process munging
194 # $i->{'link'} =~ s/&amp;/&/gi;
195 26 $i->{'link'} =~ s/ /%20/g;
196 }
197 }
198 }
199  
200 sub parseXML{
201 2 ▶ my($hash, $xml, $tag, $comments) = @_;
Subroutine parseXML: called CC=8 SCAR=32.2
Pod documented
202 2 my($begin, $end, @comments);
203 2 local $_;
204  
205 #Kill comments
206 2 ▶ while( ($begin = index(${$xml}, '<!--')) > -1 &&
Branch: 1 branch
BranchTrueFalseTotal
if (($begin = index(${$xml;}, "<!--")) > -1 and ${$xml;} =~ /<!--.*?--(>)/gs) 0 2 2
Condition33%1/3
($begin = index(${$xml;}, "<!--")) > -1 and ${$xml;} =~ /<!--.*?--(>)/gs
!ll&&!rl&&r
200
MC/DC0%0/2
($begin = index(${$xml;}, "<!--")) > -1 and ${$xml;} =~ /<!--.*?--(>)/gs
($begin = index(${$xml;}, "<!--")) > -1 ${$xml;} =~ /<!--.*?--(>)/gs
Truth table33%1/3
A and B
A= ($begin = index(${$xml;}, "<!--")) > -1B= ${$xml;} =~ /<!--.*?--(>)/gs
ABresult
0X0
100
111
207 0 ${$xml} =~ m%<!--.*?--(>)%sg ){
208 0 my $str = substr(${$xml}, $begin, pos(${$xml})-$begin, '');
209
210 #Save them if requested
211 0 ▶ do{ unshift @comments, [$begin, substr($str, 4, length($str)-7)] }
Branch: 1 branch
BranchTrueFalseTotal
if ($comments) 0 0 0
212 if $comments;
213 }
214  
215 2 _parseXML($hash, $xml, $tag);
216  
217 # #XXX Context of comment is lost!
218 # #Expose comments if requested
219 # do{ push(@$comments, $_->[1]) for @comments } if ref($comments) eq 'ARRAY';
220 2 ▶ if( $comments ){
Branch: 1 branch
BranchTrueFalseTotal
if ($comments) 0 2 2
221 #Restore comments if requested
222 0 substr(${$xml}, $_->[0], 0, '<!--'.$_->[1].'-->') for @comments;
223  
224 #Expose comments if requested
225 0 ▶ do{ push(@$comments, $_->[1]) for @comments } if ref($comments) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $comments eq "ARRAY") 0 0 0
226 }
227 }
228  
229 sub _parseXML{
230 34 ▶ my($hash, $xml, $tag, $index) = @_;
Subroutine _parseXML: called CC=19 SCAR=29.8
231 34 my($begin, $end);
232  
233 #Find topTag and set pos to start matching from there
234 34 ${$xml} =~ /<$tag(?:>|\s)/g;
235 34 ▶ ($begin, $end) = (0, pos(${$xml})||0);
Condition50%1/2
pos ${$xml;} || 0
l!l
340
MC/DC0%0/1
pos ${$xml;} || 0
pos ${$xml;}
Truth table50%1/2
A
A= pos ${$xml;}
Aresult
11
00
236  
237 #Match either <foo></foo> or <bar />, optional attributes, stash tag name
238 34 ▶ while( ${$xml} =~ m%<([^\s>]+)(?:\s+[^>]*?)?(?:/|>.*?</\1)>%sg ){
Branch: 1 branch
BranchTrueFalseTotal
if (${$xml;} =~ m[<([^\s>]+)(?:\s+[^>]*?)?(?:/|>.*?</\1)>]gs) 145 34 179
239  
240 #Save the tag name, we'll need it
241 145 ▶ $tag = $1 || $2;
Condition33%1/3
$1 || $2
l!l&&r!l&&!r
14500
MC/DC0%0/2
$1 || $2
$1 $2
Truth table33%1/3
A || B
A= $1B= $2
ABresult
1X1
011
000
242  
243 #Save the new beginning and end
244 145 ($begin, $end) = ($end, pos(${$xml}));
245  
246 #Get the bit we just matched.
247 145 my $str = substr(${$xml}, $begin, $end-$begin);
248
249 #Extract the actual attributes and contents of the tag
250 145 ▶ $str =~ m%<\Q$tag\E\s*([^>]*?)?>(.*?)</\Q$tag\E>%s ||
Branch: 1 branch
BranchTrueFalseTotal
$str =~ m[<\Q$tag\E\s*([^>]*?)?>(.*?)</\Q$tag\E>]s or $str =~ m[<\Q$tag\E\s*([^>]*?)?\s*/>] 21 124 145
251 #XXX pointed out by hv
252 # $str =~ s%^.*?<$tag\s*([^>]*?)?>(.*?)</$tag>%<$tag>$2</$tag>%s ||
253 $str =~ m%<\Q$tag\E\s*([^>]*?)?\s*/>%;
254 145 my($attr, $content) = ($1, $2);
255  
256 #Did we get attributes? clean them up and chuck them in a hash.
257 145 ▶ if( $attr ){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 43 102 145
258 43 ($_, $attr) = ($attr, {});
259 43 ▶ $attr->{$1} = $3 while m/([^\s=]+)\s*=\s*(['"]?)([^\2>]*?)(?:\2|$)/g;
Branch: 1 branch
BranchTrueFalseTotal
/([^\s=]+)\s*=\s*(['"]?)([^\2>]*?)(?:\2|$)/g and do { $attr->{$1} = $3 } 43 43 86
260 }
261  
262 145 my $inhash;
263 #Recurse if contents has more tags, replace contents with reference we get
264 145 ▶ if( $content && index($content, '<') > -1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($content and index($content, "<") != -1) 32 113 145
Condition100%3/3
$content and index($content, "<") != -1
!ll&&!rl&&r
219232
MC/DC100%2/2
$content and index($content, "<") != -1
$content index($content, "<") != -1
Truth table100%3/3
A and B
A= $contentB= index($content, "<") != -1
ABresult
0X0
100
111
265 32 _parseXML($inhash={}, \$str, $tag);
266 #Was there any data in the contents? We should extract that...
267 32 ▶ if( $str =~ />[^><]+</ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ />[^><]+</) 32 0 32
268 #The odd RE above shortcircuits unnecessary entry
269  
270 #Clean whitespace between tags
271 #$str =~ s%(?<=>)?\s*(?=<)%%g; #XXX ~same speed, wacko warning
272 #$str =~ s%(>?)\s*<%$1<%g;
273 #XXX #$str =~ s%(?:^|(?<=>))\s*(?:(?=<)|\z)%%g
274  
275 32 my $qr = qr{@{[join('|', map { quotemeta } keys %{$inhash})]}};
276 32 $content =~ s%<($qr)\s*(?:[^>]*?)?(?:/|>.*?</\1)>%%sg;
277  
278 32 ▶ $inhash->{'<>'} = $content if $content =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\S/ 0 32 32
279 }
280 }
281  
282 145 ▶ if( ref($inhash) ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $inhash) { } 32 113 145
elsif (ref $attr) { } 21 92 113
283 #We have attributes? Then we should merge them.
284 32 ▶ if( ref($attr) ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr) 22 10 32
285 22 for( keys %{$attr} ){
286 $inhash->{$_} = exists($inhash->{$_}) ?
287 (ref($inhash->{$_}) eq 'ARRAY' ?
288 0 [@{$inhash->{$_}}, $attr->{$_}] :
289 22 ▶ [ $inhash->{$_}, $attr->{$_}] ) : $attr->{$_};
Branch: 2 branches
BranchTrueFalseTotal
exists $inhash->{$_} ? : 0 22 22
ref $inhash->{$_} eq 'ARRAY' ? : 0 0 0
290 }
291 }
292 }
293 elsif( ref($attr) ){
294 21 $inhash = $attr;
295 }
296 else{
297 #Otherwise save our content
298 92 $inhash = $content;
299 }
300
301 $hash->{$tag} = exists($hash->{$tag}) ?
302 (ref($hash->{$tag}) eq 'ARRAY' ?
303 40 [@{$hash->{$tag}}, $inhash] :
304 145 ▶ [ $hash->{$tag}, $inhash] ) : $inhash;
Branch: 2 branches
BranchTrueFalseTotal
exists $hash->{$tag} ? : 44 101 145
ref $hash->{$tag} eq 'ARRAY' ? : 40 4 44
305 }
306 }
307  
308 2 1;
309 __END__
↑ Summary