Mercurial > pub > Impl
diff Lib/IMPL/DOM/Schema/Property.pm @ 389:5aff94ba842f
DOM Schema refactoring complete
author | cin |
---|---|
date | Wed, 12 Feb 2014 13:36:24 +0400 |
parents | ced5937ff21a |
children |
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Schema/Property.pm Tue Feb 11 20:22:01 2014 +0400 +++ b/Lib/IMPL/DOM/Schema/Property.pm Wed Feb 12 13:36:24 2014 +0400 @@ -2,56 +2,66 @@ use strict; use warnings; -use parent qw(IMPL::DOM::Schema::SimpleNode); -require IMPL::DOM::Node; -use IMPL::Class::Property; -use IMPL::DOM::Property qw(_dom); - -__PACKAGE__->PassThroughArgs; - -BEGIN { - public _dom property messageRequired => prop_all; -} - -our %CTOR = ( - 'IMPL::DOM::Schema::SimpleNode' => sub { - my %args = @_; - - $args{maxOccur} = 1; - $args{minOccur} = delete $args{optional} ? 0 : 1; - $args{nodeName} ||= 'Property'; - $args{messageInflateError} ||= "Failed to inflate a property '%schema.name%' of a node '%node.path%': %error.message%"; - - return %args; - } -); +use IMPL::declare { + require => { + Label => 'IMPL::DOM::Schema::Label', + DOMNode => 'IMPL::DOM::Node', + ValidationError => 'IMPL::DOM::Schema::ValidationError' + }, + base => [ + 'IMPL::DOM::Schema::SimpleNode' => sub { + my %args = @_; + + $args{maxOccur} = 1; + $args{minOccur} = delete $args{optional} ? 0 : 1; + $args{nodeName} ||= 'Property'; + + return %args; + } + ], + props => [ + messageRequired => { get => 1, set => 1, dom => 1 } + ] +}; sub CTOR { my ($this,%args) = @_; - $this->messageRequired($args{messageRequired} || 'A property %schema.name% is required in the %node.qname%'); + $this->messageRequired($args{messageRequired} || 'A property %schemaNode.name% is required in the %node.qname%'); } sub Validate { my ($this,$node,$ctx) = @_; - my $prop = $this->name; - # buld a pseudo node for the property value - my $nodeProp = new IMPL::DOM::Node(nodeName => '::property', nodeValue => eval { $node->$prop() } || $node->nodeProperty($prop)); + my $nodeValue = $node->nodeProperty($this->name); - if ($nodeProp->nodeValue) { - # we have a value so validate it - return $this->SUPER::Validate($nodeProp,$ctx); + if (length $nodeValue) { + # we have a value so validate it + + # buld a pseudo node for the property value + my $nodeProp = DOMNode->new(nodeName => '::property', nodeValue => $nodeValue); + + return $this->SUPER::Validate($nodeProp); + } elsif($this->minOccur) { # we don't have a value but it's a mandatory property - return new IMPL::DOM::Schema::ValidationError( - message => $this->messageRequired, + return ValidationError->new( + message => $this->_MakeLabel($this->messageRequired), node => $node, - schema => $this, - source => $ctx && $ctx->{Source} || $this + schemaNode => $this ); } return (); } +sub _MakeLabel { + my ($this,$label) = @_; + + if ($label =~ /^ID:(\w+)$/) { + return Label->new($this->document->stringMap, $1); + } else { + return $label; + } +} + 1;