Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * Licensed 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. */ package jp.terasoluna.fw.util; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import java.util.Map; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; /** * ?? * * <p> * ???HTML?SQLLIKE? * ???????? * </p> * */ public class StringUtil { /** * ?OS???? */ public static final String LINE_SEP = System.getProperty("line.separator"); /** * */ private static final String ZENKAKU_LIST = "?????" + "?" + "" + "????" + "???" + "?" + ""; /** * (????)? */ private static final String ZENKAKU_KASATAHA_LIST = "??"; /** * (????)? */ private static final String ZENKAKU_GAZADABA_LIST = "?"; /** * ("[¥30f7]) */ private static final Character ZENKAKU_WA_DAKUTEN = Character.valueOf('\u30f7'); /** * ("[¥30fa]) */ private static final Character ZENKAKU_WO_DAKUTEN = Character.valueOf('\u30fa'); /** * () */ private static final String ZENKAKU_PA_LIST = "?"; /** * ? */ private static final String HANKAKU_LIST = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGH" + "IJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnop" + "qrstuvwxyz{|}~" + "??? "; /** * ?()? */ private static final String HANKAKU_KASATAHA_LIST = "??"; /** * ?() */ private static final String HANKAKU_HA_LIST = "?"; /** * ????????? * ?? StringUtil ???? * * @param c * @return ?????? true */ public static boolean isWhitespace(char c) { return c == ' '; } /** * ???????????? * ?? StringUtil ???? * * @param c * @return ?????????? true */ public static boolean isZenHankakuSpace(char c) { return (c == '' || c == ' '); } /** * ?????? * null ????? null ? * * <p> * ?? Oracle ??? CHAR ?? * ResultSet.getString() ???????? * ????? VARCHAR2 ??? * ????????????????? * ????????????????? * ???????? VARCHAR2 ???? * ????????????? * ?????????????? * </p> * ???? * * @param str ??? * @return ?? */ public static String rtrim(String str) { if (str == null) { return null; } int length = str.length(); while ((0 < length) && isWhitespace(str.charAt(length - 1))) { length--; } return length < str.length() ? str.substring(0, length) : str; } /** * ???? * * ? null ????? null ?<br> * ???? * * @param str ??? * @return ?? */ public static String ltrim(String str) { if (str == null) { return null; } int start = 0; int length = str.length(); while ((start < length) && isWhitespace(str.charAt(start))) { start++; } return start > 0 ? str.substring(start, length) : str; } /** * ???? * * ? null ????? null ?<br> * ???? * * @param str ??? * @return ?? */ public static String trim(String str) { return StringUtils.trim(str); } /** * ????????? * null ????? null ? * * <p> * ?? Oracle ??? CHAR ?? * ResultSet.getString() ???????? * ????? VARCHAR2 ??? * ????????????????? * ????????????????? * ???????? VARCHAR2 ???? * ????????????? * ?????????????? * </p> * * @param str ??? * @return ?? */ public static String rtrimZ(String str) { if (str == null) { return null; } int length = str.length(); while ((0 < length) && isZenHankakuSpace(str.charAt(length - 1))) { length--; } return length < str.length() ? str.substring(0, length) : str; } /** * ??????? * * ? null ????? null ?<br> * * @param str ??? * @return ?? */ public static String ltrimZ(String str) { if (str == null) { return null; } int start = 0; int length = str.length(); while ((start < length) && isZenHankakuSpace(str.charAt(start))) { start++; } return start > 0 ? str.substring(start, length) : str; } /** * ??????? * * ? null ????? null ?<br> * * @param str ??? * @return ?? */ public static String trimZ(String str) { return ltrimZ(rtrimZ(str)); } /** * ??????????? * * @param longClassName ?? * @return ?? */ public static String toShortClassName(String longClassName) { return ClassUtils.getShortClassName(longClassName); } /** * ??????? * * ??????? * name?null???null? * * @param name ???????? * @return ? */ public static String getExtension(String name) { if (name == null) { return null; } int index = name.lastIndexOf('.'); return (index < 0) ? "" : name.substring(index); } /** * ??16??? * * @param byteArray ?? * @param delim * @return 16 */ public static String toHexString(byte[] byteArray, String delim) { if (delim == null) { delim = ""; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < byteArray.length; i++) { if (i > 0) { sb.append(delim); } String hex = Integer.toHexString(byteArray[i] & 0x00ff).toUpperCase(); if (hex.length() < 2) { sb.append("0"); } sb.append(hex); } return sb.toString(); } /** * ????? * * @param str ??? * @return ?? */ public static String capitalizeInitial(String str) { if (str == null || "".equals(str)) { return str; } char[] chars = str.toCharArray(); chars[0] = Character.toUpperCase(chars[0]); return new String(chars); } /** * CSV??????? * * <p> * ??? * ????????????????? * ????????????????????? * ??</p> * <p>?????????????</p> * <p>csvString ? null ??????? * ?0????? * * @param csvString CSV?? * @return ???????? */ public static String[] parseCSV(String csvString) { if (csvString == null) { return new String[0]; } if ("".equals(csvString)) { return new String[] { csvString }; } Collection<String> result = new ArrayList<String>(); char[] chars = csvString.toCharArray(); int prevCommaIndex = -1; for (int i = 0; i < chars.length; i++) { if (chars[i] == ',') { if (i == prevCommaIndex + 1) { result.add(""); } else { result.add(new String(chars, prevCommaIndex + 1, i - (prevCommaIndex + 1))); } if (i == chars.length - 1) { result.add(""); } prevCommaIndex = i; } else if (i == chars.length - 1) { result.add(new String(chars, prevCommaIndex + 1, i - (prevCommaIndex + 1) + 1)); } } return result.toArray(new String[0]); } /** * CSV??????? * * <p> * ??? * ????????????????? * ????????????????????? * ??</p> * <p>?????????????</p> * <p>csvString ? null ??????? * ?0?????<br> * ??????? * ????????<br> * ?????? * ???? * </p> * * @param csvString CSV?? * @param escapeString * @return ???????? */ public static String[] parseCSV(String csvString, String escapeString) { if (csvString == null) { return new String[0]; } if ("".equals(csvString)) { return new String[] { csvString }; } Collection<String> result = new ArrayList<String>(); char[] chars = csvString.toCharArray(); StringBuilder str = new StringBuilder(); boolean escape = false; for (int i = 0; i < chars.length; i++) { if (!escape && chars[i] == ',') { result.add(str.toString()); str = new StringBuilder(); escape = false; } else { if (escapeString != null && escapeString.indexOf(chars[i]) >= 0) { // ????? // ?? if (escape) { str.append(chars[i]); escape = false; } else { escape = true; } } else { escape = false; str.append(chars[i]); } } } result.add(str.toString()); return result.toArray(new String[0]); } /** * ???? * * ???????????? * toString()??? * * @param map * @return */ public static String dump(Map<?, ?> map) { if (map == null) { return null; } StringBuilder sb = new StringBuilder(); sb.append(LINE_SEP); sb.append("Map{"); sb.append(LINE_SEP); for (Map.Entry<?, ?> ent : map.entrySet()) { // String appendKey = null; if (ent.getKey() == null) { appendKey = "null"; } else { appendKey = ent.getKey().toString(); } sb.append(appendKey); sb.append('='); Object valueObj = ent.getValue(); if (valueObj == null) { sb.append("null"); } else if (valueObj.getClass().isArray()) { // ??????? sb.append(getArraysStr((Object[]) valueObj)); } else { sb.append(valueObj.toString()); } sb.append(LINE_SEP); } sb.append("}"); return sb.toString(); } /** * ??????? * ???????????? * * @param arrayObj ? * @return ? */ public static String getArraysStr(Object[] arrayObj) { return ArrayUtils.toString(arrayObj, null); } /** * ???? * * <p> * ???????????????????<br> * () '' + '' => ''</p> * * <p>????????????</p> * * <p><ul> * <li>?</li> * <li>"?(''??¥u30f7)</li> * <li>"?(''??¥30fa)</li> * </ul></p> * * @param value ? * @return */ public static String hankakuToZenkaku(String value) { if (value == null || "".equals(value)) { return value; } char[] chars = value.toCharArray(); StringBuilder returnValue = new StringBuilder(); String getValue = null; Character nextvalue = null; for (int i = 0; i < chars.length; i++) { getValue = getZenkakuMoji(chars[i]); if (getValue != null) { returnValue.append(getValue); } else if (i == (chars.length - 1)) { // ? getValue = getZenkakuKasatahaMoji(chars[i]); if (getValue != null) { // ?? returnValue.append(getValue); } else if (Character.valueOf(chars[i]).equals( Character.valueOf(''))) { returnValue.append(""); } else if (Character.valueOf(chars[i]).equals( Character.valueOf(''))) { returnValue.append(""); } else { returnValue.append(String.valueOf(chars[i])); } } else { nextvalue = Character.valueOf(chars[i + 1]); if (nextvalue.equals(Character.valueOf(''))) { getValue = getZenkakuDakuMoji(chars[i]); if (getValue != null) { // ?? returnValue.append(getValue); i++; } else if (Character.valueOf(chars[i]).equals( Character.valueOf(''))) { // returnValue.append(ZENKAKU_WA_DAKUTEN); i++; } else if (Character.valueOf(chars[i]).equals( Character.valueOf(''))) { // returnValue.append(ZENKAKU_WO_DAKUTEN); i++; } else { returnValue.append((String.valueOf(chars[i]) + "")); i++; } } else if (nextvalue.equals(Character.valueOf(''))) { getValue = getZenkakuHandakuMoji(chars[i]); if (getValue != null) { // ? returnValue.append(getValue); i++; } else { // ? getValue = getZenkakuKasatahaMoji(chars[i]); if (getValue == null && (Character.valueOf('')).equals(chars[i])) { returnValue.append("").append(""); } else if ((Character.valueOf('')).equals(chars[i])) { returnValue.append("").append(""); } else { returnValue.append(String.valueOf(getValue)).append(""); } i++; } } else { getValue = getZenkakuKasatahaMoji(chars[i]); if (getValue != null) { // ?? returnValue.append(getValue); } else if (Character.valueOf(chars[i]).equals( Character.valueOf(''))) { returnValue.append(""); } else if (Character.valueOf(chars[i]).equals( Character.valueOf(''))) { returnValue.append(""); } else { returnValue.append(String.valueOf(chars[i])); } } } } return returnValue.toString(); } /** * ???? * * <p> * ????????????<br> * () '' => '' + ''</p> * * <p>??????????????</p> * * <p><ul> * <li>?</li> * <li>"?(''??¥u30f7)</li> * <li>"?(''??¥30fa)</li> * </ul></p> * * @param value * @return ? */ public static String zenkakuToHankaku(String value) { if (value == null || "".equals(value)) { return value; } char[] chars = value.toCharArray(); StringBuilder returnValue = new StringBuilder(); String getValue = null; for (int i = 0; i < chars.length; i++) { getValue = getHankakuMoji(chars[i]); if (getValue != null) { returnValue.append(getValue); } else { returnValue.append(String.valueOf(chars[i])); } } return returnValue.toString(); } /** * ???? * * ???? * * @param c ? * @return */ private static String getZenkakuMoji(char c) { int index = HANKAKU_LIST.indexOf(c); if (index >= 0) { return String.valueOf(ZENKAKU_LIST.charAt(index)); } return null; } /** * ???? * * (????)????? * * @param c ? * @return */ private static String getZenkakuDakuMoji(char c) { int index = HANKAKU_KASATAHA_LIST.indexOf(c); if (index >= 0) { return String.valueOf(ZENKAKU_GAZADABA_LIST.charAt(index)); } return null; } /** * ???? * * ()???? * * @param c ? * @return */ private static String getZenkakuHandakuMoji(char c) { int index = HANKAKU_HA_LIST.indexOf(c); if (index >= 0) { return String.valueOf(ZENKAKU_PA_LIST.charAt(index)); } return null; } /** * ???? * * (????)????? * * @param c ? * @return */ private static String getZenkakuKasatahaMoji(char c) { int index = HANKAKU_KASATAHA_LIST.indexOf(c); if (index >= 0) { return String.valueOf(ZENKAKU_KASATAHA_LIST.charAt(index)); } return null; } /** * ???? * * ???????????<br> * * <p><ul> * <li>?</li> * <li>?()?</li> * <li>?()?</li> * <li>?()</li> * <li>?()</li> * </ul></p> * * @param c * @return ? */ private static String getHankakuMoji(char c) { int index = 0; String value = null; index = ZENKAKU_LIST.indexOf(c); if (index >= 0) { return String.valueOf(HANKAKU_LIST.charAt(index)); } index = ZENKAKU_KASATAHA_LIST.indexOf(c); if (index >= 0) { // ?? return String.valueOf(HANKAKU_KASATAHA_LIST.charAt(index)); } index = ZENKAKU_GAZADABA_LIST.indexOf(c); if (index >= 0) { // "? value = String.valueOf(HANKAKU_KASATAHA_LIST.charAt(index)); return value + ""; } index = ZENKAKU_PA_LIST.indexOf(c); if (index >= 0) { // ? value = String.valueOf(HANKAKU_HA_LIST.charAt(index)); return value + ""; } else if ((Character.valueOf(c)).equals(Character.valueOf(''))) { // return ""; } else if ((Character.valueOf(c)).equals(Character.valueOf(''))) { // return ""; } else if ((Character.valueOf(c)).equals(ZENKAKU_WA_DAKUTEN)) { // "[\u30f7] return ""; } else if ((Character.valueOf(c)).equals(ZENKAKU_WO_DAKUTEN)) { // "[\u30fa] return ""; } else { // ?? return null; } } /** * HTML? * * <p> * "<"?">"?"&"?"""?"'"?????HTML? * ?????????? * "&lt;"?"&gt;"?"&amp;"?"&quot;"?"&#39;" * ??? * </p> * <p> * null?????null? * </p * * @param str ?? * @return ?? */ public static String filter(String str) { if (str == null) { return null; } char[] cbuf = str.toCharArray(); StringBuilder sbui = new StringBuilder(); for (int i = 0; i < cbuf.length; i++) { if (cbuf[i] == '&') { sbui.append("&"); } else if (cbuf[i] == '<') { sbui.append("<"); } else if (cbuf[i] == '>') { sbui.append(">"); } else if (cbuf[i] == '"') { sbui.append("""); } else if (cbuf[i] == '\'') { sbui.append("'"); } else { sbui.append(cbuf[i]); } } return sbui.toString(); } /** * LIKE??? */ public static final String LIKE_ESC_CHAR = "~"; /** * <p>?LIKE????</p> * * <p>???</p> * * <ol> * <li><code>LIKE_ESC_CHAR</code> <code>LIKE_ESC_CHAR</code> ? * ?</li> * <li>'%'?'_'?''?'' <code>LIKE_ESC_CHAR</code> ??</li> * <li>?'%'?</li> * </ol> * * <p>condition?null???null?</p> * * <p> * <b>?</b> ??LIKE???? * ''?''?????<br> * DB2?Oracle 11gR2(11.2.0.3)???RDBMS?? * ??????????? * ????<br> * ???RDBMS?LIKE?''?''? * ????????????? * </p> * * @param condition ? * @return ??? */ public static String toLikeCondition(String condition) { if (condition == null) { return null; } final char esc = (LIKE_ESC_CHAR.toCharArray())[0]; StringBuilder result = new StringBuilder(); for (int i = 0; i < condition.length(); i++) { char c = condition.charAt(i); if (c == esc) { result.append(esc); result.append(esc); } else if (c == '%' || c == '_' || c == '' || c == '') { result.append(esc); result.append(c); } else { result.append(c); } } result.append('%'); return result.toString(); } /** * ?????? * ???????? * ???????????? * ???? * * @param value ???? * @param encoding * @return ? * @throws UnsupportedEncodingException ?????? * ?????? */ public static int getByteLength(String value, String encoding) throws UnsupportedEncodingException { if (value == null || "".equals(value)) { return 0; } byte[] bytes = null; if (encoding == null || "".equals(encoding)) { bytes = value.getBytes(); } else { try { bytes = value.getBytes(encoding); } catch (UnsupportedEncodingException e) { throw e; } } return bytes == null ? 0 : bytes.length; } }