view Lib/IMPL/DOM/Schema/Property.pm @ 245:7c517134c42f

Added Unsupported media type Web exception corrected resourceLocation setting in the resource Implemented localizable resources for text messages fixed TT view scopings, INIT block in controls now sets globals correctly.
author sergey
date Mon, 29 Oct 2012 03:15:22 +0400
parents b8c724f6de36
children 0f59b2de72af
line wrap: on
line source

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

use parent qw(IMPL::DOM::Schema::SimpleNode);
require IMPL::DOM::Schema;
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;
    }
);

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;