| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 14 |  |  | 14 |  | 80 | use strict; | 
|  | 14 |  |  |  |  | 25 |  | 
|  | 14 |  |  |  |  | 468 |  | 
| 2 | 14 |  |  | 14 |  | 79 | use warnings; | 
|  | 14 |  |  |  |  | 26 |  | 
|  | 14 |  |  |  |  | 467 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package Net::Lighthouse::Util; | 
| 5 | 14 |  |  | 14 |  | 7254 | use DateTime; | 
|  | 14 |  |  |  |  | 858796 |  | 
|  | 14 |  |  |  |  | 373 |  | 
| 6 | 14 |  |  | 14 |  | 14931 | use XML::TreePP; | 
|  | 14 |  |  |  |  | 147844 |  | 
|  | 14 |  |  |  |  | 1771 |  | 
| 7 |  |  |  |  |  |  | my $tpp = XML::TreePP->new; | 
| 8 |  |  |  |  |  |  | $tpp->set( xml_decl => '' ); | 
| 9 |  |  |  |  |  |  | $tpp->set( output_encoding => 'UTF-8' ); | 
| 10 |  |  |  |  |  |  | $tpp->set( utf8_flag => 1 ); | 
| 11 |  |  |  |  |  |  | $tpp->set( text_node_key => 'content' ); | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | BEGIN { | 
| 14 | 14 |  |  | 14 |  | 37 | local $@; | 
| 15 | 14 |  |  |  |  | 32 | eval { require YAML::Syck; }; | 
|  | 14 |  |  |  |  | 14187 |  | 
| 16 | 14 | 50 |  |  |  | 35283 | if ($@) { | 
| 17 | 0 |  |  |  |  | 0 | require YAML; | 
| 18 | 0 |  |  |  |  | 0 | *_Load     = *YAML::Load; | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  | else { | 
| 21 | 14 |  |  |  |  | 12024 | *_Load     = *YAML::Syck::Load; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub read_xml { | 
| 26 | 35 |  |  | 35 | 1 | 3478 | my $self = shift; | 
| 27 | 35 |  |  |  |  | 224 | return $tpp->parse( shift ); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub write_xml { | 
| 31 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 32 | 0 |  |  |  |  | 0 | return $tpp->write(shift); | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub translate_from_xml { | 
| 37 | 62 |  |  | 62 | 1 | 532 | my $class = shift; | 
| 38 | 62 |  |  |  |  | 98 | my $ref = shift; | 
| 39 | 62 | 50 |  |  |  | 304 | return unless $ref; | 
| 40 | 62 | 100 |  |  |  | 241 | $ref = Net::Lighthouse::Util->read_xml( $ref ) unless ref $ref; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # remove root | 
| 43 | 62 | 100 |  |  |  | 217730 | if ( keys %$ref == 1 ) { | 
| 44 | 16 |  |  |  |  | 57 | ($ref) = values %$ref; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 62 |  |  |  |  | 364 | %$ref = map { my $new = $_; $new =~ s/-/_/g; $new => $ref->{$_} } keys %$ref; | 
|  | 888 |  |  |  |  | 989 |  | 
|  | 888 |  |  |  |  | 2253 |  | 
|  | 888 |  |  |  |  | 3050 |  | 
| 48 | 62 |  |  |  |  | 577 | for my $k ( keys %$ref ) { | 
| 49 | 888 | 100 |  |  |  | 7144 | $ref->{$k} = '' unless $ref->{$k}; | 
| 50 | 888 | 100 |  |  |  | 2311 | if ( ref $ref->{$k} eq 'HASH' ) { | 
| 51 | 549 | 100 | 66 |  |  | 10888 | if ( $ref->{$k}{-nil} && $ref->{$k}{-nil} eq 'true' ) { | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 50 | 33 |  |  |  |  | 
|  |  | 50 | 33 |  |  |  |  | 
| 52 | 96 |  |  |  |  | 286 | $ref->{$k} = undef; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  | elsif ( $ref->{$k}{-type} && $ref->{$k}{-type} eq 'boolean' ) { | 
| 55 | 53 | 100 |  |  |  | 139 | if ( $ref->{$k}{content} eq 'true' ) { | 
| 56 | 18 |  |  |  |  | 49 | $ref->{$k} = 1; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | else { | 
| 59 | 35 |  |  |  |  | 96 | $ref->{$k} = 0; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | elsif ( $ref->{$k}{-type} && $ref->{$k}{-type} eq 'datetime' ) { | 
| 63 | 99 |  |  |  |  | 925 | $ref->{$k} = | 
| 64 |  |  |  |  |  |  | $class->datetime_from_string( $ref->{$k}{content} ); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | elsif ( $ref->{$k}{-type} && $ref->{$k}{-type} eq 'yaml' ) { | 
| 67 | 7 |  |  |  |  | 52 | $ref->{$k} = _Load( $ref->{$k}{content} ); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | elsif ( $ref->{$k}{-type} && $ref->{$k}{-type} eq 'integer' ) { | 
| 70 | 286 | 100 | 66 |  |  | 1362 | if ( defined $ref->{$k}{content} && $ref->{$k}{content} ne '' ) { | 
| 71 | 280 |  |  |  |  | 1669 | $ref->{$k} = $ref->{$k}{content}; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | else { | 
| 74 | 6 |  |  |  |  | 26 | $ref->{$k} = undef; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 | 8 |  |  |  |  | 55 | elsif ( defined $ref->{$k}{content} ) { | 
| 78 | 0 |  |  |  |  | 0 | $ref->{$k} = $ref->{$k}{content}; | 
| 79 |  |  |  |  |  |  | } | 
| 80 | 8 |  |  |  |  | 64 | elsif ( keys %{ $ref->{$k} } == 0 | 
| 81 |  |  |  |  |  |  | || keys %{ $ref->{$k} } == 1 && exists $ref->{$k}{-type} ) | 
| 82 |  |  |  |  |  |  | { | 
| 83 | 0 |  |  |  |  | 0 | $ref->{$k} = ''; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | } | 
| 87 | 62 |  |  |  |  | 808 | return $ref; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub translate_to_xml { | 
| 91 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 92 | 0 |  |  |  |  | 0 | my $ref  = shift; | 
| 93 | 0 |  |  |  |  | 0 | my %args = @_; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  | 0 | my %normal = map { $_ => 1 } keys %$ref; | 
|  | 0 |  |  |  |  | 0 |  | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 | 0 |  |  |  | 0 | if ( $args{boolean} ) { | 
| 98 | 0 |  |  |  |  | 0 | for my $boolean ( @{ $args{boolean} } ) { | 
|  | 0 |  |  |  |  | 0 |  | 
| 99 | 0 |  |  |  |  | 0 | delete $normal{$_}; | 
| 100 | 0 | 0 |  |  |  | 0 | next unless exists $ref->{$boolean}; | 
| 101 | 0 | 0 |  |  |  | 0 | if ( $ref->{$boolean} ) { | 
| 102 | 0 |  |  |  |  | 0 | $ref->{$boolean} = { content => 'true', -type => 'boolean' }; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  | else { | 
| 105 | 0 |  |  |  |  | 0 | $ref->{$boolean} = { content => 'false', -type => 'boolean' }; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 0 |  |  |  |  | 0 | for my $normal ( keys %normal ) { | 
| 111 | 0 | 0 |  |  |  | 0 | next unless exists $ref->{$normal}; | 
| 112 | 0 |  |  |  |  | 0 | $ref->{$normal} = { content => $ref->{$normal} }; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 0 | 0 |  |  |  | 0 | $ref = { $args{root} => $ref } if $args{root}; | 
| 116 | 0 |  |  |  |  | 0 | return Net::Lighthouse::Util->write_xml($ref); | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | sub datetime_from_string { | 
| 120 | 99 |  |  | 99 | 1 | 151 | my $class  = shift; | 
| 121 | 99 |  |  |  |  | 157 | my $string = shift; | 
| 122 | 99 | 100 |  |  |  | 251 | return unless $string; | 
| 123 | 94 | 50 |  |  |  | 669 | if ( $string =~ | 
| 124 |  |  |  |  |  |  | /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|[+-]\d{2}:\d{2})/ ) | 
| 125 |  |  |  |  |  |  | { | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | #    2009-06-01T13:00:10Z | 
| 128 | 94 | 100 |  |  |  | 755 | my $dt = DateTime->new( | 
| 129 |  |  |  |  |  |  | year      => $1, | 
| 130 |  |  |  |  |  |  | month     => $2, | 
| 131 |  |  |  |  |  |  | day       => $3, | 
| 132 |  |  |  |  |  |  | hour      => $4, | 
| 133 |  |  |  |  |  |  | minute    => $5, | 
| 134 |  |  |  |  |  |  | second    => $6, | 
| 135 |  |  |  |  |  |  | time_zone => $7 eq 'Z' ? 'UTC' : $7, | 
| 136 |  |  |  |  |  |  | ); | 
| 137 | 94 |  |  |  |  | 40870 | $dt->set_time_zone( 'UTC' ); | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | 1; | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | __END__ |