Mercurial > pub > Impl
comparison Lib/IMPL/DOM/Schema/NodeList.pm @ 24:7f00786f8210
Первая рабочая реазизация схемы и навигаторов
author | Sergey |
---|---|
date | Mon, 05 Oct 2009 00:48:49 +0400 |
parents | 267460284fb3 |
children | 16ada169ca75 |
comparison
equal
deleted
inserted
replaced
23:716b287d4795 | 24:7f00786f8210 |
---|---|
16 } | 16 } |
17 | 17 |
18 sub CTOR { | 18 sub CTOR { |
19 my ($this,%args) = @_; | 19 my ($this,%args) = @_; |
20 | 20 |
21 $this->messageUnexpected($args{messageUnexpected} || 'A %Node.nodeName% isn\'t allowed here'); | 21 $this->messageUnexpected($args{messageUnexpected} || 'A %Node.nodeName% isn\'t allowed in %Node.parentNode.path%'); |
22 $this->messageNodesRequired($args{messageNodesRequired} || 'A content of the node %Node.nodeName% is incomplete'); | 22 $this->messageNodesRequired($args{messageNodesRequired} || 'A %Schema.name% is required in the node %Node.path%'); |
23 } | 23 } |
24 | 24 |
25 sub Validate { | 25 sub Validate { |
26 my ($this,$node) = @_; | 26 my ($this,$node) = @_; |
27 | 27 |
28 my @nodes = map { | 28 my @nodes = map { |
29 {nodeName => $_->name, anyNode => $_->isa('IMPL::DOM::Schema::AnyNode') , Schema => $_, Min => $_->minOccur eq 'unbounded' ? undef : $_->maxOccur, Max => $_->maxOccur, Seen => 0 } | 29 {nodeName => $_->name, anyNode => $_->isa('IMPL::DOM::Schema::AnyNode') , Schema => $_, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur, Min => $_->minOccur, Seen => 0 } |
30 } @{$this->childNodes}; | 30 } @{$this->childNodes}; |
31 | 31 |
32 my $info = shift @nodes; | 32 my $info = shift @nodes; |
33 | 33 |
34 foreach my $child ( @{$node->childNodes} ) { | 34 foreach my $child ( @{$node->childNodes} ) { |
35 #skip schema elements | 35 #skip schema elements |
36 while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) { | 36 while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) { |
37 # if possible of course :) | 37 # if possible of course :) |
38 return new IMPL::DOM::Schema::VaidationError ( | 38 return new IMPL::DOM::Schema::ValidationError ( |
39 Message => $this->messageUnexpected, | 39 Message => $this->messageUnexpected, |
40 Node => $child, | 40 Node => $child, |
41 Schema => $info->{Schema}, | 41 Schema => $info->{Schema}, |
42 Source => $this | 42 Source => $this |
43 ) if $info->{Min} > $info->{Seen}; | 43 ) if $info->{Min} > $info->{Seen}; |
44 | 44 |
45 $info = shift @nodes; | 45 $info = shift @nodes; |
46 } | 46 } |
47 | 47 |
48 # return error if no more children allowed | 48 # return error if no more children allowed |
49 return new IMPL::DOM::Schema::VaidationError ( | 49 return new IMPL::DOM::Schema::ValidationError ( |
50 Message => $this->messageUnexpected, | 50 Message => $this->messageUnexpected, |
51 Node => $child, | 51 Node => $child, |
52 Source => $this | 52 Source => $this |
53 ) unless $info; | 53 ) unless $info; |
54 | 54 |
55 # it's ok, we found schema element for him | 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 | |
56 $info->{Seen}++; | 67 $info->{Seen}++; |
57 | 68 |
58 # check count limits | 69 # check count limits |
59 return new IMPL::DOM::Schema::VaidationError ( | 70 return new IMPL::DOM::Schema::ValidationError ( |
60 Error => 1, | 71 Error => 1, |
61 Message => $this->messageUnexpected, | 72 Message => $this->messageUnexpected, |
62 Node => $child, | 73 Node => $child, |
63 Source => $this, | 74 Source => $this, |
64 ) if $info->{Max} and $info->{Seen} > $info->{Max}; | 75 ) if $info->{Max} and $info->{Seen} > $info->{Max}; |
65 | |
66 # validate | |
67 if (my @errors = $info->{Schema}->Validate($child)) { | |
68 return @errors; | |
69 } | |
70 } | 76 } |
71 | 77 |
72 # no more children left (but may be should :) | 78 # no more children left (but may be should :) |
73 while ($info) { | 79 while ($info) { |
74 return new IMPL::DOM::Schema::VaidationError ( | 80 return new IMPL::DOM::Schema::ValidationError ( |
75 Error => 1, | 81 Error => 1, |
76 Message => $this->messageNodesRequired, | 82 Message => $this->messageNodesRequired, |
77 Node => $node, | 83 Node => $node, |
78 Source => $this | 84 Source => $this, |
85 Schema => $info->{Schema} | |
79 ) if $info->{Seen} < $info->{Min}; | 86 ) if $info->{Seen} < $info->{Min}; |
80 | 87 |
81 $info = shift @nodes; | 88 $info = shift @nodes; |
82 } | 89 } |
90 return; | |
83 } | 91 } |
84 | 92 |
85 1; | 93 1; |
86 | 94 |
87 __END__ | 95 __END__ |