annotate lib/IMPL/DOM/Transform/PostToDOM.pm @ 410:9335cf010b23 ref20150831

refactoring
author cin
date Mon, 14 Sep 2015 01:11:53 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::DOM::Transform::PostToDOM;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 Builder => 'IMPL::DOM::Navigator::Builder'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 'IMPL::Transform' => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 -plain => \&TransformPlain,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 HASH => \&TransformContainer,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 CGI => \&TransformCGI,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 CGIWrapper => \&TransformCGI
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 documentClass => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 documentSchema => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 prefix => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 _navi => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 errors => PROP_RW | PROP_LIST,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 _schema => PROP_RW
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 my ($this,$docClass,$docSchema,$prefix) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 $docClass ||= 'IMPL::DOM::Document';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 $this->_navi(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 IMPL::DOM::Navigator::Builder->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 $docClass,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 $docSchema
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 $this->_schema($docSchema);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 $this->prefix($prefix) if $prefix;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 sub TransformContainer {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 my ($this,$data) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my $navi = $this->_navi;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 foreach my $key (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 map [$_,/(\w+)(?:\[(\d+)\])?/], keys %$data
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 ){
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 my $value = $data->{$key->[0]};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 my $node = $navi->NavigateCreate($key->[1]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 $node->nodeProperty(instanceId => $key->[2]) if defined $key->[2];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 $this->Transform($value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 $navi->Back();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 return $navi->Current;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 sub TransformPlain {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 my ($this,$data) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 $this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 sub TransformCGI {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 my ($this,$query) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 my $data={};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 my $prefix = $this->prefix;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 my $delim = $this->delimiter;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 length (my $value = $query->param($param)) or next;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 my @parts = split /\//,$param;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 my $node = $data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 while ( my $part = shift @parts ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 if (@parts) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 $node = ($node->{$part} ||= {});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 $node->{$part} = $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 if (keys %$data > 1) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 $data = { document => $data };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 my $doc = $this->Transform($data);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 $doc->nodeProperty( query => $query );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 $this->errors->Append( $this->_navi->BuildErrors);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 $this->errors->Append( $this->_schema->Validate($doc));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 return $doc;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 C<IMPL::DOM::Transform::PostToDOM> - Преобразование объекта C<CGI> в DOM документ.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 =head1 SINOPSYS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 my $schema = IMPL::DOM::Schema->LoadSchema('Data/user.add.schema.xml');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 my $transform = IMPL::DOM::Transform::PostToDOM->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 undef, # default class
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 $schema,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 $schema->selectSingleNode('ComplexNode')->name
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 my $doc = $transform->Transform(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 CGI->new({
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 'user/login' => 'bob',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 'user/fullName' => 'Bob Marley',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 'user/password' => 'secret',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 'user/password_retype' => 'secret',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 'user/birthday' => '1978-12-17',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 'user/email[1]' => 'bob@marley.com',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 'user/email[2]' => 'bob.marley@google.com',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 process => 1
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 })
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 Используется для преобразования CGI запроса в DOM документ. Для этого используются параметры запроса, имена которых
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 начинаются со значение из свойства C<prefix>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 Имена параметров интерпретируются следующим образом
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 =item 1 Имя параметра составляется из имени узла, имен всех его родителей и указанием номера экземпляра.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 =item 2 Имена узлов могут содержать только буквы, цифры и символ _
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 =item 3 В случае когда узел может повторяться несколько раз, в квадратных скобках указывается
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 послеовательный номер экземпляра.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 =item 4 Имена параметров объединяются через символ '/'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 =cut