XML.pm

filter: Statementstmt 76.0% 54 / 71 Branchbran 55.5% 10 / 18 Conditioncond 28.5% 2 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 80.0% 12 / 15 Podpod 100% 5 / 5 totaltotal 70.0% 84 / 120 CC 15 SCAR 17.9
CC
15
Cov
69%
put
12.0
BUILD
11.8
get_one
8.7
CRAP 6.0
↑ Summary XML/Node.pm »
1 package POE::Filter::XML;
2 {
3 2 $POE::Filter::XML::VERSION = '1.140700';
4 }
5  
6 #ABSTRACT: XML parsing for the POE framework
7  
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 2 extends 'Moose::Object','POE::Filter';
13  
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use POE::Filter::XML::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 has buffer =>
20 (
21 is => 'ro',
22 traits => [ 'Array' ],
23 isa => 'ArrayRef',
24 lazy => 1,
25 clearer => '_clear_buffer',
26 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 handles =>
28 {
29 has_buffer => 'count',
30 all_buffer => 'elements',
31 push_buffer => 'push',
32 shift_buffer => 'shift',
33 join_buffer => 'join',
34 }
35 );
36  
37  
38 has callback =>
39 (
40 is => 'ro',
41 isa => 'CodeRef',
42 lazy => 1,
43 0 ▶ default => sub { sub { Carp::confess('Parsing error happened: '. join("\n", @_)) } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 2 );
45  
46  
47 2 has handler =>
48 (
49 is => 'ro',
50 isa => 'POE::Filter::XML::Handler',
51 lazy => 1,
52 builder => '_build_handler',
53 );
54  
55  
56 2 has parser =>
57 (
58 is => 'ro',
59 isa => 'XML::LibXML',
60 lazy => 1,
61 builder => '_build_parser',
62 clearer => '_clear_parser'
63 );
64  
65  
66 2 has not_streaming =>
67 (
68 is => 'ro',
69 isa => 'Bool',
70 default => 0,
71 );
72  
73 2 ▶sub _build_handler {
Subroutine _build_handler: called CC=1 SCAR=0.0
74 2 my ($self) = @_;
75 2 POE::Filter::XML::Handler->new(not_streaming => $self->not_streaming)
76 }
77  
78 2 ▶sub _build_parser {
Subroutine _build_parser: called CC=1 SCAR=0.0
79 2 my ($self) = @_;
80 2 XML::LibXML->new(Handler => $self->handler)
81 }
82  
83  
84 2 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=24.6
Pod documented
85 2 my ($self) = @_;
86 2 ▶ if($self->has_buffer)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_buffer) 0 2 2
87 {
88 eval
89 {
90 0 $self->parser->parse_chunk($self->join_buffer("\n"));
91 0 1;
92 }
93 or do
94 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->parser->parse_chunk($self->join_buffer("\n")); 1 } } or do { my $err = $@ || "Zombie Error"; $self->callback->($err) } 0 0 0
95 0 ▶ my $err = $@ || 'Zombie Error';
Condition0%0/2
$@ || 'Zombie Error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
96 0 $self->callback->($err);
97 };
98  
99 0 $self->_clear_buffer();
100 }
101 }
102  
103  
104 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
105  
106 1 my ($self) = @_;
107 1 $self->handler->reset();
108 1 $self->_clear_parser();
109 1 $self->_clear_buffer();
110 }
111  
112  
113 2 ▶sub get_one_start {
Subroutine get_one_start: called CC=3 SCAR=11.1
Pod documented
114  
115 2 my ($self, $raw) = @_;
116  
117 2 ▶ if (defined $raw)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw) 2 0 2
118 {
119 2 foreach my $raw_data (@$raw)
120 {
121 2 $self->push_buffer(split(/(?=\x0a?\x0d|\x0d\x0a?)/s, $raw_data));
122 }
123 }
124 }
125  
126  
127 7 ▶sub get_one {
Subroutine get_one: called CC=8 SCAR=21.6
Pod documented
128  
129 7 my ($self) = @_;
130  
131 7 ▶ if($self->handler->has_finished_nodes())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->handler->has_finished_nodes) { } 4 3 7
132 {
133 4 return [$self->handler->get_finished_node()];
134  
135 }
136 else
137 {
138 3 ▶ while($self->has_buffer())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_buffer) 2 1 3
139 {
140 2 my $line = $self->shift_buffer();
141  
142 eval
143 {
144 2 $self->parser->parse_chunk($line);
145 2 1;
146 }
147 or do
148 2 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->parser->parse_chunk($line); 1 } } or do { my $err = $@ || "Zombie error"; $self->callback->($err) } 2 0 2
149 0 ▶ my $err = $@ || 'Zombie error';
Condition0%0/2
$@ || 'Zombie error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
150 0 $self->callback->($err);
151 };
152  
153 2 ▶ if($self->handler->has_finished_nodes())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->handler->has_finished_nodes) 2 0 2
154 {
155 2 my $node = $self->handler->get_finished_node();
156  
157 2 ▶ if($node->stream_end() or $self->not_streaming)
Branch: 1 branch
BranchTrueFalseTotal
if ($node->stream_end or $self->not_streaming) 1 1 2
Condition66%2/3
$node->stream_end or $self->not_streaming
l!l&&r!l&&!r
011
MC/DC50%1/2
$node->stream_end or $self->not_streaming
$node->stream_end $self->not_streaming
Truth table66%2/3
A or B
A= $node->stream_endB= $self->not_streaming
ABresult
1X1
011
000
158 {
159 1 $self->parser->parse_chunk('', 1);
160 1 $self->reset();
161 }
162  
163 2 return [$node];
164 }
165 }
166 1 return [];
167 }
168 }
169  
170  
171 0 ▶sub put {
Subroutine put: not called CC=3 SCAR=24.8
Pod documented
172 0 my ($self, $nodes) = @_;
173 0 my $output = [];
174  
175 0 foreach my $node (@$nodes)
176 {
177 0 ▶ if($node->stream_start())
Branch: 1 branch
BranchTrueFalseTotal
if ($node->stream_start) 0 0 0
178 {
179 0 $self->reset();
180 }
181 0 push(@$output, $node->toString());
182 }
183  
184 0 return $output;
185 }
186  
187 2 1;
188  
189  
190 =pod
191  
192 =head1 NAME
193  
194 POE::Filter::XML - XML parsing for the POE framework
195  
196 =head1 VERSION
197  
198 version 1.140700
199  
200 =head1 SYNOPSIS
201  
202 use POE::Filter::XML;
203 my $filter = POE::Filter::XML->new();
204  
205 my $wheel = POE::Wheel:ReadWrite->new(
206 Filter => $filter,
207 InputEvent => 'input_event',
208 );
209  
210 =head1 DESCRIPTION
211  
212 POE::Filter::XML provides POE with a completely encapsulated XML parsing
213 strategy for POE::Wheels that will be dealing with XML streams.
214  
215 The parser is XML::LibXML
216  
217 =head1 PUBLIC_ATTRIBUTES
218  
219 =head2 not_streaming
220  
221 is: ro, isa: Bool, default: false
222  
223 Setting the not_streaming attribute to true via new() will put this filter into
224 non-streaming mode, meaning that whole documents are parsed before nodes are
225 returned. This is handy for XMLRPC or other short documents.
226  
227 =head1 PRIVATE_ATTRIBUTES
228  
229 =head2 buffer
230  
231 is: ro, isa: ArrayRef, traits: Array
232  
233 buffer holds the raw data to be parsed. Raw data should be split on network
234 new lines before being added to the buffer. Access to this attribute is
235 provided by the following methods:
236  
237 handles =>
238 {
239 has_buffer => 'count',
240 all_buffer => 'elements',
241 push_buffer => 'push',
242 shift_buffer => 'shift',
243 join_buffer => 'join',
244 }
245  
246 =head2 callback
247  
248 is: ro, isa: CodeRef
249  
250 callback holds the CodeRef to be call in the event that there is an exception
251 generated while parsing content. By default it holds a CodeRef that simply
252 calls Carp::confess.
253  
254 =head2 handler
255  
256 is: ro, isa: POE::Filter::XML::Handler
257  
258 handler holds the SAX handler to be used for processing events from the parser.
259 By default POE::Filter::XML::Handler is instantiated and used.
260  
261 The L</not_streaming> attribute is passed to the constructor of Handler.
262  
263 =head2 parser
264  
265 is: ro, isa: XML::LibXML
266  
267 parser holds an instance of the XML::LibXML parser. The L</handler> attribute
268 is passed to the constructor of XML::LibXML.
269  
270 =head1 PUBLIC_METHODS
271  
272 =head2 get_one_start
273  
274 (ArrayRef $raw?)
275  
276 This method is part of the POE::Filter API. See L<POE::Filter/get_one_start>
277 for an explanation of its usage.
278  
279 =head2 get_one
280  
281 returns (ArrayRef)
282  
283 This method is part of the POE::Filter API. See L<POE::Filter/get_one> for an
284 explanation of its usage.
285  
286 =head2 put
287  
288 (ArrayRef $nodes) returns (ArrayRef)
289  
290 This method is part of the POE::Filter API. See L<POE::Filter/put> for an
291 explanation of its usage.
292  
293 =head1 PROTECTED_METHODS
294  
295 =head2 reset
296  
297 reset() is an internal method that gets called when either a stream_start(1)
298 POE::Filter::XML::Node gets placed into the filter via L</put>, or when a
299 stream_end(1) POE::Filter::XML::Node is pulled out of the queue of finished
300 Nodes via L</get_one>. This facilitates automagical behavior when using the
301 Filter within the XMPP protocol that requires many new stream initiations.
302 This method is also called after every document when not in streaming mode.
303 Useful for handling XMLRPC processing.
304  
305 This method really should never be called outside of the Filter, but it is
306 documented here in case the Filter is used outside of the POE context.
307  
308 =head1 PRIVATE_METHODS
309  
310 =head2 BUILD
311  
312 A BUILD method is provided to parse the initial buffer (if any was included
313 when constructing the filter).
314  
315 =head1 AUTHOR
316  
317 Nicholas R. Perez <nperez@cpan.org>
318  
319 =head1 COPYRIGHT AND LICENSE
320  
321 This software is copyright (c) 2014 by Nicholas R. Perez <nperez@cpan.org>.
322  
323 This is free software; you can redistribute it and/or modify it under
324 the same terms as the Perl 5 programming language system itself.
325  
326 =cut
327  
328  
329 __END__
↑ Summary XML/Node.pm »