49
|
1 package IMPL::DOM::Schema::NodeSet;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::DOM::Node);
|
|
6 use IMPL::Class::Property;
|
152
|
7 use IMPL::DOM::Property qw(_dom);
|
49
|
8
|
|
9 our %CTOR = (
|
|
10 'IMPL::DOM::Node' => sub { nodeName => 'NodeSet' }
|
|
11 );
|
|
12
|
|
13 BEGIN {
|
152
|
14 public _dom property messageUnexpected => prop_all;
|
|
15 public _dom property messageMax => prop_all;
|
|
16 public _dom property messageMin => prop_all;
|
49
|
17 }
|
|
18
|
|
19 sub CTOR {
|
|
20 my ($this,%args) = @_;
|
|
21
|
|
22 $this->messageMax( $args{messageMax} || 'Too many %Node.nodeName% nodes');
|
|
23 $this->messageMin( $args{messageMin} || '%Schema.name% nodes expected');
|
|
24 $this->messageUnexpected( $args{messageUnexpected} || 'A %Node.nodeName% isn\'t allowed in %Node.parentNode.path%');
|
|
25 }
|
|
26
|
|
27 sub Validate {
|
125
|
28 my ($this,$node,$ctx) = @_;
|
49
|
29
|
|
30 my @errors;
|
|
31
|
|
32 my %nodes;
|
|
33 my $anyNode;
|
125
|
34 my $sourceSchema = $ctx->{Source} || $this->parentNode;
|
|
35
|
49
|
36 foreach (@{$this->childNodes}) {
|
|
37 if ($_->isa('IMPL::DOM::Schema::AnyNode')) {
|
|
38 $anyNode = {Schema => $_, Min => $_->minOccur, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , Seen => 0 };
|
|
39 } else {
|
|
40 $nodes{$_->name} = {Schema => $_, Min => $_->minOccur, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , Seen => 0 };
|
|
41 }
|
|
42 }
|
|
43
|
|
44 foreach my $child ( @{$node->childNodes} ) {
|
|
45 if (my $info = $nodes{$child->nodeName} || $anyNode) {
|
|
46 $info->{Seen}++;
|
|
47 push @errors,new IMPL::DOM::Schema::ValidationError (
|
125
|
48 Source => $sourceSchema,
|
49
|
49 Node => $child,
|
102
|
50 Parent => $node,
|
49
|
51 Schema => $info->{Schema},
|
|
52 Message => $this->messageMax
|
|
53 ) if ($info->{Max} and $info->{Seen} > $info->{Max});
|
|
54
|
|
55 if (my @localErrors = $info->{Schema}->Validate($child)) {
|
|
56 push @errors,@localErrors;
|
|
57 }
|
|
58 } else {
|
|
59 push @errors, new IMPL::DOM::Schema::ValidationError (
|
125
|
60 Source => $sourceSchema,
|
49
|
61 Node => $child,
|
102
|
62 Parent => $node,
|
49
|
63 Message => $this->messageUnexpected
|
|
64 )
|
|
65 }
|
|
66 }
|
|
67
|
|
68 foreach my $info (values %nodes) {
|
|
69 push @errors, new IMPL::DOM::Schema::ValidationError (
|
125
|
70 Source => $sourceSchema,
|
49
|
71 Schema => $info->{Schema},
|
102
|
72 Parent => $node,
|
49
|
73 Message => $this->messageMin
|
|
74 ) if $info->{Min} > $info->{Seen};
|
|
75 }
|
|
76
|
|
77 return @errors;
|
|
78 }
|
|
79
|
|
80 1;
|
|
81
|
|
82 __END__
|
|
83
|
|
84 =pod
|
|
85
|
|
86 =head1 DESCRIPTION
|
|
87
|
|
88 Содержимое для сложного узла. Порядок не важен. Дочерними элементами могут быть
|
|
89 только C<IMPL::DOM::Schema::ComplexNode> и C<IMPL::DOM::Schema::SimpleNode>.
|
|
90
|
|
91 При проверке данного правила, проверяются имеющиеся элементы на соответсие схемы
|
|
92 и количества встречаемости, после чего проверяются количественные ограничения
|
|
93 для несуществующих элементов.
|
|
94
|
|
95 =cut
|