diff Lib/IMPL/DOM/Schema/NodeList.pm @ 24:7f00786f8210

Первая рабочая реазизация схемы и навигаторов
author Sergey
date Mon, 05 Oct 2009 00:48:49 +0400
parents 267460284fb3
children 16ada169ca75
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Schema/NodeList.pm	Wed Sep 30 17:43:52 2009 +0400
+++ b/Lib/IMPL/DOM/Schema/NodeList.pm	Mon Oct 05 00:48:49 2009 +0400
@@ -18,15 +18,15 @@
 sub CTOR {
     my ($this,%args) = @_;
     
-    $this->messageUnexpected($args{messageUnexpected} || 'A %Node.nodeName% isn\'t allowed here');
-    $this->messageNodesRequired($args{messageNodesRequired} || 'A content of the node %Node.nodeName% is incomplete');
+    $this->messageUnexpected($args{messageUnexpected} || 'A %Node.nodeName% isn\'t allowed in %Node.parentNode.path%');
+    $this->messageNodesRequired($args{messageNodesRequired} || 'A %Schema.name% is required in the node %Node.path%');
 }
 
 sub Validate {
     my ($this,$node) = @_;
     
     my @nodes = map {
-        {nodeName => $_->name, anyNode => $_->isa('IMPL::DOM::Schema::AnyNode') , Schema => $_, Min => $_->minOccur eq 'unbounded' ? undef : $_->maxOccur, Max => $_->maxOccur, Seen => 0 }
+        {nodeName => $_->name, anyNode => $_->isa('IMPL::DOM::Schema::AnyNode') , Schema => $_, Max => $_->maxOccur eq 'unbounded' ? undef : $_->maxOccur, Min => $_->minOccur, Seen => 0 }
     } @{$this->childNodes};
     
     my $info = shift @nodes;
@@ -35,7 +35,7 @@
         #skip schema elements
         while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) {
             # if possible of course :)
-            return new IMPL::DOM::Schema::VaidationError (
+            return new IMPL::DOM::Schema::ValidationError (
                 Message => $this->messageUnexpected,
                 Node => $child,
                 Schema => $info->{Schema},
@@ -46,40 +46,48 @@
         }
         
         # return error if no more children allowed
-        return new IMPL::DOM::Schema::VaidationError (
+        return new IMPL::DOM::Schema::ValidationError (
             Message => $this->messageUnexpected,
             Node => $child,
             Source => $this
         ) unless $info;
         
-        # it's ok, we found schema element for him
+        # it's ok, we found schema element for child
+        # but it may be any node or switching node wich would not satisfy current child
+
+        # validate
+        while (my @errors = $info->{Schema}->Validate($child)) {
+            if( $info->{anyNode} and $info->{Seen} >= $info->{Min} ) {
+                # in case of any or switch node, skip it if possible
+                next if $info = shift @nodes;
+            }
+            return @errors;
+        }
+        
         $info->{Seen}++;
         
         # check count limits
-        return new IMPL::DOM::Schema::VaidationError (
+        return new IMPL::DOM::Schema::ValidationError (
             Error => 1,
             Message => $this->messageUnexpected,
             Node => $child,
             Source => $this,
         ) if $info->{Max} and $info->{Seen} > $info->{Max};
-        
-        # validate
-        if (my @errors = $info->{Schema}->Validate($child)) {
-            return @errors;
-        }
     }
     
     # no more children left (but may be should :)
     while ($info) {
-        return new IMPL::DOM::Schema::VaidationError (
+        return new IMPL::DOM::Schema::ValidationError (
             Error => 1,
             Message => $this->messageNodesRequired,
             Node => $node,
-            Source => $this
+            Source => $this,
+            Schema => $info->{Schema}
         ) if $info->{Seen} < $info->{Min};
         
         $info = shift @nodes;
     }
+    return;
 }
 
 1;