| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catalyst::Plugin::Charsets::Japanese; |
|
2
|
11
|
|
|
11
|
|
304666
|
use strict; |
|
|
11
|
|
|
|
|
26
|
|
|
|
11
|
|
|
|
|
457
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
11
|
|
|
11
|
|
59
|
use base qw/Class::Data::Inheritable/; |
|
|
11
|
|
|
|
|
22
|
|
|
|
11
|
|
|
|
|
11098
|
|
|
5
|
11
|
|
|
11
|
|
86074
|
use Jcode; |
|
|
11
|
|
|
|
|
2136583
|
|
|
|
11
|
|
|
|
|
1817
|
|
|
6
|
11
|
|
|
11
|
|
26538
|
use NEXT; |
|
|
11
|
|
|
|
|
33502
|
|
|
|
11
|
|
|
|
|
9285
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata('charsets'); |
|
9
|
|
|
|
|
|
|
__PACKAGE__->charsets( Catalyst::Plugin::Charsets::Japanese::Handler->new ); |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub finalize { |
|
14
|
0
|
|
|
0
|
0
|
0
|
my $c = shift; |
|
15
|
0
|
0
|
0
|
|
|
0
|
unless ( $c->response->body and not ref $c->response->body ) { |
|
16
|
0
|
|
|
|
|
0
|
return $c->NEXT::finalize; |
|
17
|
|
|
|
|
|
|
} |
|
18
|
0
|
0
|
|
|
|
0
|
unless ( $c->response->content_type =~ /^text|xml$|javascript$/ ) { |
|
19
|
0
|
|
|
|
|
0
|
return $c->NEXT::finalize; |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
0
|
my $content_type = $c->response->content_type; |
|
23
|
0
|
|
|
|
|
0
|
$content_type =~ s/\;\s*$//; |
|
24
|
0
|
|
|
|
|
0
|
$content_type =~ s/\;*\s*charset\s*\=.*$//i; |
|
25
|
0
|
|
|
|
|
0
|
$content_type .= sprintf("; charset=%s", $c->charsets->out->name ); |
|
26
|
0
|
|
|
|
|
0
|
$c->response->content_type($content_type); |
|
27
|
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
0
|
my $body = $c->response->body; |
|
29
|
|
|
|
|
|
|
|
|
30
|
0
|
0
|
0
|
|
|
0
|
if( $c->charsets->in->name eq 'UTF-8' && utf8::is_utf8($body) ) { |
|
31
|
0
|
|
|
|
|
0
|
utf8::encode($body); |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
0
|
my $in = $c->charsets->in->abbreviation; |
|
35
|
0
|
|
|
|
|
0
|
my $out = $c->charsets->out->method; |
|
36
|
0
|
|
|
|
|
0
|
$body = Jcode->new($body, $in)->$out; |
|
37
|
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
0
|
$c->response->body($body); |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
0
|
$c->NEXT::finalize; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub prepare_parameters { |
|
44
|
0
|
|
|
0
|
0
|
0
|
my $c = shift; |
|
45
|
0
|
|
|
|
|
0
|
$c->NEXT::prepare_parameters; |
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
0
|
my $in = $c->charsets->in->method; |
|
48
|
0
|
|
|
|
|
0
|
my $out = $c->charsets->out->abbreviation; |
|
49
|
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
0
|
for my $value ( values %{ $c->request->{parameters} } ) { |
|
|
0
|
|
|
|
|
0
|
|
|
51
|
0
|
0
|
0
|
|
|
0
|
if( ref $value && ref $value ne 'ARRAY' ) { |
|
52
|
0
|
|
|
|
|
0
|
next; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
0
|
0
|
|
|
|
0
|
for ( ref($value) ? @{$value} : $value ) { |
|
|
0
|
|
|
|
|
0
|
|
|
55
|
0
|
|
|
|
|
0
|
$_ = Jcode->new($_, $out)->h2z->$in; |
|
56
|
0
|
0
|
|
|
|
0
|
utf8::decode($_) if $c->charsets->in->name eq 'UTF-8'; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub setup { |
|
62
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
|
63
|
0
|
|
|
|
|
0
|
$self->NEXT::setup(@_); |
|
64
|
0
|
|
0
|
|
|
0
|
my $setting = $self->config->{charsets} || 'UTF-8' ; |
|
65
|
0
|
0
|
|
|
|
0
|
if(ref $setting eq 'HASH') { |
|
66
|
0
|
|
|
|
|
0
|
$self->charsets->set_inner($setting->{in}); |
|
67
|
0
|
|
|
|
|
0
|
$self->charsets->set_outer($setting->{out}) |
|
68
|
|
|
|
|
|
|
} else { |
|
69
|
0
|
|
|
|
|
0
|
$self->charsets->set_inner($setting); |
|
70
|
0
|
|
|
|
|
0
|
$self->charsets->set_outer($setting); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
0
|
0
|
|
|
|
0
|
if($self->debug){ |
|
73
|
0
|
|
|
|
|
0
|
$self->log->debug($self->charsets->in->name." is selected for inner code."); |
|
74
|
0
|
|
|
|
|
0
|
$self->log->debug($self->charsets->out->name." is selected for outer code."); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
package Catalyst::Plugin::Charsets::Japanese::Handler; |
|
79
|
11
|
|
|
11
|
|
383
|
use base qw/Class::Accessor::Fast/; |
|
|
11
|
|
|
|
|
30
|
|
|
|
11
|
|
|
|
|
12158
|
|
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/in out/); |
|
82
|
|
|
|
|
|
|
|
|
83
|
12
|
|
|
12
|
|
101
|
sub new { bless {}, $_[0] } |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub set_inner { |
|
86
|
10
|
|
|
10
|
|
18805
|
my($self, $code) = @_; |
|
87
|
10
|
|
|
|
|
46
|
$self->in(Catalyst::Plugin::Charsets::Japanese::Charset->new($code)); |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
sub set_outer { |
|
90
|
10
|
|
|
10
|
|
19434
|
my($self, $code) = @_; |
|
91
|
10
|
|
|
|
|
36
|
$self->out(Catalyst::Plugin::Charsets::Japanese::Charset->new($code)); |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub name { |
|
95
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
96
|
0
|
|
|
|
|
0
|
return $self->in->name; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub abbreviation { |
|
100
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
101
|
0
|
|
|
|
|
0
|
return $self->in->abbreviation; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub method { |
|
105
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
106
|
0
|
|
|
|
|
0
|
return $self->in->method; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
package Catalyst::Plugin::Charsets::Japanese::Charset; |
|
110
|
11
|
|
|
11
|
|
66210
|
use base qw/Class::Accessor::Fast/; |
|
|
11
|
|
|
|
|
40
|
|
|
|
11
|
|
|
|
|
3806
|
|
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/name abbreviation method/); |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my @TYPES = ( |
|
115
|
|
|
|
|
|
|
[qw/UTF-8 utf8/], |
|
116
|
|
|
|
|
|
|
[qw/EUC-JP euc/ ], |
|
117
|
|
|
|
|
|
|
[qw/Shift_JIS sjis/], |
|
118
|
|
|
|
|
|
|
); |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub new { |
|
121
|
20
|
|
|
20
|
|
30
|
my $class = shift; |
|
122
|
20
|
|
|
|
|
78
|
my $self = bless {}, $class; |
|
123
|
20
|
|
|
|
|
48
|
$self->_init(@_); |
|
124
|
20
|
|
|
|
|
71
|
return $self; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub _init { |
|
128
|
20
|
|
|
20
|
|
28
|
my $self = shift; |
|
129
|
20
|
|
|
|
|
25
|
my $code = shift; |
|
130
|
20
|
|
|
|
|
37
|
foreach my $type ( @TYPES ) { |
|
131
|
42
|
|
|
|
|
74
|
foreach ( @$type ) { |
|
132
|
72
|
100
|
|
|
|
215
|
if( lc($code) eq lc($_) ) { |
|
133
|
20
|
|
|
|
|
72
|
$self->name($type->[0]); |
|
134
|
20
|
|
|
|
|
165
|
$self->abbreviation($type->[1]); |
|
135
|
20
|
|
|
|
|
137
|
$self->method($type->[1]); |
|
136
|
20
|
|
|
|
|
123
|
return; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
} |
|
140
|
0
|
|
|
|
|
|
$self->_croak("wrong charset detected."); |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _croak { |
|
144
|
0
|
|
|
0
|
|
|
my($self, $msg) = @_; |
|
145
|
0
|
|
|
|
|
|
require Carp; Carp::croak($msg); |
|
|
0
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
|
149
|
|
|
|
|
|
|
__END__ |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 NAME |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Catalyst::Plugin::Charsets::Japanese - Japanese specific charsets handler |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
use Catalyst 'Charsets::Japanese'; |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# set charset |
|
160
|
|
|
|
|
|
|
MyApp->config->{charsets} = 'UTF-8'; |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# you can set two charsets. |
|
163
|
|
|
|
|
|
|
# one is for inner, another is for output response. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
MyApp->config->{charsets} = { |
|
166
|
|
|
|
|
|
|
in => 'EUC-JP', |
|
167
|
|
|
|
|
|
|
out => 'Shift_JIS', |
|
168
|
|
|
|
|
|
|
}; |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Japanese usually use the charsets, UTF-8, EUC-JP, and Shift_JIS, |
|
173
|
|
|
|
|
|
|
when they develop web applications. |
|
174
|
|
|
|
|
|
|
This module allows you to deal with things related to Japanese charset automatically. |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 charsets |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This plugin implements 'charsets' accessor to context object. |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub default : Private { |
|
181
|
|
|
|
|
|
|
my( $self, $c ) = @_; |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# charset's name. UTF-8, EUC-JP, Shift_JIS |
|
184
|
|
|
|
|
|
|
my $inner_charset = $c->charsets->in->name; |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# charset's abbreviation. utf8, euc, shiftjis |
|
187
|
|
|
|
|
|
|
my $inner_abbrev = $c->charsets->in->abbreviation; |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Jcode method's name. utf8, euc, sjis |
|
190
|
|
|
|
|
|
|
my $inner_method = $c->charsets->in->method; |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# and you can get information about charset for output response. |
|
193
|
|
|
|
|
|
|
my $outer_charset = $c->charsets->out->name; |
|
194
|
|
|
|
|
|
|
} |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
L<Jcode> |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
L<Catalyst> |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
L<Catalyst::Plugin::Charsets::Japanese::Nihongo.pod> |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 AUTHOR |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Lyo Kato E<lt>lyo.kato@gmail.com<gt> |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
211
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |
|
214
|
|
|
|
|
|
|
|