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

DOM Schema refactoring complete
author cin
date Wed, 12 Feb 2014 13:36:24 +0400
parents 89179bb8c388
children
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Schema/Validator/Compare.pm	Tue Feb 11 20:22:01 2014 +0400
+++ b/Lib/IMPL/DOM/Schema/Validator/Compare.pm	Wed Feb 12 13:36:24 2014 +0400
@@ -1,30 +1,32 @@
 package IMPL::DOM::Schema::Validator::Compare;
 use strict;
 
-use parent qw(IMPL::DOM::Schema::Validator);
-
+use IMPL::Const qw(:prop);
+use IMPL::declare {
+	require => {
+		Label => 'IMPL::DOM::Schema::Label',
+		ValidationError => 'IMPL::DOM::Schema::ValidationError'
+	},
+	base => [
+		'IMPL::DOM::Schema::Validator' => sub {
+	        my %args = @_;
+	        $args{nodeName} ||= 'Compare';
+	        delete @args{qw(targetProperty op nodePath optional message)};
+	        %args;
+	    }
+	],
+	props => [
+		targetProperty => PROP_RW,
+		op => PROP_RW,
+		nodePath => PROP_RW,
+		optional => PROP_RW,
+		_pathTranslated => PROP_RW,
+		_targetNode => PROP_RW,
+		_schemaNode => PROP_RW,
+		message => PROP_RW
+	] 
+};
 use IMPL::Resources::Format qw(FormatMessage);
-use IMPL::Class::Property;
-
-BEGIN {
-    public property targetProperty => prop_all;
-    public property op => prop_all;
-    public property nodePath => prop_all;
-    public property optional => prop_all;
-    private property _pathTranslated => prop_all;
-    private property _targetNode => prop_all;
-    private property _sourceSchema => prop_all;
-    public property message => prop_all;
-}
-
-our %CTOR = (
-    'IMPL::DOM::Schema::Validator' => sub {
-        my %args = @_;
-        $args{nodeName} ||= 'Compare';
-        delete @args{qw(targetProperty op nodePath optional message)};
-        %args;
-    }
-);
 
 our %Ops = (
     '='  => \&_equals,
@@ -47,7 +49,7 @@
     $this->targetProperty($args{targetProperty} || 'nodeValue');
     $this->op( $Ops{ $args{op} || '=' } ) or die new IMPL::InvalidArgumentException("Invalid parameter value",'op',$args{op},$this->path);
     $this->nodePath($args{nodePath}) or die new IMPL::InvalidArgumentException("The argument is required", 'nodePath', $this->path);
-    $this->message($args{message} || 'The value of %node.path% %source.op% %value% (%source.nodePath%)' );
+    $this->message($args{message} || 'The value of %node.path% %schemaNode.op% %value% (%schemaNode.nodePath%)' );
     $this->optional($args{optional}) if $args{optional};
 }
 
@@ -94,8 +96,10 @@
                             Schema => $this->parentNode,
                             Node => $this->_targetNode,
                             schema => $this->parentNode,
+                            schemaType => $this->parentNode,
                             node => $this->_targetNode,
-                            source => $this->_sourceSchema
+                            source => $this->_schemaNode,
+                            schemaNode => $this->_schemaNode
                         },\&_resovleProperty)
                     ) or return 0 foreach @parsedFilters;
                 return 1;
@@ -117,9 +121,10 @@
     
     my @result;
     
-    my $Source = $ctx && $ctx->{Source} || $this->parentNode;
+    my $schemaNode = $ctx->{schemaNode};
+    my $schemaType = $ctx->{schemaType};
     
-    $this->_sourceSchema($Source);
+    $this->_schemaNode($schemaNode);
     
     $this->_targetNode($node);
     
@@ -138,26 +143,26 @@
             $value = $foreignNode->nodeValue;
         }
         
-        push @result, new IMPL::DOM::Schema::ValidationError(
+        push @result, ValidationError->new(
             node => $node,
             foreignNode => $foreignNode,
             value => $value,
-            source => $Source,
-            schema => $this->parentNode,
-            message => $this->message
+            schemaNode => $schemaNode,
+            schemaType => $schemaType,
+            message => $this->_MakeLabel($this->message)
         ) unless $this->op->(_resovleProperty($node,$this->targetProperty),$value);
     } elsif (not $this->optional) {
-        push @result,  new IMPL::DOM::Schema::ValidationError(
+        push @result, ValidationError->new(
             node => $node,
             value => '',
-            source => $Source,
-            schema => $this->parentNode,
-            message => $this->message
+            schemaNode => $schemaNode,
+            schemaType => $schemaType,
+            message => $this->_MakeLabel( $this->message )
         );
     }
     
     $this->_targetNode(undef);
-    $this->_sourceSchema(undef);
+    $this->_schemaNode(undef);
     
     return @result;
 }
@@ -216,6 +221,16 @@
     $_[0] >= $_[1];
 }
 
+sub _MakeLabel {
+	my ($this,$label) = @_;
+	
+	if ($label =~ /^ID:(\w+)$/) {
+		return Label->new($this->document->stringMap, $1);
+	} else {
+		return $label;
+	}
+}
+
 1;
 
 __END__
@@ -235,8 +250,8 @@
 
 <schema>
     <SimpleType type="retype_field">
-        <Property name="linkedNode" message="Для узла %Node.nodeName% необходимо задать свойство %Source.name%"/>
-        <Compare op="eq" nodePath="sibling:*[nodeName eq '%Node.linkedNode%']"/>
+        <Property name="linkedNode" message="Для узла %node.nodeName% необходимо задать свойство %schemaNode.name%"/>
+        <Compare op="eq" nodePath="sibling:*[nodeName eq '%node.linkedNode%']"/>
     </SimpleType>
 </schema>