annotate lib/IMPL/DOM/Schema/NodeSet.pm @ 410:9335cf010b23 ref20150831

refactoring
author cin
date Mon, 14 Sep 2015 01:11:53 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::DOM::Schema::NodeSet;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 Label => 'IMPL::DOM::Schema::Label',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 ValidationError => 'IMPL::DOM::Schema::ValidationError',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 AnyNode => '-IMPL::DOM::Schema::AnyNode'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 'IMPL::DOM::Node' => sub { nodeName => 'NodeSet' }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 messageUnexpected => { get => 1, set => 1, dom => 1},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 messageMax => { get => 1, set => 1, dom => 1},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 messageMin => { get => 1, set => 1, dom => 1}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 my ($this,%args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $this->messageMax( $args{messageMax} || 'Too many %node.nodeName% nodes');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 $this->messageMin( $args{messageMin} || '%schemaNode.name% nodes expected');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $this->messageUnexpected( $args{messageUnexpected} || 'A %node.nodeName% isn\'t allowed in %node.parentNode.path%');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 sub Validate {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 my ($this,$node,$ctx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 my @errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my %nodes;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 my $anyNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 foreach (@{$this->childNodes}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 if ($_->isa(AnyNode)) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 $anyNode = {schemaNode => $_, min => $_->minOccur, max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , seen => 0 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 $nodes{$_->name} = {schemaNode => $_, min => $_->minOccur, max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , seen => 0 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 foreach my $child ( @{$node->childNodes} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 if (my $info = $nodes{$child->nodeName} || $anyNode) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $info->{seen}++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 push @errors,ValidationError->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 schemaNode => $info->{schemaNode},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 node => $child,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 parent => $node,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 message => $this->_MakeLabel($this->messageMax)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 ) if ($info->{max} and $info->{seen} > $info->{max});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 if (my @localErrors = $info->{schemaNode}->Validate($child)) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 push @errors,@localErrors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 push @errors, ValidationError->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 node => $child,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 parent => $node,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 message => $this->_MakeLabel($this->messageUnexpected)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 foreach my $info (values %nodes) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 push @errors, ValidationError->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 schemaNode => $info->{schemaNode},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 parent => $node,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 message => $this->_MakeLabel($this->messageMin)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 ) if $info->{min} > $info->{seen};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 return @errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 sub _MakeLabel {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 my ($this,$label) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 if ($label =~ /^ID:(\w+)$/) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 return Label->new($this->document->stringMap, $1);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 return $label;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 Содержимое для сложного узла. Порядок не важен. Дочерними элементами могут быть
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 только C<IMPL::DOM::Schema::ComplexNode> и C<IMPL::DOM::Schema::SimpleNode>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 При проверке данного правила, проверяются имеющиеся элементы на соответсие схемы
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 и количества встречаемости, после чего проверяются количественные ограничения
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 для несуществующих элементов.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 =cut