diff Lib/IMPL/Resources/Format.pm @ 104:196bf443b5e1

DOM::Schema RC0 inflators support, validation and some other things, Minor and major fixes almost for everything. A 'Source' property of the ValidationErrors generated from a NodeSet or a NodeList is subject to change in the future.
author wizard
date Tue, 11 May 2010 02:42:59 +0400
parents 16ada169ca75
children e6447ad85cb4
line wrap: on
line diff
--- a/Lib/IMPL/Resources/Format.pm	Fri May 07 18:17:40 2010 +0400
+++ b/Lib/IMPL/Resources/Format.pm	Tue May 11 02:42:59 2010 +0400
@@ -7,26 +7,33 @@
 our @EXPORT_OK = qw(&FormatMessage);
 
 sub FormatMessage {
-    my ($string,$args) = @_;
+    my ($string,$args,$resolver) = @_;
     
-    $string =~ s/%(\w+(?:\.\w+)*)%/_getvalue($args,$1,"\[$1\]")/ge;
+    $resolver ||= \&_defaultResolver;
+    
+    $string =~ s/%(\w+(?:\.\w+)*)%/_getvalue($args,$1,"\[$1\]",$resolver)/ge;
     
     return $string;
 }
 
 sub _getvalue {
-    my ($obj,$path,$default) = @_;
+    my ($obj,$path,$default,$resolver) = @_;
     
     foreach my $chunk (split /\./,$path) {
-        if (eval { $obj->can( $chunk ) } ) {
-            $obj = $obj->$chunk();
-        } elsif (UNIVERSAL::isa($obj,'HASH')) {
+    	return $default unless $obj;
+        if (ref $obj eq 'HASH') {
             $obj = $obj->{$chunk};
         } else {
-            return $default;
+            $obj = $resolver->($obj,$chunk);
         }
     }
     return $obj;
 }
 
+sub _defaultResolver {
+	my ($obj,$prop) = @_;
+	
+	return ( eval { $obj->can($prop) } ? $obj->$prop() : undef );
+}
+
 1;