comparison lib/IMPL/DOM/Schema/Property.pm @ 407:c6e90e02dd17 ref20150831

renamed Lib->lib
author cin
date Fri, 04 Sep 2015 19:40:23 +0300
parents
children
comparison
equal deleted inserted replaced
406:f23fcb19d3c1 407:c6e90e02dd17
1 package IMPL::DOM::Schema::Property;
2 use strict;
3 use warnings;
4
5 use IMPL::declare {
6 require => {
7 Label => 'IMPL::DOM::Schema::Label',
8 DOMNode => 'IMPL::DOM::Node',
9 ValidationError => 'IMPL::DOM::Schema::ValidationError'
10 },
11 base => [
12 'IMPL::DOM::Schema::SimpleNode' => sub {
13 my %args = @_;
14
15 $args{maxOccur} = 1;
16 $args{minOccur} = delete $args{optional} ? 0 : 1;
17 $args{nodeName} ||= 'Property';
18
19 return %args;
20 }
21 ],
22 props => [
23 messageRequired => { get => 1, set => 1, dom => 1 }
24 ]
25 };
26
27 sub CTOR {
28 my ($this,%args) = @_;
29
30 $this->messageRequired($args{messageRequired} || 'A property %schemaNode.name% is required in the %node.qname%');
31 }
32
33 sub Validate {
34 my ($this,$node,$ctx) = @_;
35
36 my $nodeValue = $node->nodeProperty($this->name);
37
38 if (length $nodeValue) {
39 # we have a value so validate it
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
46 } elsif($this->minOccur) {
47 # we don't have a value but it's a mandatory property
48 return ValidationError->new(
49 message => $this->_MakeLabel($this->messageRequired),
50 node => $node,
51 schemaNode => $this
52 );
53 }
54 return ();
55 }
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
67 1;