| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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'))
MC/DC0%0/1
$config->{'sequence'} || (return $self->error('no sequence defined'))
$config->{'sequence'}
Truth table50%1/2
A
A= $config->{'sequence'}
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 3 | ▶ | return $self->error("sequence expects an array ref") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 3 | foreach $p (@$sequence) { | |||||||||||||||||||
| 62 | 6 | my $particle = $factory->create( particle => $p ); | |||||||||||||||||||
| 63 | 6 | ▶ | unless (defined $particle) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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'))
MC/DC0%0/1
$self->{'particles'} || (return $self->decline('empty particle sequence'))
$self->{'particles'}
Truth table0%0/2
A
A= $self->{'particles'}
| |||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | my $pset = $self->{ _PSET }; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | ▶ | if (@$pset) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | my $pnow = shift @$pset; | |||||||||||||||||||
| 124 | 0 | ▶ | while ($pnow) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 0 | $self->TRACE("clearing ", $pnow->ID); | |||||||||||||||||||
| 126 | 0 | ▶ | $pnow->end() | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | || return $self->error($pnow->error()); | ||||||||||||||||||||
| 128 | 0 | ▶ | if ($pnow = shift(@$pset)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | ▶ | $pnow->start() | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 137 | $min > 1 ? 's' : '', " expected") | ||||||||||||||||||||
| 138 | if $occurs < $min; | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | ▶ | return $self->error("maximum of $max $name element", | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | while ($pnow) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | # true value returned indicates success | ||||||||||||||||||||
| 161 | 0 | ▶ | return $element | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | if ($element = $pnow->element($name)); | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # undefined value returned indicates error | ||||||||||||||||||||
| 165 | 0 | ▶ | unless (defined $element) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | $self->TRACE('DECLINED'); | |||||||||||||||||||
| 167 | 0 | ▶ | return $satisfied ? $self->decline("unexpected <$name> element") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 177 | || return $self->error($pnow->error()); | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 0 | shift(@$pset); | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | ▶ | if (@$pset) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | 0 | $pnow = $pset->[0]; | |||||||||||||||||||
| 183 | 0 | ▶ | $pnow->start() || return $self->error($pnow->error()); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | # otherwise restart the sequence | ||||||||||||||||||||
| 197 | 0 | ▶ | return unless $self->_start_sequence(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | 0 | $pset = $self->{ _PSET }; | |||||||||||||||||||
| 199 | 0 | ▶ | $pnow = @$pset ? $pset->[0] : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 0 | $restarted++; | |||||||||||||||||||
| 202 | 0 | ▶ | $satisfied++ if $occurs >= $min; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 0 | ▶ | return $satisfied ? $self->decline("unexpected <$name> element") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | : $self->error("unexpected <$name> element"); | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 2 | 1; | |||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | |||||||||||||||||||||