Mercurial > pub > Impl
diff Lib/IMPL/DOM/Schema/NodeSet.pm @ 389:5aff94ba842f
DOM Schema refactoring complete
author | cin |
---|---|
date | Wed, 12 Feb 2014 13:36:24 +0400 |
parents | b8c724f6de36 |
children |
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Schema/NodeSet.pm Tue Feb 11 20:22:01 2014 +0400 +++ b/Lib/IMPL/DOM/Schema/NodeSet.pm Wed Feb 12 13:36:24 2014 +0400 @@ -2,25 +2,28 @@ use strict; use warnings; -use parent qw(IMPL::DOM::Node); -use IMPL::Class::Property; -use IMPL::DOM::Property qw(_dom); - -our %CTOR = ( - 'IMPL::DOM::Node' => sub { nodeName => 'NodeSet' } -); - -BEGIN { - public _dom property messageUnexpected => prop_all; - public _dom property messageMax => prop_all; - public _dom property messageMin => prop_all; -} +use IMPL::Const qw(:prop); +use IMPL::declare { + require => { + Label => 'IMPL::DOM::Schema::Label', + ValidationError => 'IMPL::DOM::Schema::ValidationError', + AnyNode => '-IMPL::DOM::Schema::AnyNode' + }, + base => [ + 'IMPL::DOM::Node' => sub { nodeName => 'NodeSet' } + ], + props => [ + messageUnexpected => { get => 1, set => 1, dom => 1}, + messageMax => { get => 1, set => 1, dom => 1}, + messageMin => { get => 1, set => 1, dom => 1} + ] +}; sub CTOR { my ($this,%args) = @_; $this->messageMax( $args{messageMax} || 'Too many %node.nodeName% nodes'); - $this->messageMin( $args{messageMin} || '%schema.name% nodes expected'); + $this->messageMin( $args{messageMin} || '%schemaNode.name% nodes expected'); $this->messageUnexpected( $args{messageUnexpected} || 'A %node.nodeName% isn\'t allowed in %node.parentNode.path%'); } @@ -31,52 +34,58 @@ my %nodes; my $anyNode; - my $sourceSchema = $ctx->{Source} || $this->parentNode; foreach (@{$this->childNodes}) { - if ($_->isa('IMPL::DOM::Schema::AnyNode')) { - $anyNode = {Schema => $_, Min => $_->minOccur, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , Seen => 0 }; + if ($_->isa(AnyNode)) { + $anyNode = {schemaNode => $_, min => $_->minOccur, max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , seen => 0 }; } else { - $nodes{$_->name} = {Schema => $_, Min => $_->minOccur, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , Seen => 0 }; + $nodes{$_->name} = {schemaNode => $_, min => $_->minOccur, max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , seen => 0 }; } } foreach my $child ( @{$node->childNodes} ) { if (my $info = $nodes{$child->nodeName} || $anyNode) { - $info->{Seen}++; - push @errors,new IMPL::DOM::Schema::ValidationError ( - source => $sourceSchema, + $info->{seen}++; + push @errors,ValidationError->new( + schemaNode => $info->{schemaNode}, node => $child, parent => $node, - schema => $info->{Schema}, - message => $this->messageMax - ) if ($info->{Max} and $info->{Seen} > $info->{Max}); + message => $this->_MakeLabel($this->messageMax) + ) if ($info->{max} and $info->{seen} > $info->{max}); - if (my @localErrors = $info->{Schema}->Validate($child)) { + if (my @localErrors = $info->{schemaNode}->Validate($child)) { push @errors,@localErrors; } } else { - push @errors, new IMPL::DOM::Schema::ValidationError ( - source => $sourceSchema, + push @errors, ValidationError->new( node => $child, parent => $node, - message => $this->messageUnexpected + message => $this->_MakeLabel($this->messageUnexpected) ) } } foreach my $info (values %nodes) { - push @errors, new IMPL::DOM::Schema::ValidationError ( - source => $sourceSchema, - schema => $info->{Schema}, + push @errors, ValidationError->new( + schemaNode => $info->{schemaNode}, parent => $node, - message => $this->messageMin - ) if $info->{Min} > $info->{Seen}; + message => $this->_MakeLabel($this->messageMin) + ) if $info->{min} > $info->{seen}; } return @errors; } +sub _MakeLabel { + my ($this,$label) = @_; + + if ($label =~ /^ID:(\w+)$/) { + return Label->new($this->document->stringMap, $1); + } else { + return $label; + } +} + 1; __END__