| File: | lib/XML/Parser/Lite/Tree/XPath.pm |
| Coverage: | 80.8% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package XML::Parser::Lite::Tree::XPath; | |||||
| 2 | ||||||
| 3 | 29 29 29 | 113 51 118 | use strict; | |||
| 4 | 29 29 29 | 232 134 249 | use XML::Parser::Lite::Tree::XPath::Tokener; | |||
| 5 | 29 29 29 | 218 161 253 | use XML::Parser::Lite::Tree::XPath::Tree; | |||
| 6 | 29 29 29 | 207 64 295 | use XML::Parser::Lite::Tree::XPath::Eval; | |||
| 7 | ||||||
| 8 | our $VERSION = '0.24'; | |||||
| 9 | ||||||
| 10 | sub new { | |||||
| 11 | 29 | 145 | my $class = shift; | |||
| 12 | 29 | 157 | my $self = bless {}, $class; | |||
| 13 | 29 | 125 | $self->{tree} = shift; | |||
| 14 | 29 | 160 | $self->{error} = 0; | |||
| 15 | ||||||
| 16 | 29 | 115 | return $self; | |||
| 17 | } | |||||
| 18 | ||||||
| 19 | sub query { | |||||
| 20 | 130 | 423 | my ($self, $xpath) = @_; | |||
| 21 | ||||||
| 22 | # | |||||
| 23 | # toke the xpath | |||||
| 24 | # | |||||
| 25 | ||||||
| 26 | 130 | 561 | my $tokener = XML::Parser::Lite::Tree::XPath::Tokener->new(); | |||
| 27 | ||||||
| 28 | 130 | 475 | unless ($tokener->parse($xpath)){ | |||
| 29 | 0 | 0 | $self->{error} = $tokener->{error}; | |||
| 30 | 0 | 0 | return 0; | |||
| 31 | } | |||||
| 32 | ||||||
| 33 | ||||||
| 34 | # | |||||
| 35 | # tree the xpath | |||||
| 36 | # | |||||
| 37 | ||||||
| 38 | 130 | 631 | my $xtree = XML::Parser::Lite::Tree::XPath::Tree->new(); | |||
| 39 | ||||||
| 40 | 130 | 586 | unless ($xtree->build_tree($tokener->{tokens})){ | |||
| 41 | 0 | 0 | $self->{error} = $xtree->{error}; | |||
| 42 | 0 | 0 | return 0; | |||
| 43 | } | |||||
| 44 | ||||||
| 45 | ||||||
| 46 | # | |||||
| 47 | # eval | |||||
| 48 | # | |||||
| 49 | ||||||
| 50 | 130 | 603 | my $eval = XML::Parser::Lite::Tree::XPath::Eval->new(); | |||
| 51 | ||||||
| 52 | 130 | 607 | my $out = $eval->query($xtree, $self->{tree}); | |||
| 53 | ||||||
| 54 | 130 | 458 | $self->{error} = $eval->{error}; | |||
| 55 | ||||||
| 56 | 130 | 1585 | return $out; | |||
| 57 | } | |||||
| 58 | ||||||
| 59 | sub select_nodes { | |||||
| 60 | 73 | 310 | my ($self, $xpath) = @_; | |||
| 61 | ||||||
| 62 | 73 | 256 | my $out = $self->query($xpath); | |||
| 63 | ||||||
| 64 | 73 | 241 | return 0 unless $out; | |||
| 65 | ||||||
| 66 | 73 | 313 | if ($out->{type} ne 'nodeset'){ | |||
| 67 | 0 | 0 | $self->{error} = "Result was not a nodeset (was a $out->{type})"; | |||
| 68 | 0 | 0 | return 0; | |||
| 69 | } | |||||
| 70 | ||||||
| 71 | 73 | 403 | return $out->{value}; | |||
| 72 | } | |||||
| 73 | ||||||
| 74 | 1; | |||||
| 75 | ||||||