lib/XML/Schema/Scope.pm

filter: Statementstmt 86.8% 79 / 91 Branchbran 62.0% 36 / 58 Conditioncond 54.8% 17 / 31 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 90.9% 10 / 11 Podpod 14.2% 1 / 7 totaltotal 68.0% 147 / 216 CC 42 SCAR 24.1
CC
42
Cov
73%
complexType
19.6
element
17.1
type
10.6
CRAP 11.2
« lib/XML/Schema/Particle/Choice.pm ↑ Summary blib/lib/XML/Schema/Handler/Schema.pm »
1 #============================================================= -*-perl-*-
2 #
3 # XML::Schema::Scope
4 #
5 # DESCRIPTION
6 # Module implementing a mixin object class for providing type
7 # management within a particular scope.
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: Scope.pm,v 1.2 2001/12/20 13:26:27 abw Exp $
21 #
22 #========================================================================
23  
24 package XML::Schema::Scope;
25  
26 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 28 ▶use XML::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 28 ▶use base qw( XML::Schema::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 28 ▶use vars qw( $VERSION $DEBUG $ERROR @OPTIONAL );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 28 $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
32 28 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::Schema::Scope::DEBUG 28 0 28
33 28 $ERROR = '';
34  
35 28 @OPTIONAL = qw( scope );
36  
37  
38 #------------------------------------------------------------------------
39 # init(\%config)
40 #
41 # Initialiser method called by base class new() constructor method.
42 #------------------------------------------------------------------------
43  
44 90 ▶sub init {
Subroutine init: called CC=4 SCAR=14.3
Pod documented
45 90 my ($self, $config) = @_;
46  
47 90 ▶ $self->init_mandopt($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->init_mandopt($config) or return 88 2 90
48 || return;
49  
50 88 ▶ $self->{ _FACTORY } ||= $XML::Schema::FACTORY;
Condition33%1/3
$self->{"_FACTORY"} ||= $XML::Schema::FACTORY
l!l&&r!l&&!r
0880
MC/DC0%0/2
$self->{"_FACTORY"} ||= $XML::Schema::FACTORY
$self->{"_FACTORY"} $XML::Schema::FACTORY
Truth table33%1/3
A ||= B
A= $self->{"_FACTORY"}B= $XML::Schema::FACTORY
ABresult
1X1
011
000
51  
52 # need to think about instantiating objects for types?
53 88 ▶ $self->{ _TYPES } = $config->{ types } || { };
Condition50%1/2
$config->{'types'} || {}
l!l
088
MC/DC0%0/1
$config->{'types'} || {}
$config->{'types'}
Truth table50%1/2
A
A= $config->{'types'}
Aresult
11
00
54  
55 # ditto for attribute_groups?
56 88 $self->{ _ATTRIBUTE_GROUPS } = { };
57  
58 88 return $self;
59 }
60  
61  
62  
63 #========================================================================
64 # Type management methods
65 #
66 # * type($name)
67 # type($type_obj)
68 #
69 # * types()
70 #
71 # * simpleType(\%type_options)
72 #
73 # * complexType(\%type_options)
74 #
75 #========================================================================
76  
77 #------------------------------------------------------------------------
78 # type($name)
79 # type($name, $type_obj)
80 #
81 # Direct way to fetch/store types against names.
82 #------------------------------------------------------------------------
83  
84 144 ▶sub type {
Subroutine type: called CC=10 SCAR=23.6
Pod undocumented
85 144 my $self = shift;
86 144 my $name = shift;
87 144 my ($type, $scope, $factory, $simple, $class);
88  
89 144 ▶ return ($self->{ _TYPES }->{ $name } = shift)
Branch: 1 branch
BranchTrueFalseTotal
if @_ 15 129 144
90 if @_;
91  
92 return $type
93 129 ▶ if ($type = $self->{ _TYPES }->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
if $type = $self->{"_TYPES"}{$name} 7 122 129
94  
95 # delegate to any defined 'scope' if type not found
96 122 ▶ if ($scope = $self->{ scope }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope = $self->{"scope"}) 34 88 122
97 34 ▶ $self->TRACE("delegating $name to $scope") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Scope::DEBUG 0 34 34
98 34 ▶ return $scope->type($name)
Condition66%2/3
$scope->type($name) || $self->error($scope->error)
l!l&&r!l&&!r
3202
MC/DC50%1/2
$scope->type($name) || $self->error($scope->error)
$scope->type($name) $self->error($scope->error)
Truth table66%2/3
A || B
A= $scope->type($name)B= $self->error($scope->error)
ABresult
1X1
011
000
99 || $self->error($scope->error());
100 }
101  
102 # otherwise look for it as a builtin simple type
103 $factory = $self->{ _FACTORY }
104 88 ▶ || return $self->error("no factory defined");
Condition50%1/2
$self->{'_FACTORY'} || (return $self->error('no factory defined'))
l!l
880
MC/DC0%0/1
$self->{'_FACTORY'} || (return $self->error('no factory defined'))
$self->{'_FACTORY'}
Truth table50%1/2
A
A= $self->{'_FACTORY'}
Aresult
11
00
105  
106 88 ▶ $simple = $factory->module('simple')
Condition50%1/2
$factory->module('simple') || (return $self->error($factory->error))
l!l
880
MC/DC0%0/1
$factory->module('simple') || (return $self->error($factory->error))
$factory->module('simple')
Truth table50%1/2
A
A= $factory->module('simple')
Aresult
11
00
107 || return $self->error($factory->error());
108
109 88 ▶ if ($class = $simple->builtin($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class = $simple->builtin($name)) { } 86 2 88
110 86 ▶ return $class->new()
Condition33%1/3
$class->new || $self->error($class->error)
l!l&&r!l&&!r
8600
MC/DC0%0/2
$class->new || $self->error($class->error)
$class->new $self->error($class->error)
Truth table33%1/3
A || B
A= $class->newB= $self->error($class->error)
ABresult
1X1
011
000
111 || $self->error($class->error());
112 }
113 else {
114 2 return $self->error("no such type: $name");
115 }
116 }
117  
118  
119 #------------------------------------------------------------------------
120 # types()
121 #
122 # Return reference to hash array of internal type definitions.
123 #------------------------------------------------------------------------
124  
125 0 ▶sub types {
Subroutine types: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my $self = shift;
127 0 return $self->{ _TYPES };
128 }
129  
130  
131 #------------------------------------------------------------------------
132 # simpleType(\%type_options)
133 #
134 # Method for creating a simpleType object and adding it to the internal
135 # type definition facility.
136 #------------------------------------------------------------------------
137  
138 13 ▶sub simpleType {
Subroutine simpleType: called CC=7 SCAR=20.3
Pod undocumented
139 13 my $self = shift;
140 13 my $factory = $self->{ _FACTORY };
141 13 my ($name, $args, $type);
142  
143 13 ▶ if (ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 13 13
elsif (scalar @_ == 1) { } 4 9 13
144 # hash array or simple type object
145 0 $args = shift;
146 }
147 elsif (scalar @_ == 1) {
148 # name requesting specific type
149 4 $name = shift;
150 4 return $self->type($name);
151 }
152 else {
153 9 $args = { @_ };
154 }
155  
156 9 ▶ if ($factory->isa( simple => $args )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($factory->isa('simple', $args)) { } 0 9 9
157 0 $type = $args;
158 }
159 else {
160 9 ▶ $type = $factory->create( simple => $args )
Condition100%2/2
$factory->create('simple', $args) || (return $self->error($factory->error))
l!l
81
MC/DC100%1/1
$factory->create('simple', $args) || (return $self->error($factory->error))
$factory->create('simple', $args)
Truth table100%2/2
A
A= $factory->create('simple', $args)
Aresult
11
00
161 || return $self->error( $factory->error() );
162 }
163 8 ▶ defined ($name = $type->name())
Branch: 1 branch
BranchTrueFalseTotal
defined($name = $type->name) or return $self->error("no name specified for simpleType") 8 0 8
164 || return $self->error('no name specified for simpleType');
165  
166 8 ▶ $self->TRACE("name => ", $type->ID) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Scope::DEBUG 0 8 8
167  
168 8 return $self->type($name => $type);
169 }
170  
171  
172 #------------------------------------------------------------------------
173 # complexType(\%type_options)
174 #
175 # Method for creating a complexType object and adding it to the internal
176 # type definition facility.
177 #------------------------------------------------------------------------
178  
179 5 ▶sub complexType {
Subroutine complexType: called CC=10 SCAR=29.7
Pod undocumented
180 5 my $self = shift;
181 5 my $factory = $self->{ _FACTORY };
182 5 my ($name, $args, $type);
183  
184 5 ▶ if (ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 5 5
elsif (scalar @_ == 1) { } 0 5 5
185 # hash array or complex type object
186 0 $args = shift;
187 }
188 elsif (scalar @_ == 1) {
189 # name requesting specific type
190 0 $name = shift;
191 0 return $self->type->{ $name };
192 }
193 else {
194 5 $args = { @_ };
195 }
196  
197 5 ▶ if ($factory->isa( complex => $args )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($factory->isa('complex', $args)) { } 0 5 5
198 0 $type = $args;
199 # define scope of complex type unless already set
200 0 ▶ $type->scope($self)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type->scope 0 0 0
201 unless defined $type->scope();
202  
203 }
204 else {
205 # define scope of complex type unless already set
206 $args->{ scope } = $self
207 if UNIVERSAL::isa($args, 'HASH')
208 5 ▶ && ! exists $args->{ scope };
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($args, "HASH") and not exists $args->{"scope"} 5 0 5
Condition33%1/3
UNIVERSAL::isa($args, "HASH") and not exists $args->{"scope"}
!ll&&!rl&&r
005
MC/DC0%0/2
UNIVERSAL::isa($args, "HASH") and not exists $args->{"scope"}
UNIVERSAL::isa($args, "HASH") not exists $args->{"scope"}
Truth table33%1/3
A and not(B)
A= UNIVERSAL::isa($args, "HASH")B= not exists $args->{"scope"}
ABresult
0X0
100
111
209  
210 5 ▶ $type = $factory->create( complex => $args )
Condition50%1/2
$factory->create('complex', $args) || (return $self->error($factory->error))
l!l
50
MC/DC0%0/1
$factory->create('complex', $args) || (return $self->error($factory->error))
$factory->create('complex', $args)
Truth table50%1/2
A
A= $factory->create('complex', $args)
Aresult
11
00
211 || return $self->error( $factory->error() );
212 }
213 5 ▶ defined ($name = $type->name())
Branch: 1 branch
BranchTrueFalseTotal
defined($name = $type->name) or return $self->error("no name specified for complexType") 5 0 5
214 || return $self->error('no name specified for complexType');
215  
216 5 ▶ $self->TRACE("name => ", $type->ID) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Scope::DEBUG 0 5 5
217  
218 5 return $self->type($name => $type);
219  
220 }
221  
222  
223  
224 #========================================================================
225 # Element management methods
226 #========================================================================
227  
228 3 ▶sub element {
Subroutine element: called CC=9 SCAR=28.4
Pod undocumented
229 3 my $self = shift;
230 my $factory = $self->{ _FACTORY }
231 3 ▶ || return $self->error("no factory defined");
Condition50%1/2
$self->{'_FACTORY'} || (return $self->error('no factory defined'))
l!l
30
MC/DC0%0/1
$self->{'_FACTORY'} || (return $self->error('no factory defined'))
$self->{'_FACTORY'}
Truth table50%1/2
A
A= $self->{'_FACTORY'}
Aresult
11
00
232  
233 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 0 3
234 3 ▶ if ($factory->isa( element => $_[0] )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($factory->isa('element', $_[0])) { } 0 3 3
235 0 ▶ $self->TRACE("returning element") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Scope::DEBUG 0 0 0
236 0 return shift;
237 }
238 else {
239 3 ▶ my $args = UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'HASH') ? : 0 3 3
240 3 ▶ $args->{ scope } = $self unless exists $args->{ scope };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"scope"} 3 0 3
241 3 ▶ $self->TRACE("creating element") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Scope::DEBUG 0 3 3
242 3 ▶ return $factory->create( element => $args )
Condition33%1/3
$factory->create('element', $args) || $self->error($factory->error)
l!l&&r!l&&!r
300
MC/DC0%0/2
$factory->create('element', $args) || $self->error($factory->error)
$factory->create('element', $args) $self->error($factory->error)
Truth table33%1/3
A || B
A= $factory->create('element', $args)B= $self->error($factory->error)
ABresult
1X1
011
000
243 || $self->error($factory->error());
244 }
245 }
246 else {
247 0 return $self->error("no element arguments");
248 }
249 }
250  
251  
252 #========================================================================
253 # Attribute Group management methods
254 #========================================================================
255  
256  
257 #------------------------------------------------------------------------
258 # attribute_group()
259 # attribute_group($new_group)
260 #------------------------------------------------------------------------
261  
262 37 ▶sub attribute_group {
Subroutine attribute_group: called CC=7 SCAR=19.5
Pod undocumented
263 37 my ($self, $group) = @_;
264 37 my $name;
265  
266 # return entire hash if called with no arguments
267 return $self->{ _ATTRIBUTE_GROUPS }
268 37 ▶ unless defined $group;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group 1 36 37
269  
270 # create and register new attribute group if group is a reference to
271 # a group object or hash of configuration options for an attribute
272 # group, otherwise...
273  
274 36 ▶ if (ref $group) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $group) { } 10 26 36
275 10 my $factory = $self->factory();
276  
277 # coerce into attribute group object, if not already so
278 10 ▶ $group = $factory->create( attribute_group => $group )
Branch: 1 branch
BranchTrueFalseTotal
unless $factory->isa("attribute_group", $group) 4 6 10
Condition100%2/2
$factory->create('attribute_group', $group) || (return $self->error($factory->error))
l!l
31
MC/DC100%1/1
$factory->create('attribute_group', $group) || (return $self->error($factory->error))
$factory->create('attribute_group', $group)
Truth table100%2/2
A
A= $factory->create('attribute_group', $group)
Aresult
11
00
279 || return $self->error( $factory->error() )
280 unless $factory->isa( attribute_group => $group );
281
282 # by what name should we reference this group?
283 9 $name = $group->name();
284  
285 9 ▶ return $self->error("no name specified for attribute group")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 9 9
286 unless defined $name;
287  
288 # install it
289 9 $self->{ _ATTRIBUTE_GROUPS }->{ $name } = $group;
290 }
291 else {
292 26 $name = $group;
293 26 ▶ $group = $self->{ _ATTRIBUTE_GROUPS }->{ $name }
Condition100%2/2
$self->{'_ATTRIBUTE_GROUPS'}{$name} || (return $self->error("no such attribute group: $name"))
l!l
251
MC/DC100%1/1
$self->{'_ATTRIBUTE_GROUPS'}{$name} || (return $self->error("no such attribute group: $name"))
$self->{'_ATTRIBUTE_GROUPS'}{$name}
Truth table100%2/2
A
A= $self->{'_ATTRIBUTE_GROUPS'}{$name}
Aresult
11
00
294 || return $self->error("no such attribute group: $name");
295 }
296  
297 34 return $group;
298 }
299  
300  
301  
302 28 1;
303  
304 __END__
« lib/XML/Schema/Particle/Choice.pm ↑ Summary blib/lib/XML/Schema/Handler/Schema.pm »