0
|
1 package IMPL::DOM::Node;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
11
|
5 use base qw(IMPL::Object);
|
0
|
6
|
4
|
7 use IMPL::Object::List;
|
0
|
8 use IMPL::Class::Property;
|
|
9 use IMPL::Class::Property::Direct;
|
|
10 use Scalar::Util qw(weaken);
|
|
11
|
1
|
12 use IMPL::Exception;
|
|
13
|
0
|
14 BEGIN {
|
4
|
15 public _direct property nodeName => prop_get | owner_set;
|
|
16 public _direct property isComplex => { get => \&_getIsComplex } ;
|
|
17 public _direct property nodeValue => prop_all;
|
|
18 public _direct property childNodes => { get => \&_getChildNodes };
|
|
19 public _direct property parentNode => prop_get ;
|
6
|
20 private _direct property _propertyMap => prop_get ;
|
0
|
21 }
|
|
22
|
|
23 sub CTOR {
|
11
|
24 my ($this,%args) = @_;
|
0
|
25
|
11
|
26 $this->nodeName($args{nodeName}) or die new IMPL::InvalidArgumentException("A name is required");
|
0
|
27 }
|
|
28
|
|
29 sub insertNode {
|
|
30 my ($this,$node,$pos) = @_;
|
4
|
31
|
|
32 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
33
|
|
34 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
35
|
|
36 $this->childNodes->InsertAt($pos,$node);
|
|
37
|
|
38 $node->_setParent( $this );
|
|
39
|
|
40 return $node;
|
|
41 }
|
|
42
|
|
43 sub _getChildNodes {
|
|
44 my ($this) = @_;
|
|
45
|
|
46 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
|
|
47 $this->{$childNodes};
|
0
|
48 }
|
|
49
|
|
50 sub removeNode {
|
|
51 my ($this,$node) = @_;
|
4
|
52
|
|
53 if ($this == $node->{$parentNode}) {
|
|
54 $this->childNodes->RemoveItem($node);
|
|
55 $node->{$parentNode} = undef;
|
|
56 return $this;
|
|
57 } else {
|
|
58 die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
|
|
59 }
|
0
|
60 }
|
|
61
|
7
|
62 sub replaceNodeAt {
|
|
63 my ($this,$index,$node) = @_;
|
|
64
|
|
65 my $nodeOld = $this->childNodes->[$index];
|
|
66
|
|
67 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
68
|
|
69 # unlink node from previous parent
|
|
70 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
71
|
|
72 # replace (or set) old node
|
|
73 $this->childNodes->[$index] = $node;
|
|
74
|
|
75 # save new parent
|
|
76 $node->_setParent( $this );
|
|
77
|
|
78 # unlink old node if we have one
|
|
79 $nodeOld->{$parentNode} = undef if $nodeOld;
|
|
80
|
|
81 # return old node
|
|
82 return $nodeOld;
|
|
83 }
|
|
84
|
0
|
85 sub removeAt {
|
|
86 my ($this,$pos) = @_;
|
4
|
87
|
|
88 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
|
|
89 $node->{$parentNode} = undef;
|
|
90 return $node;
|
|
91 } else {
|
|
92 return undef;
|
|
93 }
|
0
|
94 }
|
|
95
|
|
96 sub selectNodes {
|
|
97 my ($this,$name) = @_;
|
4
|
98
|
|
99 my @result = grep $_->nodeName eq $name, @{$this->childNodes};
|
|
100
|
|
101 return wantarray ? @result : \@result;
|
0
|
102 }
|
|
103
|
7
|
104 sub firstChild {
|
|
105 @_ >=2 ? $_[0]->replaceNodeAt(0,$_[1]) : $_[0]->childNodes->[0];
|
|
106 }
|
|
107
|
4
|
108 sub _getIsComplex {
|
|
109 $_[0]->childNodes->Count ? 1 : 0;
|
|
110 }
|
|
111
|
|
112 sub _setParent {
|
11
|
113 my ($this,$node) = @_;
|
4
|
114
|
11
|
115 $this->{$parentNode} = $node;
|
4
|
116 weaken($this->{$parentNode});
|
0
|
117 }
|
|
118
|
|
119 sub text {
|
|
120 my ($this) = @_;
|
4
|
121
|
|
122 join '', $this->nodeValue, map $_->nodeValue, @{$this->childNodes};
|
0
|
123 }
|
|
124
|
|
125 sub Property {
|
|
126 my $this = shift;
|
|
127 my $name = shift;
|
|
128
|
|
129 if (@_) {
|
|
130 # set
|
6
|
131 return $this->{$_propertyMap}{$name} = shift;
|
0
|
132 } else {
|
6
|
133 return $this->{$_propertyMap}{$name};
|
0
|
134 }
|
|
135 }
|
|
136
|
|
137 1;
|