/usr/share/perl5/XML/TreePuller/Element.pm is in libxml-treepuller-perl 0.1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | #!/usr/bin/env perl
#things got ugly in here when XPath
#support was hammered in - should be cleaned up
package XML::TreePuller::Element;
our $VERSION = '0.1.0';
use strict;
use warnings;
use Carp qw(croak);
use XML::LibXML::Reader;
use Tree::XPathEngine::Number;
use Data::Dumper;
use Scalar::Util qw(weaken);
use Tree::XPathEngine;
use XML::TreePuller::Constants;
sub new {
my ($class, $tree) = @_;
if ($tree->[XML_TREEPULLER_ELEMENT_TYPE] != XML_READER_TYPE_ELEMENT) {
croak("must specify an element node");
}
bless($tree, $class);
$tree->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE] = Tree::XPathEngine->new;
$tree->_init($tree, 0);
return $tree;
}
sub get_elements {
my ($self, $path) = @_;
my @results;
if (! defined($path)) {
@results = _extract_elements(@{$self->[XML_TREEPULLER_ELEMENT_CHILDREN]});
} else {
@results = $self->_recursive_get_child_elements(split('/', $path));
}
if (wantarray()) {
return @results;
}
return shift(@results);
}
sub xpath {
my @return = $_[0]->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE]->findnodes($_[1], XML::TreePuller::Element::Document->new($_[0]));
if (wantarray()) {
return @return;
}
return shift(@return);
}
sub name {
my ($tree) = @_;
return $tree->[XML_TREEPULLER_ELEMENT_NAME];
}
sub text {
my ($self) = @_;
my @content;
foreach (@{$self->[XML_TREEPULLER_ELEMENT_CHILDREN]}) {
if ($_->[XML_TREEPULLER_ELEMENT_TYPE] == XML_READER_TYPE_TEXT || $_->[0] == XML_READER_TYPE_CDATA) {
push(@content, $_->[XML_TREEPULLER_ELEMENT_NAME]);
} elsif ($_->[XML_TREEPULLER_ELEMENT_TYPE] == XML_READER_TYPE_ELEMENT) {
push(@content, $_->text);
}
}
return join('', @content);
}
sub attribute {
my ($tree, $name) = @_;
my $attr = $tree->[XML_TREEPULLER_ELEMENT_ATTRIBUTES];
$attr = {} unless defined $attr;
if (! defined($name)) {
return $attr;
}
return $attr->{$name};
}
#private methods
sub _extract_elements {
return grep { $_->[XML_TREEPULLER_ELEMENT_TYPE] == XML_READER_TYPE_ELEMENT } @_;
}
#an easier to understand algorithm would be nice
sub _recursive_get_child_elements {
my ($tree, @path) = @_;
my $child_nodes = $tree->[XML_TREEPULLER_ELEMENT_CHILDREN];
my @results;
my $target;
if (! scalar(@path)) {
return $tree;
}
$target = shift(@path);
return () unless defined $child_nodes;
foreach (_extract_elements(@$child_nodes)) {
next unless $_->[XML_TREEPULLER_ELEMENT_NAME] eq $target;
push(@results, _recursive_get_child_elements($_, @path));
}
return @results;
}
sub _init {
my ($self, $root, $depth) = @_;
my @elements = $self->get_elements;
$self->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE] = $root->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE];
$self->[XML_TREEPULLER_ELEMENT_PARENT] = undef;
$self->[XML_TREEPULLER_ELEMENT_ROOT] = $root;
$self->[XML_TREEPULLER_ELEMENT_DEPTH] = $depth;
weaken($self->[XML_TREEPULLER_ELEMENT_ROOT]);
$depth++;
for(my $i = 0; $i < @elements; $i++) {
my $before = $elements[$i - 1];
my $after = $elements[$i + 1];
if ($i - 1 < 0) {
$before = undef;
}
$elements[$i]->[XML_TREEPULLER_ELEMENT_NEXT_SIBLING] = $before;
$elements[$i]->[XML_TREEPULLER_ELEMENT_PREV_SIBLING] = $after;
weaken($elements[$i]->[XML_TREEPULLER_ELEMENT_NEXT_SIBLING]);
weaken($elements[$i]->[XML_TREEPULLER_ELEMENT_PREV_SIBLING]);
}
foreach (@elements) {
#set the parent and root of each element
$_->[XML_TREEPULLER_ELEMENT_PARENT] = $self;
$_->[XML_TREEPULLER_ELEMENT_ROOT] = $root;
$_[XML_TREEPULLER_ELEMENT_DEPTH] = $depth;
weaken($_->[XML_TREEPULLER_ELEMENT_PARENT]);
weaken($_->[XML_TREEPULLER_ELEMENT_ROOT]);
bless($_, 'XML::TreePuller::Element');
$_->_init($root, $depth);
}
}
#methods for Tree::XPathEngine
sub xpath_get_name {
return name(@_);
}
sub xpath_string_value {
return (text(@_));
}
sub xpath_get_parent_node {
return $_[0]->[XML_TREEPULLER_ELEMENT_ROOT] || XML::TreePuller::Element::Document->new($_[0]);
}
sub xpath_get_child_nodes {
return $_[0]->get_elements;
}
sub xpath_is_element_node {
return 1;
}
sub xpath_is_document_node {
return 0;
}
sub xpath_is_attribute_node {
return 0;
}
sub xpath_to_string {
return $_[0];
}
sub xpath_to_number {
return Tree::XPathEngine::Number->new($_[0]->xpath_to_string);
}
sub xpath_cmp {
return $_[0]->[XML_TREEPULLER_ELEMENT_DEPTH] cmp $_[1]->[XML_TREEPULLER_ELEMENT_DEPTH];
}
sub xpath_get_attributes {
my $elt= shift;
my $atts= $elt->attribute;
my $rank=-1;
my @atts= map { bless( { name => $_, value => $atts->{$_}, elt => $elt, rank => $rank -- },
'XML::TreePuller::Element::Attribute')
}
sort keys %$atts;
return @atts;
}
sub xpath_get_next_sibling {
return $_[0]->[XML_TREEPULLER_ELEMENT_NEXT_SIBLING];
}
sub xpath_get_prev_sibling {
return $_[0]->[XML_TREEPULLER_ELEMENT_PREV_SIBLING];
}
sub xpath_get_root_node {
my ($node) = @_;
return $node->[XML_TREEPULLER_ELEMENT_ROOT]->xpath_get_parent_node;
}
package XML::TreePuller::Element::Document;
use strict;
use warnings;
use XML::TreePuller::Constants;
sub new {
my ($class, $root) = @_;
my $self = [ $root ];
$self->[XML_TREEPULLER_ELEMENT_DEPTH] = -1;
return bless($self, $class);
}
sub xpath_get_child_nodes { return( $_[0]->[0] ); }
sub xpath_get_attributes { return (); }
sub xpath_is_document_node { return 1 }
sub xpath_is_element_node { return 0 }
sub xpath_is_attribute_node { return 0 }
sub xpath_get_parent_node { return; }
sub xpath_get_root_node { return $_[0] }
sub xpath_get_name { return; }
sub xpath_get_next_sibling { return; }
sub xpath_get_previous_sibling { return; }
package XML::TreePuller::Element::Attribute;
use strict;
use warnings;
sub xpath_get_value { return $_[0]->{value}; }
sub xpath_get_name { return $_[0]->{name} ; }
sub xpath_string_value { return $_[0]->{value}; }
sub xpath_to_number { return Tree::XPathEngine::Number->new( $_[0]->{value}); }
sub xpath_is_document_node { 0 }
sub xpath_is_element_node { 0 }
sub xpath_is_attribute_node { 1 }
sub to_string { return qq{$_[0]->{name}="$_[0]->{value}"}; }
1;
|