Essex/Filter/OutputMonitor.pm

filter: Statementstmt 91.5% 76 / 83 Branchbran 69.4% 25 / 36 Conditioncond 62.5% 15 / 24 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 16 / 16 Podpod 91.6% 11 / 12 totaltotal 80.4% 148 / 184 CC 28 SCAR 25.3
CC
28
Cov
81%
end_element
31.3
end_document
4.4
finish
4.0
CRAP 12.5
« Essex/NamespaceMap.pm ↑ Summary Essex.pm »
1 package XML::Essex::Filter::OutputMonitor;
2  
3 8 $VERSION = 0.000_1;
4  
5 =head1 NAME
6  
7 XML::Essex::Filter::OutputMonitor - Enforce and cajol Essex output
8  
9 =head1 SYNOPSIS
10  
11 ## Internal use only
12  
13 =head1 DESCRIPTION
14  
15 Used by XML::Generator::Essex and XML::Filter::Essex instances to watch
16 what's emitted downstream so as to capture result codes and notice
17 incomplete documents.
18  
19 Fills in partially filled end_element events and dies on mismatched
20 end_element events. Fills in missing start_document and end_document
21 events (unless L<auto_document_events|/auto_document_events> is
22 cleared).
23  
24 NOTE: This could actually fill in missing end_element events, but I
25 think that would lead to subtle bugs and would not help in many cases.
26 It's safer to leave it out and we can always add this feature in the
27 future, whereas taking it out would be difficult.
28  
29 Returns a 1 from the end_document if no downstream handler is found.
30  
31 =for test_script XML-Generator-Essex.t
32  
33 =head2 Methods
34  
35 =over
36  
37 =cut
38  
39 8 ▶use XML::SAX::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 @ISA = qw( XML::SAX::Base );
41  
42 # Not sure why Carp is still reporting errors in XML::Essex.
43 8 @CARP_NOT = ( @ISA, qw( XML::Essex XML::Essex::Event XML::Filter::Essex ) );
44  
45 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 ▶use XML::Essex::Model ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 34 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
50 34 my $self = shift->SUPER::new( @_ );
51 34 $self->auto_document_events( 1 );
52 34 return $self;
53 }
54  
55  
56 38 ▶sub start_document {
Subroutine start_document: called CC=1 SCAR=0.0
Pod documented
57 38 my $self = shift;
58  
59 38 $self->start_document_seen( 1 );
60 38 $self->end_document_seen( 0 );
61 38 $self->end_document_result( undef );
62 38 $self->end_document_result_has_been_set( 0 );
63 38 $self->{Stack} = [];
64  
65 38 $self->SUPER::start_document( @_ );
66 }
67  
68  
69 60 ▶sub start_element {
Subroutine start_element: called CC=3 SCAR=11.0
Pod documented
70 60 my $self = shift;
71  
72 60 ▶ $self->start_document( {} )
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_document_events and not $self->start_document_seen 1 59 60
Condition66%2/3
$self->auto_document_events and not $self->start_document_seen
!ll&&!rl&&r
0591
MC/DC50%1/2
$self->auto_document_events and not $self->start_document_seen
$self->auto_document_events not $self->start_document_seen
Truth table66%2/3
A and not(B)
A= $self->auto_document_eventsB= not $self->start_document_seen
ABresult
0X0
100
111
73 if $self->auto_document_events
74 && ! $self->start_document_seen;
75  
76 60 push @{$self->{Stack}}, $_[0];
77  
78 60 $self->SUPER::start_element( @_ );
79 }
80  
81 60 ▶sub end_element {
Subroutine end_element: called CC=15 SCAR=34.4
Pod documented
82 60 my $self = shift;
83 60 my ( $elt ) = @_;
84  
85 60 my $s = $self->{Stack};
86  
87 60 ▶ Carp::croak "extra end_element at end of document: </",
Branch: 1 branch
BranchTrueFalseTotal
unless @$s 0 60 60
88 XML::Essex::Model::_render_event_name( $elt ),
89 ">"
90 unless @$s;
91  
92 # Only DWIM if nothing has been set so that partially built
93 # end_elements are more likely to cause downstream errors.
94 60 ▶ if ( ! defined $elt->{NamespaceURI}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $elt->{"NamespaceURI"} or defined $elt->{"LocalName"} or defined $elt->{"Prefix"} or defined $elt->{"Name"}) 10 50 60
Condition77%7/9
defined $elt->{"NamespaceURI"} or defined $elt->{"LocalName"} or defined $elt->{"Prefix"} or defined $elt->{"Name"}
l!l&&r!l&&!r
50010
defined $elt->{"NamespaceURI"} or defined $elt->{"LocalName"} or defined $elt->{"Prefix"}
l!l&&r!l&&!r
50010
defined $elt->{"NamespaceURI"} or defined $elt->{"LocalName"}
l!l&&r!l&&!r
173310
MC/DC50%2/4
defined $elt->{"NamespaceURI"} or defined $elt->{"LocalName"} or defined $elt->{"Prefix"} or defined $elt->{"Name"}
defined $elt->{"NamespaceURI"} defined $elt->{"LocalName"} defined $elt->{"Prefix"} defined $elt->{"Name"}
Truth table60%3/5
A or B or C or D
A= defined $elt->{"NamespaceURI"}B= defined $elt->{"LocalName"}C= defined $elt->{"Prefix"}D= defined $elt->{"Name"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
95 && ! defined $elt->{LocalName}
96 && ! defined $elt->{Prefix}
97 && ! defined $elt->{Name}
98 ) {
99 10 @{$elt}{qw(
100 NamespaceURI
101 LocalName
102 Prefix
103 Name
104 10 )} = @{$s->[-1]}{qw(
105 NamespaceURI
106 LocalName
107 Prefix
108 Name
109 )};
110 }
111  
112 my $ns_uri = defined $elt->{NamespaceURI}
113 ? $elt->{NamespaceURI}
114 60 ▶ : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $elt->{'NamespaceURI'} ? : 17 43 60
115  
116 60 ▶ if ( $s->[-1]->{LocalName} eq $elt->{LocalName}
Branch: 2 branches
BranchTrueFalseTotal
if ($s->[-1]{'LocalName'} eq $elt->{'LocalName'} and (defined $s->[-1]{'NamespaceURI'} ? $s->[-1]{'NamespaceURI'} : '') eq $ns_uri) { } 60 0 60
defined $s->[-1]{'NamespaceURI'} ? : 17 43 60
Condition33%1/3
$s->[-1]{"LocalName"} eq $elt->{"LocalName"} and (defined $s->[-1]{"NamespaceURI"} ? $s->[-1]{"NamespaceURI"} : "") eq $ns_uri
!ll&&!rl&&r
0060
MC/DC0%0/2
$s->[-1]{"LocalName"} eq $elt->{"LocalName"} and (defined $s->[-1]{"NamespaceURI"} ? $s->[-1]{"NamespaceURI"} : "") eq $ns_uri
$s->[-1]{"LocalName"} eq $elt->{"LocalName"} (defined $s->[-1]{"NamespaceURI"} ? $s->[-1]{"NamespaceURI"} : "") eq $ns_uri
Truth table33%1/3
A and B
A= $s->[-1]{"LocalName"} eq $elt->{"LocalName"}B= (defined $s->[-1]{"NamespaceURI"} ? $s->[-1]{"NamespaceURI"} : "") eq $ns_uri
ABresult
0X0
100
111
117 && (
118 defined $s->[-1]->{NamespaceURI}
119 ? $s->[-1]->{NamespaceURI}
120 : ""
121 )
122 eq $ns_uri
123 ) {
124 60 pop @$s;
125 }
126 else {
127 0 my @missing;
128 0 for ( reverse @$s ) {
129 last
130 if $_->{LocalName} eq $elt->{LocalName}
131 && (
132 defined $_->{NamespaceURI}
133 ? $_->{NamespaceURI}
134 0 ▶ : ""
Branch: 2 branches
BranchTrueFalseTotal
if $_->{"LocalName"} eq $elt->{"LocalName"} and (defined $_->{"NamespaceURI"} ? $_->{"NamespaceURI"} : "") eq $ns_uri 0 0 0
defined $_->{'NamespaceURI'} ? : 0 0 0
Condition0%0/3
$_->{"LocalName"} eq $elt->{"LocalName"} and (defined $_->{"NamespaceURI"} ? $_->{"NamespaceURI"} : "") eq $ns_uri
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{"LocalName"} eq $elt->{"LocalName"} and (defined $_->{"NamespaceURI"} ? $_->{"NamespaceURI"} : "") eq $ns_uri
$_->{"LocalName"} eq $elt->{"LocalName"} (defined $_->{"NamespaceURI"} ? $_->{"NamespaceURI"} : "") eq $ns_uri
Truth table0%0/3
A and B
A= $_->{"LocalName"} eq $elt->{"LocalName"}B= (defined $_->{"NamespaceURI"} ? $_->{"NamespaceURI"} : "") eq $ns_uri
ABresult
0X0
100
111
135 ) eq $ns_uri;
136 0 push @missing, $_;
137 }
138  
139 0 ▶ Carp::croak( "end_element mismatch: expected </",
Branch: 1 branch
BranchTrueFalseTotal
!@missing ? : 0 0 0
140 XML::Essex::Model::_render_event_name( $s->[-1] ),
141 ">, got </",
142 XML::Essex::Model::_render_event_name( $elt ),
143 ">",
144 ! @missing
145 ? ()
146 : (
147 ". These may have been skipped: ",
148 map "</"
149 . XML::Essex::Model::_render_event_name( $_ )
150 . ">",
151 @missing
152 ),
153 );
154 }
155  
156 60 $self->SUPER::end_element( @_ );
157 }
158  
159  
160 38 ▶sub end_document {
Subroutine end_document: called CC=4 SCAR=14.8
Pod documented
161 38 my $self = shift;
162  
163 38 $self->end_document_seen( 1 );
164  
165 Carp::croak( "end_document sent but no start_document" )
166 38 ▶ unless $self->{Stack};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Stack"} 0 38 38
167  
168 38 ▶ if ( @{$self->{Stack}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"Stack"};}) 0 38 38
169 Carp::croak( "missing end_element(s) at end of document: ",
170 map "</"
171 . XML::Essex::Model::_render_event_name( $_ )
172 . ">",
173 0 reverse @{$self->{Stack}}
174 );
175 }
176  
177 38 delete $self->{Stack};
178  
179 # Use a scalar to catch the result so a return of ()
180 # converts to "undef".
181 38 ▶ my $r = $self->SUPER::get_handler
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::get_handler ? : 38 0 38
182 ? $self->SUPER::end_document( @_ )
183 : 1;
184  
185 38 $self->end_document_result( $r );
186 38 return $r;
187 }
188  
189  
190 =item reset
191  
192 Undefines all state variables.
193  
194 =cut
195  
196 43 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
197 43 my $self = shift;
198  
199 43 $self->start_document_seen( undef );
200 43 $self->end_document_seen( undef );
201 43 $self->end_document_result( undef );
202 43 $self->end_document_result_has_been_set( 0 );
203 }
204  
205  
206 =item finish
207  
208 Emits an end_doc if need be.
209  
210 =cut
211  
212 36 ▶sub finish {
Subroutine finish: called CC=4 SCAR=13.9
Pod documented
213 36 my $self = shift;
214  
215 36 ▶ $self->end_document( {} )
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_document_events and $self->start_document_seen and not $self->end_document_seen 1 35 36
Condition83%5/6
$self->auto_document_events and $self->start_document_seen and not $self->end_document_seen
!ll&&!rl&&r
5301
$self->auto_document_events and $self->start_document_seen
!ll&&!rl&&r
0531
MC/DC66%2/3
$self->auto_document_events and $self->start_document_seen and not $self->end_document_seen
$self->auto_document_events $self->start_document_seen not $self->end_document_seen
Truth table75%3/4
A and B and not(C)
A= $self->auto_document_eventsB= $self->start_document_seenC= not $self->end_document_seen
ABCresult
0XX0
10X0
1100
1111
216 if $self->auto_document_events
217 && $self->start_document_seen
218 && ! $self->end_document_seen;
219 }
220  
221  
222 =item start_document_seen
223  
224 Sets/gets whether the start_document event was seen.
225  
226 =cut
227  
228 218 ▶sub start_document_seen {
Subroutine start_document_seen: called CC=2 SCAR=6.9
Pod documented
229 218 my $self = shift;
230 218 ▶ $self->{EssexStartDocumentSeen} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 81 137 218
231 218 return $self->{EssexStartDocumentSeen};
232 }
233  
234 =item end_document_seen
235  
236 Sets/gets whether the end_document event was seen. Will be set if the
237 downstream filter's C<end_document()> throws an exception.
238  
239 =cut
240  
241 181 ▶sub end_document_seen {
Subroutine end_document_seen: called CC=2 SCAR=6.9
Pod documented
242 181 my $self = shift;
243 181 ▶ $self->{EssexEndDocumentSeen} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 119 62 181
244 181 return $self->{EssexEndDocumentSeen};
245 }
246  
247 =item end_document_result
248  
249 Sets/gets the result returned by the downstream filter's C<end_document()>
250 event handler. Set to undef if nothing else, for instance if the
251 downstream C<end_document()> throws an exception.
252  
253 =cut
254  
255 157 ▶sub end_document_result {
Subroutine end_document_result: called CC=2 SCAR=6.9
Pod documented
256 157 my $self = shift;
257 157 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 119 38 157
258 119 $self->{EssexEndDocumentResult} = shift;
259 119 $self->{EssexEndDocumentResultHasBeenSet} = 1;
260 }
261 157 return $self->{EssexEndDocumentResult};
262 }
263  
264 =item end_document_result_has_been_set
265  
266 Set if the end_document_result() has been called.
267  
268 =cut
269  
270 124 ▶sub end_document_result_has_been_set {
Subroutine end_document_result_has_been_set: called CC=2 SCAR=6.9
Pod documented
271 124 my $self = shift;
272 124 ▶ $self->{EssexEndDocumentResultHasBeenSet} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 81 43 124
273 124 return $self->{EssexEndDocumentResultHasBeenSet};
274 }
275  
276 =item auto_document_events
277  
278 When set (the default), a start_document will be emitted before the
279 first event unless it is start_document event and an end_document will
280 be emitted after the last event unless an end_document was emitted.
281 When cleared, this automation does not occur. The automatic end_document
282 will not be emitted if an exception is thrown so as not to cause
283 a stateful downstream handler to throw an additional exception.
284  
285 This does allow well-balanced chunks of XML to be emitted, but there
286 will be start_ and end_document events around them. Clear this
287 member is you don't want to emit them, or if you want to emit them
288 yourself.
289  
290 This is not affected by L<reset()|/reset>.
291  
292 =cut
293  
294 130 ▶sub auto_document_events {
Subroutine auto_document_events: called CC=2 SCAR=6.9
Pod documented
295 130 my $self = shift;
296 130 ▶ $self->{AutoDocumentEvents} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 34 96 130
297 130 return $self->{AutoDocumentEvents};
298 }
299  
300 =back
301  
302 =head1 LIMITATIONS
303  
304 =head1 COPYRIGHT
305  
306 Copyright 2002, R. Barrie Slaymaker, Jr., All Rights Reserved
307  
308 =head1 LICENSE
309  
310 You may use this module under the terms of the BSD, Artistic, oir GPL licenses,
311 any version.
312  
313 =head1 AUTHOR
314  
315 Barrie Slaymaker <barries@slaysys.com>
316  
317 =cut
318  
319 8 1;
« Essex/NamespaceMap.pm ↑ Summary Essex.pm »