annotate Lib/IMPL/DOM/Transform/PostToDOM.pm @ 216:e9fd7ff3f54c

sync
author sergey
date Thu, 09 Aug 2012 17:24:07 +0400
parents 4d0e1962161c
children a4d9126edcbb
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
112
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
5 use IMPL::DOM::Navigator::Builder;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
6 use IMPL::Class::Property;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
7
166
4267a2ac3d46 Added Class::Template,
wizard
parents: 157
diff changeset
8 use parent qw(IMPL::Transform);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
9
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
10 BEGIN {
106
83e356614c1e DOM Builder now is a navigator like SimpleBuilder
wizard
parents: 79
diff changeset
11 public property documentClass => prop_get | owner_set;
83e356614c1e DOM Builder now is a navigator like SimpleBuilder
wizard
parents: 79
diff changeset
12 public property documentSchema => prop_get | owner_set;
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
13 public property prefix => prop_get | owner_set;
106
83e356614c1e DOM Builder now is a navigator like SimpleBuilder
wizard
parents: 79
diff changeset
14 private property _navi => prop_all;
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
15 public property Errors => prop_all | prop_list;
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
16 private property _schema => prop_all;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
17 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
18
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
19 our %CTOR = (
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
20 'IMPL::Transform' => sub {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
21 -plain => \&TransformPlain,
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
22 HASH => \&TransformContainer,
130
06a34c197b05 Added support for utf-8 and old versions of CGI module
wizard
parents: 126
diff changeset
23 CGI => \&TransformCGI,
06a34c197b05 Added support for utf-8 and old versions of CGI module
wizard
parents: 126
diff changeset
24 CGIWrapper => \&TransformCGI
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
25 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
26 );
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;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
76
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
77 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
78 length (my $value = $query->param($param)) or next;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
79
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
80 my @parts = split /\//,$param;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
81
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
82 my $node = $data;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
83 while ( my $part = shift @parts ) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
84 if (@parts) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
85 $node = ($node->{$part} ||= {});
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
86 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
87 $node->{$part} = $value;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
88 }
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 if (keys %$data > 1) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
93 $data = { document => $data };
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
94 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
95
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
96 my $doc = $this->Transform($data);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
97 $doc->nodeProperty( query => $query );
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
98 $this->Errors->Append( $this->_navi->BuildErrors);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
99 $this->Errors->Append( $this->_schema->Validate($doc));
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
100 return $doc;
106
83e356614c1e DOM Builder now is a navigator like SimpleBuilder
wizard
parents: 79
diff changeset
101 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
102
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 7
diff changeset
103 1;
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
104
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
105 __END__
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
106
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
107 =pod
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
108
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
109 =head1 NAME
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
110
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
111 C<IMPL::DOM::Transform::PostToDOM> - Преобразование объекта C<CGI> в DOM документ.
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
112
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
113 =head1 SINOPSYS
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
114
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
115 =begin code
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
116
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
117 my $schema = IMPL::DOM::Schema->LoadSchema('Data/user.add.schema.xml');
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
118
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
119 my $transform = IMPL::DOM::Transform::PostToDOM->new(
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
120 undef, # default class
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
121 $schema,
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
122 $schema->selectSingleNode('ComplexNode')->name
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
123 );
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
124
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
125 my $doc = $transform->Transform(
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
126 CGI->new({
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
127 'user/login' => 'bob',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
128 'user/fullName' => 'Bob Marley',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
129 'user/password' => 'secret',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
130 'user/password_retype' => 'secret',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
131 'user/birthday' => '1978-12-17',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
132 'user/email[1]' => 'bob@marley.com',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
133 'user/email[2]' => 'bob.marley@google.com',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
134 process => 1
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
135 })
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
136 );
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
137
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
138 =end code
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
139
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
140 =head1 DESCRIPTION
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
141
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
142 Используется для преобразования CGI запроса в DOM документ. Для этого используются параметры запроса, имена которых
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
143 начинаются со значение из свойства C<prefix>.
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
144
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
145 Имена параметров интерпретируются следующим образом
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
146
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
147 =over
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
148
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
149 =item 1 Имя параметра составляется из имени узла, имен всех его родителей и указанием номера экземпляра.
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
150
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
151 =item 2 Имена узлов могут содержать только буквы, цифры и символ _
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
152
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
153 =item 3 В случае когда узел может повторяться несколько раз, в квадратных скобках указывается
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
154 послеовательный номер экземпляра.
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
155
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
156 =item 4 Имена параметров объединяются через символ '/'
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
157
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
158 =back
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
159
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
160 =cut