-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationUtil.java
More file actions
356 lines (302 loc) · 12 KB
/
ValidationUtil.java
File metadata and controls
356 lines (302 loc) · 12 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
/**
* バリデーションチェックのためのメソッド群.
*
* 漢字や記号のうち,Shift_JISとして扱えない文字を検証します.
* また,ハイフン/ダッシュ系の記号とチルダを置換します.
*
*/
public class ValidationUtil {
protected static final Logger devLogger = Logger.getLogger("develop_log");
private static final int ALLOW_EXCEPT_TILDE = 1;
private static final int ALLOW_ALL = 2;
/**
* 許可しないハイフン/ダッシュ系文字列.
*
* 「‒」 U+2012 フィギアダッシュ
* 「–」 U+2013 ENダッシュ 数値で使用する
*/
private static char[] NG_DASHS = {'\u2012','\u2013'};
/**
* 変換先のハイフン/ダッシュ.
*
* 「—」 U+2014 EM DASH/全角ダッシュ
*/
private static char TO_DASH ='\u2014';
/**
* 許可するハイフン/ダッシュ系文字列.
*
* 「-」 U+2010 HYPHEN-MINUS
* 「‐」 U+002D HYPHEN
* 「—」 U+2014 EM DASH/全角ダッシュ
* 「─」 U+2500 罫線横
* 「-」 U+FF0D FULLWIDTH HYPHEN-MINUS ×Shift_JIS
* 「−」 U+2212 MINUS SIGN
*/
private static char[] PERMIT_BARS = {'\u002D','\u2010','\u2014','\u2500','\uFF0D','\u2212'};
/**
* 許可しないチルダ.
*
* 「〜」 U+301C WAVE DASH/波ダッシュ
* 「~」 U+007E TILDE
* 「˜」 U+02DC SMALL TILDA
* 「∼」 U+223C TILDE OPERATOR
*/
private static char[] NG_TILDAS = {'\u301c','\u007E','\u02DC','\u223C'};
/**
* 許可するチルダ.
* 変換先としても使用.
*
* 「~」 U+FF5E 全角チルダ
*/
private static char FULLWIDTH_TILDA = '\uFF5E';
/**
* 許可するドット系記号.
*
* 中点
* 中点(半角)
*/
private static char[] PERMIT_DOTS = {'\u30FB','\uFF65'};
/**
* 記号系の禁則文字列パターン.
*
* oracleDiarect では U+301C(WAVE DASH) を U+FF5E(FULL WIDTH TILDA)に変換される.
* oracle 以外を考慮して,U+301C(WAVE DASH) を禁則文字として扱う.
*
* 「~」 U+007E TILDE
* 「 ―」 U+2015 HORIZONTAL BAR
*/
private static final Pattern DENY_PATTERN = Pattern.compile("[^<>\"&,*$%|∥£€=`#~^\\[\\]();:{}\\u301c^―]*");
private static boolean isPermitExceptTilde(char c) {
// BAR
for (char sign : PERMIT_BARS) {
if (sign == c) {
return true;
}
}
// DOT
for (char sign : PERMIT_DOTS) {
if (sign == c) {
return true;
}
}
return false;
}
private static boolean isPermitAllInputs(char c) {
// TILDA
if (FULLWIDTH_TILDA == c){
return true;
}
// BAR
for (char sign : PERMIT_BARS) {
if (sign == c) {
return true;
}
}
// DOT
for (char sign : PERMIT_DOTS) {
if (sign == c){
return true;
}
}
return false;
}
/**
* ダッシュとチルダを置換する.
*
*/
public static String replaceDashTilda(String str) {
if (str == null) {
return null;
}
StringBuffer sb = new StringBuffer();
for (int i=0; i<str.length(); i++) {
String s = String.valueOf(str.charAt(i));
char c = str.charAt(i);
// DASH
for (char sign : NG_DASHS) {
if (sign == c) {
s = String.valueOf(TO_DASH);
}
}
// TILDA
for (char sign : NG_TILDAS) {
if (sign == c) {
s = String.valueOf(FULLWIDTH_TILDA);
}
}
sb.append(s);
}
return sb.toString();
}
/**
* 記号系の禁則文字が含まれるかを検証する.
*
*/
public static boolean isForbidden(String arg) {
Matcher m = DENY_PATTERN.matcher(arg);
return !m.matches();
}
/**
* 機種依存文字でない全角記号文字の有無を検証する.
*
* バイトコードの参照URL: http://charset.7jp.net/sjis.html
*
* Shift_JIS 変換不可文字が混入する文字列だった場合,true を返す.
* 以下で構成された文字列だった場合,false を返す.
* - 許可された記号
* - 半角数字、英字、カタカナ、記号
* - 1byte文字
* - 全角ひらがな、カタカナ、全角数字、全角英字
* - Shift_JIS変換可能漢字
*
* @param str 検証対象文字列
* @param checkType チェックオプションのフラグ
* 0: TILDA,BAR,DOT を許可しない
* 1: BAR,DOT を許可
* 2: TILDA,BAR,DOT を許可
*/
public static boolean isSymbolsForbidden(String str, int checkType) {
boolean ret = false;
byte[] bt = null;
String s = null;
char c;
for (int i = 0; i < str.length(); i++) {
try {
s = str.substring(i, i+1);
c = s.charAt(0);
bt = s.getBytes("Shift_JIS");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return true;
}
byte firstByte = bt[0];
String sFirst = Integer.toHexString(firstByte & 0xff);
// チェックオプション 1
if (checkType == ALLOW_EXCEPT_TILDE && isPermitExceptTilde(c)) {
devLogger.debug("[" + s + "]: ALLOW_EXCEPT_TILDE");
// チェックオプション 2
} else if (checkType == ALLOW_ALL && isPermitAllInputs(c)) {
devLogger.debug("[" + s + "]: ALLOW_ALL");
// Shift_JISに変換できない文字列
} else if (firstByte == 63 && !s.equals("?")){
devLogger.debug("[" + s + "]: Shift_JISに変換できない文字");
ret = true;
} else if ( sFirst.compareToIgnoreCase("0") >= 0 && sFirst.compareToIgnoreCase("127") < 0
|| sFirst.compareToIgnoreCase("161") >= 0 && sFirst.compareToIgnoreCase("223") < 0) {
devLogger.debug("[" + s + "]: 半角数字、英字、カタカナ、記号");
// 1byteチェック
}else if (bt.length == 1){
devLogger.debug("[" + s + "]: 1byte文字");
// 2byte文字列チェック
} else {
byte secondByte = bt[1];
String doubleByteStr = sFirst.concat(Integer.toHexString(secondByte & 0xff));
if (doubleByteStr.compareToIgnoreCase("8158") == 0 // 々
|| doubleByteStr.compareToIgnoreCase("815b") >= 0 && doubleByteStr.compareToIgnoreCase("815d") <= 0 // ー ― ‐
|| doubleByteStr.compareToIgnoreCase("824f") >= 0 && doubleByteStr.compareToIgnoreCase("8258") <= 0
|| doubleByteStr.compareToIgnoreCase("8260") >= 0 && doubleByteStr.compareToIgnoreCase("8279") <= 0
|| doubleByteStr.compareToIgnoreCase("8281") >= 0 && doubleByteStr.compareToIgnoreCase("829a") <= 0
|| doubleByteStr.compareToIgnoreCase("829f") >= 0 && doubleByteStr.compareToIgnoreCase("82f1") <= 0
|| doubleByteStr.compareToIgnoreCase("8340") >= 0 && doubleByteStr.compareToIgnoreCase("8396") <= 0) {
devLogger.debug("[" + s + "]: 全角ひらがな、カタカナ、全角数字、全角英字");
} else if (doubleByteStr.compareToIgnoreCase("889f") >= 0 && doubleByteStr.compareToIgnoreCase("9872") <= 0
|| doubleByteStr.compareToIgnoreCase("989f") >= 0 && doubleByteStr.compareToIgnoreCase("9ffc") <= 0
|| doubleByteStr.compareToIgnoreCase("e040") >= 0 && doubleByteStr.compareToIgnoreCase("eaa4") <= 0) {
devLogger.debug("[" + s + "]: Permit.");
} else {
devLogger.debug("[" + s + "]: Forbidden!");
ret = true;
break;
}
}
}
return ret;
}
/**
* Shift_JISへ変換できない漢字(旧字など)の有無を検証する.
*
* 変換不可文字が混入する文字列だった場合,true を返す.
* 以下で構成された文字列だった場合,false を返す.
* - 許可された記号
* - 半角数字、英字、カタカナ、記号
* - 1byte文字
* - 全角ひらがな、カタカナ、全角数字、全角英字
* - Shift_JIS変換可能漢字
*
* @param str 検証対象文字列
* @param checkType チェックオプションのフラグ
* 0: TILDA,BAR,DOTを許可しない
* 1: BAR,DOTを許可
* 2: TILDA,BAR,DOTを許可
*/
public static boolean isKanjiForbidden(String str, int checkType) {
boolean ret = false;
byte[] bt = null;
String s = null;
char c;
for (int i = 0; i < str.length(); i++) {
try {
s = str.substring(i, i+1);
c = s.charAt(0);
bt = s.getBytes("Shift_JIS");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return true;
}
byte firstByte = bt[0];
String sFirst = Integer.toHexString(firstByte & 0xff);
// チェックオプション 1
if (checkType == ALLOW_EXCEPT_TILDE && isPermitExceptTilde(c)) {
devLogger.debug("[" + s + "]: ALLOW_EXCEPT_TILDE");
// チェックオプション 2
} else if (checkType == ALLOW_ALL && isPermitAllInputs(c)) {
devLogger.debug("[" + s + "]: ALLOW_ALL");
// Shift_JISに変換できない文字列
} else if (firstByte == 63 && !s.equals("?")){
devLogger.debug("[" + s + "]: Shift_JISに変換できない文字");
ret = true;
} else if ( sFirst.compareToIgnoreCase("0") >= 0 && sFirst.compareToIgnoreCase("127") < 0
|| sFirst.compareToIgnoreCase("161") >= 0 && sFirst.compareToIgnoreCase("223") < 0) {
devLogger.debug("[" + s + "]: 半角数字、英字、カタカナ、記号");
// 1byteチェック
} else if (bt.length == 1) {
devLogger.debug("[" + s + "]: 1byte文字");
// 2byte文字列チェック
} else {
byte secondByte = bt[1];
String doubleByteStr = sFirst.concat(Integer.toHexString(secondByte & 0xff));
if (doubleByteStr.compareToIgnoreCase("8140") >= 0 && doubleByteStr.compareToIgnoreCase("81ac") <= 0
|| doubleByteStr.compareToIgnoreCase("81b8") >= 0 && doubleByteStr.compareToIgnoreCase("81bf") <= 0
|| doubleByteStr.compareToIgnoreCase("81c8") >= 0 && doubleByteStr.compareToIgnoreCase("81ce") <= 0
|| doubleByteStr.compareToIgnoreCase("81da") >= 0 && doubleByteStr.compareToIgnoreCase("81e8") <= 0
|| doubleByteStr.compareToIgnoreCase("81f0") >= 0 && doubleByteStr.compareToIgnoreCase("81f7") <= 0
|| doubleByteStr.compareToIgnoreCase("81fc") >= 0 && doubleByteStr.compareToIgnoreCase("81fc") <= 0
|| doubleByteStr.compareToIgnoreCase("824f") >= 0 && doubleByteStr.compareToIgnoreCase("8258") <= 0
|| doubleByteStr.compareToIgnoreCase("8260") >= 0 && doubleByteStr.compareToIgnoreCase("8279") <= 0
|| doubleByteStr.compareToIgnoreCase("8281") >= 0 && doubleByteStr.compareToIgnoreCase("829a") <= 0
|| doubleByteStr.compareToIgnoreCase("829f") >= 0 && doubleByteStr.compareToIgnoreCase("82f1") <= 0
|| doubleByteStr.compareToIgnoreCase("8340") >= 0 && doubleByteStr.compareToIgnoreCase("8396") <= 0
|| doubleByteStr.compareToIgnoreCase("839f") >= 0 && doubleByteStr.compareToIgnoreCase("83b6") <= 0
|| doubleByteStr.compareToIgnoreCase("83bf") >= 0 && doubleByteStr.compareToIgnoreCase("83d6") <= 0
|| doubleByteStr.compareToIgnoreCase("8440") >= 0 && doubleByteStr.compareToIgnoreCase("8460") <= 0
|| doubleByteStr.compareToIgnoreCase("8470") >= 0 && doubleByteStr.compareToIgnoreCase("8491") <= 0
|| doubleByteStr.compareToIgnoreCase("849f") >= 0 && doubleByteStr.compareToIgnoreCase("84be") <= 0) {
devLogger.debug("[" + s + "]: 全角ひらがな、カタカナ、全角数字、全角英字、全角記号");
} else if (doubleByteStr.compareToIgnoreCase("889f") >= 0 && doubleByteStr.compareToIgnoreCase("9872") <= 0
|| doubleByteStr.compareToIgnoreCase("989f") >= 0 && doubleByteStr.compareToIgnoreCase("9ffc") <= 0
|| doubleByteStr.compareToIgnoreCase("e040") >= 0 && doubleByteStr.compareToIgnoreCase("eaa4") <= 0) {
devLogger.debug("[" + s + "]: Permit.");
} else {
devLogger.debug("[" + s + "]: Forbidden!");
ret = true;
break;
}
}
}
return ret;
}
}