| 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.17; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | sub import{ | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | shift; | |||||||||||||||||||||||||||||||
| 11 | 2 | my $pkg = scalar caller(); | |||||||||||||||||||||||||||||||
| 12 | 2 | *{"${pkg}::parseRSS"} = \&parseRSS; | |||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | *{"${pkg}::parseXML"} = \&parseXML if grep($_ eq 'parseXML', @_); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | sub parseRSS { | ||||||||||||||||||||||||||||||
|
Subroutine
parseRSS: called CC=8 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 3 | my($rr, $cref, $strip) = @_; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | die "$rr is not a hash reference" unless ref($rr) eq 'HASH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | die "$cref is not a scalar reference" unless ref($cref) eq 'SCALAR'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # Gotta have some content to parse | ||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | return unless $$cref; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 3 | preprocess($cref, $strip); | |||||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | _parseRSS($rr, $cref), last if index(${$cref}, '<rss')+1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | _parseRDF($rr, $cref), last if index(${$cref}, '<rdf:RDF')+1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | _parseSN( $rr, $cref), last if index(${$cref}, '<scriptingnews')+1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | _parseWL( $rr, $cref), last if index(${$cref}, '<weblog')+1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | 0 | die "Content must be RSS|RDF|ScriptingNews|Weblog|reasonably close"; | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | 3 | postprocess($rr); | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | sub preprocess { | ||||||||||||||||||||||||||||||
|
Subroutine
preprocess: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 38 | 3 | my($cref, $strip) = @_; | |||||||||||||||||||||||||||||||
| 39 | 3 | $$cref =~ y/\r\n/\n/s; | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | if( ! defined($strip) ){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 42 | 2 | $$cref =~ y{\n\t ~0-9\-+!@#$%^&*()_=a-zA-Z[]\\;':",./<>?}{ }cs; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | elsif($strip eq '1' ){ | ||||||||||||||||||||||||||||||||
| 45 | 1 | $$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 | 3 | $$cref =~ s{(?:<|<)/?(?:b|i|h[1-6]|p|center|quote|strong)(?:>|>)}{}gsi; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub _parseRSS { | ||||||||||||||||||||||||||||||
|
Subroutine
_parseRSS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | 2 | parseXML($_[0], $_[1], 'channel', 0); | |||||||||||||||||||||||||||||||
| 55 | 2 | $_[0]->{'items'} = $_[0]->{'item'}; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | sub _parseRDF { | ||||||||||||||||||||||||||||||
|
Subroutine
_parseRDF: called CC=7 SCAR=33.5
| |||||||||||||||||||||||||||||||||
| 59 | 1 | my ($rr, $cref) = @_; | |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||||||
| 72 | 0 | foreach $item ( @{$li} ){ | |||||||||||||||||||||||||||||||
| 73 | 0 | my %ia; | |||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (exists $item->{'dc:description'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 0 | $ia{'description'} = $item->{'dc:description'}; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if (exists $item->{'dc:title'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 0 | $ia{'title'} = $item->{'dc:title'}; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (exists $item->{'dc:identifier'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 0 | $ia{'link'} = delete($item->{'dc:identifier'}); | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 0 | push(@{$rr->{'items'}}, \%ia); | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _parseSN { | ||||||||||||||||||||||||||||||
|
Subroutine
_parseSN: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 90 | 0 | my ($rr, $cref) = @_; | |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 101 | 0 | $ia{'description'} = $item->{'text'}; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if (exists $item->{'linetext'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 0 | $ia{'title'} = $item->{'linetext'}; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (exists $item->{'url'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 0 | $ia{'link'} = $item->{'url'}; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 0 | push(@{$rr->{'items'}}, \%ia); | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub _parseWL { | ||||||||||||||||||||||||||||||
|
Subroutine
_parseWL: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 116 | 0 | my ($rr, $cref) = @_; | |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 128 | 0 | $ia{'link'} = delete($item->{'url'}); | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 0 | push(@{$rr->{'items'}}, \%ia); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 3 | ▶ | sub postprocess { | ||||||||||||||||||||||||||||||
|
Subroutine
postprocess: called CC=19 SCAR=48.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 137 | 3 | my $rr = shift; | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | #XXX Not much to do, what about un-munging URL's in source, etc.?! | ||||||||||||||||||||||||||||||||
| 140 | 3 | ▶ | return unless defined($rr->{'items'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 3 | ▶ | $rr->{'items'} = [$rr->{'items'}] unless ref($rr->{'items'}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 3 | foreach my $i (@{$rr->{'items'}}) { | |||||||||||||||||||||||||||||||
| 144 | 32 | ▶ | $i->{description} = $i->{description}->{'<>'} if ref($i->{description}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | # Put stuff into the right name if necessary | ||||||||||||||||||||||||||||||||
| 147 | 32 | ▶ | if( not $i->{'link'} ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if( defined($i->{'url'}) ){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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 | 32 | $i->{'link'} =~ s/\s+$//; | |||||||||||||||||||||||||||||||
| 168 | 32 | $i->{'link'} =~ s/^\s+//; | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | # Make sure you've got an http/ftp link | ||||||||||||||||||||||||||||||||
| 171 | 32 | ▶ | if( exists( $i->{'link'}) && $i->{'link'} !~ m{^(https?|ftp)://}i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $i->{"link"} and not $i->{"link"} =~ m[^(https?|ftp)://]i
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
| |||||||||||||||||||||||||||||||||
| 172 | ## Rip link out of anchor tag | ||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if( ref($i->{'link'}) && $i->{'link'}->{a}->{href} ){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
ref $i->{"link"} and $i->{"link"}{"a"}{"href"}
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"}
| |||||||||||||||||||||||||||||||||
| 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?://]
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?://]
| |||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if (substr($i->{'link'}, 0, 1) ne '/') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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 | 32 | ▶ | unless( defined($i->{'title'}) ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 189 | 0 | $i->{'title'} = $i->{'link'}; | |||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | 32 | ▶ | if( exists($i->{'link'}) ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | #XXX # Fix pre-process munging | ||||||||||||||||||||||||||||||||
| 194 | # $i->{'link'} =~ s/&/&/gi; | ||||||||||||||||||||||||||||||||
| 195 | 32 | $i->{'link'} =~ s/ /%20/g; | |||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 3 | ▶ | sub parseXML{ | ||||||||||||||||||||||||||||||
|
Subroutine
parseXML: called CC=8 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 201 | 3 | my($hash, $xml, $tag, $comments) = @_; | |||||||||||||||||||||||||||||||
| 202 | 3 | my($begin, $end, @comments); | |||||||||||||||||||||||||||||||
| 203 | 3 | local $_; | |||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | #Kill comments | ||||||||||||||||||||||||||||||||
| 206 | 3 | ▶ | while( ($begin = index(${$xml}, '<!--')) > -1 && | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
($begin = index(${$xml;}, "<!--")) > -1 and ${$xml;} =~ /<!--.*?--(>)/gs
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 212 | if $comments; | ||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | 3 | _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 | 3 | ▶ | if( $comments ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 43 | ▶ | sub _parseXML{ | ||||||||||||||||||||||||||||||
|
Subroutine
_parseXML: called CC=19 SCAR=29.8
| |||||||||||||||||||||||||||||||||
| 230 | 43 | my($hash, $xml, $tag, $index) = @_; | |||||||||||||||||||||||||||||||
| 231 | 43 | my($begin, $end); | |||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | #Find topTag and set pos to start matching from there | ||||||||||||||||||||||||||||||||
| 234 | 43 | ${$xml} =~ /<$tag(?:>|\s)/g; | |||||||||||||||||||||||||||||||
| 235 | 43 | ▶ | ($begin, $end) = (0, pos(${$xml})||0); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
pos ${$xml;} || 0
MC/DC0%0/1
pos ${$xml;} || 0
pos ${$xml;}
Truth table50%1/2
A
A= pos ${$xml;}
| |||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | #Match either <foo></foo> or <bar />, optional attributes, stash tag name | ||||||||||||||||||||||||||||||||
| 238 | 43 | ▶ | while( ${$xml} =~ m%<([^\s>]+)(?:\s+[^>]*?)?(?:/|>.*?</\1)>%sg ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | #Save the tag name, we'll need it | ||||||||||||||||||||||||||||||||
| 241 | 190 | ▶ | $tag = $1 || $2; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$1 || $2
MC/DC0%0/2
$1 || $2
$1
$2
Truth table33%1/3
A || B
A= $1B= $2
| |||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | #Save the new beginning and end | ||||||||||||||||||||||||||||||||
| 244 | 190 | ($begin, $end) = ($end, pos(${$xml})); | |||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | #Get the bit we just matched. | ||||||||||||||||||||||||||||||||
| 247 | 190 | my $str = substr(${$xml}, $begin, $end-$begin); | |||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | #Extract the actual attributes and contents of the tag | ||||||||||||||||||||||||||||||||
| 250 | 190 | ▶ | $str =~ m%<\Q$tag\E\s*([^>]*?)?>(.*?)</\Q$tag\E>%s || | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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 | 190 | my($attr, $content) = ($1, $2); | |||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | #Did we get attributes? clean them up and chuck them in a hash. | ||||||||||||||||||||||||||||||||
| 257 | 190 | ▶ | if( $attr ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 258 | 43 | ($_, $attr) = ($attr, {}); | |||||||||||||||||||||||||||||||
| 259 | 43 | ▶ | $attr->{$1} = $3 while m/([^\s=]+)\s*=\s*(['"]?)([^\2>]*?)(?:\2|$)/g; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | 190 | my $inhash; | |||||||||||||||||||||||||||||||
| 263 | #Recurse if contents has more tags, replace contents with reference we get | ||||||||||||||||||||||||||||||||
| 264 | 190 | ▶ | if( $content && index($content, '<') > -1 ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$content and index($content, "<") != -1
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
| |||||||||||||||||||||||||||||||||
| 265 | 40 | _parseXML($inhash={}, \$str, $tag); | |||||||||||||||||||||||||||||||
| 266 | #Was there any data in the contents? We should extract that... | ||||||||||||||||||||||||||||||||
| 267 | 40 | ▶ | if( $str =~ />[^><]+</ ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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 | 40 | my $qr = qr{@{[join('|', map { quotemeta } keys %{$inhash})]}}; | |||||||||||||||||||||||||||||||
| 276 | 40 | $content =~ s%<($qr)\s*(?:[^>]*?)?(?:/|>.*?</\1)>%%sg; | |||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 40 | ▶ | $inhash->{'<>'} = $content if $content =~ /\S/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | 190 | ▶ | if( ref($inhash) ){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 283 | #We have attributes? Then we should merge them. | ||||||||||||||||||||||||||||||||
| 284 | 40 | ▶ | if( ref($attr) ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||
| 293 | elsif( ref($attr) ){ | ||||||||||||||||||||||||||||||||
| 294 | 21 | $inhash = $attr; | |||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||
| 296 | else{ | ||||||||||||||||||||||||||||||||
| 297 | #Otherwise save our content | ||||||||||||||||||||||||||||||||
| 298 | 129 | $inhash = $content; | |||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | $hash->{$tag} = exists($hash->{$tag}) ? | ||||||||||||||||||||||||||||||||
| 302 | (ref($hash->{$tag}) eq 'ARRAY' ? | ||||||||||||||||||||||||||||||||
| 303 | 44 | [@{$hash->{$tag}}, $inhash] : | |||||||||||||||||||||||||||||||
| 304 | 190 | ▶ | [ $hash->{$tag}, $inhash] ) : $inhash; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 2 | 1; | |||||||||||||||||||||||||||||||
| 309 | __END__ | ||||||||||||||||||||||||||||||||