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%');
|
102
|
22 $this->messageNodesRequired($args{messageNodesRequired} || 'A %Schema.name% is required in the node %Parent.path%');
|
49
|
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,
|
102
|
41 Parent => $node,
|
49
|
42 Schema => $info->{Schema},
|
|
43 Source => $this
|
|
44 ) if $info->{Min} > $info->{Seen};
|
|
45
|
|
46 $info = shift @nodes;
|
|
47 }
|
|
48
|
|
49 # return error if no more children allowed
|
|
50 return new IMPL::DOM::Schema::ValidationError (
|
|
51 Message => $this->messageUnexpected,
|
|
52 Node => $child,
|
102
|
53 Parent => $node,
|
49
|
54 Source => $this
|
|
55 ) unless $info;
|
|
56
|
|
57 # it's ok, we found schema element for child
|
|
58 # but it may be any node or switching node wich would not satisfy current child
|
|
59
|
|
60 # validate
|
|
61 while (my @errors = $info->{Schema}->Validate($child)) {
|
|
62 if( $info->{anyNode} and $info->{Seen} >= $info->{Min} ) {
|
|
63 # in case of any or switch node, skip it if possible
|
|
64 next if $info = shift @nodes;
|
|
65 }
|
|
66 return @errors;
|
|
67 }
|
|
68
|
|
69 $info->{Seen}++;
|
|
70
|
|
71 # check count limits
|
|
72 return new IMPL::DOM::Schema::ValidationError (
|
|
73 Error => 1,
|
|
74 Message => $this->messageUnexpected,
|
|
75 Node => $child,
|
102
|
76 Parent => $node,
|
49
|
77 Source => $this,
|
|
78 ) if $info->{Max} and $info->{Seen} > $info->{Max};
|
|
79 }
|
|
80
|
|
81 # no more children left (but may be should :)
|
|
82 while ($info) {
|
|
83 return new IMPL::DOM::Schema::ValidationError (
|
|
84 Error => 1,
|
|
85 Message => $this->messageNodesRequired,
|
|
86 Source => $this,
|
102
|
87 Parent => $node,
|
49
|
88 Schema => $info->{Schema}
|
|
89 ) if $info->{Seen} < $info->{Min};
|
|
90
|
|
91 $info = shift @nodes;
|
|
92 }
|
|
93 return;
|
|
94 }
|
|
95
|
|
96 1;
|
|
97
|
|
98 __END__
|
|
99
|
|
100 =pod
|
|
101
|
|
102 =head1 DESCRIPTION
|
|
103
|
|
104 Содержимое для сложного узла. Порядок важен. Дочерними элементами могут быть
|
|
105 только C<IMPL::DOM::Schema::ComplexNode> и C<IMPL::DOM::Schema::SimpleNode>.
|
|
106
|
|
107 =cut
|