Mercurial > pub > Impl
comparison Lib/IMPL/DOM/Schema/NodeList.pm @ 389:5aff94ba842f
DOM Schema refactoring complete
| author | cin |
|---|---|
| date | Wed, 12 Feb 2014 13:36:24 +0400 |
| parents | 648dfaf642e0 |
| children |
comparison
equal
deleted
inserted
replaced
| 388:648dfaf642e0 | 389:5aff94ba842f |
|---|---|
| 4 | 4 |
| 5 | 5 |
| 6 use IMPL::declare { | 6 use IMPL::declare { |
| 7 require => { | 7 require => { |
| 8 ValidationError => 'IMPL::DOM::Schema::ValidationError', | 8 ValidationError => 'IMPL::DOM::Schema::ValidationError', |
| 9 AnyNode => '-IMPL::DOM::Schema::AnyNode' | 9 AnyNode => '-IMPL::DOM::Schema::AnyNode', |
| 10 Label => 'IMPL::DOM::Schema::Label' | |
| 10 }, | 11 }, |
| 11 base => [ | 12 base => [ |
| 12 'IMPL::DOM::Node' => sub { nodeName => 'NodeList' } | 13 'IMPL::DOM::Node' => sub { nodeName => 'NodeList' } |
| 13 ], | 14 ], |
| 14 props => [ | 15 props => [ |
| 19 | 20 |
| 20 sub CTOR { | 21 sub CTOR { |
| 21 my ($this,%args) = @_; | 22 my ($this,%args) = @_; |
| 22 | 23 |
| 23 $this->messageUnexpected($args{messageUnexpected} || 'A %node.nodeName% isn\'t allowed in %node.parentNode.path%'); | 24 $this->messageUnexpected($args{messageUnexpected} || 'A %node.nodeName% isn\'t allowed in %node.parentNode.path%'); |
| 24 $this->messageNodesRequired($args{messageNodesRequired} || 'A %schema.name% is required in the node %parent.path%'); | 25 $this->messageNodesRequired($args{messageNodesRequired} || 'A %schemaNode.name% is required in the node %parent.path%'); |
| 25 } | 26 } |
| 26 | 27 |
| 27 sub Validate { | 28 sub Validate { |
| 28 my ($this,$node,$ctx) = @_; | 29 my ($this,$node,$ctx) = @_; |
| 29 | 30 |
| 36 foreach my $child ( @{$node->childNodes} ) { | 37 foreach my $child ( @{$node->childNodes} ) { |
| 37 #skip schema elements | 38 #skip schema elements |
| 38 while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) { | 39 while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) { |
| 39 # if possible of course :) | 40 # if possible of course :) |
| 40 return ValidationError->new ( | 41 return ValidationError->new ( |
| 41 message => $this->messageUnexpected, | 42 message => $this->_MakeLabel( $this->messageUnexpected ), |
| 42 node => $child, | 43 node => $child, |
| 43 parent => $node, | 44 parent => $node, |
| 44 schemaNode => $info->{schemaNode} | 45 schemaNode => $info->{schemaNode} |
| 45 ) if $info->{Min} > $info->{Seen}; | 46 ) if $info->{min} > $info->{seen}; # we trying to skip a schema node which has a quantifier |
| 46 | 47 |
| 47 $info = shift @nodes; | 48 $info = shift @nodes; |
| 48 } | 49 } |
| 49 | 50 |
| 50 # return error if no more children allowed | 51 # return error if no more children allowed |
| 51 return ValidationError->new ( | 52 return ValidationError->new ( |
| 52 message => $this->messageUnexpected, | 53 message => $this->_MakeLabel( $this->messageUnexpected ), |
| 53 node => $child, | 54 node => $child, |
| 54 parent => $node | 55 parent => $node |
| 55 ) unless $info; | 56 ) unless $info; |
| 56 | 57 |
| 57 # it's ok, we found schema element for child | 58 # 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 | 59 |
| 60 # validate | 60 # validate |
| 61 while (my @errors = $info->{schemaNode}->Validate($child)) { | 61 while (my @errors = $info->{schemaNode}->Validate( $child ) ) { |
| 62 if( $info->{anyNode} and $info->{Seen} >= $info->{Min} ) { | 62 if( $info->{anyNode} and $info->{seen} >= $info->{min} ) { |
| 63 # in case of any or switch node, skip it if possible | 63 # in case of any or switch node, skip it if possible |
| 64 next if $info = shift @nodes; | 64 next if $info = shift @nodes; |
| 65 } | 65 } |
| 66 return @errors; | 66 return @errors; |
| 67 } | 67 } |
| 68 | 68 |
| 69 $info->{Seen}++; | 69 $info->{seen}++; |
| 70 | 70 |
| 71 # check count limits | 71 # check count limits |
| 72 return new IMPL::DOM::Schema::ValidationError ( | 72 return ValidationError->new( |
| 73 message => $this->messageUnexpected, | 73 message => $this->_MakeLabel( $this->messageUnexpected ), |
| 74 node => $child, | 74 node => $child, |
| 75 parent => $node, | 75 parent => $node, |
| 76 source => $sourceSchema, | 76 schemaNode => $info->{schemaNode}, |
| 77 ) if $info->{Max} and $info->{Seen} > $info->{Max}; | 77 ) if $info->{max} and $info->{seen} > $info->{max}; |
| 78 } | 78 } |
| 79 | 79 |
| 80 # no more children left (but may be should :) | 80 # no more children left (but may be should :) |
| 81 while ($info) { | 81 while ($info) { |
| 82 return new IMPL::DOM::Schema::ValidationError ( | 82 return ValidationError->new( |
| 83 error => 1, | 83 message => $this->_MakeLabel( $this->messageNodesRequired ), |
| 84 message => $this->messageNodesRequired, | |
| 85 source => $sourceSchema, | |
| 86 parent => $node, | 84 parent => $node, |
| 87 schema => $info->{Schema} | 85 schemaNode => $info->{schemaNode} |
| 88 ) if $info->{Seen} < $info->{Min}; | 86 ) if $info->{seen} < $info->{min}; |
| 89 | 87 |
| 90 $info = shift @nodes; | 88 $info = shift @nodes; |
| 91 } | 89 } |
| 92 return; | 90 return; |
| 91 } | |
| 92 | |
| 93 sub _MakeLabel { | |
| 94 my ($this,$label) = @_; | |
| 95 | |
| 96 if ($label =~ /^ID:(\w+)$/) { | |
| 97 return Label->new($this->document->stringMap, $1); | |
| 98 } else { | |
| 99 return $label; | |
| 100 } | |
| 93 } | 101 } |
| 94 | 102 |
| 95 1; | 103 1; |
| 96 | 104 |
| 97 __END__ | 105 __END__ |
