Translator/V1.pm

filter: Statementstmt 87.8% 252 / 287 Branchbran 78.9% 30 / 38 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 91.3% 21 / 23 Podpod 100% 18 / 18 totaltotal 87.2% 328 / 376 CC 27 SCAR 12.9
CC
27
Cov
87%
read_map_handle
7.0
read_v1_type
5.2
read_typed_list
5.0
CRAP 3.6
« Translator/V2.pm ↑ Summary Translator.pm »
1 package Hessian::Translator::V1;
2  
3 21 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #
6 21 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Hessian::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Hessian::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use feature "switch";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #use Smart::Comments;
11  
12 21 has 'string_chunk_prefix' => ( is => 'ro', isa => 'Str', default => 's' );
13 21 has 'string_final_chunk_prefix' => ( is => 'ro', isa => 'Str', default => 'S' );
14 21 has 'end_of_datastructure_symbol' => (is => 'ro', isa => 'Str', default => 'z');
15  
16 7 ▶sub read_message_chunk_data { #{{{
Subroutine read_message_chunk_data: called CC=3 SCAR=14.2
Pod documented
17 7 my ( $self, $first_bit ) = @_;
18 7 my $input_handle = $self->input_handle();
19 7 my $datastructure;
20 7 given ($first_bit) {
21 7 when (/\x63/) { # version 1 call
22 1 my $hessian_version = $self->read_version();
23 1 my $rpc_data = $self->read_rpc();
24 1 $datastructure = {
25 hessian_version => $hessian_version,
26 call => $rpc_data
27 };
28  
29 }
30 6 when (/\x66/) { # version 1 fault
31 0 my @tokens;
32 0 eval {
33 0 ▶ while ( my $token = $self->deserialize_data() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->deserialize_data) 0 0 0
34 {
35 0 push @tokens, $token;
36 }
37 };
38 0 ▶ if ( Exception::Class->caught('EndOfInput::X') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Exception::Class"->caught("EndOfInput::X")) 0 0 0
39 0 my $exception_name = $tokens[1];
40 0 my $exception_description = $tokens[3];
41 0 $exception_name->throw( error => $exception_description );
42 }
43 }
44 6 when (/\x72/) { # version 1 reply
45 0 my $hessian_version = $self->read_version();
46 0 $datastructure =
47 { hessian_version => $hessian_version, state => 'reply' };
48 }
49 6 default {
50 6 my $param = { first_bit => $first_bit };
51 6 $datastructure = $self->deserialize_data($param);
52 }
53 }
54 7 return $datastructure;
55  
56 }
57  
58 14 ▶sub read_composite_data { #{{{
Subroutine read_composite_data: called CC=1 SCAR=0.0
Pod documented
59 14 my ( $self, $first_bit ) = @_;
60 ### read_composite_data
61 14 my $input_handle = $self->input_handle();
62 14 my ( $datastructure, $save_reference );
63 14 given ($first_bit) {
64 14 when (/\x72/ ){
65 1 $datastructure = $self->read_remote_object();
66 }
67 13 when (/[\x56\x76]/) { # typed lists
68 4 push @{ $self->reference_list() }, [];
69 4 $datastructure = $self->read_typed_list($first_bit);
70 }
71 9 when (/\x4d/) { # typed map
72 7 push @{ $self->reference_list() }, {
73 };
74 7 $datastructure = $self->read_map_handle();
75 }
76 2 when (/[\x4f\x6f]/) { # object definition or reference
77 2 push @{ $self->reference_list() }, {
78 };
79 2 $datastructure = $self->read_class_handle( $first_bit, );
80 }
81 }
82 14 return $datastructure;
83  
84 }
85  
86 4 ▶sub read_typed_list { #{{{
Subroutine read_typed_list: called CC=5 SCAR=16.1
Pod documented
87 4 my ( $self, $first_bit ) = @_;
88 ### read_typed_list
89 4 my $input_handle = $self->input_handle();
90 4 my $v1_type = $self->read_v1_type($first_bit);
91 4 my ( $entity_type, $next_bit ) = @{$v1_type}{qw/type next_bit/};
92 4 ▶ return $self->read_untyped_list($next_bit) unless defined $entity_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entity_type 3 1 4
93  
94 1 my $type = $self->store_fetch_type($entity_type);
95 1 my $array_length;
96 1 ▶ read $input_handle, $next_bit, 1 unless $next_bit;
Branch: 1 branch
BranchTrueFalseTotal
unless $next_bit 1 0 1
97 1 ▶ if ( $next_bit eq 'l' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_bit eq "l") 1 0 1
98 1 $array_length = $self->read_list_length($next_bit);
99 }
100  
101 1 my $datastructure = $self->reference_list()->[-1];
102 1 my $index = 0;
103 LISTLOOP:
104 {
105  
106 # last LISTLOOP if ( $array_length and ( $index == $array_length ) );
107 1 my $element;
108 3 eval { $element = $self->read_typed_list_element($type); };
109 3 ▶ last LISTLOOP if Exception::Class->caught('EndOfInput::X');
Branch: 1 branch
BranchTrueFalseTotal
if "Exception::Class"->caught("EndOfInput::X") 1 2 3
110 2 push @{$datastructure}, $element;
111 2 $index++;
112 2 redo LISTLOOP;
113 }
114 1 return $datastructure;
115 }
116  
117 1 ▶sub read_remote_object { #{{{
Subroutine read_remote_object: called CC=1 SCAR=0.0
Pod documented
118 1 my $self = shift;
119 ### read_remote_object
120 1 my $input_handle = $self->input_handle();
121 1 my $remote_type = $self->read_v1_type()->{type};
122 1 $remote_type =~ s/\./::/g;
123 1 my $class_definition = {
124 type => $remote_type,
125 fields => ['remote_url']
126 };
127 ### class definition: Dump($class_definition)
128 1 return $self->assemble_class(
129 {
130 type => $remote_type,
131 data => {},
132 class_def => $class_definition
133 }
134 );
135 }
136  
137 12 ▶sub read_v1_type { #{{{
Subroutine read_v1_type: called CC=5 SCAR=16.5
Pod documented
138 12 my ( $self, $list_bit ) = @_;
139 ### read_v1_type
140 12 my ( $type, $first_bit, $array_length );
141 12 my $input_handle = $self->input_handle();
142 12 ▶ if ( $list_bit and $list_bit =~ /\x76/ ) { # v
Branch: 1 branch
BranchTrueFalseTotal
if ($list_bit and $list_bit =~ /\x76/) { } 0 12 12
Condition66%2/3
$list_bit and $list_bit =~ /\x76/
!ll&&!rl&&r
840
MC/DC0%0/2
$list_bit and $list_bit =~ /\x76/
$list_bit $list_bit =~ /\x76/
Truth table66%2/3
A and B
A= $list_bitB= $list_bit =~ /\x76/
ABresult
0X0
100
111
143 0 read $input_handle, $type, 1;
144 0 read $input_handle, $array_length, 1;
145 }
146 else {
147 12 read $input_handle, $first_bit, 1;
148 12 ▶ if ( $first_bit =~ /t/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_bit =~ /t/) 6 6 12
149 6 $type = $self->read_hessian_chunk( { first_bit => 'S' } );
150 }
151 }
152 ### found type: $type
153 12 ▶ return { type => $type, next_bit => $array_length } if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 6 6 12
154 6 return { next_bit => $first_bit };
155 }
156  
157 2 ▶sub read_class_handle { #{{{
Subroutine read_class_handle: called CC=1 SCAR=0.0
Pod documented
158 2 my ( $self, $first_bit ) = @_;
159 ### read_class_handle
160 2 my $input_handle = $self->input_handle();
161 2 my ( $save_reference, $datastructure );
162 2 given ($first_bit) {
163 2 when (/\x4f/) { # Read class definition
164 1 my $class_name_length = $self->read_hessian_chunk();
165 1 my $class_type;
166 1 read $input_handle, $class_type, $class_name_length;
167  
168 1 $class_type =~ s/\./::/g; # get rid of java stuff
169 # Get number of fields
170 1 $datastructure = $self->store_class_definition($class_type);
171 }
172 1 when (/\x6f/) { # The class definition is in the ref list
173 1 $save_reference = 1;
174 1 $datastructure = $self->fetch_class_for_data();
175 }
176 }
177  
178 2 return $datastructure;
179 }
180  
181 7 ▶sub read_map_handle { #{{{
Subroutine read_map_handle: called CC=7 SCAR=19.5
Pod documented
182 7 my $self = shift;
183 ### read_map_handle
184 7 my $input_handle = $self->input_handle();
185 7 my $v1_type = $self->read_v1_type();
186 7 my ( $entity_type, $next_bit ) = @{$v1_type}{qw/type next_bit/};
187 7 my $type;
188 7 ▶ $type = $self->store_fetch_type($entity_type) if $entity_type;
Branch: 1 branch
BranchTrueFalseTotal
if $entity_type 4 3 7
189 7 my $key;
190 7 ▶ if ($next_bit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_bit) 3 4 7
191 3 $key = $self->read_hessian_chunk( { first_bit => $next_bit } );
192 }
193  
194 # For now only accept integers or strings as keys
195 7 my @key_value_pairs;
196 MAPLOOP:
197 {
198 7 ▶ eval { $key = $self->read_hessian_chunk(); } unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 24 3 27
199 27 ▶ last MAPLOOP if Exception::Class->caught('EndOfInput::X');
Branch: 1 branch
BranchTrueFalseTotal
if "Exception::Class"->caught("EndOfInput::X") 7 20 27
200 20 my $value = $self->read_hessian_chunk();
201 20 push @key_value_pairs, $key => $value;
202 20 undef $key;
203 20 redo MAPLOOP;
204 }
205  
206 # should throw an exception if @key_value_pairs has an odd number of
207 # elements
208  
209 7 my $datastructure = $self->reference_list()->[-1];
210 7 my $hash = {@key_value_pairs};
211 7 foreach my $key ( keys %{$hash} ) {
212 20 $datastructure->{$key} = $hash->{$key};
213 }
214 7 ▶ my $map = defined $type ? bless $datastructure => $type : $datastructure;
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 4 3 7
215 7 return $map;
216  
217 }
218  
219 3 ▶sub read_untyped_list { #{{{
Subroutine read_untyped_list: called CC=5 SCAR=16.1
Pod documented
220 3 my ( $self, $first_bit ) = @_;
221 ### read_untyped_list
222 3 my $input_handle = $self->input_handle();
223 3 my $array_length;
224 3 my $datastructure = $self->reference_list()->[-1];
225 3 my $index = 0;
226 3 ▶ if ( $first_bit eq 'l' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_bit eq 'l') { } 1 2 3
227 1 $array_length = $self->read_list_length( $first_bit, );
228 }
229 else {
230 2 my $param = { first_bit => $first_bit };
231 2 my $first_element = $self->read_hessian_chunk($param);
232 2 push @{$datastructure}, $first_element;
233 2 $index++;
234 }
235 LISTLOOP:
236 {
237 3 ▶ last LISTLOOP if ( $array_length and ( $index == $array_length ) );
Branch: 1 branch
BranchTrueFalseTotal
if $array_length and $index == $array_length 1 7 8
Condition100%3/3
$array_length and $index == $array_length
!ll&&!rl&&r
521
MC/DC100%2/2
$array_length and $index == $array_length
$array_length $index == $array_length
Truth table100%3/3
A and B
A= $array_lengthB= $index == $array_length
ABresult
0X0
100
111
238 7 my $element;
239 7 eval { $element = $self->read_hessian_chunk(); };
240 last LISTLOOP
241 7 ▶ if Exception::Class->caught('EndOfInput::X');
Branch: 1 branch
BranchTrueFalseTotal
if "Exception::Class"->caught("EndOfInput::X") 2 5 7
242  
243 5 push @{$datastructure}, $element;
244 5 $index++;
245 5 redo LISTLOOP;
246 }
247 3 return $datastructure;
248 }
249  
250 76 ▶sub read_simple_datastructure { #{{{
Subroutine read_simple_datastructure: called CC=1 SCAR=0.1
Pod documented
251 76 my ( $self, $first_bit ) = @_;
252 ### read_simple_datastructure V1 translator
253 ### first bit: $first_bit
254 76 my $input_handle = $self->input_handle();
255 76 my $element;
256 76 given ($first_bit) {
257 76 when (/\x00/) {
258 0 $element = $self->read_hessian_chunk();
259 }
260 76 when (/\x4e/) { # 'N' for NULL
261 0 $element = undef;
262 }
263 76 when (/[\x46\x54]/) { # 'T'rue or 'F'alse
264 0 $element = $self->read_boolean_handle_chunk($first_bit);
265 }
266 76 when (/[\x49\x80-\xaf\xc0-\xcf\xd0-\xd7]/) {
267 19 $element = $self->read_integer_handle_chunk($first_bit);
268 }
269 57 when (/[\x4c\xd8-\xef\xf0-\xff\x38-\x3f]/) {
270 0 $element = $self->read_long_handle_chunk($first_bit);
271 }
272 57 when (/\x44/) {
273 0 $element = $self->read_double_handle_chunk($first_bit);
274 }
275 57 when (/\x64/ ){
276 1 $element = $self->read_date_handle_chunk($first_bit);
277 }
278 56 when (/[\x53\x58\x73\x78\x00-\x0f]/) { # for version 1: \x73
279 40 $element = $self->read_string_handle_chunk($first_bit);
280 }
281 16 when (/[\x42\x62]/) {
282 0 $element = $self->read_binary_handle_chunk($first_bit);
283 }
284 16 when (/[\x4d\x4f\x56\x6f\x72\x76]/) { # recursive datastructure
285 14 $element = $self->read_composite_datastructure( $first_bit, );
286 }
287 2 when (/\x52/) {
288 1 my $reference_id = $self->read_integer_handle_chunk('I');
289 1 $element = $self->reference_list()->[$reference_id];
290 }
291 1 when (/[\x48\x6d]/) { # a header or method name
292 1 $element = $self->read_string_handle_chunk('S');
293 }
294 }
295 76 binmode( $input_handle, 'bytes' );
296 76 return $element;
297  
298 }
299  
300 0 ▶sub read_list_type { #{{{
Subroutine read_list_type: not called CC=1 SCAR=6.9
Pod documented
301 ### read_list_type
302 0 my $self = shift;
303 0 my $input_handle = $self->input_handle();
304 0 my $type_length;
305 0 read $input_handle, $type_length, 1;
306 0 my $type = $self->read_string_handle_chunk( $type_length, $input_handle );
307 0 binmode( $input_handle, 'bytes' );
308 0 return $type;
309 }
310  
311 1 ▶sub read_rpc { #{{{
Subroutine read_rpc: called CC=3 SCAR=11.2
Pod documented
312 ### read_rpc
313 1 my $self = shift;
314 1 my $input_handle = $self->input_handle();
315 1 my $call_data = {};
316 1 my $call_args;
317 my $in_header;
318 RPCSTRUCTURE: {
319 1 my $first_bit;
320 4 read $input_handle, $first_bit, 1;
321 4 my $element;
322 4 eval {
323 4 $element = $self->read_hessian_chunk( { first_bit => $first_bit } );
324 };
325 4 ▶ last RPCSTRUCTURE if Exception::Class->caught('EndOfInput::X');
Branch: 1 branch
BranchTrueFalseTotal
if "Exception::Class"->caught("EndOfInput::X") 1 3 4
326 3 given ($first_bit) {
327 3 when (/\x6d/) {
328 1 $in_header = 0;
329 1 $call_data->{method} = $element;
330 }
331 2 when (/\x48/) {
332 0 $in_header = 1;
333 0 push @{ $call_data->{headers} }, { header => $element };
334 }
335  
336 2 default {
337 2 ▶ if ($in_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_header) { } 0 2 2
338 0 push @{ $call_data->{headers}->[-1]->{elements} }, $element;
339 }
340 else {
341 2 push @{$call_args}, $element;
342 }
343 }
344 }
345 3 redo RPCSTRUCTURE;
346 }
347 1 $call_data->{arguments} = $call_args;
348 1 return $call_data;
349 }
350  
351 2 ▶sub write_hessian_hash { #{{{
Subroutine write_hessian_hash: called CC=2 SCAR=6.9
Pod documented
352 ### write_hessian_hash
353 2 my ( $self, $datastructure ) = @_;
354 2 my $anonymous_map_string = "M"; # start an anonymous hash
355 2 foreach my $key ( keys %{$datastructure} ) { ### writing hash... done
356 6 my $hessian_key = $self->write_scalar_element($key);
357 6 my $value = $datastructure->{$key};
358 6 my $hessian_value = $self->write_hessian_chunk($value);
359 6 $anonymous_map_string .= $hessian_key . $hessian_value;
360 }
361 2 $anonymous_map_string .= $self->end_of_datastructure_symbol();
362 2 return $anonymous_map_string;
363 }
364  
365 2 ▶sub write_hessian_array { #{{{
Subroutine write_hessian_array: called CC=2 SCAR=6.9
Pod documented
366 ### write_hessian_array
367 2 my ( $self, $datastructure ) = @_;
368 2 my $anonymous_array_string = "V";
369 2 foreach my $element ( @{$datastructure} ) { ### writing array... done
370 5 my $hessian_element = $self->write_hessian_chunk($element);
371 5 $anonymous_array_string .= $hessian_element;
372 }
373 2 $anonymous_array_string .= "z";
374 2 return $anonymous_array_string;
375 }
376  
377 19 ▶sub write_hessian_string { #{{{
Subroutine write_hessian_string: called CC=1 SCAR=0.0
Pod documented
378 ### write_hessian_string
379 19 my ( $self, $chunks ) = @_;
380 19 return $self->write_string( { chunks => $chunks } );
381  
382 }
383  
384 #sub write_hessian_date { #{{{
385 # my ( $self, $datetime ) = @_;
386 # my $epoch = $datetime->epoch();
387 # return $self->write_date( $epoch, 'd' );
388 #}
389  
390 3 ▶sub write_hessian_call { #{{{
Subroutine write_hessian_call: called CC=2 SCAR=6.9
Pod documented
391 ### write_hessian_call
392 3 my ( $self, $datastructure ) = @_;
393 3 my $hessian_call = "c\x01\x00";
394 3 my $method = $datastructure->{method};
395 3 my $hessian_method = $self->write_scalar_element($method);
396 3 $hessian_method =~ s/^S/m/;
397 3 $hessian_call .= $hessian_method;
398 3 my $arguments = $datastructure->{arguments};
399 3 foreach my $argument ( @{$arguments} ) { ### writing call... done
400 2 my $hessian_arg = $self->write_hessian_chunk($argument);
401 2 $hessian_call .= $hessian_arg;
402 }
403 3 $hessian_call .= "z";
404 3 return $hessian_call;
405 }
406  
407 1 ▶sub write_object { #{{{
Subroutine write_object: called CC=2 SCAR=6.9
Pod documented
408 ### write_object
409 1 my ($self , $datastructure) = @_;
410 1 my $type = ref $datastructure;
411 1 my $hessian_string = "\x4d";
412 1 my $hessian_type = $self->write_scalar_element($type);
413 1 $hessian_type =~ s/^S/t/;
414 1 $hessian_string .= $hessian_type;
415 1 my @fields = keys %{$datastructure};
416 1 foreach my $field (@fields) { ### object fields... done
417 3 my $hessian_field = $self->write_scalar_element($field);
418 3 my $value = $datastructure->{$field};
419 3 my $hessian_value = $self->write_hessian_chunk($value);
420 3 $hessian_string .= $hessian_field . $hessian_value;
421 }
422 1 $hessian_string .= "z";
423 1 return $hessian_string;
424  
425 }
426  
427 0 ▶sub write_referenced_data { #{{{
Subroutine write_referenced_data: not called CC=1 SCAR=6.9
Pod documented
428 ### write_referenced_data
429 0 my ( $self, $index) = @_;
430 0 my $hessian_string = "R";
431 # Bypass write integer for now
432 0 my $new_int = pack 'N', $index;
433 0 $hessian_string .= $new_int;
434 0 return $hessian_string;
435 }
436  
437 3 ▶sub serialize_message { #{{{
Subroutine serialize_message: called CC=1 SCAR=0.0
Pod documented
438 ### serialize_message
439 3 my ( $self, $datastructure ) = @_;
440 3 my $result = $self->write_hessian_message($datastructure);
441 3 return $result;
442 }
443  
444 21 "one, but we're not the same";
445  
446 __END__
« Translator/V2.pm ↑ Summary Translator.pm »