lib/XML/Schema/Attribute.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 84.6% 44 / 52 Conditioncond 69.2% 9 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 13 / 13 Podpod 14.2% 1 / 7 totaltotal 86.7% 151 / 174 CC 31 SCAR 20.2
CC
31
Cov
91%
instance
12.3
init
11.4
default
4.0
CRAP 7.6
« lib/XML/Schema/Particle.pm ↑ Summary lib/XML/Schema/Type/Complex.pm »
1 #============================================================= -*-perl-*-
2 #
3 # XML::Schema::Attribute.pm
4 #
5 # DESCRIPTION
6 # Module implementing a base class for XML Schema attributes.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@kfs.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2001 Canon Research Centre Europe Ltd.
13 # All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 # REVISION
19 # $Id: Attribute.pm,v 1.3 2001/12/20 13:26:27 abw Exp $
20 #
21 #========================================================================
22  
23 package XML::Schema::Attribute;
24  
25 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 11 ▶use XML::Schema::Scoped;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶use XML::Schema::Scheduler;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use XML::Schema::Constants qw( :attribs );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 11 ▶use base qw( XML::Schema::Scoped XML::Schema::Scheduler );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶use vars qw( $VERSION $DEBUG $ERROR @MANDATORY @OPTIONAL @SCHEDULES );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 11 $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
35 11 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::Schema::Attribute::DEBUG 11 0 11
36 11 $ERROR = '';
37  
38 # mandatory 'type' implied by XML::Schema::Scoped base class
39 11 @MANDATORY = qw( name );
40 # optional 'scope' implied by XML::Schema::Scoped base class
41 11 @OPTIONAL = qw( namespace annotation );
42 11 @SCHEDULES = qw( instance );
43  
44  
45 #------------------------------------------------------------------------
46 # build regexen to match valid constraints values
47 #------------------------------------------------------------------------
48  
49 11 my @constraints = ( FIXED, DEFAULT );
50 11 my $constraints_regex = join('|', @constraints);
51 11 $constraints_regex = qr/^$constraints_regex$/;
52  
53  
54  
55 #------------------------------------------------------------------------
56 # init()
57 #
58 # Initiliasation method called by base class new() constructor.
59 #------------------------------------------------------------------------
60  
61 92 ▶sub init {
Subroutine init: called CC=11 SCAR=24.4
Pod documented
62 92 my ($self, $config) = @_;
63 92 my ($value);
64  
65 # call base class (XML::Schema::Scoped) initialiser
66 92 ▶ $self->SUPER::init($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init($config) or return 87 5 92
67 || return;
68  
69 # call XML::Schema::Scheduler initialiser
70 87 ▶ $self->init_scheduler($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->init_scheduler($config) or return 87 0 87
71 || return;
72  
73 # set value constraint specified as any of the config
74 # options: fixed, default or constraint
75 87 $self->{ constraint } = [ ];
76  
77 # it easy to forget if it's 'constrain' or 'constraint'
78 87 ▶ $self->{ constraint } ||= $self->{ constrain };
Condition33%1/3
$self->{"constraint"} ||= $self->{"constrain"}
l!l&&r!l&&!r
8700
MC/DC0%0/2
$self->{"constraint"} ||= $self->{"constrain"}
$self->{"constraint"} $self->{"constrain"}
Truth table33%1/3
A ||= B
A= $self->{"constraint"}B= $self->{"constrain"}
ABresult
1X1
011
000
79  
80 87 ▶ if (defined ($value = $config->{ fixed })) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined($value = $config->{'fixed'})) { } 2 85 87
elsif (defined($value = $config->{'default'})) { } 3 82 85
elsif (defined($value = $config->{'constraint'})) { } 3 79 82
81 2 ▶ $self->fixed($value) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->fixed($value) or return 2 0 2
82 }
83 elsif (defined ($value = $config->{ default })) {
84 3 ▶ $self->default($value) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->default($value) or return 3 0 3
85 }
86 elsif (defined ($value = $config->{ constraint })) {
87 3 ▶ return $self->error('constraint value must be an array ref')
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($value, "ARRAY") 1 2 3
88 unless UNIVERSAL::isa($value, 'ARRAY');
89 2 ▶ $self->constraint(@$value) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->constraint(@$value) or return 1 1 2
90 }
91  
92 85 return $self;
93 }
94  
95  
96 #------------------------------------------------------------------------
97 # name()
98 #
99 # Simple accessor method to return name value.
100 #------------------------------------------------------------------------
101  
102 21 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
103 21 my $self = shift;
104 21 return $self->{ name };
105 }
106  
107  
108 #------------------------------------------------------------------------
109 # namespace( $namespace )
110 #
111 # Simple accessor method to return existing namespace value or set new
112 # namespace when called with an argument.
113 #------------------------------------------------------------------------
114  
115 3 ▶sub namespace {
Subroutine namespace: called CC=2 SCAR=6.9
Pod undocumented
116 3 my $self = shift;
117 3 ▶ return @_ ? ($self->{ namespace } = shift) : $self->{ namespace };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 2 3
118 }
119  
120  
121 #------------------------------------------------------------------------
122 # constrain( default => 'some_value' ) # set default constraint
123 # constrain('default') # fetch default constraint
124 # constrain( fixed => 'other_value' ) # set fixed value constraint
125 # constrain('fixed') # fetch fixed value constraint
126 # ($type, $value) = constraint() # fetch current type/value
127 #
128 # Fetch or store a value constraint as a pair of ($type, $value) where
129 # type must be one of 'fixed' or 'default'.
130 #------------------------------------------------------------------------
131  
132 11 *constraint = \&constrain; # use typos; :-)
133  
134 17 ▶sub constrain {
Subroutine constrain: called CC=3 SCAR=11.0
Pod undocumented
135 17 my $self = shift;
136  
137 17 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 14 3 17
138 14 my $type = lc shift;
139 14 ▶ return $self->error_value('constraint type', $type, @constraints)
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /$constraints_regex/ 2 12 14
140 unless $type =~ $constraints_regex;
141 12 $self->$type(@_);
142 }
143 else {
144 3 return @{ $self->{ constraint } };
145 }
146 }
147  
148  
149 #------------------------------------------------------------------------
150 # default()
151 # default($value)
152 #
153 # Get/set default value constraint.
154 #------------------------------------------------------------------------
155  
156 14 ▶sub default {
Subroutine default: called CC=4 SCAR=13.9
Pod undocumented
157 14 my $self = shift;
158  
159 14 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 5 9 14
elsif ($self->{'constraint'}[0] eq 'default') { } 6 3 9
160 5 my $value = shift;
161 5 ▶ return $self->error('no default value specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 5 5
162 unless defined $value;
163 5 $self->{ constraint } = [ default => $value ];
164 }
165 elsif ($self->{ constraint }->[0] eq DEFAULT) {
166 6 return $self->{ constraint }->[1];
167 }
168 else {
169 3 return $self->error('attribute does not define a default value');
170 }
171 }
172  
173  
174 #------------------------------------------------------------------------
175 # fixed()
176 # fixed($value)
177 #
178 # Get/set fixed value constraint.
179 #------------------------------------------------------------------------
180  
181 28 ▶sub fixed {
Subroutine fixed: called CC=4 SCAR=13.9
Pod undocumented
182 28 my $self = shift;
183  
184 28 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 10 18 28
elsif ($self->{'constraint'}[0] eq 'fixed') { } 16 2 18
185 10 my $value = shift;
186 10 ▶ return $self->error('no fixed value specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 10 10
187 unless defined $value;
188 10 $self->{ constraint } = [ fixed => $value ];
189 }
190 elsif ($self->{ constraint }->[0] eq FIXED) {
191 16 return $self->{ constraint }->[1];
192 }
193 else {
194 2 return $self->error('attribute does not define a fixed value');
195 }
196 }
197  
198  
199  
200 #------------------------------------------------------------------------
201 # instance($value)
202 #------------------------------------------------------------------------
203  
204 104 ▶sub instance {
Subroutine instance: called CC=12 SCAR=25.1
Pod undocumented
205 104 my ($self, $value, $xml_instance) = @_;
206 104 my $constraint = $self->{ constraint };
207 104 my $result;
208  
209 # fetch type object via local scope
210 104 ▶ my $type = $self->type()
Condition100%2/2
$self->type || (return)
l!l
1031
MC/DC100%1/1
$self->type || (return)
$self->type
Truth table100%2/2
A
A= $self->type
Aresult
11
00
211 || return;
212  
213 # accept DEFAULT or FIXED value if none was provided
214 103 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 20 83 103
215 20 ▶ if ($constraint->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint->[0]) { } 6 14 20
216 6 $value = $constraint->[1];
217 }
218 else {
219 # NOTE: it's important not to change this error message
220 # as the parent attribute group calling it looks for it
221 14 return $self->error('no value provided');
222 }
223 }
224  
225 # instantiate the type
226 89 ▶ my $infoset = $type->instance($value, $xml_instance)
Condition100%2/2
$type->instance($value, $xml_instance) || (return $self->error($type->error))
l!l
854
MC/DC100%1/1
$type->instance($value, $xml_instance) || (return $self->error($type->error))
$type->instance($value, $xml_instance)
Truth table100%2/2
A
A= $type->instance($value, $xml_instance)
Aresult
11
00
227 || return $self->error( $type->error() );
228  
229 # check any FIXED constraint against post-validation (but pre-activation) result
230 85 ▶ if (@$constraint && $constraint->[0] eq FIXED) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$constraint and $constraint->[0] eq "fixed") 2 83 85
Condition100%3/3
@$constraint and $constraint->[0] eq "fixed"
!ll&&!rl&&r
7582
MC/DC100%2/2
@$constraint and $constraint->[0] eq "fixed"
@$constraint $constraint->[0] eq "fixed"
Truth table100%3/3
A and B
A= @$constraintB= $constraint->[0] eq "fixed"
ABresult
0X0
100
111
231 return $self->error("value does not match FIXED value of ", $constraint->[1])
232 2 ▶ unless $infoset->{ value } eq $constraint->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $infoset->{"value"} eq $constraint->[1] 1 1 2
233 }
234  
235 # TODO: what about ID and IDREF?
236 0 $self->DEBUG("attribute magic: @{ $infoset->{ magic } }\n")
237 84 ▶ if $DEBUG && $infoset->{ magic };
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Attribute::DEBUG and $infoset->{"magic"} 0 84 84
Condition33%1/3
$XML::Schema::Attribute::DEBUG and $infoset->{"magic"}
!ll&&!rl&&r
8400
MC/DC0%0/2
$XML::Schema::Attribute::DEBUG and $infoset->{"magic"}
$XML::Schema::Attribute::DEBUG $infoset->{"magic"}
Truth table33%1/3
A and B
A= $XML::Schema::Attribute::DEBUGB= $infoset->{"magic"}
ABresult
0X0
100
111
238  
239 84 ▶ $self->activate_instance($infoset)
Branch: 1 branch
BranchTrueFalseTotal
$self->activate_instance($infoset) or return 84 0 84
240 || return;
241 # || return if @{ $self->{ _SCHEDULE_instance } };
242  
243  
244 return wantarray ? @$infoset{ qw( result magic ) }
245 84 ▶ : $infoset->{ result };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 80 4 84
246
247 }
248  
249  
250  
251 11 1;
252  
253 __END__
« lib/XML/Schema/Particle.pm ↑ Summary lib/XML/Schema/Type/Complex.pm »