annotate lib/IMPL/DOM/Schema/NodeList.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56: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::NodeList;
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
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 ValidationError => 'IMPL::DOM::Schema::ValidationError',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 AnyNode => '-IMPL::DOM::Schema::AnyNode',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 Label => 'IMPL::DOM::Schema::Label'
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 => 'NodeList' }
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 messageNodesRequired => { get => 1, set => 1, dom => 1}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 ]
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 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 my ($this,%args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 $this->messageUnexpected($args{messageUnexpected} || 'A %node.nodeName% isn\'t allowed in %node.parentNode.path%');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $this->messageNodesRequired($args{messageNodesRequired} || 'A %schemaNode.name% is required in the node %parent.path%');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 sub Validate {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 my ($this,$node,$ctx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 my @nodes = map {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 {nodeName => $_->name, anyNode => $_->isa(AnyNode) , schemaNode => $_, max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur, min => $_->minOccur, seen => 0 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 } @{$this->childNodes};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my $info = shift @nodes;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 foreach my $child ( @{$node->childNodes} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 #skip schema elements
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 # if possible of course :)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 return ValidationError->new (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 message => $this->_MakeLabel( $this->messageUnexpected ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 node => $child,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 parent => $node,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 schemaNode => $info->{schemaNode}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 ) if $info->{min} > $info->{seen}; # we trying to skip a schema node which has a quantifier
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $info = shift @nodes;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 # return error if no more children allowed
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 return ValidationError->new (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 message => $this->_MakeLabel( $this->messageUnexpected ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 node => $child,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 parent => $node
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 ) unless $info;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 # it's ok, we found schema element for child
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 # validate
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 while (my @errors = $info->{schemaNode}->Validate( $child ) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 if( $info->{anyNode} and $info->{seen} >= $info->{min} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 # in case of any or switch node, skip it if possible
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 next if $info = shift @nodes;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 return @errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 $info->{seen}++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 # check count limits
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 return ValidationError->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 message => $this->_MakeLabel( $this->messageUnexpected ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 node => $child,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 parent => $node,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 schemaNode => $info->{schemaNode},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 ) if $info->{max} and $info->{seen} > $info->{max};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 # no more children left (but may be should :)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 while ($info) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 return ValidationError->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 message => $this->_MakeLabel( $this->messageNodesRequired ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 parent => $node,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 schemaNode => $info->{schemaNode}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 ) if $info->{seen} < $info->{min};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 $info = shift @nodes;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 sub _MakeLabel {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 my ($this,$label) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 if ($label =~ /^ID:(\w+)$/) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 return Label->new($this->document->stringMap, $1);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 return $label;
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 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 Содержимое для сложного узла. Порядок важен. Дочерними элементами могут быть
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 только C<IMPL::DOM::Schema::ComplexNode> и C<IMPL::DOM::Schema::SimpleNode>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 =cut