Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Transform/PostToDOM.pm @ 149:b04e978d6d5a
minor changes
| author | wizard |
|---|---|
| date | Wed, 18 Aug 2010 03:14:57 +0400 |
| parents | e6447ad85cb4 |
| children | e36ffd8c29db |
| 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 | |
|
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 | 137 |
| 138 =end code | |
| 139 | |
| 140 =head1 DESCRIPTION | |
| 141 | |
| 142 Используется для преобразования CGI запроса в DOM документ. Для этого используются параметры запроса, имена которых | |
| 143 начинаются со значение из свойства C<prefix>. | |
| 144 | |
| 145 Имена параметров интерпретируются следующим образом | |
| 146 | |
| 147 =over | |
| 148 | |
| 149 =item 1 Имя параметра составляется из имени узла, имен всех его родителей и указанием номера экземпляра. | |
| 150 | |
| 151 =item 2 Имена узлов могут содержать только буквы, цифры и символ _ | |
| 152 | |
| 153 =item 3 В случае когда узел может повторяться несколько раз, в квадратных скобках указывается | |
| 154 послеовательный номер экземпляра. | |
| 155 | |
| 156 =item 4 Имена параметров объединяются через символ '/' | |
| 157 | |
| 158 =back | |
| 159 | |
| 160 =cut |
