49
|
1 package IMPL::DOM::Schema::NodeList;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4 use base qw(IMPL::DOM::Node);
|
|
5
|
|
6 use IMPL::Class::Property;
|
|
7 require IMPL::DOM::Schema::ValidationError;
|
|
8
|
|
9 our %CTOR = (
|
|
10 'IMPL::DOM::Node' => sub { nodeName => 'NodeList' }
|
|
11 );
|
|
12
|
|
13 BEGIN {
|
|
14 public property messageUnexpected => prop_all;
|
|
15 public property messageNodesRequired => prop_all;
|
|
16 }
|
|
17
|
|
18 sub CTOR {
|
|
19 my ($this,%args) = @_;
|
|
20
|
|
21 $this->messageUnexpected($args{messageUnexpected} || 'A %Node.nodeName% isn\'t allowed in %Node.parentNode.path%');
|
|
22 $this->messageNodesRequired($args{messageNodesRequired} || 'A %Schema.name% is required in the node %Node.path%');
|
|
23 }
|
|
24
|
|
25 sub Validate {
|
|
26 my ($this,$node) = @_;
|
|
27
|
|
28 my @nodes = map {
|
|
29 {nodeName => $_->name, anyNode => $_->isa('IMPL::DOM::Schema::AnyNode') , Schema => $_, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur, Min => $_->minOccur, Seen => 0 }
|
|
30 } @{$this->childNodes};
|
|
31
|
|
32 my $info = shift @nodes;
|
|
33
|
|
34 foreach my $child ( @{$node->childNodes} ) {
|
|
35 #skip schema elements
|
|
36 while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) {
|
|
37 # if possible of course :)
|
|
38 return new IMPL::DOM::Schema::ValidationError (
|
|
39 Message => $this->messageUnexpected,
|
|
40 Node => $child,
|
|
41 Schema => $info->{Schema},
|
|
42 Source => $this
|
|
43 ) if $info->{Min} > $info->{Seen};
|
|
44
|
|
45 $info = shift @nodes;
|
|
46 }
|
|
47
|
|
48 # return error if no more children allowed
|
|
49 return new IMPL::DOM::Schema::ValidationError (
|
|
50 Message => $this->messageUnexpected,
|
|
51 Node => $child,
|
|
52 Source => $this
|
|
53 ) unless $info;
|
|
54
|
|
55 # it's ok, we found schema element for child
|
|
56 # but it may be any node or switching node wich would not satisfy current child
|
|
57
|
|
58 # validate
|
|
59 while (my @errors = $info->{Schema}->Validate($child)) {
|
|
60 if( $info->{anyNode} and $info->{Seen} >= $info->{Min} ) {
|
|
61 # in case of any or switch node, skip it if possible
|
|
62 next if $info = shift @nodes;
|
|
63 }
|
|
64 return @errors;
|
|
65 }
|
|
66
|
|
67 $info->{Seen}++;
|
|
68
|
|
69 # check count limits
|
|
70 return new IMPL::DOM::Schema::ValidationError (
|
|
71 Error => 1,
|
|
72 Message => $this->messageUnexpected,
|
|
73 Node => $child,
|
|
74 Source => $this,
|
|
75 ) if $info->{Max} and $info->{Seen} > $info->{Max};
|
|
76 }
|
|
77
|
|
78 # no more children left (but may be should :)
|
|
79 while ($info) {
|
|
80 return new IMPL::DOM::Schema::ValidationError (
|
|
81 Error => 1,
|
|
82 Message => $this->messageNodesRequired,
|
|
83 Source => $this,
|
|
84 Schema => $info->{Schema}
|
|
85 ) if $info->{Seen} < $info->{Min};
|
|
86
|
|
87 $info = shift @nodes;
|
|
88 }
|
|
89 return;
|
|
90 }
|
|
91
|
|
92 1;
|
|
93
|
|
94 __END__
|
|
95
|
|
96 =pod
|
|
97
|
|
98 =head1 DESCRIPTION
|
|
99
|
|
100 Содержимое для сложного узла. Порядок важен. Дочерними элементами могут быть
|
|
101 только C<IMPL::DOM::Schema::ComplexNode> и C<IMPL::DOM::Schema::SimpleNode>.
|
|
102
|
|
103 =cut
|