annotate Lib/IMPL/DOM/Schema/NodeList.pm @ 14:65a7bb156fb7

Дом модель и схема
author Sergey
date Fri, 04 Sep 2009 16:38:15 +0400
parents fffb153be599
children 1ca530e5c9c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
1 package IMPL::DOM::Schema::NodeList;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
2 use strict;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
3 use warnings;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
4 use base qw(IMPL::DOM::Schema::Item);
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
5 use IMPL::Class::Property;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
6
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
7 BEGIN {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
8 public property MessageUnexpected => prop_all;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
9 public property MessageNodesRequired => prop_all;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
10 }
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
11
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
12 sub Validate {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
13 my ($this,$node) = @_;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
14
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
15 my @nodes = map {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
16 {nodeName => $_->nodeName, Schema => $_, Min => $_->minOccur, Max => $_->maxOccur, Seen => 0 }
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
17 } @{$this->childNodes};
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
18
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
19 my $info = shift @nodes;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
20
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
21 foreach my $child ( @{$node->childNodes} ) {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
22 #skip schema elements
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
23 while ($info and $info->{nodeName} ne $child->nodeName) {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
24 # if possible of course :)
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
25 return {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
26 Error => 1,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
27 Message => $this->MessageUnexpected,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
28 Node => $child,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
29 Source => $this
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
30 } if $info->{Min} > $info->{Seen};
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
31
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
32 $info = shift @nodes;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
33 }
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
34
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
35 # return error if no more children allowed
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
36 return {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
37 Error => 1,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
38 Message => $this->MessageUnexpected,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
39 Node => $child,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
40 Source => $this
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
41 } unless $info;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
42
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
43 # it's ok, we found schema element for him
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
44 $info->{Seen}++;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
45
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
46 # check count limits
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
47 return {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
48 Error => 1,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
49 Message => $this->MessageUnexpected,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
50 Node => $child,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
51 Source => $this,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
52 } if $info->{Seen} > $info->{Max};
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
53
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
54 # validate
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
55 if (my @errors = $info->{Schema}->Validate($child)) {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
56 return @errors;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
57 }
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
58 }
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
59
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
60 # no more children left (but may be should :)
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
61 while ($info) {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
62 return {
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
63 Error => 1,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
64 Message => $this->MessageNodesRequired,
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
65 Source => $this
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
66 } if $info->{Seen} < $info->{Min};
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
67
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
68 $info = shift @nodes;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
69 }
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
70 }
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
71
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
72 1;
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
73
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
74 __END__
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
75
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
76 =pod
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
77
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
78 =head1 DESCRIPTION
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
79
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
80 . .
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
81 C<IMPL::DOM::Schema::ComplexNode> C<IMPL::DOM::Schema::SimpleNode>.
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
82
fffb153be599 DOM Schema
Sergey
parents:
diff changeset
83 =cut