WDDX/Array.pm

filter: Statementstmt 4.9% 6 / 121 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 7.6% 2 / 26 Podpod n/a totaltotal 3.8% 8 / 208 CC 31 SCAR 29.7
CC
31
Cov
3%
splice
56.0
start_tag
42.0
end_tag
30.0
CRAP 19.5
« WDDX/Struct.pm ↑ Summary WDDX/Boolean.pm »
1 #!/usr/bin/perl -w
2 #
3 # $Id: Array.pm,v 1.2 2003/10/28 17:00:51 andy Exp $
4 #
5 # This code is copyright 1999-2000 by Scott Guelich <scott@scripted.com>
6 # and is distributed according to the same conditions as Perl itself
7 # Please visit http://www.scripted.com/wddx/ for more information
8 #
9  
10 package WDDX::Array;
11  
12 # Auto-inserted by build scripts
13 $VERSION = "1.01";
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 require WDDX;
19  
20 { my $i_hate_the_w_flag_sometimes = [
21 $WDDX::PACKET_HEADER,
22 $WDDX::PACKET_FOOTER,
23 $WDDX::Array::VERSION
24 ] }
25  
26 1;
27  
28  
29 #/-----------------------------------------------------------------------
30 # Public Methods
31 #
32  
33 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
34 0 my( $class, $arrayref ) = @_;
35
36 0 ▶ croak "You must supply an array ref when creating a new $class object\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $arrayref 0 0 0
37 unless $arrayref;
38
39 0 my $self = {
40 value => $arrayref,
41 };
42
43 0 bless $self, $class;
44 0 return $self;
45 }
46  
47  
48 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
49 0 return "array";
50 }
51  
52  
53 0 ▶sub as_packet {
Subroutine as_packet: not called CC=1 SCAR=6.9
54 0 my( $self ) = @_;
55 0 my $output = $WDDX::PACKET_HEADER .
56 $self->_serialize .
57 $WDDX::PACKET_FOOTER;
58 }
59  
60  
61 0 ▶sub as_arrayref {
Subroutine as_arrayref: not called CC=1 SCAR=6.9
62 0 my( $self ) = @_;
63 0 return $self->_deserialize;
64 }
65  
66  
67 0 ▶sub as_javascript {
Subroutine as_javascript: not called CC=2 SCAR=17.9
68 0 my( $self, $js_var ) = @_;
69 0 my $arrayref = $self->{value};
70 0 my $output = "$js_var=new Array();";
71
72 0 ▶ for ( my $i = 0; $i < @$arrayref; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arrayref) 0 0 0
73 0 $output .= $arrayref->[$i]->as_javascript( $js_var . "[$i]" );
74 }
75 0 return $output;
76 }
77  
78  
79 #/-----------------------------------------------------------------------
80 # Other Public Methods
81 #
82  
83  
84 0 ▶sub get_element {
Subroutine get_element: not called CC=1 SCAR=6.9
85 0 my( $self, $index ) = @_;
86 0 return $self->{value}[$index];
87 }
88  
89  
90 # Method alias
91 *get = *get = \&get_element;
92  
93  
94 0 ▶sub set {
Subroutine set: not called CC=3 SCAR=24.8
95 0 my( $self, %pairs ) = @_;
96 0 my( $index, $value );
97
98 0 ▶ while ( ( $index, $value ) = each %pairs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pairs) 0 0 0
99 croak "The values assigned must be WDDX data objects.\n"
100 0 ▶ unless eval { $value->can( "_serialize" ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $value->can("_serialize") } } 0 0 0
101 0 $self->{value}[$index] = $value;
102 }
103 }
104  
105  
106 0 ▶sub splice {
Subroutine splice: not called CC=7 SCAR=40.3
107 0 my( $self, $offset, $length, @values ) = @_;
108 0 my @result;
109
110 0 ▶ if ( @values ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@values) { } 0 0 0
elsif (defined $length) { } 0 0 0
111 0 foreach ( @values ) {
112 croak "The values assigned must be WDDX data objects.\n"
113 0 ▶ unless eval { $_->can( "_serialize" ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $_->can("_serialize") } } 0 0 0
114 }
115 0 @result = splice @{ $self->{value} }, $offset, $length, @values;
116 }
117 elsif ( defined $length ) {
118 0 @result = splice @{ $self->{value} }, $offset, $length;
119 }
120 else {
121 0 @result = splice @{ $self->{value} }, $offset;
122 }
123
124 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
125 0 return @result;
126 }
127 else {
128 0 ▶ return @result ? pop @result : undef;
Branch: 1 branch
BranchTrueFalseTotal
@result ? : 0 0 0
129 }
130 }
131  
132  
133 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
134 0 my( $self ) = @_;
135 0 return scalar @{ $self->{value} };
136 }
137  
138  
139 0 ▶sub push {
Subroutine push: not called CC=3 SCAR=24.8
140 0 my( $self, @values ) = @_;
141 0 foreach ( @values ) {
142 croak "The values assigned must be WDDX data objects.\n"
143 0 ▶ unless eval { $_->can( "_serialize" ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $_->can("_serialize") } } 0 0 0
144 }
145 0 push @{ $self->{value} }, @values;
146 }
147  
148  
149 0 ▶sub pop {
Subroutine pop: not called CC=1 SCAR=6.9
150 0 my( $self ) = @_;
151 0 pop @{ $self->{value} };
152 }
153  
154  
155 0 ▶sub shift {
Subroutine shift: not called CC=1 SCAR=6.9
156 0 my( $self ) = @_;
157 0 shift @{ $self->{value} };
158 }
159  
160  
161 0 ▶sub unshift {
Subroutine unshift: not called CC=3 SCAR=24.8
162 0 my( $self , @values ) = @_;
163 0 foreach ( @values ) {
164 croak "The values assigned must be WDDX data objects.\n"
165 0 ▶ unless eval { $_->can( "_serialize" ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $_->can("_serialize") } } 0 0 0
166 }
167 0 unshift @{ $self->{value} }, @values;
168 }
169  
170  
171 #/-----------------------------------------------------------------------
172 # Private Methods
173 #
174  
175 0 ▶sub is_parser {
Subroutine is_parser: not called CC=1 SCAR=6.9
176 0 return 0;
177 }
178  
179  
180 0 ▶sub _serialize {
Subroutine _serialize: not called CC=2 SCAR=17.9
181 0 my( $self ) = @_;
182 0 my $value = $self->{value};
183
184 0 my $length = @$value;
185 0 my $output = "<array length='$length'>";
186
187 0 foreach ( @$value ) {
188 0 $output .= $_->_serialize();
189 }
190 0 $output .= "</array>";
191 0 return $output;
192 }
193  
194  
195 0 ▶sub _deserialize {
Subroutine _deserialize: not called CC=1 SCAR=6.9
196 0 my( $self ) = @_;
197 0 my @val_array = map $_->_deserialize, @{ $self->{value} };
198
199 0 return \@val_array;
200 }
201  
202 #/-----------------------------------------------------------------------
203 # Parsing Code
204 #
205  
206 package WDDX::Array::Parser;
207  
208  
209 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
210 0 my $class = shift;
211
212 0 my $self = {
213 value => [],
214 'length' => 0,
215 parse_var => undef,
216 seen_arrays => 0,
217 };
218 0 return bless $self, $class;
219 }
220  
221  
222 0 ▶sub start_tag {
Subroutine start_tag: not called CC=6 SCAR=37.4
223 0 my( $self, $element, $attribs ) = @_;
224 0 my $parse_var = $self->parse_var;
225
226 0 ▶ if ( $element eq "array" and not $self->{seen_arrays}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq 'array' and not $self->{'seen_arrays'}++) { } 0 0 0
Condition0%0/3
$element eq "array" and not $self->{"seen_arrays"}++
!ll&&!rl&&r
000
MC/DC0%0/2
$element eq "array" and not $self->{"seen_arrays"}++
$element eq "array" not $self->{"seen_arrays"}++
Truth table0%0/3
A and not(B)
A= $element eq "array"B= not $self->{"seen_arrays"}++
ABresult
0X0
100
111
227 0 ▶ unless ( $attribs->{'length'} + 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attribs->{"length"} + 0) 0 0 0
228 0 die "Invalid value for length attribute in <array> tag";
229 }
230 0 $self->{'length'} = $attribs->{'length'};
231 }
232 else {
233 0 ▶ unless ( $parse_var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parse_var) 0 0 0
234 0 ▶ $parse_var = WDDX::Parser->create_var( $element ) or
Branch: 1 branch
BranchTrueFalseTotal
$parse_var = "WDDX::Parser"->create_var($element) or die 'Expecting some data element (e.g., <string>), ' . "found: <$element>\n" 0 0 0
235 die "Expecting some data element (e.g., <string>), " .
236 "found: <$element>\n";
237 0 $self->push( $parse_var );
238 }
239 0 $parse_var->start_tag( $element, $attribs );
240 }
241
242 0 return $self;
243 }
244  
245  
246 0 ▶sub end_tag {
Subroutine end_tag: not called CC=5 SCAR=34.0
247 0 my( $self, $element ) = @_;
248 0 my $parse_var = $self->parse_var;
249
250 0 ▶ if ( $element eq "array" and not --$self->{seen_arrays} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq 'array' and not --$self->{'seen_arrays'}) { } 0 0 0
Condition0%0/3
$element eq "array" and not --$self->{"seen_arrays"}
!ll&&!rl&&r
000
MC/DC0%0/2
$element eq "array" and not --$self->{"seen_arrays"}
$element eq "array" not --$self->{"seen_arrays"}
Truth table0%0/3
A and not(B)
A= $element eq "array"B= not --$self->{"seen_arrays"}
ABresult
0X0
100
111
251 # If fewer elements than declared, pad with null objects??
252 0 ▶ while ( $self->num_elements < $self->{'length'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_elements < $self->{"length"}) 0 0 0
253 0 $self->push( new WDDX::Null() );
254 }
255 0 $self = new WDDX::Array( $self->{value} );
256 }
257 else {
258 0 ▶ unless ( $parse_var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parse_var) 0 0 0
259 # XML::Parser should actually catch this
260 0 die "Found </$element> before <$element>\n";
261 }
262 0 $self->parse_var( $parse_var->end_tag( $element ) );
263 }
264
265 0 return $self;
266 }
267  
268  
269 0 ▶sub append_data {
Subroutine append_data: not called CC=3 SCAR=24.8
270 0 my( $self, $data ) = @_;
271 0 my $parse_var = $self->parse_var;
272
273 0 ▶ if ( $parse_var ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parse_var) { } 0 0 0
elsif ($data =~ /\S/) { } 0 0 0
274 0 $parse_var->append_data( $data );
275 }
276 elsif ( $data =~ /\S/ ) {
277 0 die "No loose character data is allowed within <array> elements\n";
278 }
279 }
280  
281  
282 0 ▶sub is_parser {
Subroutine is_parser: not called CC=1 SCAR=6.9
283 0 return 1;
284 }
285  
286  
287 0 ▶sub parse_var {
Subroutine parse_var: not called CC=4 SCAR=30.0
288 0 my( $self, $var ) = @_;
289 0 my $last_idx = $self->num_elements - 1;
290
291 0 ▶ $self->{value}[ $last_idx ] = $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 0 0 0
292 0 my $curr_var = $self->{value}[ $last_idx ];
293 0 ▶ return ( ref $curr_var && $curr_var->is_parser ) ? $curr_var : "";
Branch: 1 branch
BranchTrueFalseTotal
ref $curr_var && $curr_var->is_parser ? : 0 0 0
Condition0%0/3
ref $curr_var and $curr_var->is_parser
!ll&&!rl&&r
000
MC/DC0%0/2
ref $curr_var and $curr_var->is_parser
ref $curr_var $curr_var->is_parser
Truth table0%0/3
A and B
A= ref $curr_varB= $curr_var->is_parser
ABresult
0X0
100
111
294 }
295  
296  
297 0 ▶sub push {
Subroutine push: not called CC=2 SCAR=17.9
298 0 my( $self, $element ) = @_;
299
300 die "Number of elements exceeds declared length of <array>\n" if
301 0 ▶ $self->num_elements >= $self->{'length'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->num_elements >= $self->{"length"} 0 0 0
302 0 push @{ $self->{value} }, $element;
303 }
304  
305  
306 0 ▶sub num_elements () {
Subroutine num_elements: not called CC=1 SCAR=6.9
307 0 my( $self ) = @_;
308 0 return scalar @{ $self->{value} };
309 }
« WDDX/Struct.pm ↑ Summary WDDX/Boolean.pm »