| 1 | #============================================================= -*-perl-*- | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # XML::Schema::Particle.pm | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||
| 6 | # A particle is an element within a content model optionally | ||||||||||||||||||||
| 7 | # specified with minOccurs and maxOccurs constraints. | ||||||||||||||||||||
| 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: Particle.pm,v 1.1.1.1 2001/08/29 14:30:17 abw Exp $ | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | #======================================================================== | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | package XML::Schema::Particle; | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 10 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 10 | ▶ | use XML::Schema; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 10 | ▶ | use base qw( XML::Schema::Base ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 10 | ▶ | use vars qw( $VERSION $DEBUG $ERROR $ETYPE @MODELS ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 10 | $VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||
| 32 | 10 | ▶ | $DEBUG = 0 unless defined $DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 10 | $ERROR = ''; | |||||||||||||||||||
| 34 | 10 | $ETYPE = 'particle'; | |||||||||||||||||||
| 35 | 10 | @MODELS = qw( element sequence choice model ); | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 10 | ▶ | use constant DECLINED => 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # alias min() to minOccurs() and max() to maxOccurs() | ||||||||||||||||||||
| 41 | 10 | *minOccurs = \&min; | |||||||||||||||||||
| 42 | 10 | *maxOccurs = \&max; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 46 | # init() | ||||||||||||||||||||
| 47 | # | ||||||||||||||||||||
| 48 | # Called automatically by base class new() method. | ||||||||||||||||||||
| 49 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 28 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=9 SCAR=29.5
Pod
documented
| |||||||||||||||||||||
| 52 | 28 | my ($self, $config) = @_; | |||||||||||||||||||
| 53 | 28 | ▶ | my $factory = $self->{ FACTORY } = $config->{ FACTORY } || $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
| |||||||||||||||||||||
| 54 | 28 | my ($content, $model, $modtype); | |||||||||||||||||||
| 55 | 28 | my $name = shift; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # allow an element, sequence, choice or model object to be defined | ||||||||||||||||||||
| 58 | # as the 'content' item, copied to the appropriate entry in the | ||||||||||||||||||||
| 59 | # $config hash | ||||||||||||||||||||
| 60 | 28 | ▶ | if ($content = $config->{ content }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | my $found = 0; | |||||||||||||||||||
| 62 | 0 | foreach $modtype (@MODELS) { | |||||||||||||||||||
| 63 | 0 | ▶ | if ($factory->isa( $modtype => $content )) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | $config->{ $modtype } = $content; | |||||||||||||||||||
| 65 | 0 | $found++; | |||||||||||||||||||
| 66 | 0 | last; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | 0 | ▶ | return $self->error("cannot determine content type for [$content]") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | unless $found; | ||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | # now look for an element, sequence, choice or model either | ||||||||||||||||||||
| 74 | # provided directly or copied from the 'content' item above | ||||||||||||||||||||
| 75 | 28 | foreach $modtype (@MODELS) { | |||||||||||||||||||
| 76 | 55 | ▶ | if ($model = $config->{ $modtype }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 22 | ▶ | return $factory->adopt( "${modtype}_particle" => $self, $config ) | ||||||||||||||||||
|
Condition66%2/3
$factory->adopt("${modtype}_particle", $self, $config) || $self->error($factory->error)
MC/DC50%1/2
$factory->adopt("${modtype}_particle", $self, $config) || $self->error($factory->error)
$factory->adopt("${modtype}_particle", $self, $config)
$self->error($factory->error)
Truth table66%2/3
A || B
A= $factory->adopt("${modtype}_particle", $self, $config)B= $self->error($factory->error)
| |||||||||||||||||||||
| 78 | || $self->error($factory->error()); | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | 6 | return $self->error("particle expects one of: ", join(', ', @MODELS)); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 24 | ▶ | sub constrain { | ||||||||||||||||||
|
Subroutine
constrain: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||
| 86 | 24 | my ($self, $config) = @_; | |||||||||||||||||||
| 87 | 24 | my ($min, $max) = @$config{ qw( minOccurs maxOccurs ) }; | |||||||||||||||||||
| 88 | 24 | ▶ | $min = $config->{ min } unless defined $min; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 24 | ▶ | $max = $config->{ max } unless defined $max; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 24 | ▶ | $min = 1 unless defined $min; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 24 | ▶ | $max = 1 unless defined $max; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 24 | ▶ | return $self->error("maxOccurs ($max) is less than minOccurs ($min)") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | if $max < $min; | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 23 | @$self{ qw( min max occurs ) } = ($min, $max, 0); | |||||||||||||||||||
| 97 | 23 | ▶ | $self->TRACE("min => $min, max => $max") if $DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 23 | return $self; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 2 | ▶ | sub type { | ||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 104 | 2 | return $_[0]->{ type }; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 27 | ▶ | sub models { | ||||||||||||||||||
|
Subroutine
models: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 109 | 27 | return @MODELS; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 12 | ▶ | sub min { | ||||||||||||||||||
|
Subroutine
min: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 114 | 12 | my $self = shift; | |||||||||||||||||||
| 115 | 12 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 1 | my $newmin = shift; | |||||||||||||||||||
| 117 | return $self->error("maxOccurs ($self->{ max }) is less than minOccurs ($newmin)") | ||||||||||||||||||||
| 118 | 1 | ▶ | if $self->{ max } < $newmin; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 1 | return ($self->{ min } = $newmin); | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | 11 | return $self->{ min }; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 13 | ▶ | sub max { | ||||||||||||||||||
|
Subroutine
max: called CC=3 SCAR=16.0
Pod
undocumented
| |||||||||||||||||||||
| 126 | 13 | my $self = shift; | |||||||||||||||||||
| 127 | 13 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 0 | my $newmax = shift; | |||||||||||||||||||
| 129 | return $self->error("maxOccurs ($newmax) is less than minOccurs ($self->{ min })") | ||||||||||||||||||||
| 130 | 0 | ▶ | if $newmax < $self->{ min }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 0 | return ($self->{ max } = $newmax); | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 13 | return $self->{ max }; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 2 | ▶ | sub occurs { | ||||||||||||||||||
|
Subroutine
occurs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 138 | 2 | return $_[0]->{ occurs }; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 7 | ▶ | sub start { | ||||||||||||||||||
|
Subroutine
start: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 143 | 7 | my $self = shift; | |||||||||||||||||||
| 144 | 7 | ▶ | $self->TRACE() if $DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 7 | $self->{ occurs } = 0; | |||||||||||||||||||
| 146 | 7 | return 1; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | ▶ | sub element { | ||||||||||||||||||
|
Subroutine
element: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 151 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 152 | 0 | return $self->error("element <$name> called in base class"); | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 3 | ▶ | sub decline { | ||||||||||||||||||
|
Subroutine
decline: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 157 | 3 | my $self = shift; | |||||||||||||||||||
| 158 | 3 | $self->error(@_); | |||||||||||||||||||
| 159 | 3 | ▶ | $self->TRACE() if $DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | 3 | return DECLINED; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 3 | ▶ | sub end { | ||||||||||||||||||
|
Subroutine
end: called CC=6 SCAR=22.8
Pod
undocumented
| |||||||||||||||||||||
| 164 | 3 | my $self = shift; | |||||||||||||||||||
| 165 | my ($min, $max, $occurs, $name ) | ||||||||||||||||||||
| 166 | 3 | = @$self{ qw( min max occurs name ) }; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 3 | ▶ | $self->TRACE() if $DEBUG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 3 | $self->{ _ERROR } = ''; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 3 | ▶ | return $self->error("minimum of $min <$name> element", | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 173 | $min > 1 ? 's' : '', " expected") | ||||||||||||||||||||
| 174 | if $occurs < $min; | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 0 | ▶ | return $self->error("maximum of $max <$name> element", | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 177 | $max > 1 ? 's' : '', " exceeded") | ||||||||||||||||||||
| 178 | if $occurs > $max; | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | return 1; | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 10 | 1; | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||