blib/lib/XML/Filter/Conditional.pm

filter: Statementstmt 100% 113 / 113 Branchbran 90.3% 47 / 52 Conditioncond 100% 42 / 42 MC/DCmcdc 100% 26 / 26 Subroutinesub 100% 16 / 16 Podpod 87.5% 7 / 8 totaltotal 97.6% 251 / 257 CC 44 SCAR 22.9
CC
44
Cov
98%
start_element
18.0
end_element
11.0
new
10.2
CRAP 9.9
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2007,2009 -- leonerd@leonerd.org.uk
5  
6 package XML::Filter::Conditional;
7  
8 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use base qw( XML::SAX::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our $VERSION = '0.05';
15  
16 =head1 NAME
17  
18 C<XML::Filter::Conditional> - an XML SAX filter for conditionally ignoring XML
19 content
20  
21 =head1 SYNOPSIS
22  
23 CODE:
24  
25 package My::XML::Filter;
26 use base qw( XML::Filter::Conditional );
27  
28 sub store_switch
29 {
30 my $self = shift;
31 my ( $e ) = @_;
32  
33 my $ename = $e->{Attributes}{'{}env'}{Value};
34 return $ENV{$ename};
35 }
36  
37 sub eval_case
38 {
39 my $self = shift;
40 my ( $value, $e ) = @_;
41  
42 return $value eq $e->{Attributes}{'{}value'}{Value};
43 }
44  
45 XML:
46  
47 <message>
48 <switch env="USER">
49 <case value="root">Hello there, root user</case>
50 <case value="mail">Hello there, mail user</case>
51 <otherwise>Hello, whoever you are</otherwise>
52 </switch>
53 </message>
54  
55 =head1 DESCRIPTION
56  
57 This module provides an abstract base class to implement a PerlSAX filter
58 which conditionally ignores part of the XML content. The base class provides
59 the implememtation of actually surpressing SAX events for filtering purposes,
60 and delegates the evaluation of matches to the subclassed instance.
61  
62 The evaluation of the matches is performed by the abstract methods
63 C<store_switch()> and C<eval_case()>; see their detail below.
64  
65 =cut
66  
67 =head1 CONSTRUCTOR
68  
69 =cut
70  
71 =head2 $filter = XML::Filter::Conditional->new( %opts )
72  
73 Takes the following options:
74  
75 =over 8
76  
77 =item Handler => OBJECT
78  
79 The PerlSAX handler (or another filter) that will receive the PerlSAX events
80 from this filter.
81  
82 =item SwitchTag => STRING or REGEXP
83  
84 =item CaseTag => STRING or REGEXP
85  
86 =item OtherwiseTag => STRING or REGEXP
87  
88 Changes the tag names used for the C<switch>, C<case> and C<otherwise>
89 elements. Can be precompiled regexp values instead of literal strings. The
90 values will be matched against the local name of the tag only, ignoring any
91 namespace prefix.
92  
93 =item NamespaceURI => STRING
94  
95 If present, the tags will only be recognised if they are part of the given
96 namespace. Defaults to the empty string, meaning tags will only be recognised
97 if they do not have a namespace prefix, and no default namespace was defined
98 for the document.
99  
100 =item MatchAll => BOOLEAN
101  
102 Determines whether all of the matching C<< <case> >> elements will be used, or
103 only the first one that matches. By default, only the first matching one will
104 be used.
105  
106 =back
107  
108 =cut
109  
110 sub new
111 8 ▶{
Subroutine new: called CC=10 SCAR=23.2
Pod documented
112 8 my $class = shift;
113 8 my %opts = @_;
114  
115 # Check that the abstract methods are implemented
116  
117 8 ▶ $class->can( "store_switch" ) or
Branch: 1 branch
BranchTrueFalseTotal
$class->can("store_switch") or croak("$class must provide ->store_switch()") 7 1 8
118 croak "$class must provide ->store_switch()";
119 7 ▶ $class->can( "eval_case" ) or
Branch: 1 branch
BranchTrueFalseTotal
$class->can("eval_case") or croak("$class must provide ->eval_case()") 7 0 7
120 croak "$class must provide ->eval_case()";
121  
122 7 ▶ my $switchtag = delete $opts{SwitchTag} || "switch";
Condition100%2/2
delete $opts{'SwitchTag'} || 'switch'
l!l
16
MC/DC100%1/1
delete $opts{'SwitchTag'} || 'switch'
delete $opts{'SwitchTag'}
Truth table100%2/2
A
A= delete $opts{'SwitchTag'}
Aresult
11
00
123 7 ▶ my $casetag = delete $opts{CaseTag} || "case";
Condition100%2/2
delete $opts{'CaseTag'} || 'case'
l!l
16
MC/DC100%1/1
delete $opts{'CaseTag'} || 'case'
delete $opts{'CaseTag'}
Truth table100%2/2
A
A= delete $opts{'CaseTag'}
Aresult
11
00
124 7 ▶ my $otherwisetag = delete $opts{OtherwiseTag} || "otherwise";
Condition100%2/2
delete $opts{'OtherwiseTag'} || 'otherwise'
l!l
16
MC/DC100%1/1
delete $opts{'OtherwiseTag'} || 'otherwise'
delete $opts{'OtherwiseTag'}
Truth table100%2/2
A
A= delete $opts{'OtherwiseTag'}
Aresult
11
00
125  
126 7 ▶ $opts{NamespaceURI} ||= "";
Condition100%2/2
$opts{"NamespaceURI"} ||= ""
l!l
16
MC/DC100%1/1
$opts{"NamespaceURI"} ||= ""
$opts{"NamespaceURI"}
Truth table100%2/2
A
A= $opts{"NamespaceURI"}
Aresult
11
00
127  
128 7 my $self = $class->SUPER::new( %opts );
129  
130 7 $self->{switch_stack} = [];
131  
132 7 ▶ $self->{switchre} = ref($switchtag) eq "Regexp" ?
Branch: 1 branch
BranchTrueFalseTotal
ref $switchtag eq 'Regexp' ? : 0 7 7
133 $switchtag :
134 qr/^\Q$switchtag\E$/;
135  
136 7 ▶ $self->{casere} = ref($casetag) eq "Regexp" ?
Branch: 1 branch
BranchTrueFalseTotal
ref $casetag eq 'Regexp' ? : 0 7 7
137 $switchtag :
138 qr/^\Q$casetag\E$/;
139  
140 7 ▶ $self->{otherwisere} = ref($otherwisetag) eq "Regexp" ?
Branch: 1 branch
BranchTrueFalseTotal
ref $otherwisetag eq 'Regexp' ? : 0 7 7
141 $otherwisetag :
142 qr/^\Q$otherwisetag\E$/;
143  
144 7 return $self;
145 }
146  
147 # We'll keep a little state machine
148 # <switch>
149 # outside
150 # <case thatmatches> in hot </case>
151 # <case> in cold </case>
152 # </switch>
153  
154 6 ▶use constant STATE_SWITCH_OUTSIDE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 6 ▶use constant STATE_SWITCH_INHOT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 6 ▶use constant STATE_SWITCH_INCOLD => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 # Define an exception subclass
159 6 @XML::Filter::Conditional::Exception::ISA = qw( XML::SAX::Exception );
160  
161 sub throw_exception
162 4 ▶{
Subroutine throw_exception: called CC=3 SCAR=11.0
Pod undocumented
163 4 my $self = shift;
164 4 my ( $message ) = @_;
165  
166 4 my %args = ( Message => $message );
167  
168 4 ▶ if( defined( my $locator = $self->{Locator} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $locator = $self->{"Locator"})) 4 0 4
169 4 $args{$_} = $locator->{$_} for (qw( LineNumber ColumnNumber ));
170 }
171  
172 4 XML::Filter::Conditional::Exception->throw( %args );
173 }
174  
175 sub set_document_locator
176 12 ▶{
Subroutine set_document_locator: called CC=1 SCAR=0.0
Pod documented
177 12 my $self = shift;
178 12 my ( $locator ) = @_;
179  
180 12 $self->{Locator} = $locator;
181  
182 12 $self->SUPER::set_document_locator( $locator );
183 }
184  
185 sub start_element
186 58 ▶{
Subroutine start_element: called CC=18 SCAR=28.9
Pod documented
187 58 my $self = shift;
188 58 my ( $e ) = @_;
189  
190 58 my $name = $e->{LocalName};
191  
192 58 ▶ my $right_namespace = ( ($e->{NamespaceURI}||"") eq $self->{NamespaceURI} );
Condition100%2/2
$e->{'NamespaceURI'} || ''
l!l
355
MC/DC100%1/1
$e->{'NamespaceURI'} || ''
$e->{'NamespaceURI'}
Truth table100%2/2
A
A= $e->{'NamespaceURI'}
Aresult
11
00
193  
194 58 ▶ if( $right_namespace and $name =~ $self->{switchre} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($right_namespace and $name =~ /$self->{'switchre'}/) { } 9 49 58
elsif ($right_namespace and $name =~ /$self->{'casere'}/) { } 18 31 49
elsif ($right_namespace and $name =~ /$self->{'otherwisere'}/) { } 7 24 31
Condition100%3/3
$right_namespace and $name =~ /$self->{"switchre"}/
!ll&&!rl&&r
4459
MC/DC100%2/2
$right_namespace and $name =~ /$self->{"switchre"}/
$right_namespace $name =~ /$self->{"switchre"}/
Truth table100%3/3
A and B
A= $right_namespaceB= $name =~ /$self->{"switchre"}/
ABresult
0X0
100
111
195 9 push @{ $self->{switch_stack} },
196 {
197 state => STATE_SWITCH_OUTSIDE,
198 didcase => 0,
199 };
200  
201 9 $self->{switch_state} = $self->{switch_stack}[-1];
202  
203 9 $self->{switch_state}{cond} = $self->store_switch( $e );
204  
205 9 return; # EAT
206 }
207 ▶ elsif( $right_namespace and $name =~ $self->{casere} ) {
Condition100%3/3
$right_namespace and $name =~ /$self->{"casere"}/
!ll&&!rl&&r
42718
MC/DC100%2/2
$right_namespace and $name =~ /$self->{"casere"}/
$right_namespace $name =~ /$self->{"casere"}/
Truth table100%3/3
A and B
A= $right_namespaceB= $name =~ /$self->{"casere"}/
ABresult
0X0
100
111
208 18 my $state = $self->{switch_state}{state};
209  
210 18 ▶ defined $state or
Branch: 1 branch
BranchTrueFalseTotal
defined $state or $self->throw_exception("Found a <$name> element outside of a containing switch") 17 1 18
211 $self->throw_exception( "Found a <$name> element outside of a containing switch" );
212  
213 17 ▶ $state == STATE_SWITCH_OUTSIDE or
Branch: 1 branch
BranchTrueFalseTotal
$state == 1 or $self->throw_exception("Found a <$name> element nested within another") 16 1 17
214 $self->throw_exception( "Found a <$name> element nested within another" );
215  
216 16 ▶ if( $self->{MatchAll} or !$self->{switch_state}{didcase} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MatchAll"} or not $self->{"switch_state"}{"didcase"}) 11 5 16
Condition100%3/3
$self->{"MatchAll"} or not $self->{"switch_state"}{"didcase"}
l!l&&r!l&&!r
385
MC/DC100%2/2
$self->{"MatchAll"} or not $self->{"switch_state"}{"didcase"}
$self->{"MatchAll"} not $self->{"switch_state"}{"didcase"}
Truth table100%3/3
A or not(B)
A= $self->{"MatchAll"}B= not $self->{"switch_state"}{"didcase"}
ABresult
1X1
011
000
217 11 ▶ if( $self->eval_case( $self->{switch_state}{cond}, $e ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eval_case($self->{"switch_state"}{"cond"}, $e)) 7 4 11
218 7 $self->{switch_state}{state} = STATE_SWITCH_INHOT;
219 7 return; # EAT
220 }
221 }
222  
223 9 $self->{switch_state}{state} = STATE_SWITCH_INCOLD;
224 9 return; # EAT
225 }
226 ▶ elsif( $right_namespace and $name =~ $self->{otherwisere} ) {
Condition100%3/3
$right_namespace and $name =~ /$self->{"otherwisere"}/
!ll&&!rl&&r
4207
MC/DC100%2/2
$right_namespace and $name =~ /$self->{"otherwisere"}/
$right_namespace $name =~ /$self->{"otherwisere"}/
Truth table100%3/3
A and B
A= $right_namespaceB= $name =~ /$self->{"otherwisere"}/
ABresult
0X0
100
111
227 7 my $state = $self->{switch_state}{state};
228  
229 7 ▶ defined $state or
Branch: 1 branch
BranchTrueFalseTotal
defined $state or $self->throw_exception("Found a <$name> element outside of a containing switch") 6 1 7
230 $self->throw_exception( "Found a <$name> element outside of a containing switch" );
231  
232 6 ▶ $state == STATE_SWITCH_OUTSIDE or
Branch: 1 branch
BranchTrueFalseTotal
$state == 1 or $self->throw_exception("Found a <$name> element nested within another") 5 1 6
233 $self->throw_exception( "Found a <$name> element nested within another" );
234  
235 # Treat it like a case which might be true
236  
237 5 ▶ if( !$self->{switch_state}{didcase} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"switch_state"}{"didcase"}) 3 2 5
238 3 $self->{switch_state}{state} = STATE_SWITCH_INHOT;
239 3 return; # EAT
240 }
241  
242 2 $self->{switch_state}{state} = STATE_SWITCH_INCOLD;
243 2 return; # EAT
244 }
245 else {
246 24 my $state = $self->{switch_state}{state};
247 24 ▶ if( defined $state and $state == STATE_SWITCH_INCOLD ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state and $state == 3) 1 23 24
Condition100%3/3
defined $state and $state == 3
!ll&&!rl&&r
2121
MC/DC100%2/2
defined $state and $state == 3
defined $state $state == 3
Truth table100%3/3
A and B
A= defined $stateB= $state == 3
ABresult
0X0
100
111
248 1 return; # EAT
249 }
250 }
251  
252 23 return $self->SUPER::start_element( $e );
253 }
254  
255 sub end_element
256 46 ▶{
Subroutine end_element: called CC=11 SCAR=24.0
Pod documented
257 46 my $self = shift;
258 46 my ( $e ) = @_;
259  
260 46 my $name = $e->{LocalName};
261  
262 46 ▶ my $right_namespace = ( ($e->{NamespaceURI}||"") eq $self->{NamespaceURI} );
Condition100%2/2
$e->{'NamespaceURI'} || ''
l!l
343
MC/DC100%1/1
$e->{'NamespaceURI'} || ''
$e->{'NamespaceURI'}
Truth table100%2/2
A
A= $e->{'NamespaceURI'}
Aresult
11
00
263  
264 46 my $state = $self->{switch_state}{state};
265  
266 46 ▶ if( $right_namespace and $name =~ $self->{switchre} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($right_namespace and $name =~ /$self->{'switchre'}/) { } 7 39 46
elsif ($right_namespace and $name =~ /$self->{'casere'}/) { } 15 24 39
elsif ($right_namespace and $name =~ /$self->{'otherwisere'}/) { } 4 20 24
Condition100%3/3
$right_namespace and $name =~ /$self->{"switchre"}/
!ll&&!rl&&r
4357
MC/DC100%2/2
$right_namespace and $name =~ /$self->{"switchre"}/
$right_namespace $name =~ /$self->{"switchre"}/
Truth table100%3/3
A and B
A= $right_namespaceB= $name =~ /$self->{"switchre"}/
ABresult
0X0
100
111
267 7 pop @{ $self->{switch_stack} };
268 7 $self->{switch_state} = $self->{switch_stack}[-1];
269  
270 7 return; # EAT
271 }
272 ▶ elsif( $right_namespace and $name =~ $self->{casere} ) {
Condition100%3/3
$right_namespace and $name =~ /$self->{"casere"}/
!ll&&!rl&&r
42015
MC/DC100%2/2
$right_namespace and $name =~ /$self->{"casere"}/
$right_namespace $name =~ /$self->{"casere"}/
Truth table100%3/3
A and B
A= $right_namespaceB= $name =~ /$self->{"casere"}/
ABresult
0X0
100
111
273 15 ▶ if( $state == STATE_SWITCH_INHOT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 2) 6 9 15
274 6 $self->{switch_state}{didcase} = 1;
275 }
276  
277 15 $self->{switch_state}{state} = STATE_SWITCH_OUTSIDE;
278  
279 15 return; # EAT
280 }
281 ▶ elsif( $right_namespace and $name =~ $self->{otherwisere} ) {
Condition100%3/3
$right_namespace and $name =~ /$self->{"otherwisere"}/
!ll&&!rl&&r
4164
MC/DC100%2/2
$right_namespace and $name =~ /$self->{"otherwisere"}/
$right_namespace $name =~ /$self->{"otherwisere"}/
Truth table100%3/3
A and B
A= $right_namespaceB= $name =~ /$self->{"otherwisere"}/
ABresult
0X0
100
111
282 4 return; # EAT
283 }
284 else {
285 20 ▶ return if( defined $state and $state == STATE_SWITCH_INCOLD );
Branch: 1 branch
BranchTrueFalseTotal
if defined $state and $state == 3 1 19 20
Condition100%3/3
defined $state and $state == 3
!ll&&!rl&&r
1811
MC/DC100%2/2
defined $state and $state == 3
defined $state $state == 3
Truth table100%3/3
A and B
A= defined $stateB= $state == 3
ABresult
0X0
100
111
286 }
287  
288 19 return $self->SUPER::end_element( $e );
289 }
290  
291 sub _surpress
292 81 ▶{
Subroutine _surpress: called CC=3 SCAR=11.0
293 81 my $self = shift;
294  
295 81 my $state = $self->{switch_state}{state};
296  
297 81 ▶ if( defined $state and $state == STATE_SWITCH_INCOLD ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state and $state == 3) 13 68 81
Condition100%3/3
defined $state and $state == 3
!ll&&!rl&&r
373113
MC/DC100%2/2
defined $state and $state == 3
defined $state $state == 3
Truth table100%3/3
A and B
A= defined $stateB= $state == 3
ABresult
0X0
100
111
298 13 return 1;
299 }
300  
301 68 return 0;
302 }
303  
304 sub characters
305 75 ▶{
Subroutine characters: called CC=2 SCAR=6.9
Pod documented
306 75 my $self = shift;
307 75 my ( $e ) = @_;
308  
309 75 ▶ return if $self->_surpress;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_surpress 11 64 75
310  
311 64 return $self->SUPER::characters( $e );
312 }
313  
314 sub comment
315 3 ▶{
Subroutine comment: called CC=2 SCAR=6.9
Pod documented
316 3 my $self = shift;
317 3 my ( $e ) = @_;
318  
319 3 ▶ return if $self->_surpress;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_surpress 1 2 3
320  
321 2 return $self->SUPER::comment( $e );
322 }
323  
324 sub processing_instruction
325 3 ▶{
Subroutine processing_instruction: called CC=2 SCAR=6.9
Pod documented
326 3 my $self = shift;
327 3 my ( $e ) = @_;
328  
329 3 ▶ return if $self->_surpress;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_surpress 1 2 3
330  
331 2 return $self->SUPER::processing_instruction( $e );
332 }
333  
334 # Keep perl happy; keep Britain tidy
335 6 1;
336  
337 __END__
↑ Summary