49
|
1 package IMPL::DOM::Node;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Object);
|
|
6
|
|
7 use IMPL::Object::List;
|
|
8 use IMPL::Class::Property;
|
|
9 use IMPL::Class::Property::Direct;
|
|
10 use Scalar::Util qw(weaken);
|
|
11
|
|
12 use IMPL::Exception;
|
|
13
|
|
14 BEGIN {
|
|
15 public _direct property nodeName => prop_get;
|
|
16 public _direct property document => prop_get;
|
|
17 public _direct property isComplex => { get => \&_getIsComplex } ;
|
|
18 public _direct property nodeValue => prop_all;
|
|
19 public _direct property childNodes => { get => \&_getChildNodes };
|
|
20 public _direct property parentNode => prop_get ;
|
|
21 private _direct property _propertyMap => prop_all ;
|
|
22 }
|
|
23
|
|
24 sub CTOR {
|
|
25 my ($this,%args) = @_;
|
|
26
|
|
27 $this->{$nodeName} = delete $args{nodeName} or die new IMPL::InvalidArgumentException("A name is required");
|
|
28 $this->{$nodeValue} = delete $args{nodeValue} if exists $args{nodeValue};
|
|
29 if ( exists $args{document} ) {
|
|
30 $this->{$document} = delete $args{document};
|
|
31 weaken($this->{$document});
|
|
32 }
|
|
33
|
|
34 $this->{$_propertyMap} = \%args;
|
|
35 }
|
|
36
|
|
37 sub insertNode {
|
|
38 my ($this,$node,$pos) = @_;
|
|
39
|
|
40 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
41
|
|
42 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
43
|
|
44 $this->childNodes->InsertAt($pos,$node);
|
|
45
|
|
46 $node->_setParent( $this );
|
|
47
|
|
48 return $node;
|
|
49 }
|
|
50
|
|
51 sub appendChild {
|
|
52 my ($this,$node) = @_;
|
|
53
|
|
54 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
55
|
|
56 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
57
|
|
58 my $children = $this->childNodes;
|
|
59 $children->Append($node);
|
|
60
|
|
61 $node->_setParent( $this );
|
|
62
|
|
63 return $node;
|
|
64 }
|
|
65
|
|
66 sub appendNode {
|
|
67 goto &appendChild;
|
|
68 }
|
|
69
|
|
70 sub appendRange {
|
|
71 my ($this,@range) = @_;
|
|
72
|
|
73 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if grep $_ == $this, @range;
|
|
74
|
|
75 foreach my $node (@range) {
|
|
76 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
77 $node->_setParent( $this );
|
|
78 }
|
|
79
|
|
80 $this->childNodes->Append(@range);
|
|
81
|
|
82 return $this;
|
|
83 }
|
|
84
|
|
85 sub _getChildNodes {
|
|
86 my ($this) = @_;
|
|
87
|
|
88 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
|
|
89 return $this->{$childNodes};
|
|
90 }
|
|
91
|
|
92 sub removeNode {
|
|
93 my ($this,$node) = @_;
|
|
94
|
|
95 if ($this == $node->{$parentNode}) {
|
|
96 $this->childNodes->RemoveItem($node);
|
|
97 $node->_setParent(undef);
|
|
98 return $node;
|
|
99 } else {
|
|
100 die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
|
|
101 }
|
|
102 }
|
|
103
|
|
104 sub replaceNodeAt {
|
|
105 my ($this,$index,$node) = @_;
|
|
106
|
|
107 my $nodeOld = $this->childNodes->[$index];
|
|
108
|
|
109 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
110
|
|
111 # unlink node from previous parent
|
|
112 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
113
|
|
114 # replace (or set) old node
|
|
115 $this->childNodes->[$index] = $node;
|
|
116
|
|
117 # set new parent
|
|
118 $node->_setParent( $this );
|
|
119
|
|
120 # unlink old node if we have one
|
|
121 $nodeOld->_setParent(undef) if $nodeOld;
|
|
122
|
|
123 # return old node
|
|
124 return $nodeOld;
|
|
125 }
|
|
126
|
|
127 sub removeAt {
|
|
128 my ($this,$pos) = @_;
|
|
129
|
|
130 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
|
|
131 $node->_setParent(undef);
|
|
132 return $node;
|
|
133 } else {
|
|
134 return undef;
|
|
135 }
|
|
136 }
|
|
137
|
|
138 sub removeLast {
|
|
139 my ($this) = @_;
|
|
140
|
|
141 if ( my $node = $this->{$childNodes} ? $this->{$childNodes}->RemoveLast() : undef) {
|
|
142 $node->_setParent(undef);
|
|
143 return $node;
|
|
144 } else {
|
|
145 return undef;
|
|
146 }
|
|
147 }
|
|
148
|
|
149 sub removeSelected {
|
|
150 my ($this,$query) = @_;
|
|
151
|
|
152 my @newSet;
|
|
153 my @result;
|
|
154
|
|
155 if (ref $query eq 'CODE') {
|
|
156 &$query($_) ? push @result, $_ : push @newSet, $_ foreach @{$this->childNodes};
|
|
157 } elsif (defined $query) {
|
|
158 $_->nodeName eq $query ? push @result, $_ : push @newSet, $_ foreach @{$this->childNodes};
|
|
159 } else {
|
|
160 my $children = $this->childNodes;
|
|
161 $_->_setParent(undef) foreach @$children;
|
|
162 delete $this->{$childNodes};
|
|
163 return wantarray ? @$children : $children;
|
|
164 }
|
|
165
|
|
166 $_->_setParent(undef) foreach @result;
|
|
167
|
|
168 $this->{$childNodes} = @newSet ? bless \@newSet ,'IMPL::Object::List' : undef;
|
|
169
|
|
170 return wantarray ? @result : \@result;
|
|
171 }
|
|
172
|
|
173 sub selectNodes {
|
|
174 my ($this,$query) = @_;
|
|
175
|
|
176 my @result;
|
|
177
|
|
178 if (ref $query eq 'CODE') {
|
|
179 @result = grep &$query($_), @{$this->childNodes};
|
|
180 } elsif (ref $query eq 'ARRAY' ) {
|
|
181 my %keys = map (($_,1),@$query);
|
|
182 @result = grep $keys{$_->nodeName}, @{$this->childNodes};
|
|
183 } elsif (defined $query) {
|
|
184 @result = grep $_->nodeName eq $query, @{$this->childNodes};
|
|
185 } else {
|
|
186 if (wantarray) {
|
|
187 return @{$this->childNodes};
|
|
188 } else {
|
|
189 @result = $this->childNodes;
|
|
190 return \@result;
|
|
191 }
|
|
192 }
|
|
193
|
|
194 return wantarray ? @result : \@result;
|
|
195 }
|
|
196
|
|
197 sub firstChild {
|
|
198 @_ >=2 ? $_[0]->replaceNodeAt(0,$_[1]) : $_[0]->childNodes->[0];
|
|
199 }
|
|
200
|
|
201 sub _getIsComplex {
|
|
202 $_[0]->childNodes->Count ? 1 : 0;
|
|
203 }
|
|
204
|
|
205 sub _updateDocRefs {
|
|
206 my ($this) = @_;
|
|
207
|
|
208 # this method is called by the parent node on his children, so we need no to check parent
|
|
209 $this->{$document} = $this->{$parentNode}->document;
|
|
210
|
|
211 # prevent cyclic
|
|
212 weaken($this->{$document}) if $this->{$document};
|
|
213
|
|
214 $_->_updateDocRefs foreach @{$this->{$childNodes}};
|
|
215 }
|
|
216
|
|
217 sub _setParent {
|
|
218 my ($this,$node) = @_;
|
|
219
|
|
220
|
|
221 if (($node || 0) != ($this->{$parentNode} || 0)) {
|
|
222 my $newOwner;
|
|
223 if ($node) {
|
|
224 $this->{$parentNode} = $node;
|
|
225 $newOwner = $node->document || 0;
|
|
226
|
|
227 # prevent from creating cyclicreferences
|
|
228 weaken($this->{$parentNode});
|
|
229
|
|
230 } else {
|
|
231 delete $this->{$parentNode};
|
|
232 $newOwner = 0;
|
|
233 }
|
|
234
|
|
235 if (($this->{$document}||0) != $newOwner) {
|
|
236 $this->{$document} = $newOwner;
|
|
237 weaken($this->{$document}) if $newOwner;
|
|
238 $_->_updateDocRefs foreach @{$this->childNodes};
|
|
239 }
|
|
240 }
|
|
241 }
|
|
242
|
|
243 sub text {
|
|
244 my ($this) = @_;
|
|
245
|
|
246 join ('', $this->nodeValue || '', map ($_->text || '', @{$this->childNodes}));
|
|
247 }
|
|
248
|
|
249 sub nodeProperty {
|
|
250 my $this = shift;
|
|
251 my $name = shift;
|
|
252
|
|
253 if (@_) {
|
|
254 # set
|
|
255 return $this->{$_propertyMap}{$name} = shift;
|
|
256 } else {
|
|
257 return $this->{$_propertyMap}{$name};
|
|
258 }
|
|
259 }
|
|
260
|
|
261 sub qname {
|
|
262 $_[0]->{$nodeName};
|
|
263 }
|
|
264
|
|
265 sub path {
|
|
266 my ($this) = @_;
|
|
267
|
|
268 if ($this->parentNode) {
|
|
269 return $this->parentNode->path.'.'.$this->qname;
|
|
270 } else {
|
|
271 return $this->qname;
|
|
272 }
|
|
273 }
|
|
274
|
|
275 1;
|