| 1 | # ex: set tabstop=4: | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | XML::TinyXML::Selector::XPath - XPath-compliant selector for XML::TinyXML | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | use XML::TinyXML; | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # first obtain an xml context: | ||||||||||||||||||||||||||||||||||||||||||
| 14 | $xml = XML::TinyXML->new("rootnode", param => "somevalue", attrs => { attr1 => v1, attr2 => v2 }); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | $selector = XML::TinyXML::Selector->new($xml, "XPath"); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | ##### | ||||||||||||||||||||||||||||||||||||||||||
| 19 | Assuming the following xml data : | ||||||||||||||||||||||||||||||||||||||||||
| 20 | <?xml version="1.0"?> | ||||||||||||||||||||||||||||||||||||||||||
| 21 | <xml> | ||||||||||||||||||||||||||||||||||||||||||
| 22 | <hello>world</hello> | ||||||||||||||||||||||||||||||||||||||||||
| 23 | <foo> | ||||||||||||||||||||||||||||||||||||||||||
| 24 | <![CDATA[ this should unescape <&; etc... :) ]]> | ||||||||||||||||||||||||||||||||||||||||||
| 25 | </foo> | ||||||||||||||||||||||||||||||||||||||||||
| 26 | <parent> | ||||||||||||||||||||||||||||||||||||||||||
| 27 | <child1/> | ||||||||||||||||||||||||||||||||||||||||||
| 28 | <child2/> | ||||||||||||||||||||||||||||||||||||||||||
| 29 | <child3/> | ||||||||||||||||||||||||||||||||||||||||||
| 30 | </parent> | ||||||||||||||||||||||||||||||||||||||||||
| 31 | <parent> | ||||||||||||||||||||||||||||||||||||||||||
| 32 | <blah>SECOND</blah> | ||||||||||||||||||||||||||||||||||||||||||
| 33 | </parent> | ||||||||||||||||||||||||||||||||||||||||||
| 34 | </xml> | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ##### | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | @res = $selector->select('//parent'); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | @res = $selector->select('//child*'); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | @res = $selector->select('/parent[2]/blah/..'); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | @res = $selector->select('//blah/..'); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | @res = $selector->select('//parent[1]/..'); | ||||||||||||||||||||||||||||||||||||||||||
| 42 | @res = $selector->select('//parent[1]/.'); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | @res = $selector->select('//blah/.'); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # or using the unabbreviated syntax: | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | @res = $selector->select('/descendant-or-self::node()/child::parent'); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | @res = $selector->select('/descendant-or-self::node()/child::child*'); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | @res = $selector->select('/child::parent[2]/child::blah/parent::node()'); | ||||||||||||||||||||||||||||||||||||||||||
| 50 | @res = $selector->select('/descendant-or-self::node()/child::blah/parent::node()'); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | @res = $selector->select('/descendant-or-self::node()/child::parent[1]/parent::node()'); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | @res = $selector->select('/descendant-or-self::node()/child::parent[1]/self::node()'); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | @res = $selector->select('/descendant-or-self::node()/child::blah/self::node()'); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # refer to XPath documentation for further examples and details on the supported syntax: | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # ( http://www.w3.org/TR/xpath ) | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | XPath-compliant selector for XML::TinyXML | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =head1 INSTANCE VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 76 | package XML::TinyXML::Selector::XPath; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | use base qw(XML::TinyXML::Selector); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | ▶ | use XML::TinyXML::Selector::XPath::Context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | use XML::TinyXML::Selector::XPath::Functions; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | ▶ | use XML::TinyXML::Selector::XPath::Axes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | our $VERSION = '0.34'; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | #our @ExprTokens = ('(', ')', '[', ']', '.', '..', '@', ',', '::'); | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | my @NODE_FUNCTIONS = qw( | |||||||||||||||||||||||||||||||||||||||||
| 90 | last | ||||||||||||||||||||||||||||||||||||||||||
| 91 | position | ||||||||||||||||||||||||||||||||||||||||||
| 92 | count | ||||||||||||||||||||||||||||||||||||||||||
| 93 | id | ||||||||||||||||||||||||||||||||||||||||||
| 94 | local-name | ||||||||||||||||||||||||||||||||||||||||||
| 95 | namespace-uri | ||||||||||||||||||||||||||||||||||||||||||
| 96 | name | ||||||||||||||||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | my @STRING_FUNCTIONS = qw( | |||||||||||||||||||||||||||||||||||||||||
| 100 | string | ||||||||||||||||||||||||||||||||||||||||||
| 101 | concat | ||||||||||||||||||||||||||||||||||||||||||
| 102 | starts-with | ||||||||||||||||||||||||||||||||||||||||||
| 103 | contains | ||||||||||||||||||||||||||||||||||||||||||
| 104 | substring-before | ||||||||||||||||||||||||||||||||||||||||||
| 105 | substring-after | ||||||||||||||||||||||||||||||||||||||||||
| 106 | substring | ||||||||||||||||||||||||||||||||||||||||||
| 107 | string-length | ||||||||||||||||||||||||||||||||||||||||||
| 108 | normalize-space | ||||||||||||||||||||||||||||||||||||||||||
| 109 | translate | ||||||||||||||||||||||||||||||||||||||||||
| 110 | boolean | ||||||||||||||||||||||||||||||||||||||||||
| 111 | not | ||||||||||||||||||||||||||||||||||||||||||
| 112 | true | ||||||||||||||||||||||||||||||||||||||||||
| 113 | false | ||||||||||||||||||||||||||||||||||||||||||
| 114 | lang | ||||||||||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | my @NUMBER_FUNCTIONS = qw( | |||||||||||||||||||||||||||||||||||||||||
| 118 | number | ||||||||||||||||||||||||||||||||||||||||||
| 119 | sum | ||||||||||||||||||||||||||||||||||||||||||
| 120 | floor | ||||||||||||||||||||||||||||||||||||||||||
| 121 | ceiling | ||||||||||||||||||||||||||||||||||||||||||
| 122 | round | ||||||||||||||||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | our @AllFunctions = (@NODE_FUNCTIONS, @STRING_FUNCTIONS, @NUMBER_FUNCTIONS); | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 4 | our @Axes = qw( | |||||||||||||||||||||||||||||||||||||||||
| 128 | child | ||||||||||||||||||||||||||||||||||||||||||
| 129 | descendant | ||||||||||||||||||||||||||||||||||||||||||
| 130 | parent | ||||||||||||||||||||||||||||||||||||||||||
| 131 | ancestor | ||||||||||||||||||||||||||||||||||||||||||
| 132 | following-sibling | ||||||||||||||||||||||||||||||||||||||||||
| 133 | preceding-sibling | ||||||||||||||||||||||||||||||||||||||||||
| 134 | following | ||||||||||||||||||||||||||||||||||||||||||
| 135 | preceding | ||||||||||||||||||||||||||||||||||||||||||
| 136 | attribute | ||||||||||||||||||||||||||||||||||||||||||
| 137 | namespace | ||||||||||||||||||||||||||||||||||||||||||
| 138 | self | ||||||||||||||||||||||||||||||||||||||||||
| 139 | descendant-or-self | ||||||||||||||||||||||||||||||||||||||||||
| 140 | ancestor-or-self | ||||||||||||||||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =item * init () | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | $self->{context} = XML::TinyXML::Selector::XPath::Context->new($self->{_xml}); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =item * select ($expr, [ $cnode ]) | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 37 | ▶ | sub select { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 37 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 37 | my $expanded_expr = $self->_expand_abbreviated($expr); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 37 | my $set = $self->_select_unabbreviated($expanded_expr); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 37 | ▶ | if ($set) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | return wantarray | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 37 | ▶ | ? @$set | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | : (scalar(@$set > 1) | ||||||||||||||||||||||||||||||||||||||||||
| 163 | ? $set | ||||||||||||||||||||||||||||||||||||||||||
| 164 | : @$set[0]) | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 603 | ▶ | sub context { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
context: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 603 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 603 | return $self->{context}; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 20 | ▶ | sub functions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
functions: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 20 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 20 | ▶ | return wantarray?@AllFunctions:__PACKAGE__."::Functions"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 14 | ▶ | sub resetContext { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resetContext: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 14 | $self->{context} = XML::TinyXML::Selector::XPath::Context->new($self->{_xml}); | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | ###### PRIVATE METHODS ###### | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 46 | ▶ | sub _expand_abbreviated { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_abbreviated: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 46 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 46 | $expr =~ s/\/\//\/descendant-or-self::node()\//g; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 46 | my @tokens = split('/', $expr); | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 46 | foreach my $i (0..$#tokens) { | |||||||||||||||||||||||||||||||||||||||||
| 192 | 104 | my $t = $tokens[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 104 | ▶ | next unless ($t); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 82 | ▶ | if($t !~ /::/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 43 | ▶ | $t = "child::$tokens[$i]" if ($t !~ /\./); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 43 | $t =~ s/\@/attribute::/g; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 43 | $t =~ s/\.\./parent::node()/g; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 43 | $t =~ s/\./self::node()/g; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 43 | $tokens[$i] = $t; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 46 | join('/', @tokens); | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | ▶ | sub _exec_function { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exec_function: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | my ($self, $fun, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 5 | ▶ | unless(grep(/^$fun$/, @AllFunctions)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | warn "Unsupported Function: '$fun'"; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 5 | $fun =~ s/-/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 5 | return XML::TinyXML::Selector::XPath::Functions->$fun($self->{context}, @args); | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | # Priveate method | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 85 | ▶ | sub _expand_axis { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_axis: called CC=3 SCAR=15.8
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 85 | my ($self, $axis) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 85 | ▶ | if ($axis =~ /(\S+)\s+(\S+)\s+(\S+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | my $a1 = $1; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | my $op = $2; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | my $a2 = $3; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $i1 = $self->_expand_axis($a1); | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $i2 = $self->_expand_axis($a2); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | return $self->context->operators->{$op}->($i1, $i2); | |||||||||||||||||||||||||||||||||||||||||
| 226 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 85 | ▶ | unless(grep(/^$axis$/, @Axes)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | warn "Unsupported Axis: '$axis'"; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 85 | $axis =~ s/-/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 85 | return XML::TinyXML::Selector::XPath::Axes->$axis($self->{context}); | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 9 | ▶ | sub _unescape { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unescape: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 9 | my ($self, $string) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 9 | ▶ | $string = substr($string, 1, length($string)-2) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | if ($string =~ /^([\"'])(?:\\\1|.)*?\1$/); | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 9 | $string =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 9 | $string =~ s/'/'/g; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 9 | $string =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 9 | $string =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 9 | $string =~ s/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 9 | return $string; | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | # Priveate method | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 9 | ▶ | sub _parse_predicate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_predicate: called CC=6 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 9 | my ($self, $predicate) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 9 | my ($attr, $child, $value); | |||||||||||||||||||||||||||||||||||||||||
| 254 | 9 | my %res; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 9 | ▶ | if ($predicate =~ /^([0-9]+)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 6 | $res{idx} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } elsif (($attr, $value) = $predicate =~ /^\@(\S+)\s*=\s*(.*)\s*$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $res{attr} = $attr; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | $res{attr_value} = $self->_unescape($value); | |||||||||||||||||||||||||||||||||||||||||
| 260 | } elsif (($child, $value) = $predicate =~ /^(\S+)\s*=\s*(.*)\s*$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 3 | $res{child} = $child; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 3 | $res{child_value} = $self->_unescape($value); | |||||||||||||||||||||||||||||||||||||||||
| 263 | } elsif (($attr) = $predicate =~ /^\@(\S+)$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | $res{attr} = $attr; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # TODO - support all predicates | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 9 | ▶ | return wantarray?%res:\%res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 107 | ▶ | sub _select_unabbreviated { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_select_unabbreviated: called CC=55 SCAR=41.0
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 107 | my ($self, $expr, $recurse) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 107 | my @tokens = split('/', $expr); | |||||||||||||||||||||||||||||||||||||||||
| 273 | 107 | my @set; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 107 | ▶ | if ($expr =~ /^\// and !$recurse) { # absolute path has been requested | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$expr =~ m[^/] and not $recurse
MC/DC50%1/2
$expr =~ m[^/] and not $recurse
$expr =~ m[^/]
not $recurse
Truth table66%2/3
A and not(B)
A= $expr =~ m[^/]B= not $recurse
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 20 | $self->context->{items} = [$self->{_xml}->rootNodes()]; | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # XPath works only in single-root mode | ||||||||||||||||||||||||||||||||||||||||||
| 278 | # (which is the only allowed mode by the xml spec anyway) | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 107 | my $state = $self->{_xml}->allowMultipleRootNodes(0); | |||||||||||||||||||||||||||||||||||||||||
| 280 | #shift(@tokens) | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # if (!$tokens[0] and $recurse); | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 107 | my $token = shift @tokens; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 107 | ▶ | if ($token and $token =~ /([\w-]+)::([\w\(\)\=]+|\*)(\[.*?\])*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$token and $token =~ /([\w-]+)::([\w\(\)\=]+|\*)(\[.*?\])*$/
MC/DC100%2/2
$token and $token =~ /([\w-]+)::([\w\(\)\=]+|\*)(\[.*?\])*$/
$token
$token =~ /([\w-]+)::([\w\(\)\=]+|\*)(\[.*?\])*$/
Truth table100%3/3
A and B
A= $tokenB= $token =~ /([\w-]+)::([\w\(\)\=]+|\*)(\[.*?\])*$/
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 85 | my $step = $1; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 85 | my $nodetest = $2; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 85 | my $full_predicate = $3; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 85 | @set = $self->_expand_axis($step); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 85 | ▶ | if ($nodetest eq '*') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 8 | $self->context->{items} = \@set; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 77 | $self->context->{items} = []; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 77 | foreach my $node (@set) { | |||||||||||||||||||||||||||||||||||||||||
| 293 | 474 | ▶ | if ($nodetest =~ /\(\)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 148 | ▶ | if ($nodetest eq 'node()') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 148 | ▶ | push (@{$self->context->{items}}, $node) if ($node->type ne "ATTRIBUTE"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | warn "Unknown NodeTest $nodetest"; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 326 | ▶ | push (@{$self->context->{items}}, $node) if ($node->name eq $nodetest); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 85 | ▶ | if ($full_predicate and $full_predicate =~ s/^\[(.*?)\]$/$1/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$full_predicate and $full_predicate =~ s/^\[(.*?)\]$/$1/
MC/DC50%1/2
$full_predicate and $full_predicate =~ s/^\[(.*?)\]$/$1/
$full_predicate
$full_predicate =~ s/^\[(.*?)\]$/$1/
Truth table66%2/3
A and B
A= $full_predicateB= $full_predicate =~ s/^\[(.*?)\]$/$1/
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 25 | my @predicates = $full_predicate; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 25 | my $op; | |||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 25 | my $saved_context = $self->context; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 25 | my %all_sets; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 25 | ▶ | while ($full_predicate =~ /\(([^()]+)\s+(and|or)\s+([^()]+)\)/ or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$full_predicate =~ /\(([^()]+)\s+(and|or)\s+([^()]+)\)/ or not $full_predicate =~ /^(?:__SET\:\S+__)$/
MC/DC100%2/2
$full_predicate =~ /\(([^()]+)\s+(and|or)\s+([^()]+)\)/ or not $full_predicate =~ /^(?:__SET\:\S+__)$/
$full_predicate =~ /\(([^()]+)\s+(and|or)\s+([^()]+)\)/
not $full_predicate =~ /^(?:__SET\:\S+__)$/
Truth table100%3/3
A or not(B)
A= $full_predicate =~ /\(([^()]+)\s+(and|or)\s+([^()]+)\)/B= not $full_predicate =~ /^(?:__SET\:\S+__)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | $full_predicate !~ /^(?:__SET\:\S+__)$/) | ||||||||||||||||||||||||||||||||||||||||||
| 312 | { | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 26 | my $tmpctx2 = XML::TinyXML::Selector::XPath::Context->new($self->{_xml}); | |||||||||||||||||||||||||||||||||||||||||
| 314 | 26 | $tmpctx2->{items} = $saved_context->items; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 26 | $self->{context} = $tmpctx2; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 26 | ▶ | my $inner_predicate = ($1 and $2 and $3)?"$1 $2 $3":$full_predicate; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$1 and $2 and $3
$1 and $2
MC/DC66%2/3
$1 and $2 and $3
$1
$2
$3
Truth table75%3/4
A and B and C
A= $1B= $2C= $3
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 26 | $inner_predicate =~ s/(^\(|\)$)//g; | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | # TODO - implement full support for complex boolean expression | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 26 | ▶ | if ($inner_predicate =~ /^(.*?)\s+(and|or)\s+(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 4 | @predicates = ($1, $3); | |||||||||||||||||||||||||||||||||||||||||
| 322 | 4 | $op = $2; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 26 | my @itemrefs; | |||||||||||||||||||||||||||||||||||||||||
| 325 | # save the actual context to ensure sending the correct context to all predicates | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 26 | my $saved_context2 = $self->context; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 26 | foreach my $predicate_string (@predicates) { | |||||||||||||||||||||||||||||||||||||||||
| 328 | # using a temporary context while iterating over all predicates | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 30 | my $tmpctx = XML::TinyXML::Selector::XPath::Context->new($self->{_xml}); | |||||||||||||||||||||||||||||||||||||||||
| 330 | 30 | $tmpctx->{items} = $saved_context2->items; | |||||||||||||||||||||||||||||||||||||||||
| 331 | 30 | $self->{context} = $tmpctx; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 30 | ▶ | if ($predicate_string =~ /^__SET:(\S+)__$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 1 | push(@itemrefs, $all_sets{$1}); | |||||||||||||||||||||||||||||||||||||||||
| 334 | } elsif ($predicate_string =~ /::/) { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 20 | my ($p, $v) = split('=', $predicate_string); | |||||||||||||||||||||||||||||||||||||||||
| 336 | 20 | ▶ | $v =~ s/(^['"]|['"]$)//g if ($v); # XXX - unsafe dequoting ... think more to find a better regexp | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 20 | my %uniq; | |||||||||||||||||||||||||||||||||||||||||
| 338 | my @nodepaths; | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 20 | foreach my $node ($self->_select_unabbreviated($p ,1)) { | |||||||||||||||||||||||||||||||||||||||||
| 340 | 20 | ▶ | if ($node->type eq "ATTRIBUTE") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 7 | my $nodepath = $node->node->path; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 7 | ▶ | next if ($v && $node->value ne $self->_unescape($v)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$v and $node->value ne $self->_unescape($v)
MC/DC0%0/2
$v and $node->value ne $self->_unescape($v)
$v
$node->value ne $self->_unescape($v)
Truth table66%2/3
A and B
A= $vB= $node->value ne $self->_unescape($v)
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 7 | ▶ | push(@nodepaths, $nodepath) if (!$uniq{$nodepath}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 7 | $uniq{$nodepath} = $node->node | |||||||||||||||||||||||||||||||||||||||||
| 345 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 13 | my $parent = $node->parent; | |||||||||||||||||||||||||||||||||||||||||
| 347 | 13 | ▶ | if ($parent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 13 | ▶ | next if ($v && $node->value ne $v); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$v and $node->value ne $v
MC/DC100%2/2
$v and $node->value ne $v
$v
$node->value ne $v
Truth table100%3/3
A and B
A= $vB= $node->value ne $v
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 10 | ▶ | push(@nodepaths, $parent->path) if (!$uniq{$parent->path}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 10 | $uniq{$parent->path} = $parent | |||||||||||||||||||||||||||||||||||||||||
| 351 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 352 | # TODO - Error Messages | ||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 20 | push (@itemrefs, [ map { $uniq{$_} } @nodepaths ]); | |||||||||||||||||||||||||||||||||||||||||
| 357 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 9 | my $predicate = $self->_parse_predicate($predicate_string); | |||||||||||||||||||||||||||||||||||||||||
| 359 | 9 | ▶ | if ($predicate->{attr}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | } elsif ($predicate->{child}) { | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 3 | ▶ | if ($predicate->{child} =~ s/\(.*?\)//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 3 | my $func = $predicate->{child}; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 3 | @set = $self->_exec_function($func); # expand lvalue function | |||||||||||||||||||||||||||||||||||||||||
| 364 | 3 | ▶ | if ($predicate->{child_value}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | my $op_string = join('|', | ||||||||||||||||||||||||||||||||||||||||||
| 366 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 39 | $_ =~ s/([\-\|\+\*\<\>=\!])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 39 | $_; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 3 | } keys(%{$self->context->operators}) | |||||||||||||||||||||||||||||||||||||||||
| 370 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 3 | my $value = $predicate->{child_value}; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 3 | ▶ | if ($value =~ s/\(.*?\)(.*)$//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 2 | my $extra = $1; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | $value = $self->_exec_function($value); # expand rvalue function | |||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | ▶ | if ($extra) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 1 | ▶ | if ($extra =~ /($op_string)(.*)$/) { # check if we must perform an extra operation | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 1 | $value = $self->context->operators->{$1}->($value, $2); | |||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | } elsif ($value =~ /^(.*?)($op_string)(.*)$/) { # check if we must perform an extra operation | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | $value = $self->context->operators->{$2}->($1, $3); | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 3 | ▶ | if ($func eq 'position') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 3 | my %pos = (@set); | |||||||||||||||||||||||||||||||||||||||||
| 385 | 3 | push (@itemrefs, [ $pos{$value} ]); | |||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | } elsif ($predicate->{idx}) { | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 6 | push (@itemrefs, [ @{$self->context->items}[$predicate->{idx}-1] ]); | |||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 30 | $self->{context} = $saved_context2; | |||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 26 | ▶ | if ($op) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 4 | $self->context->{items} = $self->context->operators->{$op}->(@itemrefs); | |||||||||||||||||||||||||||||||||||||||||
| 398 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 22 | $self->context->{items} = $itemrefs[0]; | |||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 26 | my $id = scalar($self->context->{items}); | |||||||||||||||||||||||||||||||||||||||||
| 402 | 26 | $all_sets{$id} = $self->context->{items}; | |||||||||||||||||||||||||||||||||||||||||
| 403 | 26 | ▶ | if ($inner_predicate eq $full_predicate) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 24 | $full_predicate = ""; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 24 | last; | |||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 2 | ▶ | last unless ($inner_predicate); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 2 | $inner_predicate =~ s/([()=])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 409 | 2 | $full_predicate =~ s/\(?$inner_predicate\)?/__SET\:${id}__/; | |||||||||||||||||||||||||||||||||||||||||
| 410 | } # while ($full_predicate =~ /\(([^()]+)\)/) | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 25 | ▶ | if ($full_predicate =~ /__SET:(\S+)__/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | 1 | $self->context->{items} = $all_sets{$1}; | |||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||
| 414 | } # if ($full_predicate and $full_predicate =~ s/^\[(.*?)\]$/$1/) | ||||||||||||||||||||||||||||||||||||||||||
| 415 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 60 | ▶ | warn "Bad predicate format : '$full_predicate'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | if ($full_predicate); | ||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 22 | my @newItems; | |||||||||||||||||||||||||||||||||||||||||
| 421 | 22 | foreach my $node (@{$self->context->items}) { | |||||||||||||||||||||||||||||||||||||||||
| 422 | 33 | ▶ | if ($token) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | # TODO - handle properly, C api has only partial support for predicates | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 12 | ▶ | if ($token =~ /\[.*?\]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | my $child = $node->getChildNodeByName($token); | |||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | push (@newItems, $child) if ($child); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 12 | foreach my $child ($node->children) { | |||||||||||||||||||||||||||||||||||||||||
| 429 | 11 | ▶ | push(@newItems, $child) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | if ($child->name eq $token); | ||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 21 | push(@newItems, $node); | |||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||
| 437 | 22 | $self->context->{items} = \@newItems; | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | 107 | ▶ | if (@tokens) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | 50 | return $self->_select_unabbreviated(join('/', @tokens), 1); # recursion here | |||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||
| 442 | 57 | $self->{_xml}->allowMultipleRootNodes($state); | |||||||||||||||||||||||||||||||||||||||||
| 443 | 57 | ▶ | return wantarray?@{$self->context->items}:$self->context->items; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | XML::TinyXML XML::TinyXML::Node XML::TinyXML::Selector | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | xant, E<lt>xant@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | Copyright (C) 2009-2010 by xant | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 467 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||||||||||||||||
| 468 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||