|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| StringUtils.java | 56.7% | 55.4% | 46.7% | 54.6% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Joey and its relative products are published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package org.asyrinx.brownie.core.lang;
|
|
| 6 |
|
|
| 7 |
import java.util.ArrayList;
|
|
| 8 |
import java.util.List;
|
|
| 9 |
import java.util.Properties;
|
|
| 10 |
import java.util.StringTokenizer;
|
|
| 11 |
import java.util.Vector;
|
|
| 12 |
|
|
| 13 |
import org.apache.commons.lang.ObjectUtils;
|
|
| 14 |
|
|
| 15 |
/**
|
|
| 16 |
* 文字列に関する操作を行うクラスです。
|
|
| 17 |
*
|
|
| 18 |
* @author Akima
|
|
| 19 |
*/
|
|
| 20 |
public final class StringUtils extends org.apache.commons.lang.StringUtils { |
|
| 21 |
|
|
| 22 |
/**
|
|
| 23 |
* StringBuffer中の文字列を置換します。
|
|
| 24 |
*
|
|
| 25 |
* @param buffer
|
|
| 26 |
* 置換される文字列
|
|
| 27 |
* @param pattern
|
|
| 28 |
* 検索文字列
|
|
| 29 |
* @param newPattern
|
|
| 30 |
* 置換文字列
|
|
| 31 |
*/
|
|
| 32 | 10 |
public static void replace(StringBuffer buffer, String pattern, String newPattern) { |
| 33 | 10 |
if (StringUtils.equals(pattern, newPattern))
|
| 34 | 0 |
return;
|
| 35 | 10 |
int patternLength = pattern.length();
|
| 36 | 10 |
int idx = buffer.toString().indexOf(pattern);
|
| 37 | 10 |
while (idx > -1) {
|
| 38 | 9 |
buffer.replace(idx, idx + patternLength, newPattern); |
| 39 | 9 |
idx = buffer.toString().indexOf(pattern, idx + newPattern.length()); |
| 40 |
} |
|
| 41 |
} |
|
| 42 |
|
|
| 43 |
/**
|
|
| 44 |
* StringBuffer中の文字列を置換します。
|
|
| 45 |
*
|
|
| 46 |
* @param buffer
|
|
| 47 |
* 置換される文字列
|
|
| 48 |
* @param pattern
|
|
| 49 |
* 検索文字
|
|
| 50 |
* @param newPattern
|
|
| 51 |
* 置換文字
|
|
| 52 |
*/
|
|
| 53 | 0 |
public static void replace(StringBuffer buffer, char oldChar, char newChar) { |
| 54 | 0 |
int idx = buffer.toString().indexOf(oldChar);
|
| 55 | 0 |
String newString = newChar + "";
|
| 56 | 0 |
while (idx > -1) {
|
| 57 | 0 |
buffer.replace(idx, idx + 1, newString); |
| 58 | 0 |
idx = buffer.toString().indexOf(oldChar, idx + 1); |
| 59 |
} |
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
/** 数字とみなされるものを並べた文字列 */
|
|
| 63 |
private static final String NUMERIC_CHARS = ".-0123456789.-0123456789"; |
|
| 64 |
|
|
| 65 |
/**
|
|
| 66 |
* 文字列の中の数字とみなされる文字(半角・全角の0~9、.-)以外を削除して返します。
|
|
| 67 |
*
|
|
| 68 |
* @param value
|
|
| 69 |
* 文字列
|
|
| 70 |
* @return 除去された後の文字列
|
|
| 71 |
*/
|
|
| 72 | 24 |
public static String deleteNotNumeric(Object value) { |
| 73 | 24 |
if (value == null) |
| 74 | 1 |
return null; |
| 75 | 23 |
final StringBuffer result = new StringBuffer();
|
| 76 | 23 |
final String str = String.valueOf(value); |
| 77 | 23 |
for (int i = 0; i < str.length(); i++) { |
| 78 | 59 |
char c = str.charAt(i);
|
| 79 | 59 |
if (NUMERIC_CHARS.indexOf(c) > -1) {
|
| 80 | 30 |
result.append(c); |
| 81 |
} |
|
| 82 |
} |
|
| 83 | 23 |
return result.toString();
|
| 84 |
} |
|
| 85 |
|
|
| 86 |
/** 数字を並べた文字列 */
|
|
| 87 |
private static final String NUMBER_CHARS = "01234567890123456789"; |
|
| 88 |
|
|
| 89 |
/**
|
|
| 90 |
* 数字かどうかを判断する。
|
|
| 91 |
*
|
|
| 92 |
* @param c
|
|
| 93 |
*/
|
|
| 94 | 12 |
public static boolean isNumeric(char c) { |
| 95 | 12 |
return NUMBER_CHARS.indexOf(c) > -1;
|
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* 数字を含むかどうかを判断する。
|
|
| 100 |
*
|
|
| 101 |
* @param string
|
|
| 102 |
*/
|
|
| 103 | 7 |
public static boolean hasNumeric(String string) { |
| 104 | 7 |
if (isEmpty(string))
|
| 105 | 2 |
return false; |
| 106 | 5 |
for (int i = 0; i < string.length(); i++) { |
| 107 | 13 |
if (NUMBER_CHARS.indexOf(string.charAt(i)) > -1)
|
| 108 | 4 |
return true; |
| 109 |
} |
|
| 110 | 1 |
return false; |
| 111 |
} |
|
| 112 |
|
|
| 113 |
/**
|
|
| 114 |
* 数字のみで構成されているかどうかを判断する。
|
|
| 115 |
*
|
|
| 116 |
* @param string
|
|
| 117 |
*/
|
|
| 118 | 7 |
public static boolean isNumericOnly(String string) { |
| 119 | 7 |
if (isEmpty(string))
|
| 120 | 2 |
return false; |
| 121 | 5 |
for (int i = 0; i < string.length(); i++) { |
| 122 | 9 |
if (NUMBER_CHARS.indexOf(string.charAt(i)) < 0)
|
| 123 | 3 |
return false; |
| 124 |
} |
|
| 125 | 2 |
return true; |
| 126 |
} |
|
| 127 |
|
|
| 128 |
/**
|
|
| 129 |
* 文字列の中の数字(半角、全角の0~9)以外を削除して返します。
|
|
| 130 |
*
|
|
| 131 |
* @param value
|
|
| 132 |
* 文字列
|
|
| 133 |
* @return 除去された後の文字列
|
|
| 134 |
*/
|
|
| 135 | 4 |
public static String deleteNotNumber(String value) { |
| 136 | 4 |
if (value == null) |
| 137 | 1 |
return null; |
| 138 | 3 |
StringBuffer result = new StringBuffer();
|
| 139 | 3 |
for (int i = 0; i < value.length(); i++) { |
| 140 | 29 |
char c = value.charAt(i);
|
| 141 | 29 |
if (NUMBER_CHARS.indexOf(c) > -1) {
|
| 142 | 10 |
result.append(c); |
| 143 |
} |
|
| 144 |
} |
|
| 145 | 3 |
return result.toString();
|
| 146 |
} |
|
| 147 |
|
|
| 148 |
public static final String REPLACE_WORD_HEADER = "${"; |
|
| 149 |
|
|
| 150 |
public static final String REPLACE_WORD_FOOTER = "}"; |
|
| 151 |
|
|
| 152 |
/**
|
|
| 153 |
* 環境変数を置換するメソッド。 環境変数は '${' と '}' で挟まれた文字列として表される。
|
|
| 154 |
*
|
|
| 155 |
* @param source
|
|
| 156 |
* 文字列
|
|
| 157 |
* @param properties
|
|
| 158 |
* 置換される文字列となりえる文字列群
|
|
| 159 |
* @return String 環境変数に置き換えたあとの文字列
|
|
| 160 |
*/
|
|
| 161 | 6 |
public static String replaceWithProperties(String source, Properties properties) { |
| 162 | 6 |
return replaceWithProperties(source, properties, REPLACE_WORD_HEADER, REPLACE_WORD_FOOTER);
|
| 163 |
} |
|
| 164 |
|
|
| 165 |
/**
|
|
| 166 |
* 環境変数を置換するメソッド。
|
|
| 167 |
*
|
|
| 168 |
* @param source
|
|
| 169 |
* 文字列
|
|
| 170 |
* @param delimBegin
|
|
| 171 |
* 開始の区切り文字
|
|
| 172 |
* @param delimEnd
|
|
| 173 |
* 終了の区切り文字
|
|
| 174 |
* @return 環境変数に置き換えたあとの文字列
|
|
| 175 |
*/
|
|
| 176 | 15 |
public static String replaceWithProperties(String source, Properties properties, String delimBegin, String delimEnd) { |
| 177 | 15 |
if (source == null) |
| 178 | 4 |
return null; |
| 179 | 11 |
if (isEmpty(delimBegin) || isEmpty(delimEnd))
|
| 180 | 4 |
throw new UnsupportedOperationException("delimeter must be not null: '" + delimBegin + "' and '" + delimEnd |
| 181 |
+ "'");
|
|
| 182 | 7 |
String key = extractString(source, delimBegin, delimEnd); |
| 183 | 7 |
while (key != null) { |
| 184 | 8 |
source = replace(source, delimBegin + key + delimEnd, properties.getProperty(key, ""));
|
| 185 | 8 |
key = extractString(source, delimBegin, delimEnd); |
| 186 |
} |
|
| 187 | 7 |
return source;
|
| 188 |
} |
|
| 189 |
|
|
| 190 |
/**
|
|
| 191 |
* 引数で指定された区切り文字列で挟まれた文字列を抽出する
|
|
| 192 |
*
|
|
| 193 |
* @param source
|
|
| 194 |
* 文字列
|
|
| 195 |
* @param delimBegin
|
|
| 196 |
* 開始の区切り文字
|
|
| 197 |
* @param delimEnd
|
|
| 198 |
* 終了の区切り文字
|
|
| 199 |
* @return 抽出された文字列
|
|
| 200 |
*/
|
|
| 201 | 15 |
public static String extractString(String source, String delimBegin, String delimEnd) { |
| 202 | 15 |
return extractString(source, delimBegin, delimEnd, 0);
|
| 203 |
} |
|
| 204 |
|
|
| 205 |
/**
|
|
| 206 |
* 引数で指定された区切り文字列で挟まれた文字列を抽出する
|
|
| 207 |
*
|
|
| 208 |
* @param source
|
|
| 209 |
* 文字列
|
|
| 210 |
* @param delimBegin
|
|
| 211 |
* 開始の区切り文字
|
|
| 212 |
* @param delimEnd
|
|
| 213 |
* 終了の区切り文字
|
|
| 214 |
* @param fromIndex
|
|
| 215 |
* 検索を開始する文字の位置
|
|
| 216 |
* @return 抽出された文字列
|
|
| 217 |
*/
|
|
| 218 | 15 |
public static String extractString(String source, String delimBegin, String delimEnd, int fromIndex) { |
| 219 | 15 |
int idxBegin = source.indexOf(delimBegin, fromIndex);
|
| 220 | 15 |
if (idxBegin < 0)
|
| 221 | 7 |
return null; |
| 222 | 8 |
int idxEnd = source.indexOf(delimEnd, idxBegin + 1);
|
| 223 | 8 |
if (idxEnd < 0)
|
| 224 | 0 |
return null; |
| 225 | 8 |
return source.substring(idxBegin + delimBegin.length(), idxEnd);
|
| 226 |
} |
|
| 227 |
|
|
| 228 |
/**
|
|
| 229 |
* 引数で指定された区切り文字列で挟まれた文字列を抽出し、 引数のListに追加する。
|
|
| 230 |
*
|
|
| 231 |
* @param source
|
|
| 232 |
* 文字列
|
|
| 233 |
* @param dest
|
|
| 234 |
* リスト
|
|
| 235 |
* @param delim
|
|
| 236 |
* 区切り文字
|
|
| 237 |
*/
|
|
| 238 | 0 |
public static void extractStrings(String source, List dest, char delim) { |
| 239 | 0 |
extractStrings(source, dest, delim, delim, 0); |
| 240 |
} |
|
| 241 |
|
|
| 242 |
/**
|
|
| 243 |
* 引数で指定された区切り文字列で挟まれた文字列を抽出し、 引数のListに追加する。
|
|
| 244 |
*
|
|
| 245 |
* @param source
|
|
| 246 |
* 文字列
|
|
| 247 |
* @param dest
|
|
| 248 |
* リスト
|
|
| 249 |
* @param delimBegin
|
|
| 250 |
* 開始の区切り文字
|
|
| 251 |
* @param delimEnd
|
|
| 252 |
* 終了の区切り文字
|
|
| 253 |
*/
|
|
| 254 | 0 |
public static void extractStrings(String source, List dest, char delimBegin, char delimEnd) { |
| 255 | 0 |
extractStrings(source, dest, delimBegin, delimEnd, 0); |
| 256 |
} |
|
| 257 |
|
|
| 258 |
/**
|
|
| 259 |
* 引数で指定された区切り文字列で挟まれた文字列を抽出し、 引数のListに追加する。
|
|
| 260 |
*
|
|
| 261 |
* @param source
|
|
| 262 |
* 文字列
|
|
| 263 |
* @param dest
|
|
| 264 |
* リスト
|
|
| 265 |
* @param delimBegin
|
|
| 266 |
* 開始の区切り文字
|
|
| 267 |
* @param delimEnd
|
|
| 268 |
* 終了の区切り文字
|
|
| 269 |
* @param fromIndex
|
|
| 270 |
* 検索を開始する文字列の位置
|
|
| 271 |
*/
|
|
| 272 | 0 |
public static void extractStrings(String source, List dest, char delimBegin, char delimEnd, int fromIndex) { |
| 273 | 0 |
int idxBegin = source.indexOf(delimBegin, fromIndex);
|
| 274 | 0 |
while (idxBegin > -1) {
|
| 275 | 0 |
int idxEnd = source.indexOf(delimEnd, idxBegin + 1);
|
| 276 | 0 |
if (idxEnd < 0)
|
| 277 | 0 |
return;
|
| 278 | 0 |
dest.add(source.substring(idxBegin + 1, idxEnd)); |
| 279 | 0 |
idxBegin = source.indexOf(delimBegin, idxEnd + 1); |
| 280 |
} |
|
| 281 |
} |
|
| 282 |
|
|
| 283 |
/**
|
|
| 284 |
* ヌルではなくてヌル文字列 いちいち定数を宣言するのが面倒なので宣言しておいた。
|
|
| 285 |
*/
|
|
| 286 |
public static final String NULL_STRING = ""; |
|
| 287 |
|
|
| 288 |
/**
|
|
| 289 |
* パラメータの値がnullならば空文字列に、それ以外はそのまま返します。
|
|
| 290 |
*
|
|
| 291 |
* @param value
|
|
| 292 |
* 文字列
|
|
| 293 |
* @return String 置き換えた後の文字列
|
|
| 294 |
*/
|
|
| 295 | 36 |
public static String nullTrim(Object value) { |
| 296 | 36 |
return nullTrim(value, false); |
| 297 |
} |
|
| 298 |
|
|
| 299 |
/**
|
|
| 300 |
* パラメータの値がnullならば空文字列に、それ以外は <BR>
|
|
| 301 |
* trimがtrueの場合はtrimして、trimがfalseの場合はそのまま返します。
|
|
| 302 |
*
|
|
| 303 |
* @param value
|
|
| 304 |
* 文字列
|
|
| 305 |
* @return String 置き換えた後の文字列
|
|
| 306 |
*/
|
|
| 307 | 36 |
public static String nullTrim(Object value, boolean trim) { |
| 308 | 36 |
if (value == null) |
| 309 | 0 |
return NULL_STRING;
|
| 310 | 36 |
return trim ? value.toString().trim() : value.toString();
|
| 311 |
} |
|
| 312 |
|
|
| 313 |
/**
|
|
| 314 |
* 配列の文字列をnullTrimしながらつなげます。 配列自体がnullのときはヌル文字列を返します。
|
|
| 315 |
*
|
|
| 316 |
* @param values
|
|
| 317 |
* 文字列が格納された文字列
|
|
| 318 |
* @param つなげたあとの文字列
|
|
| 319 |
*/
|
|
| 320 | 0 |
public static String nullTrim(Object[] values) { |
| 321 | 0 |
return nullTrim(values, false); |
| 322 |
} |
|
| 323 |
|
|
| 324 |
/**
|
|
| 325 |
* 配列の文字列をnullTrimしながらつなげます。
|
|
| 326 |
* <P>
|
|
| 327 |
* 配列自体がnullのときはヌル文字列を返します。
|
|
| 328 |
*
|
|
| 329 |
* @param values
|
|
| 330 |
* 文字列が格納された文字列
|
|
| 331 |
* @param つなげたあとの文字列
|
|
| 332 |
*/
|
|
| 333 | 0 |
public static String nullTrim(Object[] values, boolean trim) { |
| 334 | 0 |
if (values == null) |
| 335 | 0 |
return NULL_STRING;
|
| 336 | 0 |
StringBuffer result = new StringBuffer();
|
| 337 | 0 |
for (int i = 0; i < values.length; i++) { |
| 338 | 0 |
result.append(nullTrim(values[i], trim)); |
| 339 |
} |
|
| 340 | 0 |
return result.toString();
|
| 341 |
} |
|
| 342 |
|
|
| 343 |
/**
|
|
| 344 |
* String型が等しいかどうかを比較する。 どちらかがnullでも比較可能。両方ともnullの場合はtrue
|
|
| 345 |
*
|
|
| 346 |
* @param target1
|
|
| 347 |
* 文字列
|
|
| 348 |
* @param target2
|
|
| 349 |
* 文字列
|
|
| 350 |
* @return boolean 等しいかどうか。
|
|
| 351 |
*/
|
|
| 352 | 0 |
public static boolean compare(String target1, String target2) { |
| 353 | 0 |
return ObjectUtils.equals(target1, target2);
|
| 354 |
} |
|
| 355 |
|
|
| 356 |
/**
|
|
| 357 |
* 文字列を指定された区切り文字列によって分割して返します。
|
|
| 358 |
*
|
|
| 359 |
* @param value
|
|
| 360 |
* 文字列
|
|
| 361 |
* @param delimiters
|
|
| 362 |
* 区切り文字
|
|
| 363 |
* @return 区切られた文字列が格納されたList
|
|
| 364 |
*/
|
|
| 365 | 0 |
public static List parseToList(String value, String delimiters) { |
| 366 | 0 |
List result = new Vector();
|
| 367 | 0 |
StringTokenizer tokenizer = new StringTokenizer(value, delimiters, false); |
| 368 | 0 |
while (tokenizer.hasMoreTokens()) {
|
| 369 | 0 |
result.add(tokenizer.nextToken()); |
| 370 |
} |
|
| 371 | 0 |
return result;
|
| 372 |
} |
|
| 373 |
|
|
| 374 |
/**
|
|
| 375 |
* DOCUMENT ME!
|
|
| 376 |
*
|
|
| 377 |
* @param dest
|
|
| 378 |
* DOCUMENT ME!
|
|
| 379 |
* @param str
|
|
| 380 |
* DOCUMENT ME!
|
|
| 381 |
* @param delim
|
|
| 382 |
* DOCUMENT ME!
|
|
| 383 |
*/
|
|
| 384 | 114 |
public static void tokenize(List dest, String str, String delim) { |
| 385 | 114 |
if (dest == null) |
| 386 | 0 |
return;
|
| 387 | 114 |
if (StringUtils.isEmpty(str))
|
| 388 | 6 |
return;
|
| 389 | 108 |
final StringBuffer buf = new StringBuffer(str);
|
| 390 |
//
|
|
| 391 | 108 |
int idx = buf.indexOf(delim);
|
| 392 | 108 |
if (idx < 0) {
|
| 393 | 19 |
dest.add(str); |
| 394 | 19 |
return;
|
| 395 |
} |
|
| 396 | 89 |
while (idx > -1) {
|
| 397 | 209 |
dest.add(buf.substring(0, idx)); |
| 398 | 209 |
buf.delete(0, idx + delim.length()); |
| 399 | 209 |
idx = buf.toString().indexOf(delim); |
| 400 |
} |
|
| 401 | 89 |
dest.add(buf.toString()); |
| 402 |
} |
|
| 403 |
|
|
| 404 |
/**
|
|
| 405 |
* Method tokenize.
|
|
| 406 |
*
|
|
| 407 |
* @param str
|
|
| 408 |
* @param delim
|
|
| 409 |
* @return Object[]
|
|
| 410 |
*/
|
|
| 411 | 68 |
public static String[] tokenizeToArray(String str, String delim) { |
| 412 | 68 |
if (str == null) |
| 413 | 0 |
return null; |
| 414 | 68 |
ArrayList resultList = new ArrayList();
|
| 415 | 68 |
tokenize(resultList, str, delim); |
| 416 | 68 |
if (resultList.size() < 1)
|
| 417 | 4 |
return new String[0]; |
| 418 | 64 |
String[] result = new String[resultList.size()];
|
| 419 | 64 |
for (int i = 0; i < resultList.size(); i++) |
| 420 | 166 |
result[i] = (String) resultList.get(i); |
| 421 | 64 |
return result;
|
| 422 |
} |
|
| 423 |
|
|
| 424 |
/**
|
|
| 425 |
* DOCUMENT ME!
|
|
| 426 |
*
|
|
| 427 |
* @param dest
|
|
| 428 |
* DOCUMENT ME!
|
|
| 429 |
* @param str
|
|
| 430 |
* DOCUMENT ME!
|
|
| 431 |
* @param delim
|
|
| 432 |
* DOCUMENT ME!
|
|
| 433 |
*/
|
|
| 434 | 25 |
public static List tokenize(String str, String delim) { |
| 435 | 25 |
final List result = new ArrayList();
|
| 436 | 25 |
tokenize(result, str, delim); |
| 437 | 25 |
return result;
|
| 438 |
} |
|
| 439 |
|
|
| 440 |
/**
|
|
| 441 |
* 引数sourceの文字配列の中に引数valueで表される文字が存在する場合、 インデックスを返します。存在しない場合には -1 を返します。
|
|
| 442 |
*/
|
|
| 443 | 0 |
public static int indexOf(char[] source, char value) { |
| 444 | 0 |
if (source == null) |
| 445 | 0 |
return -1;
|
| 446 | 0 |
for (int i = 0; i < source.length; i++) { |
| 447 | 0 |
if (source[i] == value)
|
| 448 | 0 |
return i;
|
| 449 |
} |
|
| 450 | 0 |
return -1;
|
| 451 |
} |
|
| 452 |
|
|
| 453 |
/**
|
|
| 454 |
* 引数sが引数charsの中のどれか一つでも含んでいればtrueを返します。 どれも含んでいないときにはfalseを返します。
|
|
| 455 |
*/
|
|
| 456 | 18 |
public static boolean containAnyChar(String s, char[] chars) { |
| 457 | 18 |
if (s == null) |
| 458 | 0 |
return false; |
| 459 | 18 |
if (chars == null) |
| 460 | 0 |
return false; |
| 461 | 18 |
for (int i = 0; i < chars.length; i++) { |
| 462 | 18 |
int idx = s.indexOf(chars[i]);
|
| 463 | 18 |
if (idx > -1)
|
| 464 | 0 |
return true; |
| 465 |
} |
|
| 466 | 18 |
return false; |
| 467 |
} |
|
| 468 |
|
|
| 469 |
static protected final char QUOTE_SINGLE = '\''; |
|
| 470 |
|
|
| 471 |
static protected final char QUOTE_DOUBLE = '"'; |
|
| 472 |
|
|
| 473 |
public static final char QUOTE_SINGLE_ZENKAKU_BEGIN = '‘'; |
|
| 474 |
|
|
| 475 |
public static final char QUOTE_SINGLE_ZENKAKU_END = '’'; |
|
| 476 |
|
|
| 477 |
public static final char QUOTE_DOUBLE_ZENKAKU_BEGIN = '“'; |
|
| 478 |
|
|
| 479 |
public static final char QUOTE_DOUBLE_ZENKAKU_END = '”'; |
|
| 480 |
|
|
| 481 |
public static final char[] QUOTE_SINGLES = new char[] { QUOTE_SINGLE, QUOTE_SINGLE_ZENKAKU_BEGIN, |
|
| 482 |
QUOTE_SINGLE_ZENKAKU_END }; |
|
| 483 |
|
|
| 484 |
public static final char[] QUOTE_DOUBLES = new char[] { QUOTE_DOUBLE, QUOTE_DOUBLE_ZENKAKU_BEGIN, |
|
| 485 |
QUOTE_DOUBLE_ZENKAKU_END }; |
|
| 486 |
|
|
| 487 |
/**
|
|
| 488 |
* 文字列中に引用符が存在した場合に引用符を二重化します。 <br>
|
|
| 489 |
* example: <br>
|
|
| 490 |
* quotes == {'\'', '‘', '’'} (半角、全角(始)、全角(終)のシングルクオーテーション) の場合 <br>
|
|
| 491 |
* [nullTrim == false] (null) -> (null) //nullを変換対象外とします。 <br>
|
|
| 492 |
* [nullTrim == true] (null) -> "" //nullはヌル文字列と同様に変換します。 <br>"" -> ""
|
|
| 493 |
* //ヌル文字列は変換対象です。 <br>
|
|
| 494 |
* "aaaa" -> "aaaa" //前後にquoteが付加されます。 <br>
|
|
| 495 |
* "aa'aa" -> "aa''aa" //前後にquoteが付加され、文字列中のquoteが2つになります。 <br>
|
|
| 496 |
* "aa\"aa" -> "aa\"aa" //ダブルクオートはただの文字として扱われます。 <br>
|
|
| 497 |
* "aa'\"aa" -> "aa''\"aa" //ダブルクオートはただの文字として扱われ、 <br>
|
|
| 498 |
* "aa''a'a" -> "aa''''a''a" //quoteがいくつ存在しても全て変換されます。 <br>
|
|
| 499 |
* "aa'’a‘a" -> "aa''’’a‘‘a" //複数のquoteが指定されていても、それぞれに二重化します。 <br>
|
|
| 500 |
*/
|
|
| 501 | 18 |
public static String duplicateQuote(String value, char[] quotes, boolean nullTrim) { |
| 502 | 18 |
if (nullTrim)
|
| 503 | 18 |
value = nullTrim(value); |
| 504 | 18 |
if (value == null) |
| 505 | 0 |
return null; |
| 506 | 18 |
if (!containAnyChar(value, quotes))
|
| 507 | 18 |
return value;
|
| 508 | 0 |
StringBuffer result = new StringBuffer();
|
| 509 | 0 |
for (int i = 0; i < value.length(); i++) { |
| 510 | 0 |
char c = value.charAt(i);
|
| 511 | 0 |
result.append(c); |
| 512 | 0 |
if (indexOf(quotes, c) > -1)
|
| 513 | 0 |
result.append(c); |
| 514 |
} |
|
| 515 | 0 |
return result.toString();
|
| 516 |
} |
|
| 517 |
|
|
| 518 |
/**
|
|
| 519 |
* valueの中の引用符を二重化して返します。おもにSQL文のために使用されます。
|
|
| 520 |
* <P>
|
|
| 521 |
* quotesには以下のいずれかを指定して下さい。 <BR>
|
|
| 522 |
* QUOTE_SINGLES:半角・全角のシングルクォーテーション <BR>
|
|
| 523 |
* QUOTE_DOUBLES:半角・全角のダブルクォーテーション <br>
|
|
| 524 |
*
|
|
| 525 |
* @param value
|
|
| 526 |
* @param quotes
|
|
| 527 |
* @return String
|
|
| 528 |
*/
|
|
| 529 | 0 |
public static String duplicateQuote(String value, char[] quotes) { |
| 530 | 0 |
return duplicateQuote(value, quotes, true); |
| 531 |
} |
|
| 532 |
|
|
| 533 |
/**
|
|
| 534 |
* 文字列中に引用符が存在した場合に引用符を二重化します。
|
|
| 535 |
*
|
|
| 536 |
* example: quote == '\''(半角のシングルクオーテーション) の場合 [nullTrim == false] (null) ->
|
|
| 537 |
* (null) //nullを変換対象外とします。 [nullTrim == true] (null) -> ""
|
|
| 538 |
* //nullはヌル文字列と同様に変換します。 "" -> "" //ヌル文字列は変換対象です。 "aaaa" -> "aaaa"
|
|
| 539 |
* //前後にquoteが付加されます。 "aa'aa" -> "aa''aa"
|
|
| 540 |
* //前後にquoteが付加され、文字列中のquoteが2つになります。 "aa\"aa" -> "aa\"aa"
|
|
| 541 |
* //ダブルクオートはただの文字として扱われます。 "aa'\"aa" -> "aa''\"aa" //ダブルクオートはただの文字として扱われ、
|
|
| 542 |
* "aa''a'a" -> "aa''''a''a" //quoteがいくつ存在しても全て変換されます。
|
|
| 543 |
*
|
|
| 544 |
*/
|
|
| 545 | 18 |
public static String duplicateQuote(String value, char quote, boolean nullTrim) { |
| 546 | 18 |
return duplicateQuote(value, new char[] { quote }, nullTrim); |
| 547 |
} |
|
| 548 |
|
|
| 549 | 0 |
public static String duplicateQuote(String value, char quote) { |
| 550 | 0 |
return duplicateQuote(value, quote, true); |
| 551 |
} |
|
| 552 |
|
|
| 553 |
/**
|
|
| 554 |
* 文字列中に引用符が存在した場合に引用符を二重化します。 <br>
|
|
| 555 |
* example: <br>
|
|
| 556 |
* quote == '\'' の場合 <br>
|
|
| 557 |
* [nullTrim == false] (null) -> (null) //nullを変換対象外とします。 <br>
|
|
| 558 |
* [nullTrim == true] (null) -> "" //nullはヌル文字列と同様に変換します。 <br>"" -> ""
|
|
| 559 |
* //ヌル文字列は変換対象です。 <br>
|
|
| 560 |
* "aaaa" -> "'aaaa'" //前後にquoteが付加されます。 <br>
|
|
| 561 |
* "aa'aa" -> "'aa''aa'" //前後にquoteが付加され、文字列中のquoteが2つになります。 <br>
|
|
| 562 |
* "aa\"aa" -> "'aa\"aa'" //ダブルクオートはただの文字として扱われます。 <br>
|
|
| 563 |
* "aa'\"aa" -> "'aa''\"aa'" //ダブルクオートはただの文字として扱われ、 <br>
|
|
| 564 |
* "aa''a'a" -> "'aa''''a''a'" //quoteがいくつ存在しても全て変換されます。 <br>
|
|
| 565 |
*/
|
|
| 566 | 18 |
public static String toQuoted(String value, char quote, boolean nullTrim) { |
| 567 | 18 |
String result = duplicateQuote(value, quote, nullTrim); |
| 568 | 18 |
if (!nullTrim && (result == null)) |
| 569 | 0 |
return null; |
| 570 |
else
|
|
| 571 | 18 |
return addQuote(result, quote);
|
| 572 |
} |
|
| 573 |
|
|
| 574 |
/**
|
|
| 575 |
* 文字列に引数quoteで指定された引用符を前後に付加します。 <br>
|
|
| 576 |
* ただし、文字列中に引用符が存在した場合には引用符を二重化します。 <br>
|
|
| 577 |
*
|
|
| 578 |
* 詳しくは以下のメソッドを参照してください。 <br>
|
|
| 579 |
*
|
|
| 580 |
* @see toQuoted(String value, char quote, boolean nullTrim)
|
|
| 581 |
*/
|
|
| 582 | 18 |
public static String toQuoted(String value, char quote) { |
| 583 | 18 |
return toQuoted(value, quote, true); |
| 584 |
} |
|
| 585 |
|
|
| 586 | 18 |
private static String addQuote(String value, char quote) { |
| 587 | 18 |
return quote + nullTrim(value) + quote;
|
| 588 |
} |
|
| 589 |
|
|
| 590 |
/**
|
|
| 591 |
* 文字列の前方に指定された文字を指定されたバイト数まで詰めます。 <br>
|
|
| 592 |
* (指定されたバイト-文字列のバイト)が詰める文字列のバイトで割り切れることが前提です。 <br>
|
|
| 593 |
* 文字列のバイト数が指定されたバイト数以上の場合はそのまま返します。 <br>
|
|
| 594 |
*
|
|
| 595 |
* @param value
|
|
| 596 |
* もとの文字列
|
|
| 597 |
* @param pad
|
|
| 598 |
* 詰める文字
|
|
| 599 |
* @param length
|
|
| 600 |
* 指定されたバイト数
|
|
| 601 |
* @return 詰めた状態の文字列
|
|
| 602 |
*/
|
|
| 603 | 0 |
public static String padHead(String value, String pad, int length) { |
| 604 |
|
|
| 605 | 0 |
if (value.getBytes().length >= length) {
|
| 606 | 0 |
return value;
|
| 607 |
} |
|
| 608 |
|
|
| 609 | 0 |
int addNum = (length - value.getBytes().length) / pad.getBytes().length;
|
| 610 |
|
|
| 611 | 0 |
StringBuffer buffer = new StringBuffer(value);
|
| 612 |
|
|
| 613 | 0 |
for (int index = 0; index < addNum; index++) { |
| 614 | 0 |
buffer.insert(0, pad); |
| 615 |
} |
|
| 616 | 0 |
return buffer.toString();
|
| 617 |
} |
|
| 618 |
|
|
| 619 |
/**
|
|
| 620 |
* 文字列の後方に指定された文字を指定されたバイト数まで詰めます。 <br>
|
|
| 621 |
* (指定されたバイト-文字列のバイト)が詰める文字列のバイトで割り切れることが前提です。 <br>
|
|
| 622 |
* 文字列のバイト数が指定されたバイト数以上の場合はそのまま返します。 <br>
|
|
| 623 |
*
|
|
| 624 |
* @param value
|
|
| 625 |
* もとの文字列
|
|
| 626 |
* @param pad
|
|
| 627 |
* 詰める文字
|
|
| 628 |
* @param length
|
|
| 629 |
* 指定されたバイト数
|
|
| 630 |
* @return 詰めた状態の文字列
|
|
| 631 |
*/
|
|
| 632 | 0 |
public static String padTail(String value, String pad, int length) { |
| 633 |
|
|
| 634 | 0 |
if (value.getBytes().length >= length) {
|
| 635 | 0 |
return value;
|
| 636 |
} |
|
| 637 |
|
|
| 638 | 0 |
int addNum = (length - value.getBytes().length) / pad.getBytes().length;
|
| 639 |
|
|
| 640 | 0 |
StringBuffer buffer = new StringBuffer(value);
|
| 641 |
|
|
| 642 | 0 |
for (int index = 0; index < addNum; index++) { |
| 643 | 0 |
buffer.append(pad); |
| 644 |
} |
|
| 645 | 0 |
return buffer.toString();
|
| 646 |
} |
|
| 647 |
|
|
| 648 | 0 |
public static String getPackageAsPath(String packageName) { |
| 649 | 0 |
return getPackageAsPath(packageName, "/"); |
| 650 |
} |
|
| 651 |
|
|
| 652 | 0 |
public static String getPackageAsPath(String packageName, String separater) { |
| 653 | 0 |
return replace(packageName, ClassUtils.PACKAGE_DELIM, separater);
|
| 654 |
} |
|
| 655 |
|
|
| 656 | 0 |
public static String valueOf(Object obj) { |
| 657 | 0 |
if (obj == null) |
| 658 | 0 |
return EMPTY;
|
| 659 | 0 |
return obj.toString();
|
| 660 |
} |
|
| 661 |
|
|
| 662 | 0 |
public static String valueOf(byte value) { |
| 663 | 0 |
return String.valueOf(value);
|
| 664 |
} |
|
| 665 |
|
|
| 666 | 0 |
public static String valueOf(short value) { |
| 667 | 0 |
return String.valueOf(value);
|
| 668 |
} |
|
| 669 |
|
|
| 670 | 0 |
public static String valueOf(int value) { |
| 671 | 0 |
return String.valueOf(value);
|
| 672 |
} |
|
| 673 |
|
|
| 674 | 0 |
public static String valueOf(long value) { |
| 675 | 0 |
return String.valueOf(value);
|
| 676 |
} |
|
| 677 |
|
|
| 678 | 0 |
public static String valueOf(double value) { |
| 679 | 0 |
return String.valueOf(value);
|
| 680 |
} |
|
| 681 |
|
|
| 682 | 0 |
public static String valueOf(float value) { |
| 683 | 0 |
return String.valueOf(value);
|
| 684 |
} |
|
| 685 |
|
|
| 686 | 0 |
public static String valueOf(char value) { |
| 687 | 0 |
return String.valueOf(value);
|
| 688 |
} |
|
| 689 |
|
|
| 690 | 0 |
public static String valueOf(boolean value) { |
| 691 | 0 |
return String.valueOf(value);
|
| 692 |
} |
|
| 693 |
|
|
| 694 |
} |
|
||||||||||