view Lib/IMPL/DOM/Schema/Property.pm @ 117:0475eb382085

Controls support (RC1)
author wizard
date Thu, 03 Jun 2010 02:41:44 +0400
parents 196bf443b5e1
children 1e7f03414b65
line wrap: on
line source

package IMPL::DOM::Schema::Property;
use strict;
use warnings;

use base qw(IMPL::DOM::Schema::SimpleNode);
require IMPL::DOM::Schema;
require IMPL::DOM::Node;
use IMPL::Class::Property;

__PACKAGE__->PassThroughArgs;

BEGIN {
    public 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;
    }
);

sub CTOR {
    my ($this,%args) = @_;
    
    $this->messageRequired($args{messageRequired} || 'A property %Schema.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));
        
    if ($nodeProp->nodeValue) {
    	# we have a value so validate it
    	return $this->SUPER::Validate($nodeProp,$ctx);
    } elsif($this->minOccur) {
    	# we don't have a value but it's a mandatory property
        return new IMPL::DOM::Schema::ValidationError(
            Message => $this->messageRequired,
            Node => $node,
            Schema => $this,
            Source => $ctx && $ctx->{Source} || $this
        );
    }
    return ();
}

1;