| 1 | package XML::Parser::Lite::Tree::XPath::Result; | ||||||||||
| 2 | |||||||||||
| 3 | 34 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 34 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # | ||||||||||
| 7 | # types: | ||||||||||
| 8 | # | ||||||||||
| 9 | # Error - value is error message string | ||||||||||
| 10 | # number - value is numeric scalar | ||||||||||
| 11 | # boolean - value is boolean scalar | ||||||||||
| 12 | # string - value is string scalar | ||||||||||
| 13 | # nodeset - value is arrayref of nodes and/or attributes | ||||||||||
| 14 | # node - value is node | ||||||||||
| 15 | # attribute - value is attribute | ||||||||||
| 16 | # | ||||||||||
| 17 | |||||||||||
| 18 | 1691 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | 1691 | my $class = shift; | |||||||||
| 20 | 1691 | my $self = bless {}, $class; | |||||||||
| 21 | |||||||||||
| 22 | 1691 | $self->{type} = shift; | |||||||||
| 23 | 1691 | $self->{value} = shift; | |||||||||
| 24 | |||||||||||
| 25 | 1691 | return $self; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 3823 | ▶ | sub is_error { | ||||||||
|
Subroutine
is_error: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 29 | 3823 | my ($self) = @_; | |||||||||
| 30 | 3823 | ▶ | return ($self->{type} eq 'Error') ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 288 | ▶ | sub normalize { | ||||||||
|
Subroutine
normalize: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 34 | 288 | my ($self) = @_; | |||||||||
| 35 | |||||||||||
| 36 | 288 | ▶ | if ($self->{type} eq 'nodeset'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | # uniquify and sort | ||||||||||
| 39 | 288 | my %seen = (); | |||||||||
| 40 | my @tags = sort { | ||||||||||
| 41 | $a->{order} <=> $b->{order} | ||||||||||
| 42 | 1703 | } grep { | |||||||||
| 43 | 1107 | ! $seen{$_->{order}} ++ | |||||||||
| 44 | 288 | } @{$self->{value}}; | |||||||||
| 45 | |||||||||||
| 46 | 288 | $self->{value} = \@tags; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 145 | ▶ | sub ret { | ||||||||
|
Subroutine
ret: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | 145 | my ($self, $a, $b) = @_; | |||||||||
| 52 | 145 | return XML::Parser::Lite::Tree::XPath::Result->new($a, $b); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 179 | ▶ | sub get_type { | ||||||||
|
Subroutine
get_type: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||
| 56 | 179 | my ($self, $type) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 179 | ▶ | return $self if $self->is_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | 179 | ▶ | return $self->get_number if $type eq 'number'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 46 | ▶ | return $self->get_boolean if $type eq 'boolean'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 23 | ▶ | return $self->get_string if $type eq 'string'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 22 | ▶ | return $self->get_nodeset if $type eq 'nodeset'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 3 | ▶ | return $self->get_node if $type eq 'node'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 2 | return $self->ret('Error', "Can't get type '$type'"); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 36 | ▶ | sub get_boolean { | ||||||||
|
Subroutine
get_boolean: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||
| 70 | 36 | my ($self) = @_; | |||||||||
| 71 | |||||||||||
| 72 | 36 | ▶ | return $self if $self->{type} eq 'boolean'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 12 | ▶ | return $self if $self->is_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | |||||||||||
| 75 | 11 | ▶ | if ($self->{type} eq 'number'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 3 | ▶ | return $self->ret('boolean', 0) if $self->{value} eq 'NaN'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 2 | return $self->ret('boolean', $self->{value} != 0); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 8 | ▶ | if ($self->{type} eq 'string'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 2 | return $self->ret('boolean', length $self->{value} > 0); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 6 | ▶ | if ($self->{type} eq 'nodeset'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 4 | return $self->ret('boolean', scalar(@{$self->{value}}) > 0); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 2 | ▶ | if ($self->{type} eq 'node'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | # todo | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 2 | return $self->ret('Error', "can't convert type $self->{type} to boolean"); | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 128 | ▶ | sub get_string { | ||||||||
|
Subroutine
get_string: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||
| 96 | 128 | my ($self) = @_; | |||||||||
| 97 | |||||||||||
| 98 | 128 | ▶ | return $self if $self->{type} eq 'string'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 20 | ▶ | return $self if $self->is_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | 19 | ▶ | if ($self->{type} eq 'nodeset'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 3 | ▶ | return $self->ret('string', '') unless scalar @{$self->{value}}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | 2 | my $node = $self->ret('node', $self->{value}->[0]); | |||||||||
| 106 | |||||||||||
| 107 | 2 | return $node->get_string; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 16 | ▶ | if ($self->{type} eq 'node'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | |||||||||||
| 112 | 7 | ▶ | return $self->ret('string', $self->{value}->{value}) if $self->{value}->{type} eq 'attribute'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 0 | die "can't convert a node of type $self->{value}->{type} to a string"; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 9 | ▶ | if ($self->{type} eq 'number'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 5 | return $self->ret('string', "$self->{value}"); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 4 | ▶ | if ($self->{type} eq 'boolean'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 2 | ▶ | return $self->ret('string', $self->{value} ? 'true' : 'false'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 2 | return $self->ret('Error', "can't convert type $self->{type} to string"); | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 166 | ▶ | sub get_nodeset { | ||||||||
|
Subroutine
get_nodeset: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 129 | 166 | my ($self) = @_; | |||||||||
| 130 | |||||||||||
| 131 | 166 | ▶ | return $self if $self->{type} eq 'nodeset'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 100 | ▶ | return $self if $self->is_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | |||||||||||
| 134 | 99 | ▶ | if ($self->{type} eq 'node'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 94 | return $self->ret('nodeset', [$self->{value}]); | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 5 | return $self->ret('Error', "can't convert type $self->{type} to nodeset"); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 6 | ▶ | sub get_node { | ||||||||
|
Subroutine
get_node: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 142 | 6 | my ($self) = @_; | |||||||||
| 143 | |||||||||||
| 144 | 6 | ▶ | return $self if $self->{type} eq 'node'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 6 | ▶ | return $self if $self->is_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | |||||||||||
| 147 | 5 | return $self->ret('Error', "can't convert type $self->{type} to node"); | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 167 | ▶ | sub get_number { | ||||||||
|
Subroutine
get_number: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||
| 151 | 167 | my ($self) = @_; | |||||||||
| 152 | |||||||||||
| 153 | 167 | ▶ | return $self if $self->{type} eq 'number'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 10 | ▶ | return $self if $self->is_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | |||||||||||
| 156 | 9 | ▶ | if ($self->{type} eq 'string'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 5 | ▶ | if ($self->{value} =~ m!^[\x20\x09\x0D\x0A]*(-?([0-9]+(\.([0-9]+)?)?)|(\.[0-9]+))[\x20\x09\x0D\x0A]*$!){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | |||||||||||
| 159 | 4 | return $self->ret('number', $1); | |||||||||
| 160 | }else{ | ||||||||||
| 161 | 1 | return $self->ret('number', 'NaN'); | |||||||||
| 162 | } | ||||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 4 | ▶ | if ($self->{type} eq 'boolean'){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | 2 | ▶ | return $self->ret('number', $self->{value}?1:0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 2 | return $self->ret('Error', "can't convert type $self->{type} to number"); | |||||||||
| 170 | } | ||||||||||
| 171 | |||||||||||
| 172 | 34 | 1; | |||||||||