annotate Lib/IMPL/DOM/Transform/PostToDOM.pm @ 180:d1676be8afcc

Перекодировка в utf-8
author sourcer
date Fri, 30 Dec 2011 23:40:00 +0300
parents 4267a2ac3d46
children 4d0e1962161c
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 {
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
21 -plain => \&TransformPlain,
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 {
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
29 my ($this,$docClass,$docSchema,$prefix) = @_;
112
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
30 $docClass ||= 'IMPL::DOM::Document';
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
31
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
32 $this->_navi(
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
33 IMPL::DOM::Navigator::Builder->new(
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
34 $docClass,
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
35 $docSchema
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
36 )
0ed8e2541b1c Form processing mechanism
wizard
parents: 106
diff changeset
37 );
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
38 $this->_schema($docSchema);
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
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 (
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
48 sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]}
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
49 map [$_,/(\w+)(?:\[(\d+)\])?/], keys %$data
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
50 ){
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
51 my $value = $data->{$key->[0]};
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
52 my $node = $navi->NavigateCreate($key->[1]);
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
53
151
e36ffd8c29db Fixed major bug in conversion from a POST request to the DOM document (when instanceId == 0)
wizard
parents: 148
diff changeset
54 $node->nodeProperty(instanceId => $key->[2]) if defined $key->[2];
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
55
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
56 $this->Transform($value);
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
57
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
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 {
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
65 my ($this,$data) = @_;
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
66
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
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 {
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
71 my ($this,$query) = @_;
126
c8dfbbdd8005 Several bug fixes
wizard
parents: 113
diff changeset
72
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
73 my $data={};
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
74
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
75 my $prefix = $this->prefix;
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
76
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
77 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
157
c7652cf29a80 Fixed PostToDom with empty fields issue.
wizard
parents: 152
diff changeset
78 length (my $value = $query->param($param)) or next;
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
79
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
80 my @parts = split /\//,$param;
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
81
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
82 my $node = $data;
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
83 while ( my $part = shift @parts ) {
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
84 if (@parts) {
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
85 $node = ($node->{$part} ||= {});
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
86 } else {
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
87 $node->{$part} = $value;
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
88 }
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
89 }
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
90 }
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
91
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
92 if (keys %$data > 1) {
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
93 $data = { document => $data };
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
94 }
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
95
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
96 my $doc = $this->Transform($data);
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
diff changeset
97 $doc->nodeProperty( query => $query );
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
98 $this->Errors->Append( $this->_navi->BuildErrors);
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
diff changeset
99 $this->Errors->Append( $this->_schema->Validate($doc));
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 112
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
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
117 my $schema = IMPL::DOM::Schema->LoadSchema('Data/user.add.schema.xml');
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
118
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
119 my $transform = IMPL::DOM::Transform::PostToDOM->new(
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
120 undef, # default class
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
121 $schema,
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
122 $schema->selectSingleNode('ComplexNode')->name
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
123 );
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
124
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
125 my $doc = $transform->Transform(
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
126 CGI->new({
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
127 'user/login' => 'bob',
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
128 'user/fullName' => 'Bob Marley',
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
129 'user/password' => 'secret',
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
130 'user/password_retype' => 'secret',
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
131 'user/birthday' => '1978-12-17',
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
132 'user/email[1]' => 'bob@marley.com',
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
133 'user/email[2]' => 'bob.marley@google.com',
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
134 process => 1
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
diff changeset
135 })
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents: 147
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 послеовательный номер экземпляра.
147
c2aa10fbb396 Post to dom improved
wizard
parents: 144
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