Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Node.pm @ 111:6c25ea91c985
ControllerUnit concept
author | wizard |
---|---|
date | Tue, 18 May 2010 01:33:37 +0400 |
parents | ddf0f037d460 |
children | 7b14e0122b79 |
rev | line source |
---|---|
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; | |
102 | 19 public _direct property childNodes => { get => \&_getChildNodes }; # prop_list |
49 | 20 public _direct property parentNode => prop_get ; |
21 private _direct property _propertyMap => prop_all ; | |
22 } | |
23 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
24 our %Axes = ( |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
25 parent => \&selectParent, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
26 siblings => \&selectSiblings, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
27 child => \&childNodes, |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
28 document => \&selectDocument |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
29 ); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
30 |
49 | 31 sub CTOR { |
32 my ($this,%args) = @_; | |
33 | |
34 $this->{$nodeName} = delete $args{nodeName} or die new IMPL::InvalidArgumentException("A name is required"); | |
35 $this->{$nodeValue} = delete $args{nodeValue} if exists $args{nodeValue}; | |
36 if ( exists $args{document} ) { | |
37 $this->{$document} = delete $args{document}; | |
38 weaken($this->{$document}); | |
39 } | |
40 | |
41 $this->{$_propertyMap} = \%args; | |
42 } | |
43 | |
44 sub insertNode { | |
45 my ($this,$node,$pos) = @_; | |
46 | |
47 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node; | |
48 | |
49 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode}); | |
50 | |
51 $this->childNodes->InsertAt($pos,$node); | |
52 | |
53 $node->_setParent( $this ); | |
54 | |
55 return $node; | |
56 } | |
57 | |
58 sub appendChild { | |
59 my ($this,$node) = @_; | |
60 | |
61 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node; | |
62 | |
63 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode}); | |
64 | |
65 my $children = $this->childNodes; | |
66 $children->Append($node); | |
67 | |
68 $node->_setParent( $this ); | |
69 | |
70 return $node; | |
71 } | |
72 | |
73 sub appendNode { | |
74 goto &appendChild; | |
75 } | |
76 | |
77 sub appendRange { | |
78 my ($this,@range) = @_; | |
79 | |
80 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if grep $_ == $this, @range; | |
81 | |
82 foreach my $node (@range) { | |
83 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode}); | |
84 $node->_setParent( $this ); | |
85 } | |
86 | |
87 $this->childNodes->Append(@range); | |
88 | |
89 return $this; | |
90 } | |
91 | |
92 sub _getChildNodes { | |
93 my ($this) = @_; | |
94 | |
95 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes}; | |
102 | 96 return wantarray ? @{ $this->{$childNodes} } : $this->{$childNodes}; |
49 | 97 } |
98 | |
109
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
99 sub childNodesRef { |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
100 my ($this) = @_; |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
101 return scalar $this->_getChildNodes; |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
102 } |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
103 |
49 | 104 sub removeNode { |
105 my ($this,$node) = @_; | |
106 | |
107 if ($this == $node->{$parentNode}) { | |
108 $this->childNodes->RemoveItem($node); | |
109 $node->_setParent(undef); | |
110 return $node; | |
111 } else { | |
112 die new IMPL::InvalidOperationException("The specified node isn't belong to this node"); | |
113 } | |
114 } | |
115 | |
116 sub replaceNodeAt { | |
117 my ($this,$index,$node) = @_; | |
118 | |
119 my $nodeOld = $this->childNodes->[$index]; | |
120 | |
121 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node; | |
122 | |
123 # unlink node from previous parent | |
124 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode}); | |
125 | |
126 # replace (or set) old node | |
127 $this->childNodes->[$index] = $node; | |
128 | |
129 # set new parent | |
130 $node->_setParent( $this ); | |
131 | |
132 # unlink old node if we have one | |
133 $nodeOld->_setParent(undef) if $nodeOld; | |
134 | |
135 # return old node | |
136 return $nodeOld; | |
137 } | |
138 | |
139 sub removeAt { | |
140 my ($this,$pos) = @_; | |
141 | |
142 if ( my $node = $this->childNodes->RemoveAt($pos) ) { | |
143 $node->_setParent(undef); | |
144 return $node; | |
145 } else { | |
146 return undef; | |
147 } | |
148 } | |
149 | |
150 sub removeLast { | |
151 my ($this) = @_; | |
152 | |
153 if ( my $node = $this->{$childNodes} ? $this->{$childNodes}->RemoveLast() : undef) { | |
154 $node->_setParent(undef); | |
155 return $node; | |
156 } else { | |
157 return undef; | |
158 } | |
159 } | |
160 | |
161 sub removeSelected { | |
162 my ($this,$query) = @_; | |
163 | |
164 my @newSet; | |
165 my @result; | |
166 | |
167 if (ref $query eq 'CODE') { | |
168 &$query($_) ? push @result, $_ : push @newSet, $_ foreach @{$this->childNodes}; | |
169 } elsif (defined $query) { | |
170 $_->nodeName eq $query ? push @result, $_ : push @newSet, $_ foreach @{$this->childNodes}; | |
171 } else { | |
172 my $children = $this->childNodes; | |
173 $_->_setParent(undef) foreach @$children; | |
174 delete $this->{$childNodes}; | |
175 return wantarray ? @$children : $children; | |
176 } | |
177 | |
178 $_->_setParent(undef) foreach @result; | |
179 | |
180 $this->{$childNodes} = @newSet ? bless \@newSet ,'IMPL::Object::List' : undef; | |
181 | |
182 return wantarray ? @result : \@result; | |
183 } | |
184 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
185 sub resolveAxis { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
186 my ($this,$axis) = @_; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
187 return $Axes{$axis}->($this) |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
188 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
189 |
49 | 190 sub selectNodes { |
108 | 191 my $this = shift; |
192 my ($path) = @_; | |
193 | |
194 $path = ref $path eq 'ARRAY' ? $path : ( @_ == 1 ? $this->translatePath($path) : [@_]); | |
195 | |
196 my @set = ($this); | |
197 | |
198 while (my $query = shift @$path) { | |
199 @set = map $_->selectNodesAxis($query), @set; | |
200 } | |
201 | |
202 return wantarray ? @set : \@set; | |
203 } | |
204 | |
109
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
205 sub selectNodesRef { |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
206 my $this = shift; |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
207 |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
208 my @result = $this->selectNodes(@_); |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
209 return \@result; |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
210 } |
ddf0f037d460
IMPL::DOM::Node updated to support TT, (added childNodesRef and selectNodesRef for using from TT)
wizard
parents:
108
diff
changeset
|
211 |
108 | 212 sub translatePath { |
213 my ($this,$path) = @_; | |
214 | |
215 # TODO: Move path compilation here from IMPL::DOM::Schema::Validator::Compare | |
216 return [$path]; | |
217 } | |
218 | |
219 sub selectNodesAxis { | |
220 my ($this,$query,$axis) = @_; | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
221 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
222 $axis ||= 'child'; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
223 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
224 die new IMPL::InvalidOperationException('Unknown axis',$axis) unless exists $Axes{$axis}; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
225 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
226 my $nodes = $this->resolveAxis($axis); |
49 | 227 |
228 my @result; | |
229 | |
230 if (ref $query eq 'CODE') { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
231 @result = grep &$query($_), @{$nodes}; |
49 | 232 } elsif (ref $query eq 'ARRAY' ) { |
233 my %keys = map (($_,1),@$query); | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
234 @result = grep $keys{$_->nodeName}, @{$nodes}; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
235 } elsif (ref $query eq 'HASH') { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
236 while( my ($axis,$filter) = each %$query ) { |
108 | 237 push @result, $this->selectNodesAxis($filter,$axis); |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
238 } |
49 | 239 } elsif (defined $query) { |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
240 @result = grep $_->nodeName eq $query, @{$nodes}; |
49 | 241 } else { |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
242 return wantarray ? @{$nodes} : $nodes; |
49 | 243 } |
244 | |
245 return wantarray ? @result : \@result; | |
246 } | |
247 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
248 sub selectParent { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
249 my ($this) = @_; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
250 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
251 if ($this->parentNode) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
252 return wantarray ? $this->parentNode : [$this->parentNode]; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
253 } else { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
254 return wantarray ? () : []; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
255 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
256 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
257 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
258 sub selectSiblings { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
259 my ($this) = @_; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
260 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
261 if ($this->parentNode) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
262 return $this->parentNode->selectNodes( sub { $_ != $this } ); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
263 } else { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
264 return wantarray ? () : []; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
265 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
266 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
267 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
268 sub selectDocument { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
269 my ($this) = @_; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
270 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
271 if ($this->document) { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
272 return wantarray ? $this->document : [$this->document]; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
273 } else { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
274 return wantarray ? () : []; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
275 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
276 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
102
diff
changeset
|
277 |
49 | 278 sub firstChild { |
279 @_ >=2 ? $_[0]->replaceNodeAt(0,$_[1]) : $_[0]->childNodes->[0]; | |
280 } | |
281 | |
282 sub _getIsComplex { | |
283 $_[0]->childNodes->Count ? 1 : 0; | |
284 } | |
285 | |
286 sub _updateDocRefs { | |
287 my ($this) = @_; | |
288 | |
289 # this method is called by the parent node on his children, so we need no to check parent | |
290 $this->{$document} = $this->{$parentNode}->document; | |
291 | |
292 # prevent cyclic | |
293 weaken($this->{$document}) if $this->{$document}; | |
294 | |
295 $_->_updateDocRefs foreach @{$this->{$childNodes}}; | |
296 } | |
297 | |
298 sub _setParent { | |
299 my ($this,$node) = @_; | |
300 | |
301 | |
302 if (($node || 0) != ($this->{$parentNode} || 0)) { | |
303 my $newOwner; | |
304 if ($node) { | |
305 $this->{$parentNode} = $node; | |
306 $newOwner = $node->document || 0; | |
307 | |
308 # prevent from creating cyclicreferences | |
309 weaken($this->{$parentNode}); | |
310 | |
311 } else { | |
312 delete $this->{$parentNode}; | |
75 | 313 |
314 #keep document | |
315 $newOwner = $this->{$document}; | |
49 | 316 } |
317 | |
318 if (($this->{$document}||0) != $newOwner) { | |
319 $this->{$document} = $newOwner; | |
320 weaken($this->{$document}) if $newOwner; | |
321 $_->_updateDocRefs foreach @{$this->childNodes}; | |
322 } | |
323 } | |
324 } | |
325 | |
326 sub text { | |
327 my ($this) = @_; | |
328 | |
329 join ('', $this->nodeValue || '', map ($_->text || '', @{$this->childNodes})); | |
330 } | |
331 | |
332 sub nodeProperty { | |
333 my $this = shift; | |
334 my $name = shift; | |
335 | |
336 if (@_) { | |
337 # set | |
338 return $this->{$_propertyMap}{$name} = shift; | |
339 } else { | |
340 return $this->{$_propertyMap}{$name}; | |
341 } | |
342 } | |
343 | |
344 sub qname { | |
345 $_[0]->{$nodeName}; | |
346 } | |
347 | |
348 sub path { | |
349 my ($this) = @_; | |
350 | |
351 if ($this->parentNode) { | |
352 return $this->parentNode->path.'.'.$this->qname; | |
353 } else { | |
354 return $this->qname; | |
355 } | |
356 } | |
357 | |
358 1; | |
75 | 359 |
360 __END__ | |
361 | |
362 =pod | |
363 | |
364 =head1 NAME | |
365 | |
366 C<IMPL::DOM::Node> Элемент DOM модели | |
367 | |
368 =head1 DESCRIPTION | |
369 | |
370 Базовый узел DOM модели. От него можно наследовать другие элементы DOM модели. | |
371 | |
372 =head1 MEMBERS | |
373 | |
374 =head2 PROPERTIES | |
375 | |
376 =over | |
377 | |
378 =item C<[get] nodeName> | |
379 | |
380 Имя узла. Задается при создании. | |
381 | |
382 =item C<[get] document> | |
383 | |
384 Документ к которому принадлежит узел. Задается при поздании узла. | |
385 | |
386 =item C<[get] isComplex> | |
387 | |
388 Определяет является ли узел сложным (тоесть есть ли дети). | |
389 | |
390 C<true> - есть, C<false> - нет. | |
391 | |
392 =item C<[get,set] nodeValue> | |
393 | |
394 Значение узла, обычно простой скаляр, но ничто не мешает туда | |
395 устанавливать любое значение. | |
396 | |
397 =item C<[get,list] childNodes> | |
398 | |
399 Список детей, является списокм C<IMPL::Object::List>. | |
400 | |
401 =item C<[get] parentNode> | |
402 | |
403 Ссылка на родительский элемент, если таковой имеется. | |
404 | |
405 =head2 METHODS | |
406 | |
407 =cut |