annotate Lib/IMPL/Web/DOM/FileNode.pm @ 202:5146e17a7b76

IMPL::Web::Application::RestResource fixes, documentation
author sergey
date Wed, 25 Apr 2012 02:49:23 +0400
parents 4d0e1962161c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
1 package IMPL::Web::DOM::FileNode;
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 151
diff changeset
2 use parent qw(IMPL::DOM::Node);
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
3
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
4 __PACKAGE__->PassThroughArgs;
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
5
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
6 use IMPL::Class::Property;
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
7 use File::Temp qw(tempfile);
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
8
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
9 BEGIN {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
10 public property parameterName => {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
11 get => sub {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
12 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
13 $this->_parameterName() or
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
14 $this->_parameterName(
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
15 join '/', ( map {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
16 (defined $_->nodeProperty('instanceId')) ?
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
17 $_->nodeName . '['.$_->nodeProperty('instanceId').']':
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
18 $_->nodeName
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
19 } $this->_selectParents, $this )
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
20 );
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
21 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
22 };
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
23 private property _parameterName => prop_all;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
24 public property fileName => {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
25 get => sub {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
26 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
27 return $this->document->query->param($this->parameterName);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
28 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
29 };
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
30 public property fileHandle => {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
31 get => sub {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
32 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
33 return $this->document->query->upload($this->parameterName);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
34 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
35 };
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
36 }
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
37
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
38 sub invokeTempFile {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
39 my ($this,$sub,$target) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
40
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
41 die new IMPL::InvalidArgumentException("A reference to a function should be specified") unless $sub && ref $sub eq 'CODE';
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
42
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
43 $target ||= $this;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
44
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
45 my $query = $this->document->nodeProperty('query') or die new IMPL::InvalidOperationException("Failed to get a CGI query from the document");
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
46 my $hFile = $query->upload($this->parameterName) or die new IMPL::IOException("Failed to open the uploaded file",$query->cgi_error,$this->parameterName,$this->nodeProperty('instanceId'));
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
47
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
48 my ($hTemp,$tempFileName) = tempfile();
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
49 binmode($hTemp);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
50
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
51 print $hTemp $_ while <$hFile>;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
52
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
53 $hTemp->flush();
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
54 seek $hTemp, 0,0;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
55 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
56 local $_ = $tempFileName;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
57 $sub->($this,$tempFileName,$hTemp);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
58 }
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
59 }
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
60
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
61 sub _selectParents {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
62 my ($node) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
63
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
64 my @result;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
65
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
66 unshift @result, $node while $node = $node->parentNode;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
67
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
68 return @result;
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
69 }
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
70
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
71 1;
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
72
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
73 __END__
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
74
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
75 =pod
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
76
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
77 =head1 NAME
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
78
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
79 C<IMPL::Web::DOM::FileNode> - узел, использующийся для представления параметра запроса в котором передан файл.
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
80
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
81 =head1 SINOPSYS
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
82
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
83 =begin code xml
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
84
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
85 <!-- input.schema.xml -->
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
86 <schema>
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
87 <SimpleType type="file" nativeType="IMPL::Web::DOM::FileNode"/>
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
88 <ComplexNode name="user">
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
89 <Node type="file" name="avatar"/>
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
90 </ComplexNode>
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
91 </schema>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
92
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
93 =end code xml
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
94
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
95 =begin code
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
96
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
97 # handle.pl
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
98 use IMPL::DOM::Transform::PostToDOM ();
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
99 use IMPL::DOM::Schema;
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
100 use CGI;
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
101 use File::Copy qw(copy);
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
102
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
103 my $t = new IMPL::DOM::Transform::PostToDOM(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
104 undef,
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
105 IMPL::DOM::Schema->LoadSchema('input.schema.xml'),
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
106 'user'
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
107 );
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
108
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
109 my $doc = $t->Transform(CGI->new());
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
110
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
111 if ($t->Errors->Count) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
112 # handle errors
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
113 }
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
114
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
115 $doc->selectSingleNode('avatar')->invokeTempFile(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
116 sub {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
117 my($node,$fname,$fhandle) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
118
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
119 # do smth with file
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
120 copy($_,'avatar.jpg');
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
121
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
122 # same thing
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
123 # copy($fname,'avatar.jpg');
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
124 }
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
125 );
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
126
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
127 =end code
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
128
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
129 =head1 DESCRIPTION
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
130
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
131 Данный класс используется для представлении параметров C<CGI> запросов при преобзаовании
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
132 запроса в ДОМ документ преобразованием C<IMPL::DOM::Transform::PostToDOM>.
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
133
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
134 Узлы данного типа расширяют стандатрный C<IMPL::DOM::Node> несколькими свойствами и
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
135 методами для доступа к файлу, переданному в виде параметра запроса.
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
136
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
137 =head1 MEMBERS
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
138
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
139 =head2 PROPERTIES
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
140
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
141 =over
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
142
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
143 =item C<[get] parameterName>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
144
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
145 Имя параметра C<CGI> запроса соответствующего данному узлу.
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
146
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
147 =item C<[get] fileName>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
148
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
149 Имя файла из параметра запроса
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
150
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
151 =item C<[get] fileHandle>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
152
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
153 Указатель на файл из параметра запроса
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
154
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
155 =back
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
156
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
157 =head2 METHODS
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
158
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
159 =over
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
160
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
161 =item C<invokeTempFile($callback,$target)>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
162
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
163 Сохраняет файл, переданный в запросе во временный, вызывает C<$callback> для обработки временного файла.
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
164
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
165 =over
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
166
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
167 =item C<$callback>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
168
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
169 Ссылка на функцию которая будет вызвана для обработки временного файла. C<callback($target,$fname,$fhandle)>
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
170
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
171 =over
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
172
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
173 =item C<$fname>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
174
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
175 Имя временного файла
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
176
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
177 =item C<$fhandle>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
178
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
179 Указатель на временный файл
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
180
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
181 =back
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
182
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
183 Также пременная C<$_> содержит имя временного файла.
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
184
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
185 =item C<$target>
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
186
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
187 Значение этого параметра будет передано первым параметром функции C<$callback>.
148
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
188
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
189 =back
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
190
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
191 =back
e6447ad85cb4 DOM objects now have a schema and schemaSource properties
wizard
parents:
diff changeset
192
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
193 =cut