annotate Lib/IMPL/DOM/Node.pm @ 16:75d55f4ee263

Окончательная концепция описания схем и построения DOM документов
author Sergey
date Tue, 08 Sep 2009 17:29:07 +0400
parents 65a7bb156fb7
children 818c74b038ae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package IMPL::DOM::Node;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use warnings;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4
11
75980091813b DOM и навигация
Sergey
parents: 7
diff changeset
5 use base qw(IMPL::Object);
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
7 use IMPL::Object::List;
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 use IMPL::Class::Property;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 use IMPL::Class::Property::Direct;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 use Scalar::Util qw(weaken);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11
1
3b418b134d8c ORM in progress
Sergey
parents: 0
diff changeset
12 use IMPL::Exception;
3b418b134d8c ORM in progress
Sergey
parents: 0
diff changeset
13
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 BEGIN {
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
15 public _direct property nodeName => prop_get | owner_set;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
16 public _direct property isComplex => { get => \&_getIsComplex } ;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
17 public _direct property nodeValue => prop_all;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
18 public _direct property childNodes => { get => \&_getChildNodes };
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
19 public _direct property parentNode => prop_get ;
6
e2cd73ccc5bd DOM In development
Sergey
parents: 4
diff changeset
20 private _direct property _propertyMap => prop_get ;
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 sub CTOR {
11
75980091813b DOM и навигация
Sergey
parents: 7
diff changeset
24 my ($this,%args) = @_;
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25
11
75980091813b DOM и навигация
Sergey
parents: 7
diff changeset
26 $this->nodeName($args{nodeName}) or die new IMPL::InvalidArgumentException("A name is required");
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 sub insertNode {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 my ($this,$node,$pos) = @_;
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
31
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
32 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
33
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
34 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
35
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
36 $this->childNodes->InsertAt($pos,$node);
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
37
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
38 $node->_setParent( $this );
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
39
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
40 return $node;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
41 }
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
42
14
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
43 sub appendNode {
16
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
44 my ($this,$node) = @_;
14
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
45
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
46 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
47
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
48 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
49
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
50 $this->childNodes->Append($node);
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
51
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
52 $node->_setParent( $this );
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
53
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
54 return $node;
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
55 }
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
56
16
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
57 sub appendRange {
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
58 my ($this,@range) = @_;
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
59
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
60 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if grep $_ == $this, @range;
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
61
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
62 foreach my $node (@range) {
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
63 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
64 $node->_setParent( $this );
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
65 }
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
66
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
67 $this->childNodes->Append(@range);
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
68
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
69 return $this;
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
70 }
75d55f4ee263 Окончательная концепция описания схем и построения DOM документов
Sergey
parents: 14
diff changeset
71
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
72 sub _getChildNodes {
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
73 my ($this) = @_;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
74
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
75 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
76 $this->{$childNodes};
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 sub removeNode {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 my ($this,$node) = @_;
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
81
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
82 if ($this == $node->{$parentNode}) {
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
83 $this->childNodes->RemoveItem($node);
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
84 $node->{$parentNode} = undef;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
85 return $this;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
86 } else {
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
87 die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
88 }
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90
7
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
91 sub replaceNodeAt {
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
92 my ($this,$index,$node) = @_;
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
93
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
94 my $nodeOld = $this->childNodes->[$index];
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
95
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
96 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
97
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
98 # unlink node from previous parent
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
99 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
100
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
101 # replace (or set) old node
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
102 $this->childNodes->[$index] = $node;
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
103
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
104 # save new parent
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
105 $node->_setParent( $this );
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
106
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
107 # unlink old node if we have one
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
108 $nodeOld->{$parentNode} = undef if $nodeOld;
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
109
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
110 # return old node
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
111 return $nodeOld;
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
112 }
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
113
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 sub removeAt {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 my ($this,$pos) = @_;
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
116
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
117 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
118 $node->{$parentNode} = undef;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
119 return $node;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
120 } else {
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
121 return undef;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
122 }
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125 sub selectNodes {
14
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
126 my ($this,$query) = @_;
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
127 my @result;
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
128
14
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
129 if (ref $query eq 'CODE') {
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
130 @result = grep &$query($_), @{$this->childNodes};
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
131 } else {
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
132 @result = grep $_->nodeName eq $query, @{$this->childNodes};
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
133 }
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
134
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
135 return wantarray ? @result : \@result;
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137
7
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
138 sub firstChild {
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
139 @_ >=2 ? $_[0]->replaceNodeAt(0,$_[1]) : $_[0]->childNodes->[0];
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
140 }
94d47b388442 Улучшены тесты
Sergey
parents: 6
diff changeset
141
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
142 sub _getIsComplex {
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
143 $_[0]->childNodes->Count ? 1 : 0;
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
144 }
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
145
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
146 sub _setParent {
11
75980091813b DOM и навигация
Sergey
parents: 7
diff changeset
147 my ($this,$node) = @_;
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
148
11
75980091813b DOM и навигация
Sergey
parents: 7
diff changeset
149 $this->{$parentNode} = $node;
14
65a7bb156fb7 Дом модель и схема
Sergey
parents: 11
diff changeset
150 # prevent from creating cyclicreferences
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
151 weaken($this->{$parentNode});
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
152 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
153
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
154 sub text {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
155 my ($this) = @_;
4
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
156
e59f44f75f20 DOM - в разработке
Sergey
parents: 1
diff changeset
157 join '', $this->nodeValue, map $_->nodeValue, @{$this->childNodes};
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
158 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
159
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
160 sub Property {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
161 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
162 my $name = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
163
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
164 if (@_) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
165 # set
6
e2cd73ccc5bd DOM In development
Sergey
parents: 4
diff changeset
166 return $this->{$_propertyMap}{$name} = shift;
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
167 } else {
6
e2cd73ccc5bd DOM In development
Sergey
parents: 4
diff changeset
168 return $this->{$_propertyMap}{$name};
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
169 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
170 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
171
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
172 1;