forked from apache/netbeans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18ncheck.pl
232 lines (198 loc) · 5.63 KB
/
i18ncheck.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env perl
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# XXX rewrite in Java: http://netbeans.org/bugzilla/show_bug.cgi?id=25859
#
# i18ncheck.pl - checks java source for internationalizable strings
# not 100% foolproof !!
#
#
require 5.005;
use File::Find;
my $fixmode = 0;
my @files = ();
my @modifiedfiles = ();
my @lines = ();
if ($#ARGV < 0) {
die "usage: $0 [-f] file | directory ...\n";
}
if ($ARGV[0] eq "-f") {
$fixmode = 1;
shift @ARGV;
}
if ($#ARGV < 0) {
die "usage: $0 [-f] file | directory ...\n";
}
$SIG{'INT'} = sub {
print_summary() if $fixmode;
exit 255;
};
foreach my $name (@ARGV) {
if (-f $name) {
push @files, $name;
} elsif (-d $name) {
find(sub {
if (-f && m,\.java$,) {
push @files, $File::Find::name;
}
},
$name);
}
}
foreach my $name (@files) {
checkfile($name);
}
print_summary() if $fixmode;
exit 0;
#
# subroutines
#
sub checkfile {
my $fname = shift;
@lines = ();
if (! open(FH, "< $fname")) {
warn "cannot open file '$fname': $!";
return;
}
@lines = <FH>;
close FH;
my $lineno = 1;
my $modified = 0;
LOOP:
while ($lineno <= $#lines) {
$_ = $lines[$lineno - 1];
if (m,/\*,,) {
while ($lineno <= $#lines) {
$_ = $lines[$lineno - 1];
if (! m,\*/,) {
$lineno++;
next;
} else {
last;
}
}
}
if (m,\@Messages\(\{,) {
while ($lineno <= $#lines) {
$_ = $lines[$lineno - 1];
if (! m,\}\),) {
$lineno++;
next;
} else {
last;
}
}
}
# skip line comment
if (m,(^ *//.*$),) {
$_ = $`;
}
if (checkline($_)) {
if ($fixmode) {
print "$fname:$lineno:\n";
if (fixline($fname, $lineno)) {
$modified = 1;
}
} else {
print "$fname:$lineno: $_";
}
}
$lineno++;
}
if ($fixmode && $modified) {
savefile($fname);
push @modifiedfiles, $fname;
}
}
sub fixline {
my $fname = shift;
my $lineno = shift;
my $answer;
print "\n";
print " " . $lines[$lineno - 4] if $lineno >= 4;
print " " . $lines[$lineno - 3] if $lineno >= 3;
print " " . $lines[$lineno - 2] if $lineno >= 2;
print " =>" . $lines[$lineno - 1] if $lineno >= 1;
print " " . $lines[$lineno] if $lineno <= $#lines;;
print " " . $lines[$lineno + 1] if $lineno + 1 <= $#lines;;
print " " . $lines[$lineno + 2] if $lineno + 2 <= $#lines;;
print "\n";
do {
print "** [M]ark this line with NOI18N -- [S]kip [m]: ";
flush;
$answer = <STDIN>;
chomp $answer;
} while (uc($answer) ne "M" && uc($answer) ne "S" && $answer ne "");
if (uc($answer) eq "S") {
return 0;
} else {
$lines[$lineno - 1] =~ s,\s*$,,;
$lines[$lineno - 1] = $lines[$lineno - 1] . " // NOI18N\n";
return 1;
}
}
sub savefile {
my $fname = shift;
if (!rename $fname, "$fname.bak") {
warn "** Cannot create backup for $fname, changes have not been saved\n";
return;
}
if (! open(FH, "> $fname")) {
warn "** Cannot save changes in $fname\n";
}
print FH @lines;
close FH;
}
sub checkline {
shift;
return (! m,NOI18N,
&& ! m,getString\s*\(,
&& ! m,System\. ?err\. ?print,
&& ! m,getProperty,
&& ! m,getBoolean,
&& ! m,assert,
&& ! m,AssertionError,
&& ! m,/\*\*,
&& ! m,//.*",
&& ! m,get(Message|LocalizedFile),
&& ! m,\. ?(info|warning|fine|finer|finest)\s*\(,
&& ! m,\. ?log\s*\(\s*Level\s*\.\s*(SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST)\s*,
&& ! m,\@SuppressWarnings\s*\(,
&& ! m,new HelpCtx\s*\(,
&& ! m,new PropertyDescriptor\s*\(,
&& ! m,setIconBase\s*\(,
&& ! m,setIconBaseWithExtension\s*\(,
&& ! m,setPrototypeDisplayValue\s*\(,
&& ! m,loadImage\s*\(,
&& ! m,getResource(AsStream)?\s*\(,
&& ! m,findResource ?\s*\(,
&& ! m,Parameters\.\s*not,
&& ! m,Parameters\.\s*java,
&& ! m,^\s*\@,
&& m,".+",
);
}
sub print_summary {
if ($#modifiedfiles < 0) {
print "\n\n** No file has been modified\n";
} else {
print "\n\n** The following files have been modified:\n\n";
foreach my $fname (@modifiedfiles) {
print " $fname\n";
}
}
}