comparison 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
comparison
equal deleted inserted replaced
388:648dfaf642e0 389:5aff94ba842f
1 package IMPL::DOM::Schema::Property; 1 package IMPL::DOM::Schema::Property;
2 use strict; 2 use strict;
3 use warnings; 3 use warnings;
4 4
5 use parent qw(IMPL::DOM::Schema::SimpleNode); 5 use IMPL::declare {
6 require IMPL::DOM::Node; 6 require => {
7 use IMPL::Class::Property; 7 Label => 'IMPL::DOM::Schema::Label',
8 use IMPL::DOM::Property qw(_dom); 8 DOMNode => 'IMPL::DOM::Node',
9 9 ValidationError => 'IMPL::DOM::Schema::ValidationError'
10 __PACKAGE__->PassThroughArgs; 10 },
11 11 base => [
12 BEGIN { 12 'IMPL::DOM::Schema::SimpleNode' => sub {
13 public _dom property messageRequired => prop_all; 13 my %args = @_;
14 } 14
15 15 $args{maxOccur} = 1;
16 our %CTOR = ( 16 $args{minOccur} = delete $args{optional} ? 0 : 1;
17 'IMPL::DOM::Schema::SimpleNode' => sub { 17 $args{nodeName} ||= 'Property';
18 my %args = @_; 18
19 19 return %args;
20 $args{maxOccur} = 1; 20 }
21 $args{minOccur} = delete $args{optional} ? 0 : 1; 21 ],
22 $args{nodeName} ||= 'Property'; 22 props => [
23 $args{messageInflateError} ||= "Failed to inflate a property '%schema.name%' of a node '%node.path%': %error.message%"; 23 messageRequired => { get => 1, set => 1, dom => 1 }
24 24 ]
25 return %args; 25 };
26 }
27 );
28 26
29 sub CTOR { 27 sub CTOR {
30 my ($this,%args) = @_; 28 my ($this,%args) = @_;
31 29
32 $this->messageRequired($args{messageRequired} || 'A property %schema.name% is required in the %node.qname%'); 30 $this->messageRequired($args{messageRequired} || 'A property %schemaNode.name% is required in the %node.qname%');
33 } 31 }
34 32
35 sub Validate { 33 sub Validate {
36 my ($this,$node,$ctx) = @_; 34 my ($this,$node,$ctx) = @_;
37 35
38 my $prop = $this->name; 36 my $nodeValue = $node->nodeProperty($this->name);
39 # buld a pseudo node for the property value
40 my $nodeProp = new IMPL::DOM::Node(nodeName => '::property', nodeValue => eval { $node->$prop() } || $node->nodeProperty($prop));
41 37
42 if ($nodeProp->nodeValue) { 38 if (length $nodeValue) {
43 # we have a value so validate it 39 # we have a value so validate it
44 return $this->SUPER::Validate($nodeProp,$ctx); 40
41 # buld a pseudo node for the property value
42 my $nodeProp = DOMNode->new(nodeName => '::property', nodeValue => $nodeValue);
43
44 return $this->SUPER::Validate($nodeProp);
45
45 } elsif($this->minOccur) { 46 } elsif($this->minOccur) {
46 # we don't have a value but it's a mandatory property 47 # we don't have a value but it's a mandatory property
47 return new IMPL::DOM::Schema::ValidationError( 48 return ValidationError->new(
48 message => $this->messageRequired, 49 message => $this->_MakeLabel($this->messageRequired),
49 node => $node, 50 node => $node,
50 schema => $this, 51 schemaNode => $this
51 source => $ctx && $ctx->{Source} || $this
52 ); 52 );
53 } 53 }
54 return (); 54 return ();
55 } 55 }
56 56
57 sub _MakeLabel {
58 my ($this,$label) = @_;
59
60 if ($label =~ /^ID:(\w+)$/) {
61 return Label->new($this->document->stringMap, $1);
62 } else {
63 return $label;
64 }
65 }
66
57 1; 67 1;