Mercurial > pub > Impl
annotate 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 |
rev | line source |
---|---|
49 | 1 package IMPL::Resources::Format; |
2 use strict; | |
3 use warnings; | |
4 | |
5 require Exporter; | |
6 our @ISA = qw(Exporter); | |
7 our @EXPORT_OK = qw(&FormatMessage); | |
8 | |
9 sub FormatMessage { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
10 my ($string,$args,$resolver) = @_; |
49 | 11 |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
12 $resolver ||= \&_defaultResolver; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
13 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
14 $string =~ s/%(\w+(?:\.\w+)*)%/_getvalue($args,$1,"\[$1\]",$resolver)/ge; |
49 | 15 |
16 return $string; | |
17 } | |
18 | |
19 sub _getvalue { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
20 my ($obj,$path,$default,$resolver) = @_; |
49 | 21 |
22 foreach my $chunk (split /\./,$path) { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
23 return $default unless $obj; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
24 if (ref $obj eq 'HASH') { |
49 | 25 $obj = $obj->{$chunk}; |
26 } else { | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
27 $obj = $resolver->($obj,$chunk); |
49 | 28 } |
29 } | |
30 return $obj; | |
31 } | |
32 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
33 sub _defaultResolver { |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
34 my ($obj,$prop) = @_; |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
35 |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
36 return ( eval { $obj->can($prop) } ? $obj->$prop() : undef ); |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
37 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
49
diff
changeset
|
38 |
49 | 39 1; |