blib/lib/XML/Atom/Stream.pm

filter: Statementstmt 26.2% 43 / 164 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 46.4% 13 / 28 Podpod 50.0% 2 / 4 totaltotal 20.0% 58 / 290 CC 39 SCAR 39.8
CC
39
Cov
18%
__ANON__
182.0
connect
72.0
start_element
42.0
CRAP 53.5
↑ Summary
1 package XML::Atom::Stream;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 our $VERSION = '0.11';
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::SAX::ParserFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::LibXML::SAX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
13 0 my($class, %param) = @_;
14 0 my $self = bless \%param, $class;
15 0 $self->init();
16 0 $self;
17 }
18  
19 0 ▶sub init {
Subroutine init: not called CC=4 SCAR=30.0
Pod undocumented
20 0 my $self = shift;
21 $self->{ua} ||= LWP::UserAgent->new(
22 agent => "XML::Atom::Stream/$VERSION",
23 0 ▶ timeout => $self->{timeout} || 15,
Condition0%0/5
$self->{"ua"} ||= "LWP::UserAgent"->new("agent", "XML::Atom::Stream/$XML::Atom::Stream::VERSION", "timeout", $self->{"timeout"} || 15)
l!l&&r!l&&!r
000
$self->{'timeout'} || 15
l!l
00
MC/DC0%0/2
$self->{"ua"} ||= "LWP::UserAgent"->new("agent", "XML::Atom::Stream/$XML::Atom::Stream::VERSION", "timeout", $self->{"timeout"} || 15)
$self->{"ua"} "LWP::UserAgent"->new("agent", "XML::Atom::Stream/$XML::Atom::Stream::VERSION", "timeout", $self->{"timeout"} || 15)
MC/DC0%0/1
$self->{'timeout'} || 15
$self->{'timeout'}
Truth table0%0/3
A ||= B
A= $self->{"ua"}B= "LWP::UserAgent"->new("agent", "XML::Atom::Stream/$XML::Atom::Stream::VERSION", "timeout", $self->{"timeout"} || 15)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
24 );
25 0 ▶ $self->{callback} or Carp::croak("no callback specified.");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"callback"} or Carp::croak("no callback specified.") 0 0 0
26 }
27  
28 0 ▶sub _setup_parser {
Subroutine _setup_parser: not called CC=1 SCAR=6.9
29 0 my $self = shift;
30 0 my $handler = XML::Atom::Stream::SAXHandler->new;
31 0 $handler->{callback} = $self->{callback};
32 0 $handler->{debug} = $self->{debug};
33 0 local $XML::SAX::ParserPackage = "XML::LibXML::SAX::Better";
34 0 my $factory = XML::SAX::ParserFactory->new;
35 0 my $parser = $factory->parser(Handler => $handler);
36 0 return $parser;
37 }
38  
39 0 ▶sub connect {
Subroutine connect: not called CC=8 SCAR=42.8
Pod documented
40 0 my($self, $url) = @_;
41 0 ▶ $url or Carp::croak("URL needed for connect()");
Branch: 1 branch
BranchTrueFalseTotal
$url or Carp::croak("URL needed for connect()") 0 0 0
42  
43 0 $self->{parser} = $self->_setup_parser;
44 0 ▶ $self->{ua}->get($url, ':content_cb' => sub { $self->on_content_cb(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
45  
46 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
47 0 ▶ warn "Disconnected.", $@ ? " ($@)" : "";
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
48 }
49  
50 0 ▶ if ($self->{reconnect} && (!$self->{__exception} || $self->{__exception} =~ /xmlParse/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"reconnect"} and !$self->{"__exception"} || $self->{"__exception"} =~ /xmlParse/) 0 0 0
Condition0%0/6
$self->{"reconnect"} and !$self->{"__exception"} || $self->{"__exception"} =~ /xmlParse/
!ll&&!rl&&r
000
not $self->{"__exception"} or $self->{"__exception"} =~ /xmlParse/
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"reconnect"} and !$self->{"__exception"} || $self->{"__exception"} =~ /xmlParse/
$self->{"reconnect"} not $self->{"__exception"} $self->{"__exception"} =~ /xmlParse/
Truth table0%0/4
A and not(B) or C
A= $self->{"reconnect"}B= not $self->{"__exception"}C= $self->{"__exception"} =~ /xmlParse/
ABCresult
0XX0
1000
11X1
1011
51 0 ▶ warn "Trying to reconnect" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
52 0 delete $self->{__exception};
53 0 $self->connect($url);
54 }
55 }
56  
57 0 ▶sub on_content_cb {
Subroutine on_content_cb: not called CC=4 SCAR=30.0
Pod undocumented
58 0 my($self, $data, $res, $proto) = @_;
59 0 ▶ warn ".\n" if $data =~ /<time>/ && $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /<time>/ and $self->{"debug"} 0 0 0
Condition0%0/3
$data =~ /<time>/ and $self->{"debug"}
!ll&&!rl&&r
000
MC/DC0%0/2
$data =~ /<time>/ and $self->{"debug"}
$data =~ /<time>/ $self->{"debug"}
Truth table0%0/3
A and B
A= $data =~ /<time>/B= $self->{"debug"}
ABresult
0X0
100
111
60 0 eval { $self->{parser}->parse_chunk($data) };
61 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
62 0 $self->{__exception} = $@;
63 0 die $@;
64 }
65 }
66  
67 package XML::Atom::Stream::SAXHandler;
68 1 ▶use XML::Atom::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use XML::Handler::Trees;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use base qw( XML::Handler::Tree );
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 0 ▶sub start_element {
Subroutine start_element: not called CC=6 SCAR=37.4
73 0 my $self = shift;
74 0 my($ref) = @_;
75 0 ▶ return if $ref->{LocalName} eq 'time' || $ref->{LocalName} eq 'atomStream';
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"LocalName"} eq "time" or $ref->{"LocalName"} eq "atomStream" 0 0 0
Condition0%0/3
$ref->{"LocalName"} eq "time" or $ref->{"LocalName"} eq "atomStream"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref->{"LocalName"} eq "time" or $ref->{"LocalName"} eq "atomStream"
$ref->{"LocalName"} eq "time" $ref->{"LocalName"} eq "atomStream"
Truth table0%0/3
A or B
A= $ref->{"LocalName"} eq "time"B= $ref->{"LocalName"} eq "atomStream"
ABresult
1X1
011
000
76  
77 0 ▶ if ($ref->{LocalName} eq 'sorryTooSlow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"LocalName"} eq "sorryTooSlow") 0 0 0
78 warn "You're too slow and missed ", $ref->{Attributes}->{'{}youMissed'}->{Value}, " entries"
79 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
80 0 return;
81 }
82  
83 0 ▶ if ($ref->{LocalName} eq 'feed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"LocalName"} eq "feed") 0 0 0
84 0 $self->{Curlist} = [];
85 }
86 0 $self->SUPER::start_element(@_);
87 }
88  
89 0 ▶sub end_element {
Subroutine end_element: not called CC=4 SCAR=30.0
90 0 my $self = shift;
91 0 my($ref) = @_;
92  
93 0 $self->SUPER::end_element(@_);
94 0 ▶ if ($ref->{LocalName} eq 'feed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"LocalName"} eq "feed") 0 0 0
95 0 my $element = $self->{Curlist};
96 0 my $xml = qq(<?xml version="1.0" encoding="utf-8"?>\n);
97 0 my %ns;
98 my $dumper;
99 0 ▶ $dumper = sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
100 0 my($ref) = @_;
101 0 my($elem, $stuff) = splice @$ref, 0, 2;
102 0 ▶ if ($elem eq '0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem eq '0') { } 0 0 0
elsif ($elem =~ /^\{(.*?)\}([\w\-]+)$/) { } 0 0 0
103 0 $xml .= encode_xml($stuff);
104 }
105 elsif ($elem =~ /^\{(.*?)\}([\w\-]+)$/) {
106 0 my($xmlns, $tag) = ($1, $2);
107 0 my $attr = shift @$stuff;
108 0 $xml .= qq(<$tag);
109  
110 0 my $has_xmlns;
111  
112 # extract and replace xmlns declarations
113 0 for my $key (keys %$attr) {
114 0 ▶ if ($key =~ m!^\{http://www\.w3\.org/2000/xmlns/\}([\w\-]+)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ m[^\{http://www\.w3\.org/2000/xmlns/\}([\w\-]+)$]) 0 0 0
115 0 my $uri = delete $attr->{$key};
116 0 $ns{$uri} = $1;
117 0 $attr->{"xmlns:$1"} = $uri;
118 }
119 }
120  
121 0 for my $key (keys %$attr) {
122 0 my $attr_key;
123 0 ▶ if ($key =~ /^\{(.*?)\}(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\{(.*?)\}(\w+)$/) { } 0 0 0
124 0 my($xmlns, $prefix) = ($1, $2);
125 0 ▶ my $ns = $ns{$xmlns} || 'xml'; # xml:lang
Condition0%0/2
$ns{$xmlns} || 'xml'
l!l
00
MC/DC0%0/1
$ns{$xmlns} || 'xml'
$ns{$xmlns}
Truth table0%0/2
A
A= $ns{$xmlns}
Aresult
11
00
126 0 $attr_key = "$ns:$prefix";
127 } else {
128 0 $attr_key = $key;
129 0 ▶ $has_xmlns = 1 if $key eq 'xmlns';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "xmlns" 0 0 0
130 }
131  
132 0 $xml .= qq( $attr_key=") . encode_xml($attr->{$key}) . qq(");
133 }
134  
135 0 ▶ $xml .= qq( xmlns="$xmlns") if $xmlns ne 'http://www.w3.org/2005/Atom' && !$has_xmlns;
Branch: 1 branch
BranchTrueFalseTotal
if $xmlns ne "http://www.w3.org/2005/Atom" and not $has_xmlns 0 0 0
Condition0%0/3
$xmlns ne "http://www.w3.org/2005/Atom" and not $has_xmlns
!ll&&!rl&&r
000
MC/DC0%0/2
$xmlns ne "http://www.w3.org/2005/Atom" and not $has_xmlns
$xmlns ne "http://www.w3.org/2005/Atom" not $has_xmlns
Truth table0%0/3
A and not(B)
A= $xmlns ne "http://www.w3.org/2005/Atom"B= not $has_xmlns
ABresult
0X0
100
111
136  
137 0 ▶ if (@$stuff) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$stuff) { } 0 0 0
138 0 $xml .= ">";
139 0 $dumper->($stuff);
140 0 $xml .= "</$tag>";
141 } else {
142 0 $xml .= "/>";
143 }
144 }
145 0 ▶ $dumper->($ref) if @$ref;
Branch: 1 branch
BranchTrueFalseTotal
if @$ref 0 0 0
146 0 };
147 0 $dumper->($element);
148 0 my $feed = eval { XML::Atom::Feed->new(Stream => \$xml) };
149 0 $dumper = 0; # to avoid memory leak
150 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
151 0 ▶ warn "Feed parse error: $@" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
152 0 return;
153 }
154  
155 0 $self->{callback}->($feed);
156 }
157 }
158  
159 1 my %Map = ('&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;',
160 '\'' => '&apos;');
161 1 my $RE = join '|', keys %Map;
162  
163 0 ▶sub encode_xml {
Subroutine encode_xml: not called CC=3 SCAR=24.8
164 0 my($str, $no_cdata) = @_;
165 0 ▶ if (!$no_cdata && $str =~ m/
Branch: 1 branch
BranchTrueFalseTotal
if (not $no_cdata and $str =~ / <[^>]+> ## HTML markup | ## or &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); ## something that looks like an HTML entity. /x) { } 0 0 0
Condition0%0/3
not $no_cdata and $str =~ / <[^>]+> ## HTML markup | ## or &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); ## something that looks like an HTML entity. /x
!ll&&!rl&&r
000
MC/DC0%0/2
not $no_cdata and $str =~ / <[^>]+> ## HTML markup | ## or &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); ## something that looks like an HTML entity. /x
not $no_cdata $str =~ / <[^>]+> ## HTML markup | ## or &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); ## something that looks like an HTML entity. /x
Truth table0%0/3
not(A) and B
A= not $no_cdataB= $str =~ / <[^>]+> ## HTML markup | ## or &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); ## something that looks like an HTML entity. /x
ABresult
0X0
100
111
166 <[^>]+> ## HTML markup
167 | ## or
168 &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
169 ## something that looks like an HTML entity.
170 /x) {
171 ## If ]]> exists in the string, encode the > to &gt;.
172 0 $str =~ s/]]>/]]&gt;/g;
173 0 $str = '<![CDATA[' . $str . ']]>';
174 } else {
175 0 $str =~ s!($RE)!$Map{$1}!g;
176 }
177 0 $str;
178 }
179  
180 # from http://code.sixapart.com/svn/djabberd/trunk/dev/xml-test.pl
181 package XML::LibXML::SAX::Better;
182 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 1 ▶use XML::SAX::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 1 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
188 0 my ($class, @params) = @_;
189 0 my $inst = $class->SUPER::new(@params);
190  
191 0 my $libxml = XML::LibXML->new;
192 0 $libxml->set_handler( $inst );
193 0 $inst->{LibParser} = $libxml;
194  
195 # setup SAX. 1 means "with SAX"
196 0 $libxml->_start_push(1);
197 0 $libxml->init_push;
198  
199 0 return $inst;
200 }
201  
202 0 ▶sub parse_chunk {
Subroutine parse_chunk: not called CC=1 SCAR=6.9
203 0 my ( $self, $chunk ) = @_;
204 0 my $libxml = $self->{LibParser};
205 0 my $rv = $libxml->push($chunk);
206 }
207  
208 0 ▶sub finish_push {
Subroutine finish_push: not called CC=2 SCAR=17.9
209 0 my $self = shift;
210 0 ▶ return 1 unless $self->{LibParser};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LibParser"} 0 0 0
211 0 my $parser = delete $self->{LibParser};
212 0 return eval { $parser->finish_push };
213 }
214  
215  
216 # compat for test:
217  
218 0 ▶sub _parse_string {
Subroutine _parse_string: not called CC=2 SCAR=17.9
219 0 my ( $self, $string ) = @_;
220 # $self->{ParserOptions}{LibParser} = XML::LibXML->new;
221 0 ▶ $self->{ParserOptions}{LibParser} = XML::LibXML->new() unless defined $self->{ParserOptions}{LibParser};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ParserOptions"}{"LibParser"} 0 0 0
222 0 $self->{ParserOptions}{ParseFunc} = \&XML::LibXML::parse_string;
223 0 $self->{ParserOptions}{ParseFuncParam} = $string;
224 0 return $self->_parse;
225 }
226  
227 0 ▶sub _parse {
Subroutine _parse: not called CC=2 SCAR=17.9
228 0 my $self = shift;
229 0 my $args = bless $self->{ParserOptions}, ref($self);
230  
231 0 $args->{LibParser}->set_handler( $self );
232 0 $args->{ParseFunc}->($args->{LibParser}, $args->{ParseFuncParam});
233  
234 0 ▶ if ( $args->{LibParser}->{SAX}->{State} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"LibParser"}{"SAX"}{"State"} == 1) 0 0 0
235 0 croak( "SAX Exception not implemented, yet; Data ended before document ended\n" );
236 }
237  
238 0 return $self->end_document({});
239 }
240  
241 package XML::Atom::Stream;
242  
243 1 1;
244 __END__
↑ Summary