annotate lib/IMPL/DOM/Transform/QueryToDOM.pm @ 411:ee36115f6a34 ref20150831

sync
author cin
date Mon, 21 Sep 2015 00:53:10 +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::QueryToDOM;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 OutOfRangeException => '-IMPL::OutOfRangeException'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 'IMPL::DOM::Transform::ObjectToDOM' => '@_'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 prefix => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 delimiter => PROP_RO
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 ]
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 our $MAX_INDEX = 1024;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 $this->templates->{'CGI'} = 'TransformCGI';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 $this->templates->{'IMPL::Web::Application::Action'} = 'TransformAction';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 $this->delimiter('[.]');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $this->prefix('');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 # inflate simple properties
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 sub TransformPlain {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 my ($this,$data) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 $this->currentNode->nodeProperty( rawValue => $data );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 $this->currentNode->nodeValue( $data );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 return $this->currentNode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 # do not store complex data as node values
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 sub StoreObject {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 my ($this,$node,$data) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 return $node;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 #TODO: support a.b[0][1].c[1]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 sub TransformCGI {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 my ($this,$query) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 my $data={};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 my $prefix = $this->prefix;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 my $delim = $this->delimiter;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 my @value = grep length($_), $query->param($param) or next;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my @parts = split /$delim/,$param;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 my $node = $data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 while ( my $part = shift @parts ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 if (my ($name,$index) = ($part =~ m/^(\w+)(?:\[(\d+)\])?$/) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 if (@parts) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 if(defined $index) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 $this->ValidateIndex($index);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 $node = ($node->{$name}[$index] ||= {});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $node = ($node->{$name} ||= {});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 if(defined $index) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 $this->ValidateIndex($index);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 $node->{$name}[$index] = (@value == 1 ? $value[0] : \@value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 $node->{$name} = (@value == 1 ? $value[0] : \@value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 return $this->Transform($data);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 sub ValidateIndex {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 my ($this,$index) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 die OutOfRangeException->new()
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 unless $index >= 0 and $index <= $MAX_INDEX;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 sub TransformAction {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 my ($this,$action) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 return $this->Transform($action->isJson ? $action->jsonData : $action->query);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 C<IMPL::DOM::Transform::QueryToDOM> - преобразование CGI запроса в DOM документ.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 use CGI();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 use IMPL::require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 Schema => 'IMPL::DOM::Schema',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 Config => 'IMPL::Config',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 QueryToDOM => 'IMPL::DOM::Transform::QueryToDOM'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 my $q = CGI->new();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 my $schema = Schema->LoadSchema(Config->AppBase('schemas','person.xml'));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 my $transorm = QueryToDOM->new('edit', $schema);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 my $form = $transform->Transform($q);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 my @errors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 push @errors, $transform->buildErrors;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 push @errors, $schema->Validate($doc);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 Наследует C<IMPL::DOM::Transform::ObjectToDOM>. Добавляет метод
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 C<TransformCGI> который применятеся к объектам типа C<CGI> (и производных).
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 Запрос C<CGI> сначала приводится к хешу, затем полученный хеш преобразуется
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 в DOM документ при помощи вызова метода C<Transform>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 Для этого выбираются параметры запроса, затем, имя каждого параметра
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 Если параметр имеет несколько значений, значит свойство является массивом.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 Также изменено поведение некоторых методов преобразования.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 =item * C<TransformPlain($value)>
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 строковые значения, а схема документа может предполпгать другие типы, при
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 преобразовании значения параметра из запроса к значению узла используется
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 метод C<< $this->inflateNodeValue($value) >>, также помимо значения
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 C<< $this->currentNode->nodeValue >> задается атрибут
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 C<< $this->currentNode->nodeProperty( rawValue => $value) >>, для того, чтобы
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 была возможность получить оригинальное значение параметра запроса (например,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 в случае когда его формат был не верным и C<nodeValue> будет C<undef>).
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 =item * C<StoreObject($node,$object)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 Данный метод вызывается если текущий узел (переданный в параметре C<$node>)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 предполагает простое значение, однако в запросе для него было передано сложное
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 содержимое. Данная реализация просто игнорирует переданный объект C<$object>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 и возвращает C<$node> без изменений.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 =head2 C<[get]delimiter>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 REGEX. Разделитель свойств в имени параметра, по-умолчанию C<'[.]'>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 =head2 C<[get]prefix>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 Строка, префикс имен параметров, которые участвуют в формировании документа.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 По-умолчанию пусто.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 =cut