lib/XML/Schema/Handler/Complex.pm

filter: Statementstmt 44.6% 50 / 112 Branchbran 12.1% 9 / 74 Conditioncond 31.5% 6 / 19 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 50.0% 7 / 14 Podpod 10.0% 1 / 10 totaltotal 30.9% 74 / 239 CC 50 SCAR 43.4
CC
50
Cov
32%
end_element
210.0
start_child
56.0
end_child
56.0
CRAP 76.9
« lib/XML/Schema/Particle/Sequence.pm ↑ Summary lib/XML/Schema.pm »
1 #============================================================= -*-perl-*-
2 #
3 # XML::Schema::Handler::Complex.pm
4 #
5 # DESCRIPTION
6 # Module implementing a parser handler for complex content.
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: Complex.pm,v 1.2 2001/12/20 13:26:27 abw Exp $
20 #
21 #========================================================================
22  
23 package XML::Schema::Handler::Complex;
24  
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use XML::Schema::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use base qw( XML::Schema::Handler );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use vars qw( $VERSION $DEBUG $ERROR @SCHEDULES @MANDATORY );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
31 2 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::Schema::Handler::Complex::DEBUG 2 0 2
32 2 $ERROR = '';
33  
34 2 @MANDATORY = qw( type element );
35 2 @SCHEDULES = qw( start_element start_child end_child end_element text );
36  
37  
38 #------------------------------------------------------------------------
39 # init(\%config)
40 #
41 # Initialiser method called by base class new() constructor.
42 #------------------------------------------------------------------------
43  
44 2 ▶sub init {
Subroutine init: called CC=5 SCAR=16.4
Pod documented
45 2 my ($self, $config) = @_;
46
47 2 ▶ $self->SUPER::init($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init($config) or return 1 1 2
48 || return;
49  
50 1 my ($type, $element) = @$self{ qw( type element ) };
51 1 foreach my $schedule (@SCHEDULES) {
52 5 my $name = "_SCHEDULE_$schedule";
53 5 my @data = ( @{ $type->{ $name } }, @{ $element->{ $name } } );
54 5 ▶ $self->{ $name } = \@data if @data;
Branch: 1 branch
BranchTrueFalseTotal
if @data 0 5 5
55 }
56  
57 1 $self->{ attributes } = $type->attributes();
58  
59 1 my $model = $self->{ model } = $type->content();
60  
61 1 ▶ if ($model) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model) { } 1 0 1
62 1 $self->{ empty } = $model->empty();
63 1 $self->{ mixed } = $model->mixed();
64 }
65 else {
66 0 $self->{ empty } = 1;
67 0 $self->{ mixed } = 0;
68 }
69  
70 1 return $self;
71 }
72  
73  
74 #------------------------------------------------------------------------
75 # start_element($instance, $name, \%attr)
76 #
77 # Called at the start tag of a complex element. The first argument
78 # is a reference to the XML::Schema::Instance in effect. The second
79 # argument provides the element name and the third, a reference to a
80 # hash array of attributes. The attributes are validated according to
81 # any attributes defined for the complex type of this element and then
82 # the content model is initialised ready for parsing element content
83 # by calling the start() method on the content particle. Finally,
84 # any actions scheduled on the start_element list are activated.
85 #------------------------------------------------------------------------
86  
87 6 ▶sub start_element {
Subroutine start_element: called CC=16 SCAR=39.5
Pod undocumented
88 6 my ($self, $instance, $name, $attribs) = @_;
89  
90 6 ▶ $self->TRACE("instance => $instance, name => $name, attribs => ", $attribs)
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 6 6
91 if $DEBUG;
92  
93 my $attrgrp = $self->{ attributes }
94 6 ▶ || return $self->error("no attribute group");
Condition50%1/2
$self->{'attributes'} || (return $self->error('no attribute group'))
l!l
60
MC/DC0%0/1
$self->{'attributes'} || (return $self->error('no attribute group'))
$self->{'attributes'}
Truth table50%1/2
A
A= $self->{'attributes'}
Aresult
11
00
95  
96 6 ▶ my $attributes = $attrgrp->validate($attribs)
Condition100%2/2
$attrgrp->validate($attribs) || (return $self->error($attrgrp->error))
l!l
42
MC/DC100%1/1
$attrgrp->validate($attribs) || (return $self->error($attrgrp->error))
$attrgrp->validate($attribs)
Truth table100%2/2
A
A= $attrgrp->validate($attribs)
Aresult
11
00
97 || return $self->error($attrgrp->error());
98  
99 4 my @ids;
100 my @idrefs;
101  
102 4 ▶ my $magic = $attributes->{ _MAGIC } || { };
Condition50%1/2
$attributes->{'_MAGIC'} || {}
l!l
04
MC/DC0%0/1
$attributes->{'_MAGIC'} || {}
$attributes->{'_MAGIC'}
Truth table50%1/2
A
A= $attributes->{'_MAGIC'}
Aresult
11
00
103 4 delete $attributes->{ _MAGIC };
104  
105 4 ▶ if (%$magic) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$magic) 0 4 4
106 0 local $" = ', ';
107 0 ▶ $self->DEBUG("zoweee! some magic!\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
108 0 foreach my $mkey (keys %$magic) {
109 0 ▶ $self->DEBUG("magic: $mkey:\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
110 0 foreach my $mitem (@{ $magic->{ $mkey } }) {
111 0 ▶ $self->DEBUG(" [ @$mitem ]\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
112 }
113 }
114 }
115 #
116 # if ($tname eq 'ID') {
117 # $self->TRACE("found an ID attribute: ", $name) if $DEBUG;
118 # push(@ids, $attributes->{ $name });
119 # }
120 # elsif ($tname eq 'IDREF') {
121 # $self->TRACE("found an IDREF attribute: ", $attr) if $DEBUG;
122 # push(@idrefs, $name);
123 # }
124  
125 $self->{ element } = {
126 4 name => $name,
127 attributes => $attributes,
128 content => [ ],
129 };
130 4 $self->{ id_fix } = $magic->{ ID };
131 4 $self->{ idref_fix } = $magic->{ IDREF };
132  
133 4 my $model = $self->{ model };
134 4 my $particle;
135  
136 # fire up the content particle
137 4 ▶ if (! $self->{ empty } && $model && ($particle = $model->particle())) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"empty"} and $model and $particle = $model->particle) 0 4 4
Condition33%2/6
not $self->{"empty"} and $model and $particle = $model->particle
!ll&&!rl&&r
400
not $self->{"empty"} and $model
!ll&&!rl&&r
400
MC/DC0%0/3
not $self->{"empty"} and $model and $particle = $model->particle
not $self->{"empty"} $model $particle = $model->particle
Truth table25%1/4
not(A) and B and C
A= not $self->{"empty"}B= $modelC= $particle = $model->particle
ABCresult
0XX0
10X0
1100
1111
138 0 $self->{ particle } = $particle;
139 0 ▶ $particle->start()
Branch: 1 branch
BranchTrueFalseTotal
$particle->start or return $self->error($particle->error) 0 0 0
140 || return $self->error($particle->error());
141 }
142  
143 # activate any scheduled actions for start of element
144 return $self->{ _SCHEDULE_start_element }
145 4 ▶ ? $self->activate_start_element($self)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_SCHEDULE_start_element'} ? : 4 0 4
146 : 1;
147 }
148  
149  
150 #------------------------------------------------------------------------
151 # end_element($instance, $name)
152 #
153 # Called at the end tag of a complex element. The $instance and $name
154 # arguments are as per start_element() above. Triggers validation of
155 # the intervening content model by calling end() on the active particle
156 # and then activates any actions scheduled on the end_element list.
157 # The $self blessed hash object acts as the infoset for collecting
158 # attributes and content for the complex element instance. It passes
159 # itself between the schedule callbacks, each of which is free to
160 # modify and/or supplement the internal data stored within it. The
161 # possibly modified $self is then return to the the caller of
162 # end_element() to indicate success.
163 #------------------------------------------------------------------------
164  
165 0 ▶sub end_element {
Subroutine end_element: not called CC=14 SCAR=53.5
Pod undocumented
166 0 my ($self, $instance, $name) = @_;
167  
168 0 my $element = $self->{ element };
169  
170 $self->throw($self->ID . " caught end of '$name' (expected $self->{ _NAME })")
171 0 ▶ unless $name eq $element->{ name };
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq $element->{"name"} 0 0 0
172  
173 0 ▶ $self->TRACE("instance => $instance, name => $name") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
174  
175 0 ▶ if (my $particle = $self->{ particle }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $particle = $self->{"particle"}) 0 0 0
176 0 ▶ $particle->end($instance, $name)
Branch: 1 branch
BranchTrueFalseTotal
$particle->end($instance, $name) or return $self->error($particle->error) 0 0 0
177 || return $self->error($particle->error());
178 }
179  
180 # $self->{ result } = {
181 # name => $self->{ name },
182 # attributes => $self->{ attributes },
183 # content => $self->{ content },
184 # };
185  
186 my $result = $self->{ _SCHEDULE_end_element }
187 0 ▶ ? $self->activate_end_element($element)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_SCHEDULE_end_element'} ? : 0 0 0
188 : $element;
189  
190 # $element = $self->{ _SCHEDULE_end_element }
191 # ? $self->activate_end_element($element)
192 # : $element;
193  
194 # fixup ID
195 0 foreach my $id (@{ $self->{ id_fix } }) {
196 0 my ($type, $name, $value) = @$id;
197 0 ▶ $self->DEBUG("fixup ID for $type $name => $value\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
198 0 ▶ $instance->id($value, $result)
Branch: 1 branch
BranchTrueFalseTotal
$instance->id($value, $result) or return $self->error($instance->error) 0 0 0
199 || return $self->error($instance->error());
200 }
201  
202 # fixup IDREF (note, this doesn't lookahead - need to schedule at end
203  
204 0 foreach my $idref (@{ $self->{ idref_fix } }) {
205 0 my ($type, $name, $value) = @$idref;
206 0 ▶ $self->DEBUG("fixup IDREF for $type $name => $value\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
207 0 ▶ my $ref = $instance->idref($value)
Condition0%0/2
$instance->idref($value) || (return $self->error($instance->error))
l!l
00
MC/DC0%0/1
$instance->idref($value) || (return $self->error($instance->error))
$instance->idref($value)
Truth table0%0/2
A
A= $instance->idref($value)
Aresult
11
00
208 || return $self->error($instance->error());
209 0 ▶ if ($type eq 'attribute') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "attribute") 0 0 0
210 0 ▶ $self->DEBUG("fixup IDREF attribute $name => ", $ref, "\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
211 0 $result->{ attributes }->{ $name } = $ref;
212 }
213 }
214  
215 # $self->DEBUG("retuning element [$element] => { ", $self->_inspect($element), " }\n");
216 0 return $result;
217 }
218  
219  
220 #------------------------------------------------------------------------
221 # start_child($instance, $name, $attr)
222 #
223 # Called against an outer (parent) element handler when an inner (child)
224 # element is detected. Delegates the call to the current particle
225 # representing the content model and then activates any actions scheduled
226 # for this point.
227 #------------------------------------------------------------------------
228  
229 0 ▶sub start_child {
Subroutine start_child: not called CC=7 SCAR=40.3
Pod undocumented
230 0 my ($self, $instance, $name, $attr) = @_;
231 0 my ($particle, $element, $handler);
232  
233 0 ▶ $self->TRACE("instance => $instance, name => $name") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
234  
235 return $self->error("empty content model cannot contain elements")
236 0 ▶ if $self->{ empty };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"empty"} 0 0 0
237  
238 ($particle = $self->{ particle })
239 0 ▶ || return $self->error("no particle");
Branch: 1 branch
BranchTrueFalseTotal
$particle = $self->{"particle"} or return $self->error("no particle") 0 0 0
240  
241 0 ▶ ($element = $particle->element($name))
Branch: 1 branch
BranchTrueFalseTotal
$element = $particle->element($name) or return $self->error($particle->error) 0 0 0
242 || return $self->error($particle->error());
243  
244 0 ▶ ($handler = $element->handler($instance))
Branch: 1 branch
BranchTrueFalseTotal
$handler = $element->handler($instance) or return $self->error($element->error) 0 0 0
245 || return $self->error($element->error());
246  
247 0 my $child = {
248 name => $name,
249 attributes => $attr,
250 element => $element,
251 handler => $handler,
252 skip => 0, # TODO
253 error => undef,
254 };
255  
256 return $self->{ _SCHEDULE_start_child }
257 0 ▶ ? $self->activate_start_child($child)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_SCHEDULE_start_child'} ? : 0 0 0
258 : $child;
259 }
260  
261  
262 #------------------------------------------------------------------------
263 # end_child()
264 #------------------------------------------------------------------------
265  
266 0 ▶sub end_child {
Subroutine end_child: not called CC=7 SCAR=40.3
Pod undocumented
267 0 my ($self, $instance, $name, $child) = @_;
268  
269 0 ▶ $self->TRACE("instance => $instance, name => $name, child => ", $child) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
270  
271 return $self->error("empty content model cannot contain elements")
272 0 ▶ if $self->{ empty };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"empty"} 0 0 0
273  
274 $child = $self->activate_end_child($child)
275 || return
276 0 ▶ if $self->{ _SCHEDULE_end_child };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_SCHEDULE_end_child"} 0 0 0
Condition0%0/2
$self->activate_end_child($child) || (return)
l!l
00
MC/DC0%0/1
$self->activate_end_child($child) || (return)
$self->activate_end_child($child)
Truth table0%0/2
A
A= $self->activate_end_child($child)
Aresult
11
00
277  
278 # use 'result' entry in child or child as it is
279 0 ▶ my $result = exists $child->{ result } ? $child->{ result } : $child;
Branch: 1 branch
BranchTrueFalseTotal
exists $child->{'result'} ? : 0 0 0
280  
281 0 ▶ push(@{ $self->{ element }->{ content } }, $result)
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 0 0 0
282 if defined $result;
283  
284 0 return $child;
285 }
286  
287  
288 #------------------------------------------------------------------------
289 # text($instance, $text)
290 #
291 # Called to store character content.
292 #------------------------------------------------------------------------
293  
294 0 ▶sub text {
Subroutine text: not called CC=6 SCAR=37.4
Pod undocumented
295 0 my ($self, $instance, $text) = @_;
296  
297 0 ▶ $self->TRACE($self->_text_snippet($text))
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Handler::Complex::DEBUG 0 0 0
298 if $DEBUG;
299  
300 return $self->error("empty content model cannot contain text")
301 0 ▶ if $self->{ empty };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"empty"} 0 0 0
302  
303 return $self->error('non-mixed content model cannot contain text')
304 0 ▶ unless $self->{ mixed } or $text =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mixed"} or $text =~ /^\s*$/ 0 0 0
Condition0%0/3
$self->{"mixed"} or $text =~ /^\s*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"mixed"} or $text =~ /^\s*$/
$self->{"mixed"} $text =~ /^\s*$/
Truth table0%0/3
A or B
A= $self->{"mixed"}B= $text =~ /^\s*$/
ABresult
1X1
011
000
305  
306 0 ▶ push(@{ $self->{ element }->{ content } }, $text) if $self->{ mixed };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mixed"} 0 0 0
307  
308 0 return 1
309 }
310  
311  
312 3 ▶sub attributes {
Subroutine attributes: called CC=1 SCAR=0.0
Pod undocumented
313 3 my $self = shift;
314 3 return $self->{ element }->{ attributes };
315 }
316  
317 0 ▶sub attribute_group {
Subroutine attribute_group: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my $self = shift;
319 0 return $self->{ attributes };
320 }
321  
322 0 ▶sub content {
Subroutine content: not called CC=1 SCAR=6.9
Pod undocumented
323 0 my $self = shift;
324 0 return $self->{ element }->{ content };
325 }
326  
327  
328  
329 0 ▶sub ID {
Subroutine ID: not called CC=1 SCAR=6.9
Pod undocumented
330 0 my $self = shift;
331 0 return "Complex_Handler[$self->{ name }]";
332 }
333  
334 2 1;
335  
336 __END__
« lib/XML/Schema/Particle/Sequence.pm ↑ Summary lib/XML/Schema.pm »