Translator/V2.pm

filter: Statementstmt 77.6% 188 / 242 Branchbran 66.6% 16 / 24 Conditioncond 54.1% 13 / 24 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 80.9% 17 / 21 Podpod 100% 16 / 16 totaltotal 74.7% 255 / 341 CC 27 SCAR 15.5
CC
27
Cov
75%
read_untyped_list
8.3
read_typed_list
7.2
read_map_handle
6.1
CRAP 4.7
« Deserializer.pm ↑ Summary Translator/V1.pm »
1 package Hessian::Translator::V2;
2  
3 22 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #
6 22 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Hessian::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Hessian::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Smart::Comments;
10 22 ▶use feature "switch";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 22 has 'string_chunk_prefix' => ( is => 'ro', isa => 'Str', default => 'R' );
13 22 has 'string_final_chunk_prefix' => ( is => 'ro', isa => 'Str', default => 'S' );
14 22 has 'end_of_datastructure_symbol' =>
15 ( is => 'ro', isa => 'Str', default => 'Z' );
16  
17 5 ▶sub read_message_chunk_data { #{{{
Subroutine read_message_chunk_data: called CC=2 SCAR=10.6
Pod documented
18 5 my ( $self, $first_bit ) = @_;
19 5 my $datastructure;
20 ### message chunk data
21 ### first bit: $first_bit
22 5 given ($first_bit) {
23 5 when (/\x48/) { # TOP with version#{{{
24 0 $self->in_interior(0);
25 0 ▶ if ( $self->chunked() ) { # use as hashmap if chunked
Branch: 1 branch
BranchTrueFalseTotal
if ($self->chunked) { } 0 0 0
26 0 my $params = { first_bit => $first_bit };
27 0 $datastructure = $self->deserialize_data($params);
28 }
29 else {
30 ### reading version
31 0 my $hessian_version = $self->read_version();
32 0 $datastructure = { hessian_version => $hessian_version };
33 }
34 }
35  
36 # when /\x43/ { # Hessian Remote Procedure Call
37 # if ( $self->in_interior() ) {
38  
39 # my $params = { first_bit => $first_bit };
40 # $datastructure = $self->deserialize_data($params);
41 # }
42 # else {
43  
44 # # call will need to be dispatched to object designated in some kind of
45 # # service descriptor
46 # my $rpc_data = $self->read_rpc();
47 # $datastructure = { call => $rpc_data };
48  
49 # }
50 # }
51 5 when (/\x45/) { # Envelope
52 0 $datastructure = $self->read_envelope();
53 }
54 5 when (/\x46/) { # Fault
55 0 $self->in_interior(1);
56 0 my $result = $self->deserialize_data();
57 0 my $exception_name = $result->{code};
58 0 my $exception_description = $result->{message};
59 0 $exception_name->throw( error => $exception_description );
60 }
61 5 when (/\x52/) { # Reply
62 0 $self->in_interior(1);
63 0 my $reply_data = $self->deserialize_data();
64 0 $datastructure = { reply_data => $reply_data };
65 }
66 5 default {
67 5 my $params = { first_bit => $first_bit };
68 5 $datastructure = $self->deserialize_data($params);
69 }
70 }
71 5 return $datastructure;
72 }
73  
74 18 ▶sub read_composite_data { #{{{
Subroutine read_composite_data: called CC=2 SCAR=6.9
Pod documented
75 18 my ( $self, $first_bit ) = @_;
76 18 my ( $datastructure, $save_reference );
77 18 given ($first_bit) {
78 18 when (/[\x55\x56\x70-\x77]/) { # typed lists
79 2 push @{ $self->reference_list() }, [];
80 2 $datastructure = $self->read_typed_list( $first_bit, );
81 }
82  
83 16 when (/[\x57\x58\x78-\x7f]/ ){ # untyped lists
84 5 push @{ $self->reference_list() }, [];
85 5 $datastructure = $self->read_untyped_list( $first_bit, );
86 }
87 11 when (/\x48/) {
88 3 push @{ $self->reference_list() }, {
89 };
90 3 $datastructure = $self->read_map_handle();
91 }
92 8 when (/\x4d/) { # typed map
93  
94 1 push @{ $self->reference_list() }, {
95 };
96  
97 # Get the type for this map. This seems to be more like a
98 # perl style object or "blessed hash".
99  
100 1 my $entity_type = $self->read_hessian_chunk();
101 1 my $map_type = $self->store_fetch_type($entity_type);
102 1 my $map = $self->read_map_handle();
103 1 $datastructure = bless $map, $map_type;
104  
105 }
106 7 when (/[\x43\x4f\x60-\x6f]/) {
107 7 ▶ if ( $first_bit !~ /\x43/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $first_bit =~ /\x43/) { } 3 4 7
108 3 push @{ $self->reference_list() }, {
109 };
110 3 $datastructure = $self->read_class_handle( $first_bit, );
111 }
112 else {
113 4 $self->read_class_handle( $first_bit, );
114 4 return;
115 }
116  
117 }
118 }
119  
120 # push @{ $self->reference_list() }, $datastructure
121 # if $save_reference;
122 14 return $datastructure;
123  
124 }
125  
126 2 ▶sub read_typed_list { #{{{
Subroutine read_typed_list: called CC=7 SCAR=19.7
Pod documented
127 2 my ( $self, $first_bit ) = @_;
128 2 my $entity_type = $self->read_hessian_chunk();
129 2 my $type = $self->store_fetch_type($entity_type);
130 2 my $array_length = $self->read_list_length($first_bit);
131 2 my $datastructure = $self->reference_list()->[-1];
132 2 my $index = 0;
133 LISTLOOP:
134 {
135 2 ▶ last LISTLOOP if ( $array_length and ( $index == $array_length ) );
Branch: 1 branch
BranchTrueFalseTotal
if $array_length and $index == $array_length 1 6 7
Condition100%3/3
$array_length and $index == $array_length
!ll&&!rl&&r
421
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
136 6 my $element;
137 6 eval { $element = $self->read_typed_list_element($type); };
138 6 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 1 5 6
139 1 ▶ if ( $e->isa('MessageIncomplete::X')
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("MessageIncomplete::X") or $first_bit =~ /\x55/ and $e->isa("EndOfInput::X")) 1 0 1
Condition33%2/6
$e->isa("MessageIncomplete::X") or $first_bit =~ /\x55/ and $e->isa("EndOfInput::X")
l!l&&r!l&&!r
010
$first_bit =~ /\x55/ and $e->isa("EndOfInput::X")
!ll&&!rl&&r
001
MC/DC0%0/3
$e->isa("MessageIncomplete::X") or $first_bit =~ /\x55/ and $e->isa("EndOfInput::X")
$e->isa("MessageIncomplete::X") $first_bit =~ /\x55/ $e->isa("EndOfInput::X")
Truth table25%1/4
A or B and C
A= $e->isa("MessageIncomplete::X")B= $first_bit =~ /\x55/C= $e->isa("EndOfInput::X")
ABCresult
1XX1
0111
00X0
0100
140 or ( $first_bit =~ /\x55/ and $e->isa('EndOfInput::X') ) )
141 {
142 1 last LISTLOOP;
143 }
144 }
145  
146 5 push @{$datastructure}, $element;
147 5 $index++;
148 5 redo LISTLOOP;
149 }
150 2 return $datastructure;
151 }
152  
153 7 ▶sub read_class_handle { #{{{
Subroutine read_class_handle: called CC=1 SCAR=0.0
Pod documented
154 7 my ( $self, $first_bit ) = @_;
155 7 my ( $save_reference, $datastructure );
156 7 given ($first_bit) {
157 7 when (/\x43/) { # Read class definition
158 4 my $class_type = $self->read_hessian_chunk();
159 4 $class_type =~ s/\./::/g; # get rid of java stuff
160 # Get number of fields
161 4 $self->store_class_definition($class_type);
162  
163 4 return;
164  
165 # $datastructure = $self->store_class_definition($class_type);
166 }
167 3 when (/\x4f/) { # Read hessian data and create instance of class
168 1 $save_reference = 1;
169 1 $datastructure = $self->fetch_class_for_data();
170 }
171 2 when (/[\x60-\x6f]/) { # The class definition is in the ref list
172 2 $save_reference = 1;
173 2 my $hex_bit = unpack 'C*', $first_bit;
174 2 my $class_definition_number = $hex_bit - 0x60;
175 2 $datastructure = $self->instantiate_class($class_definition_number);
176 }
177 }
178  
179 # push @{ $self->reference_list() }, $datastructure
180 # if $save_reference;
181 3 return $datastructure;
182 }
183  
184 4 ▶sub read_map_handle { #{{{
Subroutine read_map_handle: called CC=6 SCAR=18.1
Pod documented
185 4 my $self = shift;
186  
187 # For now only accept integers or strings as keys
188 4 my @key_value_pairs;
189 MAPLOOP:
190 {
191 4 my $key;
192 15 eval { $key = $self->read_hessian_chunk(); };
193 15 ▶ last if not $key or $key eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not $key or $key eq "" 4 11 15
Condition66%2/3
not $key or $key eq ""
l!l&&r!l&&!r
4011
MC/DC50%1/2
not $key or $key eq ""
not $key $key eq ""
Truth table66%2/3
not(A) or B
A= not $keyB= $key eq ""
ABresult
1X1
011
000
194 last MAPLOOP
195 11 ▶ if ( Exception::Class->caught('EndOfInput::X')
Branch: 1 branch
BranchTrueFalseTotal
if "Exception::Class"->caught("EndOfInput::X") or "Exception::Class"->caught("MessageIncomplete::X") 0 11 11
Condition33%1/3
"Exception::Class"->caught("EndOfInput::X") or "Exception::Class"->caught("MessageIncomplete::X")
l!l&&r!l&&!r
0011
MC/DC0%0/2
"Exception::Class"->caught("EndOfInput::X") or "Exception::Class"->caught("MessageIncomplete::X")
"Exception::Class"->caught("EndOfInput::X") "Exception::Class"->caught("MessageIncomplete::X")
Truth table33%1/3
A or B
A= "Exception::Class"->caught("EndOfInput::X")B= "Exception::Class"->caught("MessageIncomplete::X")
ABresult
1X1
011
000
196 or Exception::Class->caught('MessageIncomplete::X') );
197 11 my $value = $self->read_hessian_chunk();
198 11 push @key_value_pairs, $key => $value;
199 11 redo MAPLOOP;
200 }
201  
202 # should throw an exception if @key_value_pairs has an odd number of
203 # elements
204 4 my $hash = {@key_value_pairs};
205 4 my $datastructure = $self->reference_list()->[-1];
206 4 foreach my $key ( keys %{$hash} ) {
207 11 $datastructure->{$key} = $hash->{$key};
208 }
209 4 return $datastructure;
210  
211 }
212  
213 5 ▶sub read_untyped_list { #{{{
Subroutine read_untyped_list: called CC=8 SCAR=21.2
Pod documented
214 5 my ( $self, $first_bit ) = @_;
215 5 my $array_length = $self->read_list_length( $first_bit, );
216 5 my $datastructure = [];
217 5 my $index = 0;
218 LISTLOOP:
219 {
220 5 ▶ last LISTLOOP if ( $array_length and ( $index == $array_length ) );
Branch: 1 branch
BranchTrueFalseTotal
if $array_length and $index == $array_length 1 31 32
Condition100%3/3
$array_length and $index == $array_length
!ll&&!rl&&r
2381
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
221 31 my $element;
222 31 eval { $element = $self->read_hessian_chunk(); };
223 31 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 4 27 31
224 4 ▶ if ( $e->isa('MessageIncomplete::X')
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("MessageIncomplete::X") or $first_bit =~ /\x57/ and $e->isa("EndOfInput::X")) 4 0 4
Condition33%2/6
$e->isa("MessageIncomplete::X") or $first_bit =~ /\x57/ and $e->isa("EndOfInput::X")
l!l&&r!l&&!r
040
$first_bit =~ /\x57/ and $e->isa("EndOfInput::X")
!ll&&!rl&&r
004
MC/DC0%0/3
$e->isa("MessageIncomplete::X") or $first_bit =~ /\x57/ and $e->isa("EndOfInput::X")
$e->isa("MessageIncomplete::X") $first_bit =~ /\x57/ $e->isa("EndOfInput::X")
Truth table25%1/4
A or B and C
A= $e->isa("MessageIncomplete::X")B= $first_bit =~ /\x57/C= $e->isa("EndOfInput::X")
ABCresult
1XX1
0111
00X0
0100
225 or ( $first_bit =~ /\x57/ and $e->isa('EndOfInput::X') ) )
226 {
227 4 last LISTLOOP;
228 }
229 }
230 27 ▶ if ( defined $element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $element) 27 0 27
231 27 push @{$datastructure}, $element;
232 27 $index++;
233  
234 }
235 27 redo LISTLOOP;
236 }
237 5 return $datastructure;
238 }
239  
240 102 ▶sub read_simple_datastructure { #{{{
Subroutine read_simple_datastructure: called CC=1 SCAR=0.0
Pod documented
241 102 my ( $self, $first_bit ) = @_;
242 102 my $element;
243 102 given ($first_bit) {
244 102 when (/\x4e/) { # 'N' for NULL
245 0 $element = undef;
246 }
247 102 when (/[\x46\x54]/ ){ # 'T'rue or 'F'alse
248 2 $element = $self->read_boolean_handle_chunk($first_bit);
249 }
250 100 when (/[\x49\x80-\xbf\xc0-\xcf\xd0-\xd7]/ ){
251 17 $element = $self->read_integer_handle_chunk($first_bit);
252 }
253 83 when (/[\x4c\x59\xd8-\xef\xf0-\xff\x38-\x3f]/) {
254 3 $element = $self->read_long_handle_chunk($first_bit);
255 }
256 80 when (/[\x44\x5b-\x5f]/) {
257 6 $element = $self->read_double_handle_chunk($first_bit);
258 }
259 74 when (/[\x4a\x4b]/) {
260 2 $element = $self->read_date_handle_chunk($first_bit);
261 }
262 72 when (/[\x52\x53\x00-\x1f\x30-\x33]/) { # for version 1: \x73
263 53 $element = $self->read_string_handle_chunk($first_bit);
264 }
265 19 when (/[\x41\x42\x20-\x2f]/) {
266 0 $element = $self->read_binary_handle_chunk($first_bit);
267 }
268 when (/[\x43\x4d\x4f\x48\x55-\x58\x60-\x6f\x70-\x7f]/)
269 19 { # recursive datastructure
270 18 $element = $self->read_composite_datastructure( $first_bit, );
271 }
272 1 when (/\x51/) {
273 1 my $reference_id = $self->read_hessian_chunk();
274 1 $element = $self->reference_list()->[$reference_id];
275  
276 }
277 }
278 102 return $element;
279  
280 }
281  
282 0 ▶sub read_rpc { #{{{
Subroutine read_rpc: not called CC=2 SCAR=17.9
Pod documented
283 0 my $self = shift;
284 0 my $call_data = {};
285 0 my $call_args;
286 0 my $method_name = $self->read_hessian_chunk();
287 0 $call_data->{method} = $method_name;
288 0 my $number_of_args = $self->read_hessian_chunk();
289 0 foreach ( 1 .. $number_of_args ) {
290 0 my $argument = $self->read_hessian_chunk();
291 0 push @{$call_args}, $argument;
292 }
293 0 $call_data->{arguments} = $call_args;
294 0 return $call_data;
295  
296 }
297  
298 2 ▶sub write_hessian_hash { #{{{
Subroutine write_hessian_hash: called CC=2 SCAR=6.9
Pod documented
299 2 my ( $self, $datastructure ) = @_;
300 2 my $anonymous_map_string = "H"; # start an anonymous hash
301 2 foreach my $key ( keys %{$datastructure} ) {
302 6 my $hessian_key = $self->write_scalar_element($key);
303 6 my $value = $datastructure->{$key};
304 6 my $hessian_value = $self->write_hessian_chunk($value);
305 6 $anonymous_map_string .= $hessian_key . $hessian_value;
306 }
307 2 $anonymous_map_string .= $self->end_of_datastructure_symbol();
308 2 return $anonymous_map_string;
309 }
310  
311 1 ▶sub write_typed_map { #{{{
Subroutine write_typed_map: called CC=2 SCAR=6.9
Pod documented
312 1 my ( $self, $datastructure ) = @_;
313 1 my $type = ref $datastructure;
314 1 my $hessian_type = $self->write_scalar_element($type);
315 1 my $typed_map_string = 'M' . $hessian_type;
316  
317 1 foreach my $key ( keys %{$datastructure} ) {
318 3 my $hessian_key = $self->write_scalar_element($key);
319 3 my $value = $datastructure->{$key};
320 3 my $hessian_value = $self->write_hessian_chunk($value);
321 3 $typed_map_string .= $hessian_key . $hessian_value;
322 }
323 1 $typed_map_string .= $self->end_of_datastructure_symbol();
324 1 return $typed_map_string;
325 }
326  
327 2 ▶sub write_hessian_array { #{{{
Subroutine write_hessian_array: called CC=2 SCAR=6.9
Pod documented
328 2 my ( $self, $datastructure ) = @_;
329 2 my $anonymous_array_string = "\x57";
330 2 foreach my $element ( @{$datastructure} ) {
331 5 my $hessian_element = $self->write_hessian_chunk($element);
332 5 $anonymous_array_string .= $hessian_element;
333 }
334 2 $anonymous_array_string .= "Z";
335 2 return $anonymous_array_string;
336 }
337  
338 16 ▶sub write_hessian_string { #{{{
Subroutine write_hessian_string: called CC=1 SCAR=0.0
Pod documented
339 16 my ( $self, $chunks ) = @_;
340 16 return $self->write_string( { chunks => $chunks } );
341  
342 }
343  
344 1 ▶sub write_object { #{{{
Subroutine write_object: called CC=1 SCAR=0.0
Pod documented
345 1 my ( $self, $datastructure ) = @_;
346 1 return $self->write_typed_map($datastructure);
347 # my $type = ref $datastructure;
348 # my @fields = keys %{$datastructure};
349 # my @class_definitions = @{ $self->class_definitions() };
350 # {
351 # ## no critic
352 # no strict 'refs';
353 # push @{ $type . '::ISA' }, 'Hessian::Simple';
354 # ## use critic
355 # }
356 # foreach my $field (@fields) {
357 # $datastructure->meta()->add_attribute( $field, is => 'rw' );
358 # }
359 # my ( $hessian_string, $class_already_stored );
360 # my $index = 0;
361 # foreach my $class_def (@class_definitions) {
362 # my $defined_type = $class_def->{type};
363 # if ( $defined_type eq $type ) {
364 # $class_already_stored = 1;
365 # last;
366 # }
367 # $index++;
368 # }
369 # if ( not $class_already_stored ) {
370 # my $hessian_type = $self->write_scalar_element($type);
371 # $hessian_string = "C" . $hessian_type;
372 # my $num_of_fields = scalar @fields;
373 # $hessian_string .= ( $self->write_scalar_element($num_of_fields) );
374 # foreach my $field (@fields) {
375 # my $hessian_field = $self->write_scalar_element($field);
376 # $hessian_string .= $hessian_field;
377 # }
378 # my $store_definition = { type => $type, fields => \@fields };
379 # push @{ $self->class_definitions() }, $store_definition;
380 # $index = ( scalar @{ $self->class_definitions } ) - 1;
381 # }
382 # $hessian_string .= 'O';
383 # $hessian_string .= ( $self->write_scalar_element($index) );
384 # foreach my $field (@fields) {
385 # my $value = $datastructure->$field();
386  
387 # # my $value = $datastructure->{$field};
388 # $hessian_string .= ( $self->write_scalar_element($value) );
389 # ### hessian_string: $hessian_string
390 # }
391 # return $hessian_string;
392 }
393  
394 0 ▶sub write_referenced_data { #{{{
Subroutine write_referenced_data: not called CC=1 SCAR=6.9
Pod documented
395 0 my ( $self, $index ) = @_;
396 0 my $hessian_string = "\x51";
397 0 my $hessian_index = $self->write_scalar_element($index);
398 0 $hessian_string .= $hessian_index;
399 0 return $hessian_string;
400 }
401  
402 0 ▶sub write_hessian_call { #{{{
Subroutine write_hessian_call: not called CC=2 SCAR=17.9
Pod documented
403 0 my ( $self, $datastructure ) = @_;
404 0 my $hessian_call = "C";
405 0 my $method = $datastructure->{method};
406 0 my $hessian_method = $self->write_scalar_element($method);
407 0 $hessian_call .= $hessian_method;
408 0 my $arguments = $datastructure->{arguments};
409 0 my $num_of_args = scalar @{$arguments};
410 0 my $hessian_num = $self->write_scalar_element($num_of_args);
411 0 $hessian_call .= $hessian_num;
412  
413 0 foreach my $argument ( @{$arguments} ) {
414 0 my $hessian_arg = $self->write_hessian_chunk($argument);
415 0 $hessian_call .= $hessian_arg;
416 }
417 0 return $hessian_call;
418 }
419  
420 0 ▶sub serialize_message { #{{{
Subroutine serialize_message: not called CC=2 SCAR=17.9
Pod documented
421 0 my ( $self, $datastructure ) = @_;
422 0 my $result = $self->write_hessian_message($datastructure);
423 ### result: $result
424 0 ▶ return $result if $self->chunked();
Branch: 1 branch
BranchTrueFalseTotal
if $self->chunked 0 0 0
425 0 my $message = "H\x02\x00" . $result;
426 0 return $message;
427 }
428  
429 22 "one, but we're not the same";
430  
431 __END__
« Deserializer.pm ↑ Summary Translator/V1.pm »