lib/XML/Schema/Particle/Sequence.pm

filter: Statementstmt 36.9% 31 / 84 Branchbran 9.3% 6 / 64 Conditioncond 23.0% 3 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.5% 5 / 9 Podpod 20.0% 1 / 5 totaltotal 25.1% 46 / 183 CC 38 SCAR 48.7
CC
38
Cov
25%
element
272.0
end
132.0
_start_sequence
20.0
CRAP 129.8
↑ Summary lib/XML/Schema/Handler/Complex.pm »
1 #============================================================= -*-perl-*-
2 #
3 # XML::Schema::Particle::Sequence.pm
4 #
5 # DESCRIPTION
6 # Subclassed particle to contain a sequence of other particles
7 # which should be matched in 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: Sequence.pm,v 1.1.1.1 2001/08/29 14:30:17 abw Exp $
21 #
22 #========================================================================
23  
24 package XML::Schema::Particle::Sequence;
25  
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use base qw( XML::Schema::Particle );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use vars qw( $VERSION $DEBUG $ERROR $ETYPE );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 $VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/);
31 2 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::Schema::Particle::Sequence::DEBUG 2 0 2
32 2 $ERROR = '';
33 2 $ETYPE = 'SequenceParticle';
34  
35 #*DEBUG = \$XML::Schema::Particle::DEBUG;
36 #*ERROR = \$XML::Schema::Particle::ERROR;
37 #*DECLINED = \&XML::Schema::Particle::DECLINED;
38  
39  
40 #------------------------------------------------------------------------
41 # init()
42 #
43 # Called automatically by base class new() method.
44 #------------------------------------------------------------------------
45  
46 3 ▶sub init {
Subroutine init: called CC=8 SCAR=21.6
Pod documented
47 3 my ($self, $config) = @_;
48  
49 3 ▶ $self->TRACE("config => ", $config) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Sequence::DEBUG 0 3 3
50  
51 my $sequence = $config->{ sequence }
52 3 ▶ || return $self->error("no sequence defined");
Condition50%1/2
$config->{'sequence'} || (return $self->error('no sequence defined'))
l!l
30
MC/DC0%0/1
$config->{'sequence'} || (return $self->error('no sequence defined'))
$config->{'sequence'}
Truth table50%1/2
A
A= $config->{'sequence'}
Aresult
11
00
53  
54 3 ▶ return $self->error("sequence expects an array ref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sequence eq "ARRAY" 0 3 3
55 unless ref $sequence eq 'ARRAY';
56  
57 3 my ($p, @particles);
58 my $factory = $self->{ _FACTORY } = $config->{ FACTORY }
59 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
60  
61 3 foreach $p (@$sequence) {
62 6 my $particle = $factory->create( particle => $p );
63 6 ▶ unless (defined $particle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $particle) 1 5 6
64 1 return $self->error("error in sequence item ", scalar @particles,
65 ': ', $factory->error());
66 }
67
68 5 push(@particles, $particle);
69 }
70 2 $self->{ particles } = \@particles;
71 2 $self->{ type } = 'sequence';
72  
73 2 ▶ $self->constrain($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->constrain($config) or return 2 0 2
74 || return;
75  
76 2 return $self;
77 }
78  
79  
80 1 ▶sub particles {
Subroutine particles: called CC=2 SCAR=8.1
Pod undocumented
81 1 my $self = shift;
82 return $self->{ particles }
83 1 ▶ || $self->error("empty particle sequence");
Condition33%1/3
$self->{'particles'} || $self->error('empty particle sequence')
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'particles'} || $self->error('empty particle sequence')
$self->{'particles'} $self->error('empty particle sequence')
Truth table33%1/3
A || B
A= $self->{'particles'}B= $self->error('empty particle sequence')
ABresult
1X1
011
000
84 }
85  
86  
87 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my $self = shift;
89  
90 0 ▶ $self->TRACE() if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Sequence::DEBUG 0 0 0
91  
92 0 $self->{ occurs } = 0;
93 0 $self->_start_sequence();
94 }
95  
96  
97 0 ▶sub _start_sequence {
Subroutine _start_sequence: not called CC=4 SCAR=30.0
98 0 my $self = shift;
99  
100 0 ▶ $self->TRACE() if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Sequence::DEBUG 0 0 0
101  
102 my $particles = $self->{ particles }
103 0 ▶ || return $self->decline("empty particle sequence");
Condition0%0/2
$self->{'particles'} || (return $self->decline('empty particle sequence'))
l!l
00
MC/DC0%0/1
$self->{'particles'} || (return $self->decline('empty particle sequence'))
$self->{'particles'}
Truth table0%0/2
A
A= $self->{'particles'}
Aresult
11
00
104  
105 0 $self->{ _PSET } = [ @$particles ];
106  
107 0 my $first = $particles->[0];
108  
109 0 ▶ return $first->start()
Condition0%0/3
$first->start || $self->error($first->error)
l!l&&r!l&&!r
000
MC/DC0%0/2
$first->start || $self->error($first->error)
$first->start $self->error($first->error)
Truth table0%0/3
A || B
A= $first->startB= $self->error($first->error)
ABresult
1X1
011
000
110 || $self->error($first->error());
111 }
112  
113  
114 0 ▶sub end {
Subroutine end: not called CC=11 SCAR=48.8
Pod undocumented
115 0 my $self = shift;
116 0 my ($min, $max, $occurs, $name) = @$self{ qw( min max occurs name ) };
117  
118 0 ▶ $self->TRACE() if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Sequence::DEBUG 0 0 0
119  
120 0 my $pset = $self->{ _PSET };
121  
122 0 ▶ if (@$pset) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pset) 0 0 0
123 0 my $pnow = shift @$pset;
124 0 ▶ while ($pnow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pnow) 0 0 0
125 0 $self->TRACE("clearing ", $pnow->ID);
126 0 ▶ $pnow->end()
Branch: 1 branch
BranchTrueFalseTotal
$pnow->end or return $self->error($pnow->error) 0 0 0
127 || return $self->error($pnow->error());
128 0 ▶ if ($pnow = shift(@$pset)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pnow = shift @$pset) 0 0 0
129 0 ▶ $pnow->start()
Branch: 1 branch
BranchTrueFalseTotal
$pnow->start or return $self->error($pnow->error) 0 0 0
130 || return $self->error($pnow->error());
131 }
132 }
133 0 $occurs++;
134 }
135  
136 0 ▶ return $self->error("minimum of $min $name element",
Branch: 2 branches
BranchTrueFalseTotal
if $occurs < $min 0 0 0
$min > 1 ? : 0 0 0
137 $min > 1 ? 's' : '', " expected")
138 if $occurs < $min;
139  
140 0 ▶ return $self->error("maximum of $max $name element",
Branch: 2 branches
BranchTrueFalseTotal
if $occurs > $max 0 0 0
$max > 1 ? : 0 0 0
141 $max > 1 ? 's' : '', " exceeded")
142 if $occurs > $max;
143  
144 0 return 1;
145 }
146  
147  
148 0 ▶sub element {
Subroutine element: not called CC=16 SCAR=56.1
Pod undocumented
149 0 my ($self, $name) = @_;
150 0 my $pset = $self->{ _PSET };
151 0 ▶ my $pnow = @$pset ? $pset->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$pset ? : 0 0 0
152 0 my ($min, $max) = @$self{ qw( min max ) };
153 0 my $element;
154 0 my $restarted = 0;
155 0 my $satisfied = 0;
156  
157 0 ▶ $self->TRACE("name => ", $name) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Particle::Sequence::DEBUG 0 0 0
158  
159 0 ▶ while ($pnow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pnow) 0 0 0
160 # true value returned indicates success
161 0 ▶ return $element
Branch: 1 branch
BranchTrueFalseTotal
if $element = $pnow->element($name) 0 0 0
162 if ($element = $pnow->element($name));
163  
164 # undefined value returned indicates error
165 0 ▶ unless (defined $element) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $element) 0 0 0
166 0 $self->TRACE('DECLINED');
167 0 ▶ return $satisfied ? $self->decline("unexpected <$name> element")
Branch: 1 branch
BranchTrueFalseTotal
$satisfied ? : 0 0 0
168 : $self->error($pnow->error())
169 }
170  
171 # defined but false value (0) indicates particle
172 # declined to accept element but was otherwise
173 # satisfied according to min/max constraints so
174 # we move on to try the next particle
175 0 $self->TRACE("ending $pnow because it declined");
176 0 ▶ $pnow->end()
Branch: 1 branch
BranchTrueFalseTotal
$pnow->end or return $self->error($pnow->error) 0 0 0
177 || return $self->error($pnow->error());
178  
179 0 shift(@$pset);
180  
181 0 ▶ if (@$pset) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pset) { } 0 0 0
182 0 $pnow = $pset->[0];
183 0 ▶ $pnow->start() || return $self->error($pnow->error());
Branch: 1 branch
BranchTrueFalseTotal
$pnow->start or return $self->error($pnow->error) 0 0 0
184 }
185 else {
186 # looks like we reached the end of the sequence...
187 0 my $occurs = ++$self->{ occurs };
188  
189 # if we've reached our max occurences then we must decline
190 0 ▶ return $self->decline("unexpected <$name> element (max. $max sequences reached)")
Branch: 1 branch
BranchTrueFalseTotal
if $occurs >= $max 0 0 0
191 if $occurs >= $max;
192  
193 # and if we've already restarted once then we shouldn't again
194 0 ▶ last if $restarted;
Branch: 1 branch
BranchTrueFalseTotal
if $restarted 0 0 0
195  
196 # otherwise restart the sequence
197 0 ▶ return unless $self->_start_sequence();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_start_sequence 0 0 0
198 0 $pset = $self->{ _PSET };
199 0 ▶ $pnow = @$pset ? $pset->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$pset ? : 0 0 0
200  
201 0 $restarted++;
202 0 ▶ $satisfied++ if $occurs >= $min;
Branch: 1 branch
BranchTrueFalseTotal
if $occurs >= $min 0 0 0
203 }
204 }
205  
206 0 ▶ return $satisfied ? $self->decline("unexpected <$name> element")
Branch: 1 branch
BranchTrueFalseTotal
$satisfied ? : 0 0 0
207 : $self->error("unexpected <$name> element");
208  
209 }
210  
211  
212
213 2 1;
214  
215  
216  
217  
218  
219  
↑ Summary lib/XML/Schema/Handler/Complex.pm »