Mercurial > pub > Impl
comparison Lib/IMPL/Web/QueryHandler/PageFormat.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 |
|---|---|
| 16 use Encode(); | 16 use Encode(); |
| 17 | 17 |
| 18 $Template::Plugin::URL::JOINT = '&'; | 18 $Template::Plugin::URL::JOINT = '&'; |
| 19 | 19 |
| 20 BEGIN { | 20 BEGIN { |
| 21 public property templatesCharset => prop_all; | 21 public property templatesCharset => prop_all; |
| 22 public property templatesBase => prop_all; | 22 public property templatesBase => prop_all; |
| 23 public property includes => prop_all | prop_list; | 23 public property includes => prop_all | prop_list; |
| 24 public property pathinfoPrefix => prop_all; | 24 public property pathinfoPrefix => prop_all; |
| 25 public property cache => prop_all; | 25 public property cache => prop_all; |
| 26 public property preprocess => prop_all; | 26 public property preprocess => prop_all; |
| 27 public property formatOutput => prop_all; | 27 public property formatOutput => prop_all; |
| 28 public property template => prop_all; | 28 public property template => prop_all; |
| 29 } | 29 } |
| 30 | 30 |
| 31 sub CTOR { | 31 sub CTOR { |
| 32 my ($this) = @_; | 32 my ($this) = @_; |
| 33 | 33 |
| 34 $this->templatesCharset('utf-8') unless $this->templatesCharset; | 34 $this->templatesCharset('utf-8') unless $this->templatesCharset; |
| 35 $this->cache(File::Spec->rel2abs($this->cache)) if $this->cache; | 35 $this->cache(File::Spec->rel2abs($this->cache)) if $this->cache; |
| 36 $this->templatesBase(File::Spec->rel2abs($this->templatesBase)) if $this->templatesBase; | 36 $this->templatesBase(File::Spec->rel2abs($this->templatesBase)) if $this->templatesBase; |
| 37 } | 37 } |
| 38 | 38 |
| 39 sub Process { | 39 sub Process { |
| 40 my ($this,$action,$nextHandler) = @_; | 40 my ($this,$action,$nextHandler) = @_; |
| 41 | 41 |
| 42 my $doc = new IMPL::Web::TT::Document(cache => $this->cache, preprocess => $this->preprocess); | 42 my $doc = new IMPL::Web::TT::Document(cache => $this->cache, preprocess => $this->preprocess); |
| 43 | 43 |
| 44 try { | 44 try { |
| 45 | 45 |
| 46 $this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase; | 46 $this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase; |
| 47 | 47 |
| 48 my ($requestUri) = split( /\?/, $ENV{REQUEST_URI} ); | 48 my ($requestUri) = split( /\?/, $ENV{REQUEST_URI} ); |
| 49 | 49 |
| 50 my $pathInfo; | 50 my $pathInfo; |
| 51 my @root = (''); | 51 my @root = (''); |
| 52 my @base; | 52 my @base; |
| 53 | 53 |
| 54 if ( $requestUri eq $ENV{SCRIPT_NAME}.$ENV{PATH_INFO} ) { | 54 if ( $requestUri eq $ENV{SCRIPT_NAME}.$ENV{PATH_INFO} ) { |
| 55 # CGI with path info, for example | 55 # CGI with path info, for example |
| 56 # /base/cgi-bin/myscript.cgi/path/info | 56 # /base/cgi-bin/myscript.cgi/path/info |
| 57 # PATH_INFO will be /path/info | 57 # PATH_INFO will be /path/info |
| 58 $pathInfo = $ENV{PATH_INFO}; | 58 $pathInfo = $ENV{PATH_INFO}; |
| 59 } else { | 59 } else { |
| 60 # usual url, for exmaple | 60 # usual url, for exmaple |
| 61 # /base/script.cgi will have PATH_INFO /base/script.cgi | 61 # /base/script.cgi will have PATH_INFO /base/script.cgi |
| 62 # /base/ will have PATH_INFO /base/index.cgi (if index.cgi is a DirectoryIndex) | 62 # /base/ will have PATH_INFO /base/index.cgi (if index.cgi is a DirectoryIndex) |
| 63 $pathInfo = $ENV{PATH_INFO}; | 63 $pathInfo = $ENV{PATH_INFO}; |
| 64 | 64 |
| 65 if (my $rx = $this->pathinfoPrefix) { | 65 if (my $rx = $this->pathinfoPrefix) { |
| 66 $requestUri =~ s/^($rx)//; | 66 $requestUri =~ s/^($rx)//; |
| 67 $pathInfo =~ s/^($rx)//; | 67 $pathInfo =~ s/^($rx)//; |
| 68 push @root, grep $_, split /\//, $1 if $1; | 68 push @root, grep $_, split /\//, $1 if $1; |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 | 71 |
| 72 @base = grep $_, split /\//, ($pathInfo ? substr $requestUri,0, -length($pathInfo) : $requestUri); | 72 @base = grep $_, split /\//, ($pathInfo ? substr $requestUri,0, -length($pathInfo) : $requestUri); |
| 73 | 73 |
| 74 local $ENV{PATH_INFO} = $pathInfo; | 74 local $ENV{PATH_INFO} = $pathInfo; |
| 75 | 75 |
| 76 my @path = grep $_, split /\//, ($ENV{PATH_INFO} || '') or die new IMPL::Exception("PATH_INFO is empty and no defaultTarget specified" ); | 76 my @path = grep $_, split /\//, ($ENV{PATH_INFO} || '') or die new IMPL::Exception("PATH_INFO is empty and no defaultTarget specified" ); |
| 77 | 77 |
| 78 my @pathContainer = @path; | 78 my @pathContainer = @path; |
| 79 pop @pathContainer; | 79 pop @pathContainer; |
| 80 | 80 |
| 81 $doc->LoadFile ( | 81 $doc->LoadFile ( |
| 82 ($this->template || File::Spec->catfile($this->templatesBase,@path)), | 82 ($this->template || File::Spec->catfile($this->templatesBase,@path)), |
| 83 $this->templatesCharset, | 83 $this->templatesCharset, |
| 84 [$this->templatesBase, $this->includes], | 84 [$this->templatesBase, $this->includes], |
| 85 { | 85 { |
| 86 result => scalar($nextHandler->()), | 86 result => scalar($nextHandler->()), |
| 87 action => $action, | 87 action => $action, |
| 88 app => $action->application, | 88 app => $action->application, |
| 89 | 89 |
| 90 absoluteUrl => sub { new URI(join ('/', @root, $_[0]) ) }, | 90 absoluteUrl => sub { new URI(join ('/', @root, $_[0]) ) }, |
| 91 baseUrl => sub { new URI (join ('/', @root, @base, $_[0]) ) }, | 91 baseUrl => sub { new URI (join ('/', @root, @base, $_[0]) ) }, |
| 92 relativeUrl => sub { new URI(join ('/', @root, @base, @pathContainer,$_[0]) ) }, | 92 relativeUrl => sub { new URI(join ('/', @root, @base, @pathContainer,$_[0]) ) }, |
| 93 | 93 |
| 94 user => IMPL::Security::Context->current->principal, | 94 user => IMPL::Security::Context->current->principal, |
| 95 session => IMPL::Security::Context->current, | 95 session => IMPL::Security::Context->current, |
| 96 | 96 |
| 97 to_json => \&to_json, | 97 to_json => \&to_json, |
| 98 escape_string => sub { $_[0] =~ s/"/"/g; $_[0] }, | 98 escape_string => sub { $_[0] =~ s/"/"/g; $_[0] }, |
| 99 } | 99 } |
| 100 ); | 100 ); |
| 101 | 101 |
| 102 $action->response->contentType('text/html'); | 102 $action->response->contentType('text/html'); |
| 103 my $hOut = $action->response->streamBody; | 103 my $hOut = $action->response->streamBody; |
| 104 if ($this->formatOutput == 1) { | 104 if ($this->formatOutput == 1) { |
| 105 my $tree = new HTML::TreeBuilder(); | 105 my $tree = new HTML::TreeBuilder(); |
| 106 try { | 106 try { |
| 107 $tree->parse_content($doc->Render()); | 107 $tree->parse_content($doc->Render()); |
| 108 print $hOut $tree->as_HTML('<>&'," ",{}); | 108 print $hOut $tree->as_HTML('<>&'," ",{}); |
| 109 } finally { | 109 } finally { |
| 110 $tree->delete; | 110 $tree->delete; |
| 111 }; | 111 }; |
| 112 } elsif ($this->formatOutput() == 2 ) { | 112 } elsif ($this->formatOutput() == 2 ) { |
| 113 (my $data = $doc->Render()) =~ s/\s+/ /g; | 113 (my $data = $doc->Render()) =~ s/\s+/ /g; |
| 114 print $hOut $data; | 114 print $hOut $data; |
| 115 } else { | 115 } else { |
| 116 print $hOut $doc->Render(); | 116 print $hOut $doc->Render(); |
| 117 } | 117 } |
| 118 } finally { | 118 } finally { |
| 119 $doc->Dispose; | 119 $doc->Dispose; |
| 120 }; | 120 }; |
| 121 } | 121 } |
| 122 | 122 |
| 123 sub URI::_query::new_params { | 123 sub URI::_query::new_params { |
| 124 my ($this,$params) = @_; | 124 my ($this,$params) = @_; |
| 125 | 125 |
| 126 my $clone = $this->clone; | 126 my $clone = $this->clone; |
| 127 if (ref $params eq 'HASH' ) { | 127 if (ref $params eq 'HASH' ) { |
| 128 my %newParams = ($clone->query_form , %$params); | 128 my %newParams = ($clone->query_form , %$params); |
| 129 $clone->query_form(map { $_, ( Encode::is_utf8( $newParams{$_} ) ? Encode::encode('utf-8', $newParams{$_}) : $newParams{$_} ) } sort keys %newParams ); | 129 $clone->query_form(map { $_, ( Encode::is_utf8( $newParams{$_} ) ? Encode::encode('utf-8', $newParams{$_}) : $newParams{$_} ) } sort keys %newParams ); |
| 130 } | 130 } |
| 131 return $clone; | 131 return $clone; |
| 132 } | 132 } |
| 133 | 133 |
| 134 1; | 134 1; |
| 135 | 135 |
| 136 __END__ | 136 __END__ |
| 146 В файле конфигурации приложения | 146 В файле конфигурации приложения |
| 147 | 147 |
| 148 =begin code xml | 148 =begin code xml |
| 149 | 149 |
| 150 <handlersQuery type="IMPL::Object::List"> | 150 <handlersQuery type="IMPL::Object::List"> |
| 151 <item type="IMPL::Web::QueryHandler::PageFormat"> | 151 <item type="IMPL::Web::QueryHandler::PageFormat"> |
| 152 <charsetTemplates>utf-8</charsetTemplates> | 152 <charsetTemplates>utf-8</charsetTemplates> |
| 153 </item> | 153 </item> |
| 154 </handlersQuery> | 154 </handlersQuery> |
| 155 | 155 |
| 156 =end code xml | 156 =end code xml |
| 157 | 157 |
| 158 Программно | 158 Программно |
| 159 | 159 |
| 160 =begin code | 160 =begin code |
| 161 | 161 |
| 162 my $app = new IMPL::Web::Application(); | 162 my $app = new IMPL::Web::Application(); |
| 163 $app->handlersQuery->Add( | 163 $app->handlersQuery->Add( |
| 164 new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' ); | 164 new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' ); |
| 165 ); | 165 ); |
| 166 | 166 |
| 167 =end | 167 =end |
| 168 | 168 |
| 169 =head1 DESCRIPTION | 169 =head1 DESCRIPTION |
