annotate Lib/IMPL/DOM/Transform/PostToDOM.pm @ 333:cd6409f66a5f

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