File Coverage

blib/lib/Tree/DAG_Node/XPath.pm
Criterion Covered Total %
statement 84 88 95.4
branch n/a
condition 4 5 80.0
subroutine 38 42 90.4
pod 12 12 100.0
total 138 147 93.8


line stmt bran cond sub pod time code
1             package Tree::DAG_Node::XPath;
2              
3             require 5.006;
4 2     2   47458 use strict;
  2         5  
  2         120  
5 2     2   11 use warnings;
  2         4  
  2         66  
6              
7              
8 2     2   10 use vars qw(@ISA $VERSION);
  2         5  
  2         204  
9             $VERSION="0.11";
10              
11 2     2   11 use base 'Tree::DAG_Node';
  2         5  
  2         2782  
12 2     2   71748 use Tree::XPathEngine;
  2         79614  
  2         1744  
13              
14             sub _init {
15 12     12   1047 my($self, $options) = @_[0,1];
16 12         46 $self->SUPER::_init($options);
17            
18 12         302 $self->_init_xpath_engine($options);
19             }
20            
21             sub _init_xpath_engine {
22 12     12   19 my($self, $options) = @_;
23             # copy options, so the delete doesn't modify the original options
24 12         28 my %options= %$options;
25 12         15 my %xpath_engine_options;
26 12         19 my @xpath_engine_options= qw{xpath_name_re};
27 12         20 foreach my $option_name ( @xpath_engine_options)
28 12         20 { $xpath_engine_options{$option_name}= $options{$option_name};
29 12         29 delete $options{$option_name};
30             }
31 12         24 $xpath_engine_options{xpath_name_re}= $options->{xpath_name_re};
32 12         50 $self->{_xpath_engine} = Tree::XPathEngine->new( %xpath_engine_options);
33             }
34              
35             sub _xpath_engine
36 103     103   1166 { return shift()->{_xpath_engine}; }
37              
38             # these are straight from Tree::XPathEngine::Node
39             sub find {
40 46     46 1 48310 my $node = shift;
41 46         87 my ($path) = @_;
42 46         169 my $xp = $node->root->_xpath_engine;
43 46         183 return $xp->find($path, $node);
44             }
45              
46              
47              
48             sub findvalue {
49 2     2 1 745 my $node = shift;
50 2         3 my ($path) = @_;
51 2         6 my $xp = $node->root->_xpath_engine;
52 2         8 return $xp->findvalue($path, $node);
53             }
54              
55             sub findnodes {
56 47     47 1 36476 my $node = shift;
57 47         84 my ($path) = @_;
58 47         142 my $xp = $node->root->_xpath_engine;
59 47         181 return $xp->findnodes($path, $node);
60             }
61              
62             sub matches {
63 8     8 1 2597 my $node = shift;
64 8         15 my ($path, $context) = @_;
65 8   66     43 $context ||= $node;
66 8         35 my $xp = $node->root->_xpath_engine;
67 8         40 return $xp->matches($node, $path, $context);
68             }
69              
70             # Tree::XPathEngine method aliased to Tree::DAG_Node method
71             *xpath_get_name = *Tree::DAG_Node::name;
72             *xpath_get_next_sibling = *Tree::DAG_Node::right_sister;
73             *xpath_get_previous_sibling = *Tree::DAG_Node::left_sister;
74              
75             sub xpath_get_root_node
76 95     95 1 72199 { my $node= shift;
77             # The parent of root is a Tree::DAG_Node::XPath::Root
78             # that helps getting the tree to mimic a DOM tree
79 95         268 return $node->root->xpath_get_parent_node; # I like this one!
80             }
81              
82             sub xpath_get_parent_node
83 134     134 1 4267 { my $node= shift;
84 134   100     434 return $node->mother || bless { root => $node }, 'Tree::DAG_Node::XPath::Root';
85             }
86              
87              
88 653     653 1 39977 sub xpath_get_child_nodes { my @daughters= shift()->daughters; return @daughters; }
  653         4876  
89 0     0 1 0 sub xpath_is_document_node { return 0; }
90 618     618 1 17586 sub xpath_is_element_node { return 1; }
91 0     0 1 0 sub xpath_is_attribute_node { return 0; }
92             #sub getValue { return '' };
93              
94             sub xpath_get_attributes
95 379     379 1 46966 { my $elt= shift;
96 379         964 my $atts= $elt->attributes;
97 379         2131 my $rank=-1;
98 379         1124 my @atts= map { bless( { name => $_, value => $atts->{$_}, elt => $elt, rank => $rank -- },
  750         8425  
99             'Tree::DAG_Node::XPath::Attribute')
100             }
101             sort keys %$atts;
102 379         1278 return @atts;
103             }
104              
105              
106              
107 858     858 1 40191 sub xpath_cmp { $_[0]->address cmp $_[1]->address }
108              
109            
110             1;
111              
112             # class for the fake root for a tree
113             package Tree::DAG_Node::XPath::Root;
114              
115            
116 139     139   9361 sub xpath_get_child_nodes { my @daughters= ( $_[0]->{root}); return @daughters; }
  139         371  
117 92     92   1626 sub address { return -1; } # the root is before all other nodes
118 4     4   247 sub xpath_get_attributes { return (); }
119 1     1   20 sub xpath_is_document_node { return 1 }
120 1     1   5 sub xpath_is_element_node { return 0 }
121 0     0   0 sub xpath_is_attribute_node { return 0 }
122 1     1   4 sub xpath_get_parent_node { return; }
123 1     1   5 sub xpath_get_root_node { return $_[0] }
124 1     1   4 sub xpath_get_name { return; }
125 1     1   4 sub xpath_get_next_sibling { return; }
126 1     1   4 sub xpath_get_previous_sibling { return; }
127              
128              
129             1;
130              
131             package Tree::DAG_Node::XPath::Attribute;
132 2     2   32 use Tree::XPathEngine::Number;
  2         4  
  2         567  
133              
134             # not used, instead xpath_get_attributes in Tree::DAG_Node::XPath directly returns an
135             # object blessed in this class
136             #sub new
137             # { my( $class, $elt, $att)= @_;
138             # return bless { name => $att, value => $elt->att( $att), elt => $elt }, $class;
139             # }
140              
141 2     2   15 sub xpath_get_value { return $_[0]->{value}; }
142 748     748   7875 sub xpath_get_name { return $_[0]->{name} ; }
143 223     223   18817 sub xpath_string_value { return $_[0]->{value}; }
144 150     150   12732 sub xpath_to_number { return Tree::XPathEngine::Number->new( $_[0]->{value}); }
145 0     0   0 sub xpath_is_document_node { 0 }
146 1     1   5 sub xpath_is_element_node { 0 }
147 1     1   70 sub xpath_is_attribute_node { 1 }
148 1     1   9 sub to_string { return qq{$_[0]->{name}="$_[0]->{value}"}; }
149             sub address
150 106     106   1248 { my $att= shift;
151 106         115 my $elt= $att->{elt};
152 106         208 return $elt->address . ':' . $att->{rank};
153             }
154            
155 53     53   1518 sub xpath_cmp { $_[0]->address cmp $_[1]->address }
156              
157             1;
158              
159              
160              
161             __END__