comparison Lib/IMPL/Web/DOM/FileNode.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
comparison
equal deleted inserted replaced
193:8e8401c0aea4 194:4d0e1962161c
5 5
6 use IMPL::Class::Property; 6 use IMPL::Class::Property;
7 use File::Temp qw(tempfile); 7 use File::Temp qw(tempfile);
8 8
9 BEGIN { 9 BEGIN {
10 public property parameterName => { 10 public property parameterName => {
11 get => sub { 11 get => sub {
12 my ($this) = @_; 12 my ($this) = @_;
13 $this->_parameterName() or 13 $this->_parameterName() or
14 $this->_parameterName( 14 $this->_parameterName(
15 join '/', ( map { 15 join '/', ( map {
16 (defined $_->nodeProperty('instanceId')) ? 16 (defined $_->nodeProperty('instanceId')) ?
17 $_->nodeName . '['.$_->nodeProperty('instanceId').']': 17 $_->nodeName . '['.$_->nodeProperty('instanceId').']':
18 $_->nodeName 18 $_->nodeName
19 } $this->_selectParents, $this ) 19 } $this->_selectParents, $this )
20 ); 20 );
21 } 21 }
22 }; 22 };
23 private property _parameterName => prop_all; 23 private property _parameterName => prop_all;
24 public property fileName => { 24 public property fileName => {
25 get => sub { 25 get => sub {
26 my ($this) = @_; 26 my ($this) = @_;
27 return $this->document->query->param($this->parameterName); 27 return $this->document->query->param($this->parameterName);
28 } 28 }
29 }; 29 };
30 public property fileHandle => { 30 public property fileHandle => {
31 get => sub { 31 get => sub {
32 my ($this) = @_; 32 my ($this) = @_;
33 return $this->document->query->upload($this->parameterName); 33 return $this->document->query->upload($this->parameterName);
34 } 34 }
35 }; 35 };
36 } 36 }
37 37
38 sub invokeTempFile { 38 sub invokeTempFile {
39 my ($this,$sub,$target) = @_; 39 my ($this,$sub,$target) = @_;
40 40
41 die new IMPL::InvalidArgumentException("A reference to a function should be specified") unless $sub && ref $sub eq 'CODE'; 41 die new IMPL::InvalidArgumentException("A reference to a function should be specified") unless $sub && ref $sub eq 'CODE';
42 42
43 $target ||= $this; 43 $target ||= $this;
44 44
45 my $query = $this->document->nodeProperty('query') or die new IMPL::InvalidOperationException("Failed to get a CGI query from the document"); 45 my $query = $this->document->nodeProperty('query') or die new IMPL::InvalidOperationException("Failed to get a CGI query from the document");
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')); 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'));
47 47
48 my ($hTemp,$tempFileName) = tempfile(); 48 my ($hTemp,$tempFileName) = tempfile();
49 binmode($hTemp); 49 binmode($hTemp);
50 50
51 print $hTemp $_ while <$hFile>; 51 print $hTemp $_ while <$hFile>;
52 52
53 $hTemp->flush(); 53 $hTemp->flush();
54 seek $hTemp, 0,0; 54 seek $hTemp, 0,0;
55 { 55 {
56 local $_ = $tempFileName; 56 local $_ = $tempFileName;
57 $sub->($this,$tempFileName,$hTemp); 57 $sub->($this,$tempFileName,$hTemp);
58 } 58 }
59 } 59 }
60 60
61 sub _selectParents { 61 sub _selectParents {
62 my ($node) = @_; 62 my ($node) = @_;
63 63
64 my @result; 64 my @result;
65 65
66 unshift @result, $node while $node = $node->parentNode; 66 unshift @result, $node while $node = $node->parentNode;
67 67
68 return @result; 68 return @result;
69 } 69 }
70 70
71 1; 71 1;
72 72
73 __END__ 73 __END__
82 82
83 =begin code xml 83 =begin code xml
84 84
85 <!-- input.schema.xml --> 85 <!-- input.schema.xml -->
86 <schema> 86 <schema>
87 <SimpleType type="file" nativeType="IMPL::Web::DOM::FileNode"/> 87 <SimpleType type="file" nativeType="IMPL::Web::DOM::FileNode"/>
88 <ComplexNode name="user"> 88 <ComplexNode name="user">
89 <Node type="file" name="avatar"/> 89 <Node type="file" name="avatar"/>
90 </ComplexNode> 90 </ComplexNode>
91 </schema> 91 </schema>
92 92
93 =end code xml 93 =end code xml
94 94
95 =begin code 95 =begin code
99 use IMPL::DOM::Schema; 99 use IMPL::DOM::Schema;
100 use CGI; 100 use CGI;
101 use File::Copy qw(copy); 101 use File::Copy qw(copy);
102 102
103 my $t = new IMPL::DOM::Transform::PostToDOM( 103 my $t = new IMPL::DOM::Transform::PostToDOM(
104 undef, 104 undef,
105 IMPL::DOM::Schema->LoadSchema('input.schema.xml'), 105 IMPL::DOM::Schema->LoadSchema('input.schema.xml'),
106 'user' 106 'user'
107 ); 107 );
108 108
109 my $doc = $t->Transform(CGI->new()); 109 my $doc = $t->Transform(CGI->new());
110 110
111 if ($t->Errors->Count) { 111 if ($t->Errors->Count) {
112 # handle errors 112 # handle errors
113 } 113 }
114 114
115 $doc->selectSingleNode('avatar')->invokeTempFile( 115 $doc->selectSingleNode('avatar')->invokeTempFile(
116 sub { 116 sub {
117 my($node,$fname,$fhandle) = @_; 117 my($node,$fname,$fhandle) = @_;
118 118
119 # do smth with file 119 # do smth with file
120 copy($_,'avatar.jpg'); 120 copy($_,'avatar.jpg');
121 121
122 # same thing 122 # same thing
123 # copy($fname,'avatar.jpg'); 123 # copy($fname,'avatar.jpg');
124 } 124 }
125 ); 125 );
126 126
127 =end code 127 =end code
128 128
129 =head1 DESCRIPTION 129 =head1 DESCRIPTION
177 =item C<$fhandle> 177 =item C<$fhandle>
178 178
179 Указатель на временный файл 179 Указатель на временный файл
180 180
181 =back 181 =back
182 182
183 Также пременная C<$_> содержит имя временного файла. 183 Также пременная C<$_> содержит имя временного файла.
184 184
185 =item C<$target> 185 =item C<$target>
186 186
187 Значение этого параметра будет передано первым параметром функции C<$callback>. 187 Значение этого параметра будет передано первым параметром функции C<$callback>.