Generator/Model.pm

filter: Statementstmt 36.0% 44 / 122 Branchbran 0.0% 0 / 28 Conditioncond 2.7% 1 / 36 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 64.2% 9 / 14 Podpod 75.0% 3 / 4 totaltotal 25.9% 57 / 220 CC 28 SCAR 49.8
CC
28
Cov
24%
setInstanceData
240.0
bindControl
182.0
new
2.0
CRAP 146.0
↑ Summary Generator/Common.pm »
1 package XML::XForms::Generator::Model;
2 ######################################################################
3 ## ##
4 ## Package: Model.pm ##
5 ## Author: D. Hageman <dhageman@dracken.com> ##
6 ## ##
7 ## Description: ##
8 ## ##
9 ## Perl object to assist in the generation of XML compliant with ##
10 ## the W3's XForms specification. ##
11 ## ##
12 ######################################################################
13  
14 ##==================================================================##
15 ## Libraries and Variables ##
16 ##==================================================================##
17  
18 5 require 5.006;
19 5 require Exporter;
20  
21 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use XML::XForms::Generator::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 our @ISA = qw( Exporter XML::LibXML::Element );
29  
30 5 our @EXPORT = qw( xforms_model );
31  
32 5 our $VERSION = "0.70";
33  
34 ##==================================================================##
35 ## Constructor(s)/Deconstructor(s) ##
36 ##==================================================================##
37  
38 ##----------------------------------------------##
39 ## new ##
40 ##----------------------------------------------##
41 ## XForms::Model default contstructor. ##
42 ##----------------------------------------------##
43 sub new
44 2 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
45 ## Pull in what type of an object we will be.
46 2 my $type = shift;
47 ## Pull in any arguments provided to the constructor.
48 2 my $attributes = shift;
49 2 my @children = @_;
50 ## The object we are generating is going to be a child class of
51 ## XML::LibXML's DOM objects.
52 2 my $self = XML::LibXML::Element->new( 'model' );
53 ## Determine what exact class we will be blessing this instance into.
54 2 ▶ my $class = ref( $type ) || $type;
Condition33%1/3
ref $type || $type
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table33%1/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
55 ## Bless the class for it is good [tm].
56 2 bless( $self, $class );
57 ## We need to set our namespace on our model element and activate it.
58 2 $self->setNamespace( $XFORMS_NAMESPACE{xforms}, "xforms", 1 );
59 ## Determine if we have an 'id' attribute and set it if we do.
60 2 __xforms_attribute( $self, $attributes );
61 2 __xforms_children( $self, @children );
62 ## Send it back to the caller all happy like.
63 2 return( $self );
64 }
65  
66 ##----------------------------------------------##
67 ## DESTROY ##
68 ##----------------------------------------------##
69 ## XForms::Model default deconstructor. ##
70 ##----------------------------------------------##
71 sub DESTROY
72 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
73 ## This is mainly a placeholder to keep things like mod_perl happy.
74 0 return;
75 }
76  
77 ##==================================================================##
78 ## Method(s) ##
79 ##==================================================================##
80  
81 ## Loop through all of the extension elements building convience methods
82 ## as we go along.
83 5 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 5 foreach my $element ( @XFORMS_MODEL_ELEMENT )
86 {
87 ## We need to temporarily remove the namespace prefix for our work.
88 20 $element =~ s/^xforms://g;
89  
90 ##----------------------------------------------##
91 ## appendCHILDENAME ##
92 ##----------------------------------------------##
93 ## Method generation for the common child ##
94 ## elements of the model element. ##
95 ##----------------------------------------------##
96 20 *{ "append" . ucfirst( $element ) } =
97 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
98  
99 0 my( $self, $attributes, @children ) = @_;
100  
101 0 my $node = XML::LibXML::Element->new( $element );
102  
103 0 $self->appendChild( $node );
104  
105 0 $node->setNamespace( $XFORMS_NAMESPACE{xforms}, "xforms", 1 );
106  
107 0 __xforms_attribute( $node, $attributes );
108 0 __xforms_children( $node, @children );
109  
110 0 return( $node );
111 20 };
112  
113 ##----------------------------------------------##
114 ## getCHILDENAME ##
115 ##----------------------------------------------##
116 ## Method generation for the retrieval of ##
117 ## common model elements. ##
118 ##----------------------------------------------##
119 20 *{ "get" . ucfirst( $element ) } =
120 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
121  
122 0 my $self = shift;
123
124 0 my @nodes =
125 $self->getChildrenByTagName( "xforms:$element" );
126
127 0 return( @nodes );
128 20 };
129 }
130  
131 5 ▶use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 ##----------------------------------------------##
134 ## bindControl ##
135 ##----------------------------------------------##
136 ## Method used to associate a control with a ##
137 ## model's instance data. ##
138 ##----------------------------------------------##
139 sub bindControl ($$$$)
140 0 ▶{
Subroutine bindControl: not called CC=13 SCAR=52.0
Pod documented
141 0 my( $self, $control, $bind, $value ) = @_;
142  
143 0 my( $instance ) = $self->getInstance();
144  
145 0 ▶ if( !defined( $instance ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $instance) 0 0 0
146 {
147 0 croak( qq|Error: Model element requires an instance child |,
148 qq|element before controls can be bound to it.| );
149 }
150  
151 ## We really want the child element of the instance node.
152 0 $instance = $instance->firstChild;
153  
154 0 ▶ if( ( defined( $bind ) ) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $bind and ref $bind and $bind->isa('XML::LibXML::Node')) { } 0 0 0
elsif (defined $bind) { } 0 0 0
Condition0%0/6
defined $bind and ref $bind and $bind->isa("XML::LibXML::Node")
!ll&&!rl&&r
000
defined $bind and ref $bind
!ll&&!rl&&r
000
MC/DC0%0/3
defined $bind and ref $bind and $bind->isa("XML::LibXML::Node")
defined $bind ref $bind $bind->isa("XML::LibXML::Node")
Truth table0%0/4
A and B and C
A= defined $bindB= ref $bindC= $bind->isa("XML::LibXML::Node")
ABCresult
0XX0
10X0
1100
1111
155 ( ref( $bind ) ) &&
156 ( $bind->isa( "XML::LibXML::Node" ) ) )
157 {
158 ## The first case exists when we are giving a prebuilt
159 ## binding node.
160 0 my @attributes = $bind->attributes;
161 0 my @children = $bind->childNodes;
162  
163 0 my $attributes = {};
164  
165 0 foreach( @attributes )
166 {
167 0 $attributes->{$_->nodeName} = $_->getValue();
168 }
169
170 ## We need to ensure we grab a copy of the nodeset and id attributes
171 ## here because appendBind is destructive.
172 0 $bind = $attributes->{nodeset};
173 0 my $id = $attributes->{id};
174
175 ## We don't attach the node that was passed in, but rather
176 ## we create another using it. This allows us to do some
177 ## error/consistancy checking.
178 0 $self->appendBind( $attributes, @children );
179
180 ## The second case is when we are just given a XPath to
181 ## to the instance data.
182 0 $bind =~ s/^\/+//g;
183 0 $bind =~ s/\/+/\//g;
184
185 ## We need to set the 'bind' attribute on the node.
186 0 $control->setAttribute( 'bind', $id);
187 0 $control->removeAttribute( 'ref' );
188
189 }
190 elsif( defined( $bind ) )
191 {
192 ## The second case is when we are just given a XPath to
193 ## to the instance data.
194 0 $bind =~ s/^\/+//g;
195 0 $bind =~ s/\/+/\//g;
196  
197 ## We need to set the 'ref' attribute on the node.
198 0 $control->setAttribute( 'ref', $bind );
199 0 $control->removeAttribute( 'bind' );
200 }
201 else
202 {
203 ## The last case is the hope that the control element
204 ## already contains an 'ref' element.
205 0 $bind = $control->getAttribute( "ref" );
206  
207 0 ▶ if( !defined( $bind ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bind) 0 0 0
208 {
209 0 croak( qq|Error: A binding expression must already exist on |,
210 qq|the control element or one must be supplied | );
211 }
212  
213 ## Clean the binding expression ...
214 0 $bind =~ s/^\/+//g;
215 0 $bind =~ s/\/+/\//g;
216
217 ## We will reset it to ensure that the clean version is used.
218 0 $control->setAttribute( 'ref', $bind );
219 0 $control->removeAttribute( 'bind' );
220 }
221  
222 ## Break up the XPath statement into chunks.
223 0 my @path = split( /\//, $bind );
224  
225 ## Loop through each of the @path statements to ensure and build
226 ## the XPath to the instance data.
227 0 ▶ for( my $search = 0; $search < scalar( @path ); $search++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($search < scalar @path) 0 0 0
228 {
229 0 my( $node ) = $instance->getChildrenByTagName( $path[ $search ] );
230  
231 ## Check to see if the node is already defined.
232 0 ▶ if( !defined( $node ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $node) { } 0 0 0
233 {
234 0 my $element = XML::LibXML::Element->new( $path[ $search ] );
235 0 $instance = $instance->appendChild( $element );
236 }
237 else
238 {
239 0 $instance = $node;
240 }
241 }
242
243 ## Look to see if any nodes exist under the current node.
244 0 ▶ if( $instance->hasChildNodes() )
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->hasChildNodes) 0 0 0
245 {
246 0 $instance->removeChildNodes();
247 }
248  
249 ## Check to see if the value is text or a node.
250 0 ▶ if( ( ref( $value ) ) && ( $value->isa( "XML::LibXML::Node" ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $value->isa('XML::LibXML::Node')) { } 0 0 0
Condition0%0/3
ref $value and $value->isa("XML::LibXML::Node")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and $value->isa("XML::LibXML::Node")
ref $value $value->isa("XML::LibXML::Node")
Truth table0%0/3
A and B
A= ref $valueB= $value->isa("XML::LibXML::Node")
ABresult
0X0
100
111
251 {
252 0 $instance->appendChild( $value );
253 }
254 else
255 {
256 0 my $text = XML::LibXML::Text->new( $value );
257 0 $instance->appendChild( $text );
258 ## The below causes a segfault. Need to figure that one out.
259 ##$instance->appendText( $value );
260 }
261
262 ## I want to make sure that we properly associate this control
263 ## with the proper model.
264 0 $control->setAttribute( 'model', $self->getAttribute( "id" ) );
265  
266 0 return( $instance );
267 }
268  
269 ##----------------------------------------------##
270 ## setInstanceData ##
271 ##----------------------------------------------##
272 ## Convience method for setting instance data ##
273 ## in the model element. ##
274 ##----------------------------------------------##
275 sub setInstanceData ($$$)
276 0 ▶{
Subroutine setInstanceData: not called CC=15 SCAR=54.8
Pod documented
277 0 my( $self, $bind, $value ) = @_;
278  
279 ## We make sure that $value is defined purely for the removal of
280 ## perl warning messages.
281 0 ▶ if( !defined( $value ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
282 {
283 0 $value = "";
284 }
285  
286 0 my( $instance ) = $self->getInstance();
287  
288 0 ▶ if( !defined( $instance ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $instance) 0 0 0
289 {
290 0 croak( qq|Error: Model element requires an instance child |,
291 qq|element before controls can be bound to it.| );
292 }
293  
294 ## We really want the child element of the instance node.
295 0 $instance = $instance->firstChild;
296  
297 ## Clean up our binding expression a bit ...
298 0 $bind =~ s/^\/+//g;
299 0 $bind =~ s/\/+/\//g;
300  
301 ## Break up the XPath statement into chunks.
302 0 my @path = split( /\//, $bind );
303  
304 ## Loop through each of the @path statements to ensure and build
305 ## the XPath to the instance data.
306 0 ▶ for( my $search = 0; $search < scalar( @path ); $search++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($search < scalar @path) 0 0 0
307 {
308 0 my( $node ) = $instance->getChildrenByTagName( $path[ $search ] );
309  
310 ## Check to see if the node is already defined.
311 0 ▶ if( !defined( $node ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $node) { } 0 0 0
312 {
313 0 my $element = XML::LibXML::Element->new( $path[ $search ] );
314 0 $instance = $instance->appendChild( $element );
315 }
316 else
317 {
318 0 $instance = $node;
319 }
320 }
321
322 ## Look to see if any nodes exist under the current node.
323 0 ▶ if( $instance->hasChildNodes() )
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->hasChildNodes) 0 0 0
324 {
325 0 $instance->removeChildNodes();
326 }
327  
328 ## Check to see if the value is text or a node.
329 0 ▶ if( ( ref( $value ) ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and ref $value ne 'ARRAY' and ref $value ne 'HASH' and ref $value ne 'SCALAR' and ref $value ne 'CODE' and ref $value ne 'REF' and ref $value ne 'LVALUE' and ref $value ne 'GLOB' and $value->isa('XML::LibXML::Node')) { } 0 0 0
Condition0%0/24
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH" and ref $value ne "SCALAR" and ref $value ne "CODE" and ref $value ne "REF" and ref $value ne "LVALUE" and ref $value ne "GLOB" and $value->isa("XML::LibXML::Node")
!ll&&!rl&&r
000
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH" and ref $value ne "SCALAR" and ref $value ne "CODE" and ref $value ne "REF" and ref $value ne "LVALUE" and ref $value ne "GLOB"
!ll&&!rl&&r
000
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH" and ref $value ne "SCALAR" and ref $value ne "CODE" and ref $value ne "REF" and ref $value ne "LVALUE"
!ll&&!rl&&r
000
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH" and ref $value ne "SCALAR" and ref $value ne "CODE" and ref $value ne "REF"
!ll&&!rl&&r
000
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH" and ref $value ne "SCALAR" and ref $value ne "CODE"
!ll&&!rl&&r
000
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH" and ref $value ne "SCALAR"
!ll&&!rl&&r
000
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH"
!ll&&!rl&&r
000
ref $value and ref $value ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/9
ref $value and ref $value ne "ARRAY" and ref $value ne "HASH" and ref $value ne "SCALAR" and ref $value ne "CODE" and ref $value ne "REF" and ref $value ne "LVALUE" and ref $value ne "GLOB" and $value->isa("XML::LibXML::Node")
ref $value ref $value ne "ARRAY" ref $value ne "HASH" ref $value ne "SCALAR" ref $value ne "CODE" ref $value ne "REF" ref $value ne "LVALUE" ref $value ne "GLOB" $value->isa("XML::LibXML::Node")
Truth table0%0/10
A and B and C and D and E and F and G and H and I
A= ref $valueB= ref $value ne "ARRAY"C= ref $value ne "HASH"D= ref $value ne "SCALAR"E= ref $value ne "CODE"F= ref $value ne "REF"G= ref $value ne "LVALUE"H= ref $value ne "GLOB"I= $value->isa("XML::LibXML::Node")
ABCDEFGHIresult
0XXXXXXXX0
10XXXXXXX0
110XXXXXX0
1110XXXXX0
11110XXXX0
111110XXX0
1111110XX0
11111110X0
1111111100
1111111111
330 ( ref( $value ) ne "ARRAY" ) &&
331 ( ref( $value ) ne "HASH" ) &&
332 ( ref( $value ) ne "SCALAR" ) &&
333 ( ref( $value ) ne "CODE" ) &&
334 ( ref( $value ) ne "REF" ) &&
335 ( ref( $value ) ne "LVALUE" ) &&
336 ( ref( $value ) ne "GLOB" ) &&
337 ( $value->isa( "XML::LibXML::Node" ) ) )
338 {
339 0 $instance->appendChild( $value );
340 }
341 else
342 {
343 0 my $text = XML::LibXML::Text->new( $value );
344 0 $instance->appendChild( $text );
345 ## The below causes a segfault. Need to figure that one out.
346 ##$instance->appendText( $value );
347 }
348  
349 0 return( $instance );
350 }
351  
352 ##==================================================================##
353 ## Function(s) ##
354 ##==================================================================##
355  
356 ##----------------------------------------------##
357 ## xforms_model ##
358 ##----------------------------------------------##
359 ## Alias for the default constructor. ##
360 ##----------------------------------------------##
361 sub xforms_model
362 2 ▶{
Subroutine xforms_model: called CC=1 SCAR=0.0
Pod undocumented
363 2 return( XML::XForms::Generator::Model->new( @_ ) );
364 }
365  
366 ##==================================================================##
367 ## Internal Function(s) ##
368 ##==================================================================##
369  
370 ##
371 ## None.
372 ##
373  
374 ##==================================================================##
375 ## End of Code ##
376 ##==================================================================##
377 5 1;
378  
379 ##==================================================================##
380 ## Plain Old Documentation (POD) ##
381 ##==================================================================##
382  
383 __END__
↑ Summary Generator/Common.pm »