Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Schema/Validator/Compare.pm @ 360:39842eedd923
language detection from request
author | sergey |
---|---|
date | Tue, 26 Nov 2013 03:22:44 +0400 |
parents | 89179bb8c388 |
children | 5aff94ba842f |
rev | line source |
---|---|
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
1 package IMPL::DOM::Schema::Validator::Compare; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
2 use strict; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
3 |
165 | 4 use parent qw(IMPL::DOM::Schema::Validator); |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
5 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
6 use IMPL::Resources::Format qw(FormatMessage); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
7 use IMPL::Class::Property; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
8 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
9 BEGIN { |
194 | 10 public property targetProperty => prop_all; |
11 public property op => prop_all; | |
12 public property nodePath => prop_all; | |
13 public property optional => prop_all; | |
14 private property _pathTranslated => prop_all; | |
15 private property _targetNode => prop_all; | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
16 private property _sourceSchema => prop_all; |
194 | 17 public property message => prop_all; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
18 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
19 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
20 our %CTOR = ( |
194 | 21 'IMPL::DOM::Schema::Validator' => sub { |
22 my %args = @_; | |
23 $args{nodeName} ||= 'Compare'; | |
24 delete @args{qw(targetProperty op nodePath optional message)}; | |
25 %args; | |
26 } | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
27 ); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
28 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
29 our %Ops = ( |
194 | 30 '=' => \&_equals, |
31 'eq' => \&_equalsString, | |
32 '!=' => \&_notEquals, | |
33 'ne' => \&_notEqualsString, | |
34 '=~' => \&_matchRx, | |
35 '!~' => \&_notMatchRx, | |
36 '<' => \&_less, | |
37 '>' => \&_greater, | |
38 'lt' => \&_lessString, | |
39 'gt' => \&_greaterString | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
40 ); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
41 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
42 my $rxOps = map qr/$_/, join( '|', keys %Ops ); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
43 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
44 sub CTOR { |
194 | 45 my ($this,%args) = @_; |
46 | |
47 $this->targetProperty($args{targetProperty} || 'nodeValue'); | |
48 $this->op( $Ops{ $args{op} || '=' } ) or die new IMPL::InvalidArgumentException("Invalid parameter value",'op',$args{op},$this->path); | |
49 $this->nodePath($args{nodePath}) or die new IMPL::InvalidArgumentException("The argument is required", 'nodePath', $this->path); | |
238 | 50 $this->message($args{message} || 'The value of %node.path% %source.op% %value% (%source.nodePath%)' ); |
194 | 51 $this->optional($args{optional}) if $args{optional}; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
52 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
53 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
54 sub TranslatePath { |
194 | 55 my ($this,$path) = @_; |
56 | |
57 $path ||= ''; | |
58 | |
59 my @selectQuery; | |
60 | |
61 my $i = 0; | |
62 | |
63 foreach my $chunk (split /\//,$path) { | |
64 $chunk = 'document:*' if $i == 0 and not length $chunk; | |
65 next if not length $chunk; | |
66 | |
67 my $query; | |
68 my ($axis,$filter) = ( $chunk =~ /^(?:(\w+):)?(.*)$/); | |
69 | |
70 if ($filter =~ /^\w+|\*$/ ) { | |
71 $query = $filter eq '*' ? undef : $filter; | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
72 } elsif ( $filter =~ /^(\w+|\*)\s*((?:\[\s*\w+\s*(?:=|!=|=~|!~|eq|ne|lt|gt)\s*["'](?:[^\\'"]|\\[\\"'])*["']\])+)$/) { |
194 | 73 my ($nodeName,$filterArgs) = ($1,$2); |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
74 |
194 | 75 |
76 my @parsedFilters = map { | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
77 my ($prop,$op,$value) = ($_ =~ /\s*(\w+)\s*(=|!=|=~|!~|eq|ne|lt|gt)\s*(?:["']((?:[^\\'"]|\\[\\"'])*)["'])/); |
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
78 |
194 | 79 $value =~ s/\\[\\'"]/$1/g; |
80 { | |
81 prop => $prop, | |
82 op => $Ops{$op}, | |
83 value => $value | |
84 } | |
85 } grep ( $_, split ( /[\]\[]+/,$filterArgs ) ); | |
86 | |
87 $query = sub { | |
88 my ($node) = shift; | |
89 | |
90 $node->nodeName eq $nodeName or return 0 if $nodeName ne '*'; | |
91 $_->{op}->( | |
92 _resovleProperty($node,$_->{prop}), | |
93 FormatMessage($_->{value},{ | |
94 Schema => $this->parentNode, | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
95 Node => $this->_targetNode, |
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
96 schema => $this->parentNode, |
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
97 node => $this->_targetNode, |
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
98 source => $this->_sourceSchema |
194 | 99 },\&_resovleProperty) |
100 ) or return 0 foreach @parsedFilters; | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
101 return 1; |
194 | 102 }; |
103 } else { | |
104 die new IMPL::Exception("Invalid query syntax",$path,$chunk); | |
105 } | |
106 | |
107 push @selectQuery, $axis ? { $axis => $query } : $query; | |
108 | |
109 $i++; | |
110 } | |
111 | |
112 return \@selectQuery; | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
113 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
114 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
115 sub Validate { |
194 | 116 my ($this,$node,$ctx) = @_; |
117 | |
118 my @result; | |
119 | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
120 my $Source = $ctx && $ctx->{Source} || $this->parentNode; |
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
121 |
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
122 $this->_sourceSchema($Source); |
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
123 |
194 | 124 $this->_targetNode($node); |
125 | |
126 my $query = $this->_pathTranslated() || $this->_pathTranslated($this->TranslatePath($this->nodePath)); | |
127 | |
128 my ($foreignNode) = $node->selectNodes(@$query); | |
129 | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
130 |
194 | 131 |
132 if ($foreignNode) { | |
133 my $value = $this->nodeValue; | |
134 | |
135 if ($value) { | |
136 $value = FormatMessage($value, { Schema => $this->parentNode, Node => $this->_targetNode, ForeignNode => $foreignNode },\&_resovleProperty); | |
137 } else { | |
138 $value = $foreignNode->nodeValue; | |
139 } | |
140 | |
141 push @result, new IMPL::DOM::Schema::ValidationError( | |
236 | 142 node => $node, |
143 foreignNode => $foreignNode, | |
144 value => $value, | |
145 source => $Source, | |
146 schema => $this->parentNode, | |
147 message => $this->message | |
194 | 148 ) unless $this->op->(_resovleProperty($node,$this->targetProperty),$value); |
149 } elsif (not $this->optional) { | |
150 push @result, new IMPL::DOM::Schema::ValidationError( | |
236 | 151 node => $node, |
152 value => '', | |
153 source => $Source, | |
154 schema => $this->parentNode, | |
155 message => $this->message | |
194 | 156 ); |
157 } | |
158 | |
159 $this->_targetNode(undef); | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
238
diff
changeset
|
160 $this->_sourceSchema(undef); |
194 | 161 |
162 return @result; | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
163 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
164 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
165 sub _resovleProperty { |
194 | 166 my ($node,$prop) = @_; |
167 | |
168 return $node->can($prop) ? $node->$prop() : $node->nodeProperty($prop); | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
169 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
170 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
171 sub _matchRx { |
194 | 172 $_[0] =~ $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
173 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
174 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
175 sub _notMatchRx { |
194 | 176 $_[0] !~ $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
177 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
178 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
179 sub _equals { |
194 | 180 $_[0] == $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
181 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
182 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
183 sub _notEquals { |
194 | 184 $_[0] != $_[0]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
185 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
186 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
187 sub _equalsString { |
194 | 188 $_[0] eq $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
189 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
190 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
191 sub _notEqualsString { |
194 | 192 $_[0] ne $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
193 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
194 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
195 sub _less { |
194 | 196 $_[0] < $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
197 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
198 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
199 sub _greater { |
194 | 200 $_[0] > $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
201 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
202 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
203 sub _lessString { |
194 | 204 $_[0] lt $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
205 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
206 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
207 sub _greaterString { |
194 | 208 $_[0] gt $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
209 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
210 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
211 sub _lessEq { |
194 | 212 $_[0] <= $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
213 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
214 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
215 sub _greaterEq { |
194 | 216 $_[0] >= $_[1]; |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
217 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
218 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
219 1; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
220 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
221 __END__ |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
222 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
223 =pod |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
224 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
225 =head1 NAME |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
226 |
180 | 227 C<IMPL::DOM::Schema::Validator::Compare> - ограничение на содержимое текущего узла, |
228 сравнивая его со значением другого узла. | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
229 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
230 =head1 SYNOPSIS |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
231 |
180 | 232 Пример типа описания поля с проверочным полем |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
233 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
234 =begin code xml |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
235 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
236 <schema> |
194 | 237 <SimpleType type="retype_field"> |
238 <Property name="linkedNode" message="Для узла %Node.nodeName% необходимо задать свойство %Source.name%"/> | |
239 <Compare op="eq" nodePath="sibling:*[nodeName eq '%Node.linkedNode%']"/> | |
240 </SimpleType> | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
241 </schema> |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
242 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
243 =begin code xml |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
244 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
245 =head1 DESCRIPTION |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
246 |
180 | 247 Позволяет сравнивать значение текущего узла со значением другого узла. |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
diff
changeset
|
248 |
180 | 249 =cut |