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.validation; import java.io.UnsupportedEncodingException; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Collection; import java.util.Date; import java.util.StringTokenizer; import jp.terasoluna.fw.util.PropertyUtil; import jp.terasoluna.fw.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.commons.validator.GenericTypeValidator; import org.apache.commons.validator.GenericValidator; import org.apache.commons.validator.UrlValidator; /** * ? * * */ public class ValidationUtil { /** * ????? */ protected static String hankakuKanaList = "?????"; /** * ???? */ protected static String zenkakuKanaList = "" + "???" + "??" + ""; /** * <code>ApplicationResources</code> * ?????? */ protected static final String HANKAKU_KANA_LIST_KEY = "validation.hankaku.kana.list"; /** * <code>ApplicationResources</code> * ????? */ protected static final String ZENKAKU_KANA_LIST_KEY = "validation.zenkaku.kana.list"; /** * UNICODE'¥u0000'?'¥u00ff'? * ?? */ protected static final String ZENKAKU_BEGIN_U00_LIST = ""; static { // ?? setHankakuKanaList(); setZenkakuKanaList(); } /** * ?? */ protected static void setHankakuKanaList() { String value = null; // ?? value = PropertyUtil.getProperty(HANKAKU_KANA_LIST_KEY); if (value != null) { hankakuKanaList = value; } } /** * ? */ protected static void setZenkakuKanaList() { // ? String value = null; value = PropertyUtil.getProperty(ZENKAKU_KANA_LIST_KEY); if (value != null) { zenkakuKanaList = value; } } /** * ?????????? * * @param c * @return ???? true */ protected static boolean isHankakuKanaChar(char c) { return hankakuKanaList.indexOf(c) >= 0; } /** * ?????????? * * @param c * @return ???? true */ protected static boolean isHankakuChar(char c) { return (c <= '\u00ff' && ZENKAKU_BEGIN_U00_LIST.indexOf(c) < 0) || isHankakuKanaChar(c); } /** * ????????? * * @param c * @return ??? true */ protected static boolean isZenkakuChar(char c) { return !isHankakuChar(c); } /** * ????????? * * @param c * @return ??? true */ protected static boolean isZenkakuKanaChar(char c) { return zenkakuKanaList.indexOf(c) >= 0; } /** * ?????????? * * @param value * @param mask ?? * @return * ???????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean matchRegexp(String value, String mask) { if (!StringUtils.isEmpty(value) && !GenericValidator.matchRegexp(value, mask)) { return false; } return true; } /** * ??????????? * * <code>null</code> ??????? * * @param value * @return * ????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isAlphaNumericString(String value) { return matchRegexp(value, "^([0-9]|[a-z]|[A-Z])*$"); } /** * ??????????? * * <code>null</code> ??????? * * @param value * @return * ????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isUpperAlphaNumericString(String value) { return matchRegexp(value, "^([0-9]|[A-Z])*$"); } /** * ??????????? * * <code>null</code> ??????? * * @param value * @return * ????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isNumericString(String value) { return matchRegexp(value, "^([0-9])*$"); } /** * ?????????? * <br> * ?????????????? * <ul> * <li>??? * <ol> * <li><code>isAccordedInteger</code>?<code>true</code>??? * ????<code>integerLength</code>?? * ???????? * * <li><code>isAccordedInteger</code>?<code>false</code>??? * ????<code>integerLength</code>????? * ?? * </ol> * * <li>???? * <ol> * <li><code>isAccordedScale</code>?<code>true</code>??? * ?????<code>scaleLength</code>?? * ???????? * * <li><code>isAccordedScale</code>?<code>true</code>??? * ?????<code>scaleLength</code>????? * ?? * </ol> * </ul> * * @param value * @param integerLength ?? * @param isAccordedInteger * ?????? * <code>true</code>? * ?????? * <code>false</code>? * @param scaleLength ??? * @param isAccordedScale * ??????? * <code>true</code>? * ??????? * <code>false</code>? * * @return * ???????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isNumber(BigDecimal value, int integerLength, boolean isAccordedInteger, int scaleLength, boolean isAccordedScale) { // ?null??true? if (value == null) { return true; } // ?? // ?? BigInteger bi = value.toBigInteger().abs(); // ? int length = bi.toString().length(); if (!checkNumberFigures(length, integerLength, isAccordedInteger)) { return false; } // ??? int scale = value.scale(); if (!checkNumberFigures(scale, scaleLength, isAccordedScale)) { return false; } return true; } /** * ?????? * * @param length ?? * @param checkLength ????? * @param isAccorded * ?????<code>true</code>? * ?????<code>false</code>? * @return * ?????????? * <code>true</code>? * ?????<code>false</code>? */ protected static boolean checkNumberFigures(int length, int checkLength, boolean isAccorded) { // ????false? if (length > checkLength) { return false; } // ?????? if (isAccorded) { // ????false? if (length != checkLength) { return false; } } return true; } /** * ???????????? * ?? * * <code>null</code> ??????? * * @param value * @return * ?????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isHankakuKanaString(String value) { // ?null?????true? if (StringUtils.isEmpty(value)) { return true; } char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!isHankakuKanaChar(chars[i])) { return false; } } return true; } /** * ???????????? * ?? * * <code>null</code> ??????? * * @param value * @return * ?????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isHankakuString(String value) { // ?null?????true? if (StringUtils.isEmpty(value)) { return true; } char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!isHankakuChar(chars[i])) { return false; } } return true; } /** * ??????????? * ?? * * <code>null</code> ??????? * * @param value * @return * ????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isZenkakuString(String value) { // ?null?????true? if (StringUtils.isEmpty(value)) { return true; } char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!isZenkakuChar(chars[i])) { return false; } } return true; } /** * ??????????? * ?? * * <code>null</code> ??????? * * @param value * @return * ????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isZenkakuKanaString(String value) { // ?null?????true? if (StringUtils.isEmpty(value)) { return true; } char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!isZenkakuKanaChar(chars[i])) { return false; } } return true; } /** * ?????????? * ????\?? * ??"?????? * \"?????? * * ??<code>null</code>????????? * * @param value * @param prohibitedChars ?? * @return ????????<code>true</code>? * ???<code>false</code>? */ public static boolean hasNotProhibitedChar(String value, String prohibitedChars) { // ?null?????true? if (StringUtils.isEmpty(value)) { return true; } char[] chars = value.toCharArray(); // ????????? if (prohibitedChars == null || "".equals(prohibitedChars)) { return true; } // for (int i = 0; i < chars.length; i++) { if (prohibitedChars.indexOf(chars[i]) >= 0) { return false; } } return true; } /** * ??????<code>Collection</code>???? * ???????????? * * ??<code>Collection</code>?<code>null</code>? * ????0????????????? * Collection???????IllegalArgumentException?? * * @param obj ??<code>Collection</code> * @param min ??? * @param max ?? * @return * ??<code>Collection</code>? * ???????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isArrayInRange(Object obj, int min, int max) { // ?? int targetLength = 0; if (obj == null) { targetLength = 0; } else if (obj instanceof Collection) { targetLength = ((Collection<?>) obj).size(); } else if (obj.getClass().isArray()) { targetLength = Array.getLength(obj); } else { // ????????IllegalArgumentException throw new IllegalArgumentException(obj.getClass().getName() + " is neither Array nor Collection."); } // ??????false? if (!GenericValidator.isInRange(targetLength, min, max)) { return false; } return true; } /** * ?URL?????????? * * <code>null</code> ??????? * * @param value * @param allowallschemes ???????? * @param allow2slashes ?????? * @param nofragments URL?????? * @param schemesVar ?? * ????? * @return * ?URL?????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isUrl(String value, boolean allowallschemes, boolean allow2slashes, boolean nofragments, String schemesVar) { if (StringUtils.isEmpty(value)) { return true; } // ? int options = 0; if (allowallschemes) { options += UrlValidator.ALLOW_ALL_SCHEMES; } if (allow2slashes) { options += UrlValidator.ALLOW_2_SLASHES; } if (nofragments) { options += UrlValidator.NO_FRAGMENTS; } // ??????GenericValidator if (options == 0 && schemesVar == null) { if (GenericValidator.isUrl(value)) { return true; } return false; } // String[]?? String[] schemes = null; if (schemesVar != null) { StringTokenizer st = new StringTokenizer(schemesVar, ","); schemes = new String[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { schemes[i++] = st.nextToken().trim(); } } // ????UrlValidator UrlValidator urlValidator = new UrlValidator(schemes, options); if (urlValidator.isValid(value)) { return true; } return false; } /** * ????????????? * ???????????? * ?? * * <code>null</code> ??????? * * @param value * @param encoding ???<code>encoding</code>?? * @param min * @param max ? * @return * ??????? * ????<code>true</code>? * ?????<code>false</code>? */ public static boolean isByteInRange(String value, String encoding, int min, int max) { // ?null?????true? if (StringUtils.isEmpty(value)) { return true; } // ??? int bytesLength = 0; try { bytesLength = StringUtil.getByteLength(value, encoding); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e.getMessage()); } // ?? if (!GenericValidator.isInRange(bytesLength, min, max)) { return false; } return true; } /** * ??????????? * * <code>null</code> ??????? * * @param value * @param startDateStr * ???? * <code>datePattern</code>????? * <code>datePatternStrict</code>???????? * @param endDateStr * ???? * <code>datePattern</code>????? * <code>datePatternStrict</code>???????? * @param datePattern ? * @param datePatternStrict ? * @return * ?????????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isDateInRange(String value, String startDateStr, String endDateStr, String datePattern, String datePatternStrict) { // ?null?????true? if (StringUtils.isEmpty(value)) { return true; } // ?? Date result = toDate(value, datePattern, datePatternStrict); if (result == null) { return false; } if (GenericValidator.isBlankOrNull(startDateStr) && GenericValidator.isBlankOrNull(endDateStr)) { // ???????????? return true; } // ?????? if (!GenericValidator.isBlankOrNull(startDateStr)) { Date startDate = toDate(startDateStr, datePattern, datePatternStrict); if (startDate == null) { throw new IllegalArgumentException("startDate is unparseable[" + startDateStr + "]"); } if (result.before(startDate)) { return false; } } // ?????? if (!GenericValidator.isBlankOrNull(endDateStr)) { Date endDate = toDate(endDateStr, datePattern, datePatternStrict); if (endDate == null) { throw new IllegalArgumentException("endDate is unparseable[" + endDateStr + "]"); } if (result.after(endDate)) { return false; } } return true; } /** * Date??? * <br> * ?????? * ???????????? * 2000/02/31????????<code>value</code> * ?????<code>null</code>??? * <ul> * <li> * <code>datePattern</code>?<code>null</code>???? * ????<br> * ?????? * ?????<code>datePattern</code>?yyyy/MM/dd??? * 2000/1/1????2000/01/01?Date??? * <li> * <code>datePatternStrict</code>?<code>null</code>???? * ????<br> * ????? * ?????<code>datePattern</code>?yyyy/MM/dd??? * 2000/1/1????null??? * <li> * <code>datePattern</code>?<code>datePatternStrict</code>? * ????<code>null</code>???? ????<br> * <code>datePattern</code>???? * <li> * <code>datePattern</code>?<code>datePatternStrict</code>? * ???<code>null</code>???????<br> * ??? * </ul> * <li> * <code>value</code>?<code>null</code>???? * ???<code>null</code>??? * * @param value ?? * @param datePattern ??? * @param datePatternStrict ?? * @return ????Date???????null */ public static Date toDate(String value, String datePattern, String datePatternStrict) { if (StringUtils.isEmpty(value)) { return null; } Date result = null; // ?????? if (datePattern != null && datePattern.length() > 0) { result = GenericTypeValidator.formatDate(value, datePattern, false); // ????? } else if (datePatternStrict != null && datePatternStrict.length() > 0) { result = GenericTypeValidator.formatDate(value, datePatternStrict, true); // ?????? } else { throw new IllegalArgumentException("datePattern or datePatternStrict must be specified."); } return result; } }