| 1 | # $Id: /tree-xpathengine/trunk/lib/Tree/XPathEngine/Function.pm 26 2006-02-15T15:46:06.515200Z mrodrigu $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Tree::XPathEngine::Function; | ||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Tree::XPathEngine::Number; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Tree::XPathEngine::Literal; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Tree::XPathEngine::Boolean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Tree::XPathEngine::NodeSet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 46 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 46 | my $class = shift; | |||||||||||||||||||||||||||
| 12 | 46 | my ($pp, $name, $params) = @_; | |||||||||||||||||||||||||||
| 13 | 46 | bless { | |||||||||||||||||||||||||||
| 14 | pp => $pp, | ||||||||||||||||||||||||||||
| 15 | name => $name, | ||||||||||||||||||||||||||||
| 16 | params => $params | ||||||||||||||||||||||||||||
| 17 | }, $class; | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | 0 | my $string = $self->{name} . "("; | |||||||||||||||||||||||||||
| 23 | 0 | my $second; | |||||||||||||||||||||||||||
| 24 | 0 | foreach (@{$self->{params}}) { | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | $string .= "," if $second++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | $string .= $_->as_string; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 0 | $string .= ")"; | |||||||||||||||||||||||||||
| 29 | 0 | return $string; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 144 | ▶ | sub evaluate { | ||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 144 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 144 | my $node = shift; | |||||||||||||||||||||||||||
| 35 | 144 | ▶ | if ($node->isa('Tree::XPathEngine::NodeSet')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $node = $node->get_node(1); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 144 | my @params; | |||||||||||||||||||||||||||
| 39 | 144 | foreach my $param (@{$self->{params}}) { | |||||||||||||||||||||||||||
| 40 | 54 | my $results = $param->evaluate($node); | |||||||||||||||||||||||||||
| 41 | 54 | push @params, $results; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 144 | $self->_execute($self->{name}, $node, @params); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 144 | ▶ | sub _execute { | ||||||||||||||||||||||||||
|
Subroutine
_execute: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 144 | my $self = shift; | |||||||||||||||||||||||||||
| 48 | 144 | my ($name, $node, @params) = @_; | |||||||||||||||||||||||||||
| 49 | 144 | $name =~ s/-/_/g; | |||||||||||||||||||||||||||
| 50 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 144 | $self->$name($node, @params); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # All functions should return one of: | ||||||||||||||||||||||||||||
| 55 | # Tree::XPathEngine::Number | ||||||||||||||||||||||||||||
| 56 | # Tree::XPathEngine::Literal (string) | ||||||||||||||||||||||||||||
| 57 | # Tree::XPathEngine::NodeSet | ||||||||||||||||||||||||||||
| 58 | # Tree::XPathEngine::Boolean | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | ### NODESET FUNCTIONS ### | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | sub last { | ||||||||||||||||||||||||||
|
Subroutine
last: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 3 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 65 | 3 | ▶ | die "last: function doesn't take parameters\n" if (@params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 2 | return Tree::XPathEngine::Number->new($self->{pp}->_get_context_size); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | sub position { | ||||||||||||||||||||||||||
|
Subroutine
position: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 4 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 72 | 4 | ▶ | if (@params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | die "position: function doesn't take parameters [ ", @params, " ]\n"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | # return pos relative to axis direction | ||||||||||||||||||||||||||||
| 76 | 3 | return Tree::XPathEngine::Number->new($self->{pp}->_get_context_pos); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 81 | 2 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 82 | 2 | ▶ | die "count: Parameter must be a NodeSet\n" unless $params[0]->isa('Tree::XPathEngine::NodeSet'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | return Tree::XPathEngine::Number->new($params[0]->size); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub id { | ||||||||||||||||||||||||||
|
Subroutine
id: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 0 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | die "id: Function takes 1 parameter\n" unless @params == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | my $results = Tree::XPathEngine::NodeSet->new(); | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($params[0]->isa('Tree::XPathEngine::NodeSet')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | # result is the union of applying id() to the | ||||||||||||||||||||||||||||
| 93 | # string value of each node in the nodeset. | ||||||||||||||||||||||||||||
| 94 | 0 | foreach my $node ($params[0]->get_nodelist) { | |||||||||||||||||||||||||||
| 95 | 0 | my $string = $node->xpath_string_value; | |||||||||||||||||||||||||||
| 96 | 0 | $results->append($self->id($node, Tree::XPathEngine::Literal->new($string))); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { # The actual id() function... | ||||||||||||||||||||||||||||
| 100 | 0 | my $string = $self->string($node, $params[0]); | |||||||||||||||||||||||||||
| 101 | 0 | $_ = $string->value; # get perl scalar | |||||||||||||||||||||||||||
| 102 | 0 | my @ids = split; # splits $_ | |||||||||||||||||||||||||||
| 103 | 0 | foreach my $id (@ids) { | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if (my $found = $node->get_element_by_id($id)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | $results->push($found); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 0 | return $results; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub name { | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 0 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (@params > 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 116 | 0 | die "name() function takes one or no parameters\n"; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | elsif (@params) { | ||||||||||||||||||||||||||||
| 119 | 0 | my $nodeset = shift(@params); | |||||||||||||||||||||||||||
| 120 | 0 | $node = $nodeset->get_node(1); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | return Tree::XPathEngine::Literal->new($node->xpath_get_name); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | ### STRING FUNCTIONS ### | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 101 | ▶ | sub string { | ||||||||||||||||||||||||||
|
Subroutine
string: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 101 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | 101 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 131 | 101 | ▶ | die "string: Too many parameters\n" if @params > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 101 | ▶ | if (@params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | return Tree::XPathEngine::Literal->new($params[0]->xpath_string_value); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # TODO - this MUST be wrong! - not sure now. -matt | ||||||||||||||||||||||||||||
| 137 | 101 | return Tree::XPathEngine::Literal->new($node->xpath_string_value); | |||||||||||||||||||||||||||
| 138 | # default to nodeset with just $node in. | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | sub concat { | ||||||||||||||||||||||||||
|
Subroutine
concat: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | 1 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 144 | 1 | ▶ | die "concat: Too few parameters\n" if @params < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | my $string = join('', map {$_->xpath_string_value} @params); | |||||||||||||||||||||||||||
| 146 | 1 | return Tree::XPathEngine::Literal->new($string); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | ▶ | sub starts_with { | ||||||||||||||||||||||||||
|
Subroutine
starts_with: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 151 | 2 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 152 | 2 | ▶ | die "starts-with: incorrect number of params\n" unless @params == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 2 | my ($string1, $string2) = ($params[0]->xpath_string_value, $params[1]->xpath_string_value); | |||||||||||||||||||||||||||
| 154 | 2 | ▶ | if (substr($string1, 0, length($string2)) eq $string2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | return Tree::XPathEngine::Boolean->_true; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 1 | return Tree::XPathEngine::Boolean->_false; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 2 | ▶ | sub contains { | ||||||||||||||||||||||||||
|
Subroutine
contains: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 162 | 2 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 163 | 2 | ▶ | die "starts-with: incorrect number of params\n" unless @params == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 2 | my $value = $params[1]->xpath_string_value; | |||||||||||||||||||||||||||
| 165 | 2 | ▶ | if ($params[0]->xpath_string_value =~ /\Q$value\E/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 1 | return Tree::XPathEngine::Boolean->_true; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 1 | return Tree::XPathEngine::Boolean->_false; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | ▶ | sub substring_before { | ||||||||||||||||||||||||||
|
Subroutine
substring_before: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | 3 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 174 | 3 | ▶ | die "starts-with: incorrect number of params\n" unless @params == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 3 | my $long = $params[0]->xpath_string_value; | |||||||||||||||||||||||||||
| 176 | 3 | my $short= $params[1]->xpath_string_value; | |||||||||||||||||||||||||||
| 177 | 3 | ▶ | if( $long=~ m{^(.*?)\Q$short}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 1 | return Tree::XPathEngine::Literal->new($1); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||
| 181 | 2 | return Tree::XPathEngine::Literal->new(''); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | ▶ | sub substring_after { | ||||||||||||||||||||||||||
|
Subroutine
substring_after: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 1 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 188 | 1 | ▶ | die "starts-with: incorrect number of params\n" unless @params == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 1 | my $long = $params[0]->xpath_string_value; | |||||||||||||||||||||||||||
| 190 | 1 | my $short= $params[1]->xpath_string_value; | |||||||||||||||||||||||||||
| 191 | 1 | ▶ | if( $long=~ m{\Q$short\E(.*)$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 1 | return Tree::XPathEngine::Literal->new($1); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | else { | ||||||||||||||||||||||||||||
| 195 | 0 | return Tree::XPathEngine::Literal->new(''); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 3 | ▶ | sub substring { | ||||||||||||||||||||||||||
|
Subroutine
substring: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 202 | 3 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 203 | 3 | ▶ | die "substring: Wrong number of parameters\n" if (@params < 2 || @params > 3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@params < 2 or @params > 3
MC/DC0%0/2
@params < 2 or @params > 3
@params < 2
@params > 3
Truth table33%1/3
A or B
A= @params < 2B= @params > 3
| |||||||||||||||||||||||||||||
| 204 | 3 | my ($str, $offset, $len); | |||||||||||||||||||||||||||
| 205 | 3 | $str = $params[0]->xpath_string_value; | |||||||||||||||||||||||||||
| 206 | 3 | $offset = $params[1]->value; | |||||||||||||||||||||||||||
| 207 | 3 | $offset--; # uses 1 based offsets | |||||||||||||||||||||||||||
| 208 | 3 | ▶ | if (@params == 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 2 | $len = $params[2]->value; | |||||||||||||||||||||||||||
| 210 | 2 | return Tree::XPathEngine::Literal->new(substr($str, $offset, $len)); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | else { | ||||||||||||||||||||||||||||
| 213 | 1 | return Tree::XPathEngine::Literal->new(substr($str, $offset)); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 2 | ▶ | sub string_length { | ||||||||||||||||||||||||||
|
Subroutine
string_length: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 219 | 2 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 220 | 2 | ▶ | die "string-length: Wrong number of params\n" if @params > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 2 | ▶ | if (@params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 2 | return Tree::XPathEngine::Number->new(length($params[0]->xpath_string_value)); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||
| 225 | 0 | return Tree::XPathEngine::Number->new( | |||||||||||||||||||||||||||
| 226 | length($node->xpath_string_value) | ||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 4 | ▶ | sub normalize_space { | ||||||||||||||||||||||||||
|
Subroutine
normalize_space: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 233 | 4 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 234 | 4 | ▶ | die "normalize-space: Wrong number of params\n" if @params > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 4 | my $str; | |||||||||||||||||||||||||||
| 236 | 4 | ▶ | if (@params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 4 | $str = $params[0]->xpath_string_value; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | else { | ||||||||||||||||||||||||||||
| 240 | 0 | $str = $node->xpath_string_value; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | 4 | $str =~ s/^\s*//; | |||||||||||||||||||||||||||
| 243 | 4 | $str =~ s/\s*$//; | |||||||||||||||||||||||||||
| 244 | 4 | $str =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 245 | 4 | return Tree::XPathEngine::Literal->new($str); | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 3 | ▶ | sub translate { | ||||||||||||||||||||||||||
|
Subroutine
translate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 249 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 250 | 3 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 251 | 3 | ▶ | die "translate: Wrong number of params\n" if @params != 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 3 | local $_ = $params[0]->xpath_string_value; | |||||||||||||||||||||||||||
| 253 | 3 | my $find = $params[1]->xpath_string_value; | |||||||||||||||||||||||||||
| 254 | 3 | my $repl = $params[2]->xpath_string_value; | |||||||||||||||||||||||||||
| 255 | 3 | $repl= substr( $repl, 0, length( $find)); | |||||||||||||||||||||||||||
| 256 | 3 | my %repl; | |||||||||||||||||||||||||||
| 257 | 3 | @repl{split //, $find}= split( //, $repl); | |||||||||||||||||||||||||||
| 258 | 3 | ▶ | s{(.)}{exists $repl{$1} ? defined $repl{$1} ? $repl{$1} : '' : $1 }ges; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 259 | 3 | return Tree::XPathEngine::Literal->new($_); | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | ### BOOLEAN FUNCTIONS ### | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 2 | ▶ | sub boolean { | ||||||||||||||||||||||||||
|
Subroutine
boolean: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 265 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 266 | 2 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 267 | 2 | ▶ | die "boolean: Incorrect number of parameters\n" if @params != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 2 | return $params[0]->xpath_to_boolean; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 1 | ▶ | sub not { | ||||||||||||||||||||||||||
|
Subroutine
not: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 272 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 273 | 1 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 274 | 1 | ▶ | $params[0] = $params[0]->xpath_to_boolean unless $params[0]->isa('Tree::XPathEngine::Boolean'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 1 | ▶ | $params[0]->value ? Tree::XPathEngine::Boolean->_false : Tree::XPathEngine::Boolean->_true; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 1 | ▶ | sub true { | ||||||||||||||||||||||||||
|
Subroutine
true: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 279 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 280 | 1 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 281 | 1 | ▶ | die "true: function takes no parameters\n" if @params > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 1 | Tree::XPathEngine::Boolean->_true; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 2 | ▶ | sub false { | ||||||||||||||||||||||||||
|
Subroutine
false: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 286 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 287 | 2 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 288 | 2 | ▶ | die "true: function takes no parameters\n" if @params > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 2 | Tree::XPathEngine::Boolean->_false; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub lang { | ||||||||||||||||||||||||||
|
Subroutine
lang: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 293 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 294 | 0 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 295 | 0 | ▶ | die "lang: function takes 1 parameter\n" if @params != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 0 | my $lang = $node->findvalue('(ancestor-or-self::*[@xml:lang]/@xml:lang)[last()]'); | |||||||||||||||||||||||||||
| 297 | 0 | my $lclang = lc($params[0]->xpath_string_value); | |||||||||||||||||||||||||||
| 298 | # warn("Looking for lang($lclang) in $lang\n"); | ||||||||||||||||||||||||||||
| 299 | 0 | ▶ | if (substr(lc($lang), 0, length($lclang)) eq $lclang) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | return Tree::XPathEngine::Boolean->_true; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | else { | ||||||||||||||||||||||||||||
| 303 | 0 | return Tree::XPathEngine::Boolean->_false; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | ### NUMBER FUNCTIONS ### | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 11 | ▶ | sub number { | ||||||||||||||||||||||||||
|
Subroutine
number: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 310 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 311 | 11 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 312 | 11 | ▶ | die "number: Too many parameters\n" if @params > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 11 | ▶ | if (@params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | 5 | ▶ | if ($params[0]->isa('Tree::XPathEngine::Node')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | return Tree::XPathEngine::Number->new( | |||||||||||||||||||||||||||
| 316 | $params[0]->xpath_string_value | ||||||||||||||||||||||||||||
| 317 | ); | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 5 | return $params[0]->xpath_to_number; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 6 | return Tree::XPathEngine::Number->new( $node->xpath_string_value ); | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 1 | ▶ | sub sum { | ||||||||||||||||||||||||||
|
Subroutine
sum: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 326 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 327 | 1 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 328 | 1 | ▶ | die "sum: Parameter must be a NodeSet\n" unless $params[0]->isa('Tree::XPathEngine::NodeSet'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 1 | my $sum = 0; | |||||||||||||||||||||||||||
| 330 | 1 | foreach my $node ($params[0]->get_nodelist) { | |||||||||||||||||||||||||||
| 331 | 5 | $sum += $self->number($node)->value; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | 1 | return Tree::XPathEngine::Number->new($sum); | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 0 | ▶ | sub floor { | ||||||||||||||||||||||||||
|
Subroutine
floor: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 337 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 338 | 0 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 339 | 0 | require POSIX; | |||||||||||||||||||||||||||
| 340 | 0 | my $num = $self->number($node, @params); | |||||||||||||||||||||||||||
| 341 | 0 | return Tree::XPathEngine::Number->new( | |||||||||||||||||||||||||||
| 342 | POSIX::floor($num->value)); | ||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | sub ceiling { | ||||||||||||||||||||||||||
|
Subroutine
ceiling: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 346 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 347 | 0 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 348 | 0 | require POSIX; | |||||||||||||||||||||||||||
| 349 | 0 | my $num = $self->number($node, @params); | |||||||||||||||||||||||||||
| 350 | 0 | return Tree::XPathEngine::Number->new( | |||||||||||||||||||||||||||
| 351 | POSIX::ceil($num->value)); | ||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub round { | ||||||||||||||||||||||||||
|
Subroutine
round: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 355 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 356 | 0 | my ($node, @params) = @_; | |||||||||||||||||||||||||||
| 357 | 0 | my $num = $self->number($node, @params); | |||||||||||||||||||||||||||
| 358 | 0 | require POSIX; | |||||||||||||||||||||||||||
| 359 | 0 | return Tree::XPathEngine::Number->new( | |||||||||||||||||||||||||||
| 360 | POSIX::floor($num->value + 0.5)); # Yes, I know the spec says don't do this... | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 6 | 1; | |||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | __END__ | ||||||||||||||||||||||||||||