lib/XML/Schema/Type/Simple.pm

filter: Statementstmt 91.2% 114 / 125 Branchbran 75.8% 47 / 62 Conditioncond 71.4% 20 / 28 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 80.0% 20 / 25 Podpod 6.6% 1 / 15 totaltotal 76.6% 207 / 270 CC 42 SCAR 19.4
CC
42
Cov
84%
init
13.1
constrain
12.2
instance
6.3
CRAP 7.0
« lib/XML/Schema/Type/Complex.pm ↑ Summary lib/XML/Schema/Particle/Element.pm »
1 #============================================================= -*-perl-*-
2 #
3 # XML::Schema::Type::Simple
4 #
5 # DESCRIPTION
6 # Module implementing a base class for simple XML Schema datatypes.
7 # Simple types are those that cannot contain other elements and
8 # cannot carry attributes.
9 #
10 # AUTHOR
11 # Andy Wardley <abw@kfs.org>
12 #
13 # COPYRIGHT
14 # Copyright (C) 2001 Canon Research Centre Europe Ltd.
15 # All Rights Reserved.
16 #
17 # This module is free software; you can redistribute it and/or
18 # modify it under the same terms as Perl itself.
19 #
20 # TODO
21 # * constrain() should accept lists of constraints, e.g.
22 # constrain(minInclusive => 2, scale => 4);
23 #
24 # * Fix strategy wrt defining 'name' and/or 'type' attributes. 'name'
25 # should define name of type within schema (e.g. myMoneyType), and
26 # 'type' or 'base' should define base?
27 #
28 # REVISION
29 # $Id: Simple.pm,v 1.2 2001/12/20 13:26:28 abw Exp $
30 #
31 #========================================================================
32  
33 package XML::Schema::Type::Simple;
34  
35 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 28 ▶use XML::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 28 ▶use XML::Schema::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 28 ▶use XML::Schema::Type::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 28 ▶use XML::Schema::Type::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 28 ▶use XML::Schema::Type::Builtin;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 28 ▶use XML::Schema::Facet::Builtin;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 28 ▶use XML::Schema::Scheduler;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 28 ▶use base qw( XML::Schema::Type XML::Schema::Scheduler );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 28 ▶use vars qw( $VERSION $DEBUG $ERROR @OPTIONAL @SCHEDULES );
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 28 $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
48 28 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::Schema::Type::Simple::DEBUG 28 0 28
49 28 $ERROR = '';
50  
51 28 @OPTIONAL = qw( annotation );
52 28 @SCHEDULES = qw( instance );
53  
54  
55
56 #------------------------------------------------------------------------
57 # init()
58 #
59 # TODO: fundamentals(), merging user-supplied facets/actions into lists.
60 #------------------------------------------------------------------------
61  
62 210 ▶sub init {
Subroutine init: called CC=13 SCAR=25.8
Pod documented
63 210 my ($self, $config) = @_;
64 210 my ($base, $name, $value);
65 210 my $class = ref $self;
66  
67 # if called as base class constructor method then look for
68 # 'base' item in config and delegate to that constructor.
69 # e.g. XML::Schema::Type::Simple->new( base => 'string', ... );
70 # => XML::Schema::Type::string->new( ... );
71  
72 210 ▶ if ($class eq __PACKAGE__ && ($base = $config->{ base })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__ and $base = $config->{"base"}) 13 197 210
Condition100%3/3
$class eq __PACKAGE__ and $base = $config->{"base"}
!ll&&!rl&&r
196113
MC/DC100%2/2
$class eq __PACKAGE__ and $base = $config->{"base"}
$class eq __PACKAGE__ $base = $config->{"base"}
Truth table100%3/3
A and B
A= $class eq __PACKAGE__B= $base = $config->{"base"}
ABresult
0X0
100
111
73 13 ▶ if ($class = $self->builtin($base)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class = $self->builtin($base)) { } 12 1 13
74 12 ▶ $self->DEBUG("base package, delegating to $base ($class)\n")
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Type::Simple::DEBUG 0 12 12
75 if $DEBUG;
76 12 return $class->new($config);
77 }
78 else {
79 1 return $self->error("invalid base type: $base");
80 }
81 }
82  
83 my ($mand, $option, $facets)
84 197 = @{ $self->_baseargs( qw( @MANDATORY %OPTIONAL @FACETS ) ) };
85  
86 197 ▶ $self->_mandatory($mand, $config)
Branch: 1 branch
BranchTrueFalseTotal
if @$mand 6 191 197
Condition80%4/5
@$mand && ($self->_mandatory($mand, $config) || (return))
!ll
19160
$self->_mandatory($mand, $config) or return
l!l
42
MC/DC0%0/2
@$mand && ($self->_mandatory($mand, $config) || (return))
@$mand $self->_mandatory($mand, $config)
Truth table66%2/3
A && B
A= @$mandB= $self->_mandatory($mand, $config)
ABresult
0X0
100
111
87 || return if @$mand;
88  
89 195 ▶ $self->_optional($option, $config)
Branch: 1 branch
BranchTrueFalseTotal
$self->_optional($option, $config) or return 195 0 195
90 || return;
91  
92 # default name to last element of package name
93 195 ▶ $self->{ name } = $config->{ name } || $self->type();
Condition66%2/3
$config->{'name'} || $self->type
l!l&&r!l&&!r
91860
MC/DC0%0/2
$config->{'name'} || $self->type
$config->{'name'} $self->type
Truth table66%2/3
A || B
A= $config->{'name'}B= $self->type
ABresult
1X1
011
000
94  
95 195 $self->{ _VARIETY } = 'atomic';
96  
97 # install facets
98 195 $self->{ _FACET_LIST } = [ ];
99 195 $self->{ _FACET_HASH } = { };
100  
101 195 ▶ while (@$facets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$facets) 370 193 563
102 370 $name = shift(@$facets);
103 370 ▶ $value = ref $name ? undef : shift(@$facets);
Branch: 1 branch
BranchTrueFalseTotal
ref $name ? : 114 256 370
104 370 ▶ $self->constrain($name, $value)
Branch: 1 branch
BranchTrueFalseTotal
$self->constrain($name, $value) or return undef 368 2 370
105 || return undef;
106 }
107  
108 # need to know which facets were installed as inbuilt facets
109 # and which get added subsequently by user
110 193 $self->{ _FACET_ORIGIN } = @{ $self->{ _FACET_LIST } };
111  
112 # initialise scheduler
113 193 ▶ $self->init_scheduler($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->init_scheduler($config) or return undef 193 0 193
114 || return undef;
115  
116 193 return $self;
117 }
118  
119  
120 #------------------------------------------------------------------------
121 # type()
122 #
123 # Return a string giving the name of the type, e.g. 'string', 'date'.
124 # If called on the base class, 'anyType' is returned, otherwise the
125 # type name is taken as the last element in the class name, e.g.
126 # XML::Schema::Type::string => 'string'.
127 #------------------------------------------------------------------------
128  
129 190 ▶sub type {
Subroutine type: called CC=2 SCAR=6.9
Pod undocumented
130 190 my $self = shift;
131 190 my $class = ref $self;
132  
133 190 ▶ if ($class eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) { } 2 188 190
134 2 return 'anyType';
135 }
136 else {
137 188 $class =~ /::(\w+)$/;
138 188 return $1;
139 }
140 }
141  
142  
143 #------------------------------------------------------------------------
144 # builtin($type)
145 #
146 # Returns a class name against which new() can be called if the
147 # $type specified equates to a builtin type, e.g. string =>
148 # XML::Schema::Type::string, etc. Otherwise returns undef.
149 #------------------------------------------------------------------------
150  
151 110 ▶sub builtin {
Subroutine builtin: called CC=3 SCAR=11.0
Pod undocumented
152 110 my ($self, $type) = @_;
153 110 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
13970
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
154  
155 # strip 'Simple' last element of XML::Schema::Type::Simple and
156 # replace with "$type" to get XML::Schema::Type::$type
157 110 $class =~ s/::\w+$/::$type/;
158  
159 110 ▶ return UNIVERSAL::can($class, 'new') ? $class : undef;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($class, 'new') ? : 106 4 110
160 }
161  
162  
163 #------------------------------------------------------------------------
164 # constrain($facet, $value)
165 #
166 # Add a new validation facet to the internal list.
167 #------------------------------------------------------------------------
168  
169 381 ▶sub constrain {
Subroutine constrain: called CC=12 SCAR=25.0
Pod undocumented
170 381 my ($self, $name, $value) = @_;
171 381 my ($flist, $fhash) = @$self{ qw( _FACET_LIST _FACET_HASH ) };
172 381 my $facet;
173  
174 # ($name, $value) can be:
175 # 'name' => $facet_ref
176 # 'name' => $code_ref
177 # $facet_ref,
178 # $code_ref
179  
180 381 ▶ if (ref ($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) { } 114 267 381
181 114 $facet = $name;
182 114 $name = '';
183 }
184 else {
185 267 $facet = $value;
186 }
187  
188 381 ▶ if (ref $facet eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $facet eq 'CODE') { } 112 269 381
elsif (UNIVERSAL::isa($facet, 'XML::Schema::Facet')) { } 2 267 269
189 112 ▶ $self->TRACE("CODE facet") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Type::Simple::DEBUG 0 112 112
190 112 push(@$flist, $facet);
191 112 ▶ $fhash->{ $name } = $facet if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 112 112
192 112 return $facet; # return if facet is a coderef
193 }
194 elsif (UNIVERSAL::isa($facet, 'XML::Schema::Facet')) {
195 2 ▶ $name = $facet->name() unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 2 0 2
196 2 ▶ $self->TRACE("OBJECT facet") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Type::Simple::DEBUG 0 2 2
197 }
198 else {
199 267 my $pkg = "XML::Schema::Facet::$name";
200 267 ▶ $self->TRACE("NEW $pkg facet") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Type::Simple::DEBUG 2 265 267
201 267 ▶ $value = { value => $value } unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 160 107 267
202 267 ▶ $facet = $pkg->new($value)
Condition100%2/2
$pkg->new($value) || (return $self->error($pkg->error))
l!l
2652
MC/DC100%1/1
$pkg->new($value) || (return $self->error($pkg->error))
$pkg->new($value)
Truth table100%2/2
A
A= $pkg->new($value)
Aresult
11
00
203 || return $self->error($pkg->error());
204 }
205  
206 # at this point, we can assume $facet is a XML::Schema::Facet or
207 # subclass; we call its install method to let it inspect the
208 # existing facet list/table to check for conflicts
209 # NOTE: facets don't do this yet, but should eventualy
210  
211 267 ▶ $facet->install($flist, $fhash)
Branch: 1 branch
BranchTrueFalseTotal
$facet->install($flist, $fhash) or return $self->error($facet->error) 267 0 267
212 || return $self->error($facet->error());
213  
214 267 return $facet;
215 }
216  
217  
218 #------------------------------------------------------------------------
219 # instance($text)
220 # instance($text, $xml_instance)
221 #
222 # Create a new instance of this type from a basic starting value (i.e.
223 # the input text read from the XML instance element). Creates a
224 # scratchpad $infoset hash which is passed first to the
225 # validate_instance() method and then to the activate_instance() method
226 # implemented by the XML::Schema::Scheduler base class. If called in
227 # the second form shown above then the second argument is assumed to
228 # be a reference to an XML instance represented by an XML::Schema::Instance
229 # object.
230 #------------------------------------------------------------------------
231  
232 423 ▶sub instance {
Subroutine instance: called CC=6 SCAR=18.5
Pod undocumented
233 423 my ($self, $text, $instance) = @_;
234 423 $self->{ _ERROR } = '';
235  
236 # $infoset captures 3 stages in the life of an instance:
237 #
238 # text - unmodified input text
239 # value - post-validated value
240 # result - post-scheduling result (default: value)
241 #
242 # validating facets modify 'value'
243 # scheduled actions modify 'result'
244  
245 423 ▶ my $infoset = ref $text ? $text : {
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 0 423 423
246 instance => $instance,
247 text => $text,
248 value => $text,
249 };
250  
251 423 ▶ $self->TRACE("infoset => ", $infoset) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Type::Simple::DEBUG 0 423 423
252  
253 # if validation is successful then the 'value' is copied
254 # to 'result', the instance schedule is activated and the
255 # infoset returned.
256  
257 return $self->validate_instance($infoset)
258 423 ▶ && do { $infoset->{ result } = $infoset->{ value } }
Condition77%7/9
$self->validate_instance($infoset) && do { $infoset->{'result'} = $infoset->{'value'} } && $self->activate_instance($infoset) && $infoset
!ll&&!rl&&r
1070316
$self->validate_instance($infoset) and do { $infoset->{"result"} = $infoset->{"value"} } and $self->activate_instance($infoset)
!ll&&!rl&&r
1070316
$self->validate_instance($infoset) and do { $infoset->{"result"} = $infoset->{"value"} }
!ll&&!rl&&r
1052316
MC/DC50%2/4
$self->validate_instance($infoset) && do { $infoset->{'result'} = $infoset->{'value'} } && $self->activate_instance($infoset) && $infoset
$self->validate_instance($infoset) do { $infoset->{"result"} = $infoset->{"value"} } $self->activate_instance($infoset) $infoset
Truth table60%3/5
A and B and C && D
A= $self->validate_instance($infoset)B= do { $infoset->{"result"} = $infoset->{"value"} }C= $self->activate_instance($infoset)D= $infoset
ABCDresult
0XXX0
10XX0
110X0
11100
11111
259 && $self->activate_instance($infoset)
260 && $infoset;
261 }
262  
263  
264  
265 #------------------------------------------------------------------------
266 # validate_instance(\%infoset)
267 #
268 # Calls the valid() method on all the validation facets for this type,
269 # passing the $infoset scratchpad hash and a self reference against
270 # which the facet can make callbacks. Returns true (1) if all facets
271 # validate the candidate instance data, or undef if not.
272 #------------------------------------------------------------------------
273  
274 426 ▶sub validate_instance {
Subroutine validate_instance: called CC=6 SCAR=17.9
Pod undocumented
275 426 my ($self, $infoset) = @_;
276  
277 426 ▶ $self->TRACE("infoset => ", $infoset) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Type::Simple::DEBUG 0 426 426
278  
279 426 foreach my $facet (@{ $self->{ _FACET_LIST } }) {
280 1016 ▶ if (ref $facet eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $facet eq 'CODE') { } 335 681 1016
281 335 ▶ &$facet($infoset, $self)
Branch: 1 branch
BranchTrueFalseTotal
&$facet($infoset, $self) or return undef 280 55 335
282 || return undef;
283 }
284 else {
285 681 ▶ $facet->valid($infoset, $self)
Branch: 1 branch
BranchTrueFalseTotal
$facet->valid($infoset, $self) or return $self->error($facet->error) 632 49 681
286 || return $self->error($facet->error());
287 }
288 }
289  
290 322 return 1;
291 }
292  
293  
294 #------------------------------------------------------------------------
295 # accessor methods
296 #------------------------------------------------------------------------
297  
298 40 ▶sub facet {
Subroutine facet: called CC=1 SCAR=0.0
Pod undocumented
299 40 my ($self, $name) = @_;
300 40 return $self->{ _FACET_HASH }->{ $name };
301 }
302  
303 3 ▶sub variety {
Subroutine variety: called CC=1 SCAR=0.0
Pod undocumented
304 3 my ($self, $name) = @_;
305 3 return $self->{ _VARIETY };
306 }
307  
308 4 ▶sub annotation {
Subroutine annotation: called CC=2 SCAR=6.9
Pod undocumented
309 4 my $self = shift;
310 4 ▶ return @_ ? ($self->{ annotation } = shift) : $self->{ annotation };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 3 4
311 }
312  
313 0 ▶sub simple {
Subroutine simple: not called CC=1 SCAR=6.9
Pod undocumented
314 0 return 1;
315 }
316  
317 0 ▶sub complex {
Subroutine complex: not called CC=1 SCAR=6.9
Pod undocumented
318 0 return 0;
319 }
320  
321 #------------------------------------------------------------------------
322 # visitor methods
323 #------------------------------------------------------------------------
324  
325 1 ▶sub visit_facets {
Subroutine visit_facets: called CC=3 SCAR=11.7
Pod undocumented
326 1 my ($self, $visitor) = @_;
327 1 my ($facets, $origin) = @$self{ qw( _FACET_LIST _FACET_ORIGIN ) };
328  
329 # we skip over the first n facets as determined by
330 # _FACET_ORIGIN because they're the builtin ones
331  
332 1 foreach my $facet (@$facets[$origin..$#$facets]) {
333 1 ▶ $facet->accept($visitor)
Branch: 1 branch
BranchTrueFalseTotal
$facet->accept($visitor) or return $self->error($facet->error) 0 1 1
334 || return $self->error($facet->error());
335 }
336  
337 0 return 1;
338 }
339
340  
341  
342 #------------------------------------------------------------------------
343 # handler($instance, $element)
344 #
345 # Calls the simple_handler($self, $element) method on the $instance
346 # reference.
347 #
348 # TODO: we could optimise away this chain of method calls by having the
349 # instance Start() method unwrap the calls.
350 #------------------------------------------------------------------------
351  
352 0 ▶sub handler {
Subroutine handler: not called CC=2 SCAR=17.9
Pod undocumented
353 0 my ($self, $instance, $element) = @_;
354 0 ▶ return $instance->simple_handler($self, $element)
Condition0%0/3
$instance->simple_handler($self, $element) || $self->error($instance->error)
l!l&&r!l&&!r
000
MC/DC0%0/2
$instance->simple_handler($self, $element) || $self->error($instance->error)
$instance->simple_handler($self, $element) $self->error($instance->error)
Truth table0%0/3
A || B
A= $instance->simple_handler($self, $element)B= $self->error($instance->error)
ABresult
1X1
011
000
355 || $self->error($instance->error());
356 }
357  
358 0 ▶sub present {
Subroutine present: not called CC=1 SCAR=6.9
Pod undocumented
359 0 my ($self, $view) = @_;
360 0 $view->view( simple => $self );
361 }
362  
363  
364 0 ▶sub ID {
Subroutine ID: not called CC=2 SCAR=17.9
Pod undocumented
365 0 my $self = shift;
366 0 my $base = $self->{ base };
367 0 ▶ $base = "-|>-$base" if $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base 0 0 0
368 0 return "simpleType[$self->{ name }$base]";
369 }
370  
371 28 1;
372  
373 __END__
« lib/XML/Schema/Type/Complex.pm ↑ Summary lib/XML/Schema/Particle/Element.pm »