annotate Lib/IMPL/DOM/Transform/QueryToDOM.pm @ 290:7b0dad6117d5

*TTView: fixed memory leak
author sergey
date Wed, 20 Feb 2013 17:24:57 +0400
parents c9c2ec29793f
children b56b1ec33b59
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
1 package IMPL::DOM::Transform::QueryToDOM;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
2 use strict;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
3
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
4 use IMPL::Const qw(:prop);
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
5 use IMPL::declare {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
6 base => [
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
7 'IMPL::DOM::Transform::ObjectToDOM' => '@_'
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
8 ],
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
9 props => [
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
10 prefix => PROP_RO,
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
11 delimiter => PROP_RO
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
12 ]
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
13 };
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
14
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
15 sub CTOR {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
16 my ($this) = @_;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
17
264
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
18 $this->templates->{'CGI'} = 'TransformCGI';
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
19
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
20 $this->delimiter('[.]');
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
21 $this->prefix('');
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
22 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
23
250
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
24 # inflate simple properties
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
25 sub TransformPlain {
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
26 my ($this,$data) = @_;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
27
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
28 $this->currentNode->nodeProperty( rawValue => $data );
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
29 $this->currentNode->nodeValue( $this->inflateNodeValue($data) );
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
30 return $this->currentNode;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
31 }
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
32
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
33 # do not store complex data as node values
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
34 sub StoreObject {
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
35 my ($this,$node,$data) = @_;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
36
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
37 return $node;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
38 }
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
39
264
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
40 #TODO: support a.b[0][1].c[1]
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
41
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
42 sub TransformCGI {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
43 my ($this,$query) = @_;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
44
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
45 my $data={};
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
46
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
47 my $prefix = $this->prefix;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
48 my $delim = $this->delimiter;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
49
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
50 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
51
a02b110da931 refactoring
sergey
parents: 238
diff changeset
52 my @value = grep length($_), $query->param($param) or next;
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
53
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
54 my @parts = split /$delim/,$param;
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
55
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
56 my $node = $data;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
57 while ( my $part = shift @parts ) {
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
58 if (my ($name,$index) = ($part =~ m/^(\w+)(?:\[(\d+)\])?$/) ) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
59 if (@parts) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
60 if(defined $index) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
61 $node = ($node->{$name}[$index] ||= {});
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
62 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
63 $node = ($node->{$name} ||= {});
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
64 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
65 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
66 if(defined $index) {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
67 $node->{$name}[$index] = (@value == 1 ? $value[0] : \@value);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
68 } else {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
69 $node->{$name} = (@value == 1 ? $value[0] : \@value);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
70 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
71 }
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
72 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
73 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
74 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
75
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
76 return $this->Transform($data);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
77 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
78
264
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
79 1;
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
80
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
81 __END__
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
82
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
83 =pod
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
84
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
85 =head1 NAME
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
86
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
87 C<IMPL::DOM::Transform::QueryToDOM> - преобразование CGI запроса в DOM документ.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
88
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
89 =head1 SYNOPSIS
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
90
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
91 =begin code
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
92
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
93 use CGI();
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
94 use IMPL::require {
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
95 Schema => 'IMPL::DOM::Schema',
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
96 Config => 'IMPL::Config',
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
97 QueryToDOM => 'IMPL::DOM::Transform::QueryToDOM'
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
98 }
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
99
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
100 my $q = CGI->new();
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
101
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
102 my $schema = Schema->LoadSchema(Config->AppBase('schemas','person.xml'));
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
103 my $transorm = QueryToDOM->new('edit', $schema);
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
104
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
105 my $form = $transform->Transform($q);
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
106
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
107 my @errors;
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
108
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
109 push @errors, $transform->buildErrors;
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
110 push @errors, $schema->Validate($doc);
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
111
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
112
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
113 =end code
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
114
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
115 =head1 DESCRIPTION
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
116
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
117 Наследует C<IMPL::DOM::Transform::ObjectToDOM>. Добавляет метод
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
118 C<TransformCGI> который применятеся к объектам типа C<CGI> (и производных).
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
119
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
120 Запрос C<CGI> сначала приводится к хешу, затем полученный хеш преобразуется
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
121 в DOM документ при помощи вызова метода C<Transform>.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
122
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
123 Для этого выбираются параметры запроса, затем, имя каждого параметра
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
124 рассматривается в виде пути к свойству, создается структура из хешей и массивов
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
125 в которую по указанному пути кладется значение.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
126
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
127 Если параметр имеет несколько значений, значит свойство является массивом.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
128
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
129 Также изменено поведение некоторых методов преобразования.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
130
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
131 =over
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
132
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
133 =item * C<TransformPlain($value)>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
134
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
135 Преобразование для простого значения свойства. Посокльку в запросе передаются
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
136 строковые значения, а схема документа может предполпгать другие типы, при
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
137 преобразовании значения параметра из запроса к значению узла используется
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
138 метод C<< $this->inflateNodeValue($value) >>, также помимо значения
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
139 C<< $this->currentNode->nodeValue >> задается атрибут
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
140 C<< $this->currentNode->nodeProperty( rawValue => $value) >>, для того, чтобы
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
141 была возможность получить оригинальное значение параметра запроса (например,
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
142 в случае когда его формат был не верным и C<nodeValue> будет C<undef>).
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
143
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
144 =item * C<StoreObject($node,$object)>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
145
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
146 Данный метод вызывается если текущий узел (переданный в параметре C<$node>)
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
147 предполагает простое значение, однако в запросе для него было передано сложное
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
148 содержимое. Данная реализация просто игнорирует переданный объект C<$object>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
149 и возвращает C<$node> без изменений.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
150
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
151 =back
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
152
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
153 =head1 MEMBERS
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
154
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
155 =head2 C<[get]delimiter>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
156
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
157 REGEX. Разделитель свойств в имени параметра, по-умолчанию C<'[.]'>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
158
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
159 =head2 C<[get]prefix>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
160
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
161 Строка, префикс имен параметров, которые участвуют в формировании документа.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
162 По-умолчанию пусто.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
163
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
164 =cut