comparison Lib/IMPL/DOM/Transform/PostToDOM.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents d1676be8afcc
children a4d9126edcbb
comparison
equal deleted inserted replaced
193:8e8401c0aea4 194:4d0e1962161c
16 private property _schema => prop_all; 16 private property _schema => prop_all;
17 } 17 }
18 18
19 our %CTOR = ( 19 our %CTOR = (
20 'IMPL::Transform' => sub { 20 'IMPL::Transform' => sub {
21 -plain => \&TransformPlain, 21 -plain => \&TransformPlain,
22 HASH => \&TransformContainer, 22 HASH => \&TransformContainer,
23 CGI => \&TransformCGI, 23 CGI => \&TransformCGI,
24 CGIWrapper => \&TransformCGI 24 CGIWrapper => \&TransformCGI
25 } 25 }
26 ); 26 );
27 27
28 sub CTOR { 28 sub CTOR {
29 my ($this,$docClass,$docSchema,$prefix) = @_; 29 my ($this,$docClass,$docSchema,$prefix) = @_;
30 $docClass ||= 'IMPL::DOM::Document'; 30 $docClass ||= 'IMPL::DOM::Document';
31 31
32 $this->_navi( 32 $this->_navi(
33 IMPL::DOM::Navigator::Builder->new( 33 IMPL::DOM::Navigator::Builder->new(
34 $docClass, 34 $docClass,
35 $docSchema 35 $docSchema
36 ) 36 )
37 ); 37 );
38 $this->_schema($docSchema); 38 $this->_schema($docSchema);
39 $this->prefix($prefix) if $prefix; 39 $this->prefix($prefix) if $prefix;
40 } 40 }
41 41
42 sub TransformContainer { 42 sub TransformContainer {
43 my ($this,$data) = @_; 43 my ($this,$data) = @_;
44 44
45 my $navi = $this->_navi; 45 my $navi = $this->_navi;
46 46
47 foreach my $key ( 47 foreach my $key (
48 sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} 48 sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]}
49 map [$_,/(\w+)(?:\[(\d+)\])?/], keys %$data 49 map [$_,/(\w+)(?:\[(\d+)\])?/], keys %$data
50 ){ 50 ){
51 my $value = $data->{$key->[0]}; 51 my $value = $data->{$key->[0]};
52 my $node = $navi->NavigateCreate($key->[1]); 52 my $node = $navi->NavigateCreate($key->[1]);
53 53
54 $node->nodeProperty(instanceId => $key->[2]) if defined $key->[2]; 54 $node->nodeProperty(instanceId => $key->[2]) if defined $key->[2];
55 55
56 $this->Transform($value); 56 $this->Transform($value);
57 57
58 $navi->Back(); 58 $navi->Back();
59 } 59 }
60 60
61 return $navi->Current; 61 return $navi->Current;
62 } 62 }
63 63
64 sub TransformPlain { 64 sub TransformPlain {
65 my ($this,$data) = @_; 65 my ($this,$data) = @_;
66 66
67 $this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) ); 67 $this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) );
68 } 68 }
69 69
70 sub TransformCGI { 70 sub TransformCGI {
71 my ($this,$query) = @_; 71 my ($this,$query) = @_;
72 72
73 my $data={}; 73 my $data={};
74 74
75 my $prefix = $this->prefix; 75 my $prefix = $this->prefix;
76 76
77 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) { 77 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
78 length (my $value = $query->param($param)) or next; 78 length (my $value = $query->param($param)) or next;
79 79
80 my @parts = split /\//,$param; 80 my @parts = split /\//,$param;
81 81
82 my $node = $data; 82 my $node = $data;
83 while ( my $part = shift @parts ) { 83 while ( my $part = shift @parts ) {
84 if (@parts) { 84 if (@parts) {
85 $node = ($node->{$part} ||= {}); 85 $node = ($node->{$part} ||= {});
86 } else { 86 } else {
87 $node->{$part} = $value; 87 $node->{$part} = $value;
88 } 88 }
89 } 89 }
90 } 90 }
91 91
92 if (keys %$data > 1) { 92 if (keys %$data > 1) {
93 $data = { document => $data }; 93 $data = { document => $data };
94 } 94 }
95 95
96 my $doc = $this->Transform($data); 96 my $doc = $this->Transform($data);
97 $doc->nodeProperty( query => $query ); 97 $doc->nodeProperty( query => $query );
98 $this->Errors->Append( $this->_navi->BuildErrors); 98 $this->Errors->Append( $this->_navi->BuildErrors);
99 $this->Errors->Append( $this->_schema->Validate($doc)); 99 $this->Errors->Append( $this->_schema->Validate($doc));
100 return $doc; 100 return $doc;
101 } 101 }
102 102
103 1; 103 1;
104 104
105 __END__ 105 __END__
112 112
113 =head1 SINOPSYS 113 =head1 SINOPSYS
114 114
115 =begin code 115 =begin code
116 116
117 my $schema = IMPL::DOM::Schema->LoadSchema('Data/user.add.schema.xml'); 117 my $schema = IMPL::DOM::Schema->LoadSchema('Data/user.add.schema.xml');
118 118
119 my $transform = IMPL::DOM::Transform::PostToDOM->new( 119 my $transform = IMPL::DOM::Transform::PostToDOM->new(
120 undef, # default class 120 undef, # default class
121 $schema, 121 $schema,
122 $schema->selectSingleNode('ComplexNode')->name 122 $schema->selectSingleNode('ComplexNode')->name
123 ); 123 );
124 124
125 my $doc = $transform->Transform( 125 my $doc = $transform->Transform(
126 CGI->new({ 126 CGI->new({
127 'user/login' => 'bob', 127 'user/login' => 'bob',
128 'user/fullName' => 'Bob Marley', 128 'user/fullName' => 'Bob Marley',
129 'user/password' => 'secret', 129 'user/password' => 'secret',
130 'user/password_retype' => 'secret', 130 'user/password_retype' => 'secret',
131 'user/birthday' => '1978-12-17', 131 'user/birthday' => '1978-12-17',
132 'user/email[1]' => 'bob@marley.com', 132 'user/email[1]' => 'bob@marley.com',
133 'user/email[2]' => 'bob.marley@google.com', 133 'user/email[2]' => 'bob.marley@google.com',
134 process => 1 134 process => 1
135 }) 135 })
136 ); 136 );
137 137
138 =end code 138 =end code
139 139
140 =head1 DESCRIPTION 140 =head1 DESCRIPTION
141 141
150 150
151 =item 2 Имена узлов могут содержать только буквы, цифры и символ _ 151 =item 2 Имена узлов могут содержать только буквы, цифры и символ _
152 152
153 =item 3 В случае когда узел может повторяться несколько раз, в квадратных скобках указывается 153 =item 3 В случае когда узел может повторяться несколько раз, в квадратных скобках указывается
154 послеовательный номер экземпляра. 154 послеовательный номер экземпляра.
155 155
156 =item 4 Имена параметров объединяются через символ '/' 156 =item 4 Имена параметров объединяются через символ '/'
157 157
158 =back 158 =back
159 159
160 =cut 160 =cut