diff Lib/IMPL/DOM/Schema/NodeList.pm @ 389:5aff94ba842f

DOM Schema refactoring complete
author cin
date Wed, 12 Feb 2014 13:36:24 +0400
parents 648dfaf642e0
children
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Schema/NodeList.pm	Tue Feb 11 20:22:01 2014 +0400
+++ b/Lib/IMPL/DOM/Schema/NodeList.pm	Wed Feb 12 13:36:24 2014 +0400
@@ -6,7 +6,8 @@
 use IMPL::declare {
 	require => {
 		ValidationError => 'IMPL::DOM::Schema::ValidationError',
-		AnyNode => '-IMPL::DOM::Schema::AnyNode'
+		AnyNode => '-IMPL::DOM::Schema::AnyNode',
+		Label => 'IMPL::DOM::Schema::Label'
 	},
 	base => [
 		'IMPL::DOM::Node' => sub { nodeName => 'NodeList' }
@@ -21,7 +22,7 @@
     my ($this,%args) = @_;
     
     $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 %parent.path%');
+    $this->messageNodesRequired($args{messageNodesRequired} || 'A %schemaNode.name% is required in the node %parent.path%');
 }
 
 sub Validate {
@@ -38,60 +39,67 @@
         while ($info and not $info->{anyNode} and $info->{nodeName} ne $child->nodeName) {
             # if possible of course :)
             return ValidationError->new (
-                message => $this->messageUnexpected,
+                message => $this->_MakeLabel( $this->messageUnexpected ),
                 node => $child,
                 parent => $node,
                 schemaNode => $info->{schemaNode}
-            ) if $info->{Min} > $info->{Seen};
+            ) if $info->{min} > $info->{seen}; # we trying to skip a schema node which has a quantifier
             
             $info = shift @nodes;
         }
         
         # return error if no more children allowed
         return ValidationError->new (
-            message => $this->messageUnexpected,
+            message => $this->_MakeLabel( $this->messageUnexpected ),
             node => $child,
             parent => $node
         ) unless $info;
         
         # 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->{schemaNode}->Validate($child)) {
-            if( $info->{anyNode} and $info->{Seen} >= $info->{Min} ) {
+        while (my @errors = $info->{schemaNode}->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}++;
+        $info->{seen}++;
         
         # check count limits
-        return new IMPL::DOM::Schema::ValidationError (
-            message => $this->messageUnexpected,
+        return ValidationError->new(
+            message => $this->_MakeLabel( $this->messageUnexpected ),
             node => $child,
             parent => $node,
-            source => $sourceSchema,
-        ) if $info->{Max} and $info->{Seen} > $info->{Max};
+            schemaNode => $info->{schemaNode},
+        ) if $info->{max} and $info->{seen} > $info->{max};
     }
     
     # no more children left (but may be should :)
     while ($info) {
-        return new IMPL::DOM::Schema::ValidationError (
-            error => 1,
-            message => $this->messageNodesRequired,
-            source => $sourceSchema,
+        return ValidationError->new(
+            message => $this->_MakeLabel( $this->messageNodesRequired ),
             parent => $node,
-            schema => $info->{Schema}
-        ) if $info->{Seen} < $info->{Min};
+            schemaNode => $info->{schemaNode}
+        ) if $info->{seen} < $info->{min};
         
         $info = shift @nodes;
     }
     return;
 }
 
+sub _MakeLabel {
+	my ($this,$label) = @_;
+	
+	if ($label =~ /^ID:(\w+)$/) {
+		return Label->new($this->document->stringMap, $1);
+	} else {
+		return $label;
+	}
+}
+
 1;
 
 __END__