Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Transform/PostToDOM.pm @ 147:c2aa10fbb396
Post to dom improved
author | wizard |
---|---|
date | Mon, 09 Aug 2010 08:45:36 +0400 |
parents | b56ebc31bf18 |
children | e6447ad85cb4 |
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 | |
8 use base qw(IMPL::Transform); | |
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 { | |
113 | 21 -plain => \&TransformPlain, |
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 { |
113 | 29 my ($this,$docClass,$docSchema,$prefix) = @_; |
112 | 30 $docClass ||= 'IMPL::DOM::Document'; |
31 | |
32 $this->_navi( | |
33 IMPL::DOM::Navigator::Builder->new( | |
34 $docClass, | |
35 $docSchema | |
36 ) | |
37 ); | |
113 | 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 ( |
48 sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} | |
49 map [$_,/(\w+)(?:\[(\d+)\])?/], keys %$data | |
50 ){ | |
51 my $value = $data->{$key->[0]}; | |
52 my $node = $navi->NavigateCreate($key->[1]); | |
113 | 53 |
147 | 54 $node->nodeProperty(instanceId => $key->[2]) if $key->[2]; |
113 | 55 |
56 $this->Transform($value); | |
57 | |
58 $navi->Back(); | |
49 | 59 } |
60 | |
113 | 61 return $navi->Current; |
62 } | |
63 | |
64 sub TransformPlain { | |
65 my ($this,$data) = @_; | |
66 | |
67 $this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) ); | |
49 | 68 } |
69 | |
113 | 70 sub TransformCGI { |
71 my ($this,$query) = @_; | |
126 | 72 |
113 | 73 my $data={}; |
74 | |
75 my $prefix = $this->prefix; | |
76 | |
147 | 77 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) { |
144
b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
wizard
parents:
130
diff
changeset
|
78 my $value = $query->param($param) or next; |
113 | 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 | |
147 | 92 if (keys %$data > 1) { |
93 $data = { document => $data }; | |
94 } | |
95 | |
113 | 96 my $doc = $this->Transform($data); |
147 | 97 $doc->nodeProperty( query => $query ); |
113 | 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 | |
111 C<IMPL::DOM::Transform::PostToDOM> - Преобразование объекта C<CGI> в DOM документ. | |
112 | |
113 =head1 SINOPSYS | |
114 | |
115 =begin code | |
116 | |
117 my $transform = new IMPL::DOM::Transform::PostToDOM( | |
118 'My::DOM::Document', | |
119 IMPL::DOM::Schema->LoadSchema('Data/user.add.schema.xml'), | |
120 'myForm' | |
121 ); | |
122 | |
123 =end code | |
124 | |
125 =head1 DESCRIPTION | |
126 | |
127 Используется для преобразования CGI запроса в DOM документ. Для этого используются параметры запроса, имена которых | |
128 начинаются со значение из свойства C<prefix>. | |
129 | |
130 Имена параметров интерпретируются следующим образом | |
131 | |
132 =over | |
133 | |
134 =item 1 Имя параметра составляется из имени узла, имен всех его родителей и указанием номера экземпляра. | |
135 | |
136 =item 2 Имена узлов могут содержать только буквы, цифры и символ _ | |
137 | |
138 =item 3 В случае когда узел может повторяться несколько раз, в квадратных скобках указывается | |
139 послеовательный номер экземпляра. | |
140 | |
141 =item 4 Имена параметров объединяются через символ '/' | |
142 | |
143 =back | |
144 | |
145 =cut |