Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Transform/PostToDOM.pm @ 227:70ad6bc20908
sync
author | sergey |
---|---|
date | Fri, 07 Sep 2012 16:32:17 +0400 |
parents | 4d0e1962161c |
children | a4d9126edcbb |
rev | line source |
---|---|
112 | 1 package IMPL::DOM::Transform::PostToDOM; |
49 | 2 use strict; |
3 use warnings; | |
4 | |
112 | 5 use IMPL::DOM::Navigator::Builder; |
49 | 6 use IMPL::Class::Property; |
7 | |
166 | 8 use parent qw(IMPL::Transform); |
49 | 9 |
10 BEGIN { | |
106 | 11 public property documentClass => prop_get | owner_set; |
12 public property documentSchema => prop_get | owner_set; | |
113 | 13 public property prefix => prop_get | owner_set; |
106 | 14 private property _navi => prop_all; |
113 | 15 public property Errors => prop_all | prop_list; |
16 private property _schema => prop_all; | |
49 | 17 } |
18 | |
19 our %CTOR = ( | |
20 'IMPL::Transform' => sub { | |
194 | 21 -plain => \&TransformPlain, |
113 | 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 | 25 } |
26 ); | |
27 | |
106 | 28 sub CTOR { |
194 | 29 my ($this,$docClass,$docSchema,$prefix) = @_; |
30 $docClass ||= 'IMPL::DOM::Document'; | |
31 | |
32 $this->_navi( | |
33 IMPL::DOM::Navigator::Builder->new( | |
34 $docClass, | |
35 $docSchema | |
36 ) | |
37 ); | |
38 $this->_schema($docSchema); | |
39 $this->prefix($prefix) if $prefix; | |
106 | 40 } |
41 | |
113 | 42 sub TransformContainer { |
49 | 43 my ($this,$data) = @_; |
44 | |
112 | 45 my $navi = $this->_navi; |
113 | 46 |
147 | 47 foreach my $key ( |
194 | 48 sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} |
49 map [$_,/(\w+)(?:\[(\d+)\])?/], keys %$data | |
147 | 50 ){ |
194 | 51 my $value = $data->{$key->[0]}; |
52 my $node = $navi->NavigateCreate($key->[1]); | |
53 | |
54 $node->nodeProperty(instanceId => $key->[2]) if defined $key->[2]; | |
55 | |
56 $this->Transform($value); | |
57 | |
58 $navi->Back(); | |
49 | 59 } |
60 | |
113 | 61 return $navi->Current; |
62 } | |
63 | |
64 sub TransformPlain { | |
194 | 65 my ($this,$data) = @_; |
66 | |
67 $this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) ); | |
49 | 68 } |
69 | |
113 | 70 sub TransformCGI { |
194 | 71 my ($this,$query) = @_; |
126 | 72 |
194 | 73 my $data={}; |
74 | |
75 my $prefix = $this->prefix; | |
76 | |
77 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) { | |
78 length (my $value = $query->param($param)) or next; | |
79 | |
80 my @parts = split /\//,$param; | |
81 | |
82 my $node = $data; | |
83 while ( my $part = shift @parts ) { | |
84 if (@parts) { | |
85 $node = ($node->{$part} ||= {}); | |
86 } else { | |
87 $node->{$part} = $value; | |
88 } | |
89 } | |
90 } | |
91 | |
92 if (keys %$data > 1) { | |
93 $data = { document => $data }; | |
94 } | |
95 | |
96 my $doc = $this->Transform($data); | |
97 $doc->nodeProperty( query => $query ); | |
98 $this->Errors->Append( $this->_navi->BuildErrors); | |
99 $this->Errors->Append( $this->_schema->Validate($doc)); | |
100 return $doc; | |
106 | 101 } |
49 | 102 |
103 1; | |
147 | 104 |
105 __END__ | |
106 | |
107 =pod | |
108 | |
109 =head1 NAME | |
110 | |
180 | 111 C<IMPL::DOM::Transform::PostToDOM> - Преобразование объекта C<CGI> в DOM документ. |
147 | 112 |
113 =head1 SINOPSYS | |
114 | |
115 =begin code | |
116 | |
194 | 117 my $schema = IMPL::DOM::Schema->LoadSchema('Data/user.add.schema.xml'); |
118 | |
119 my $transform = IMPL::DOM::Transform::PostToDOM->new( | |
120 undef, # default class | |
121 $schema, | |
122 $schema->selectSingleNode('ComplexNode')->name | |
123 ); | |
124 | |
125 my $doc = $transform->Transform( | |
126 CGI->new({ | |
127 'user/login' => 'bob', | |
128 'user/fullName' => 'Bob Marley', | |
129 'user/password' => 'secret', | |
130 'user/password_retype' => 'secret', | |
131 'user/birthday' => '1978-12-17', | |
132 'user/email[1]' => 'bob@marley.com', | |
133 'user/email[2]' => 'bob.marley@google.com', | |
134 process => 1 | |
135 }) | |
136 ); | |
147 | 137 |
138 =end code | |
139 | |
140 =head1 DESCRIPTION | |
141 | |
180 | 142 Используется для преобразования CGI запроса в DOM документ. Для этого используются параметры запроса, имена которых |
143 начинаются со значение из свойства C<prefix>. | |
147 | 144 |
180 | 145 Имена параметров интерпретируются следующим образом |
147 | 146 |
147 =over | |
148 | |
180 | 149 =item 1 Имя параметра составляется из имени узла, имен всех его родителей и указанием номера экземпляра. |
147 | 150 |
180 | 151 =item 2 Имена узлов могут содержать только буквы, цифры и символ _ |
147 | 152 |
180 | 153 =item 3 В случае когда узел может повторяться несколько раз, в квадратных скобках указывается |
154 послеовательный номер экземпляра. | |
194 | 155 |
180 | 156 =item 4 Имена параметров объединяются через символ '/' |
147 | 157 |
158 =back | |
159 | |
160 =cut |