lib/XML/Schema/Particle/Choice.pm

filter: Statementstmt 73.8% 48 / 65 Branchbran 30.0% 12 / 40 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 87.5% 7 / 8 Podpod 20.0% 1 / 5 totaltotal 54.2% 71 / 131 CC 25 SCAR 30.3
CC
25
Cov
56%
end
42.0
element
30.1
init
8.6
CRAP 20.7
« lib/XML/Schema/Attribute/Group.pm ↑ Summary lib/XML/Schema/Scope.pm »
1 #============================================================= -*-perl-*-
2 #
3 # XML::Schema::Particle::Choice.pm
4 #
5 # DESCRIPTION
6 # Subclassed particle to contain a choice of other particles
7 # which can be matched in any order.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@kfs.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2001 Canon Research Centre Europe Ltd.
14 # All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 # REVISION
20 # $Id: Choice.pm,v 1.1.1.1 2001/08/29 14:30:17 abw Exp $
21 #
22 #========================================================================
23  
24 package XML::Schema::Particle::Choice;
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use base qw( XML::Schema::Particle );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use vars qw( $VERSION $DEBUG $ERROR $ETYPE );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/);
31 1 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::Schema::Particle::Choice::DEBUG 1 0 1
32 1 $ERROR = '';
33 1 $ETYPE = 'ChoiceParticle';
34  
35  
36 #------------------------------------------------------------------------
37 # init()
38 #
39 # Called automatically by base class new() method.
40 #------------------------------------------------------------------------
41  
42 3 ▶sub init {
Subroutine init: called CC=8 SCAR=21.6
Pod documented
43 3 my ($self, $config) = @_;
44  
45 3 ▶ $self->TRACE("config => ", $config) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Choice::DEBUG 0 3 3
46  
47 my $choice = $config->{ choice }
48 3 ▶ || return $self->error("no choice defined");
Condition50%1/2
$config->{'choice'} || (return $self->error('no choice defined'))
l!l
30
MC/DC0%0/1
$config->{'choice'} || (return $self->error('no choice defined'))
$config->{'choice'}
Truth table50%1/2
A
A= $config->{'choice'}
Aresult
11
00
49  
50 3 ▶ return $self->error("choice expects an array ref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $choice eq "ARRAY" 0 3 3
51 unless ref $choice eq 'ARRAY';
52  
53 3 my ($p, @particles);
54 my $factory = $self->{ _FACTORY } = $config->{ FACTORY }
55 3 ▶ || $XML::Schema::FACTORY;
Condition33%1/3
$config->{'FACTORY'} || $XML::Schema::FACTORY
l!l&&r!l&&!r
030
MC/DC0%0/2
$config->{'FACTORY'} || $XML::Schema::FACTORY
$config->{'FACTORY'} $XML::Schema::FACTORY
Truth table33%1/3
A || B
A= $config->{'FACTORY'}B= $XML::Schema::FACTORY
ABresult
1X1
011
000
56  
57 3 foreach $p (@$choice) {
58 6 my $particle = $factory->create( particle => $p );
59 6 ▶ unless (defined $particle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $particle) 1 5 6
60 1 return $self->error("error in choice item ", scalar @particles,
61 ': ', $factory->error());
62 }
63
64 5 push(@particles, $particle);
65 }
66 2 $self->{ particles } = \@particles;
67 2 $self->{ type } = 'choice';
68  
69 2 ▶ $self->constrain($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->constrain($config) or return 2 0 2
70 || return;
71  
72 2 return $self;
73 }
74  
75  
76 1 ▶sub particles {
Subroutine particles: called CC=2 SCAR=8.1
Pod undocumented
77 1 my $self = shift;
78 return $self->{ particles }
79 1 ▶ || $self->error("empty particle choice");
Condition33%1/3
$self->{'particles'} || $self->error('empty particle choice')
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'particles'} || $self->error('empty particle choice')
$self->{'particles'} $self->error('empty particle choice')
Truth table33%1/3
A || B
A= $self->{'particles'}B= $self->error('empty particle choice')
ABresult
1X1
011
000
80 }
81  
82  
83 1 ▶sub start {
Subroutine start: called CC=2 SCAR=7.0
Pod undocumented
84 1 my $self = shift;
85  
86 1 ▶ $self->TRACE() if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Choice::DEBUG 0 1 1
87  
88 1 $self->{ occurs } = 0;
89 1 $self->{ _pnow } = undef;
90  
91 1 return 1;
92 }
93  
94  
95 #------------------------------------------------------------------------
96 # element($name)
97 #
98 # Iterates through the list of particles to find one which can accept
99 # a <$name> element. Each particle is started via a call to start()
100 # (e.g. to initialise a sequence particle) and then its element($name)
101 # method is called. If it returns a true value (an element ref) then
102 # that particle is latched in as the current target particle (_pnow)
103 # and will be given first refusal on subsequent element() calls. If
104 # the particle does not accept the element() call then its end()
105 # method is called and the process continues onto the next particle in
106 # the choice.
107 #------------------------------------------------------------------------
108  
109 1 ▶sub element {
Subroutine element: called CC=11 SCAR=34.0
Pod undocumented
110 1 my ($self, $name) = @_;
111 1 my $particles = $self->{ particles };
112 1 my $pnow = $self->{ _pnow };
113 1 my $element;
114  
115 1 ▶ $self->TRACE("name => ", $name) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Choice::DEBUG 0 1 1
116  
117 # if there is an active particle (i.e. one previously selected by
118 # this element() method) then we first give it the opportunity to
119 # handle this new element.
120  
121 1 ▶ if ($pnow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pnow) 0 1 1
122 # true value returned indicates success
123 0 ▶ return $element
Branch: 1 branch
BranchTrueFalseTotal
if $element = $pnow->element($name) 0 0 0
124 if ($element = $pnow->element($name));
125  
126 # undefined value returned indicates error
127 0 ▶ return $self->error($pnow->error())
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 0 0 0
128 unless defined $element;
129  
130 # defined but false value (0) indicates particle
131 # declined to accept element but was otherwise
132 # satisfied according to min/max constraints so
133 # we move on to try the next particle
134 0 $self->TRACE("ending $pnow because it declined");
135 0 ▶ $pnow->end()
Branch: 1 branch
BranchTrueFalseTotal
$pnow->end or return $self->error($pnow->error) 0 0 0
136 || return $self->error($pnow->error());
137  
138 0 my $occurs = ++$self->{ occurs };
139  
140 # if we've reached our max occurences then we must decline
141 return $self->decline("unexpected <$name> element")
142 0 ▶ if $occurs >= $self->{ max };
Branch: 1 branch
BranchTrueFalseTotal
if $occurs >= $self->{"max"} 0 0 0
143 }
144  
145 # iterate through each particle to see if any can accept it
146 1 foreach $pnow (@$particles) {
147 2 ▶ $pnow->start()
Branch: 1 branch
BranchTrueFalseTotal
$pnow->start or return $self->error($pnow->error) 2 0 2
148 || return $self->error($pnow->error());
149  
150 2 ▶ if ($element = $pnow->element($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element = $pnow->element($name)) 1 1 2
151 # save reference to active particle for next time
152 1 $self->{ _pnow } = $pnow;
153 1 return $element;
154 }
155  
156 # ignore errors that are likely to be "min <whatever> expected"
157 1 $pnow->end();
158 }
159  
160 # didn't find anything to handle this element so we return an
161 # error or decline depending on us having any minimum occurence
162 # requirements
163 return $self->{ occurs } >= $self->{ min }
164 0 ▶ ? $self->decline("unexpected <$name> element")
Branch: 1 branch
BranchTrueFalseTotal
$self->{'occurs'} >= $self->{'min'} ? : 0 0 0
165 : $self->error("unexpected <$name> element");
166 }
167  
168  
169  
170 #------------------------------------------------------------------------
171 # end()
172 #
173 # If we've got an active particle on the go then we call its end() method
174 # to give it a chance to perform its own sanity check. Then we go on
175 # to inspect our own min/max constraints and return an appropriate
176 # true (ok) or false (not ok) value.
177 #------------------------------------------------------------------------
178  
179 0 ▶sub end {
Subroutine end: not called CC=6 SCAR=37.4
Pod undocumented
180 0 my $self = shift;
181 0 my $pnow = $self->{ _pnow };
182  
183 0 ▶ $self->TRACE if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Choice::DEBUG 0 0 0
184  
185 # if there is an active particle (i.e. one previously selected by
186 # this element() method) then we must end() it and make sure it is
187 # a happy particle
188 0 ▶ if ($pnow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pnow) 0 0 0
189 0 ▶ $pnow->end()
Branch: 1 branch
BranchTrueFalseTotal
$pnow->end or return $self->error($pnow->error) 0 0 0
190 || return $self->error($pnow->error());
191  
192 # chalk up another one
193 0 ++$self->{ occurs };
194 }
195  
196 # make sure that we're a happy particle
197 0 my ($min, $max, $occurs) = @$self{ qw( min max occurs ) };
198  
199 0 ▶ return $self->error("minimum of $min choice expected")
Branch: 1 branch
BranchTrueFalseTotal
if $occurs < $min 0 0 0
200 if $occurs < $min;
201  
202 0 ▶ return $self->error("maximum of $max choice exceeded")
Branch: 1 branch
BranchTrueFalseTotal
if $occurs > $max 0 0 0
203 if $occurs > $max;
204  
205 0 return 1;
206 }
207  
208
209 1 1;
210  
211  
212  
213  
214  
215  
« lib/XML/Schema/Attribute/Group.pm ↑ Summary lib/XML/Schema/Scope.pm »