Mercurial > pub > Impl
comparison Lib/IMPL/DOM/Schema/NodeSet.pm @ 19:1ca530e5c9c5
DOM схема, требует переработки в части схемы для описания схем. Автоверификация не проходит
author | Sergey |
---|---|
date | Fri, 11 Sep 2009 16:30:39 +0400 |
parents | 94d47b388442 |
children | 267460284fb3 |
comparison
equal
deleted
inserted
replaced
18:818c74b038ae | 19:1ca530e5c9c5 |
---|---|
1 package IMPL::DOM::Schema::NodeSet; | 1 package IMPL::DOM::Schema::NodeSet; |
2 use strict; | 2 use strict; |
3 use warnings; | 3 use warnings; |
4 | 4 |
5 use base qw(IMPL::DOM::Schema::Item); | 5 use base qw(IMPL::DOM::Node); |
6 use IMPL::Class::Property; | 6 use IMPL::Class::Property; |
7 | 7 |
8 our %CTOR = ( | |
9 'IMPL::DOM::Node' => sub { nodeName => 'NodeSet' } | |
10 ); | |
11 | |
8 BEGIN { | 12 BEGIN { |
9 public property UnexpectedMessage => prop_all; | 13 public property messageUnexpected => prop_all; |
10 public property MaxMessage => prop_all; | 14 public property messageMax => prop_all; |
11 public property MinMessage => prop_all; | 15 public property messageMin => prop_all; |
16 } | |
17 | |
18 sub CTOR { | |
19 my ($this,%args) = @_; | |
20 | |
21 $this->messageMax( $args{messageMax} || 'Too many %Node.nodeName% nodes'); | |
22 $this->messageMin( $args{messageMin} || '%Schema.nodeName% nodes expected'); | |
23 $this->messageUnexpected( $args{messageUnexpected} || 'A %Node.nodeName% isn\'t allowed here'); | |
12 } | 24 } |
13 | 25 |
14 sub Validate { | 26 sub Validate { |
15 my ($this,$node) = @_; | 27 my ($this,$node) = @_; |
16 | 28 |
17 my @errors; | 29 my @errors; |
18 | 30 |
19 my %nodes = map { | 31 my %nodes; |
20 $_->nodeName , | 32 my $anyNode; |
21 {Schema => $_, Min => $_->minOccur, Max => $_->maxOccur, Seen => 0 } | 33 foreach (@{$this->childNodes}) { |
22 } @{$this->childNodes}; | 34 if ($_->isa('IMPL::DOM::Schema::AnyNode')) { |
35 $anyNode = {Schema => $_, Min => $_->minOccur, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , Seen => 0 }; | |
36 } else { | |
37 $nodes{$_->nodeName} = {Schema => $_, Min => $_->minOccur, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur , Seen => 0 }; | |
38 } | |
39 } | |
23 | 40 |
24 foreach my $child ( @{$node->childNodes} ) { | 41 foreach my $child ( @{$node->childNodes} ) { |
25 if (my $info = $nodes{$child->nodeName}) { | 42 if (my $info = $nodes{$child->nodeName} || $anyNode) { |
26 $info->{Seen}++; | 43 $info->{Seen}++; |
27 push @errors,{ | 44 push @errors,new IMPL::DOM::Schema::VaidationError ( |
28 Error => 1, | |
29 Source => $this, | 45 Source => $this, |
30 Node => $child, | 46 Node => $child, |
31 Message => $this->MaxMessage | 47 Schema => $info->{Schema}, |
32 } if ($info->{Seen} > $info->{Max}); | 48 Message => $this->messageMax |
49 ) if ($info->{Max} and $info->{Seen} > $info->{Max}); | |
33 | 50 |
34 push @errors,$info->{Schema}->Validate($child); | 51 if (my @localErrors = $info->{Schema}->Validate($child)) { |
52 push @errors,@localErrors; | |
53 } | |
35 } else { | 54 } else { |
36 push @errors, { | 55 push @errors, new IMPL::DOM::Schema::VaidationError ( |
37 Error => 1, | |
38 Source => $this, | 56 Source => $this, |
39 Node => $child, | 57 Node => $child, |
40 Message => $this->UnexpectedMessage | 58 Schema => $info->{Schema}, |
41 } | 59 Message => $this->messageUnexpected |
60 ) | |
42 } | 61 } |
43 } | 62 } |
44 | 63 |
45 foreach my $info (values %nodes) { | 64 foreach my $info (values %nodes) { |
46 push @errors, { | 65 push @errors, new IMPL::DOM::Schema::VaidationError ( |
47 Error => 1, | |
48 Source => $this, | 66 Source => $this, |
49 Message => $this->MinMessage | 67 Schema => $info->{Schema}, |
50 } if $info->{Min} > $info->{Seen}; | 68 Node => $node, |
69 Message => $this->messageMin | |
70 ) if $info->{Min} > $info->{Seen}; | |
51 } | 71 } |
52 | 72 |
53 return @errors; | 73 return @errors; |
54 } | 74 } |
55 | 75 |