0
|
1 package IMPL::DOM::Navigator;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
11
|
5 use base qw(IMPL::Object);
|
|
6 use IMPL::Class::Property;
|
|
7 use IMPL::Class::Property::Direct;
|
|
8 BEGIN {
|
|
9 public _direct property Path => prop_get | owner_set;
|
|
10 public _direct property Current => prop_get | owner_set;
|
|
11 }
|
|
12
|
|
13 sub CTOR {
|
|
14 my ($this,$CurrentNode) = @_;
|
|
15
|
|
16 $this->{$Current} = $CurrentNode or die IMPL::InvalidArgumentException("A starting node is a required paramater");
|
|
17 }
|
0
|
18
|
11
|
19 sub Navigate {
|
|
20 my ($this,$query) = @_;
|
|
21
|
|
22 if ( my ($newNode) = $this->{$Current}->selectNodes($query) ) {
|
|
23 push @{$this->{$Path}}, $this->{$Current};
|
|
24 return $this->{$Current} = $newNode;
|
|
25 } else {
|
|
26 return undef;
|
|
27 }
|
|
28 }
|
0
|
29
|
18
|
30 sub _NavigateNode {
|
|
31 my ($this,$newNode) = @_;
|
|
32 push @{$this->{$Path}}, $this->{$Current};
|
|
33 return $this->{$Current} = $newNode;
|
|
34 }
|
|
35
|
|
36 sub _NavigateNodeStirct {
|
|
37 my ($this,$newNode) = @_;
|
|
38
|
|
39 die new IMPL::InvalidOperationException("A newNode doesn't belongs to the current") unless $newNode->parentNode == $this->{$Current};
|
|
40 push @{$this->{$Path}}, $this->{$Current};
|
|
41 return $this->{$Current} = $newNode;
|
|
42 }
|
|
43
|
11
|
44 sub Back {
|
|
45 my ($this) = @_;
|
|
46
|
|
47 if ( my $newNode = $this->{$Path} ? pop @{$this->{$Path}} : undef ) {
|
|
48 return $this->{$Current} = $newNode;
|
|
49 } else {
|
|
50 return undef;
|
|
51 }
|
|
52 }
|
|
53
|
|
54 sub PathToString {
|
|
55 my $this = shift;
|
|
56
|
|
57 join('/',map $_->nodeName, $this->{$Path} ? (@{$this->{$Path}}, $this->{$Current}) : $this->{$Current});
|
|
58 }
|
0
|
59
|
|
60 1;
|
11
|
61
|
|
62 __END__
|
|
63 =pod
|
|
64
|
|
65 =head1 DESCRIPTION
|
|
66
|
|
67 DOM .
|
|
68
|
|
69 =head1 METHODS
|
|
70
|
|
71 =over
|
|
72
|
|
73 =item C<$obj->new($nodeStart)>
|
|
74
|
|
75 .
|
|
76
|
|
77 =item C<$obj->Navigate($query)>
|
|
78
|
|
79 C<$query>.
|
|
80 .
|
|
81 , .
|
|
82
|
|
83 , C<undef>.
|
|
84
|
|
85 =item C<$obj->Back()>
|
|
86
|
|
87 , .
|
|
88
|
|
89 , C<undef>.
|
|
90
|
|
91 =back
|
|
92
|
|
93 =cut |