?? genericparamutil.java
字號:
/*
* $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/GenericParamUtil.java,v 1.6.2.1 2006/07/10 12:55:29 phuongpdd Exp $
* $Author: phuongpdd $
* $Revision: 1.6.2.1 $
* $Date: 2006/07/10 12:55:29 $
*
* ====================================================================
*
* Copyright (C) 2002-2006 by MyVietnam.net
*
* All copyright notices regarding MyVietnam and MyVietnam CoreLib
* MUST remain intact in the scripts and source code.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Correspondence and Marketing Questions can be sent to:
* info at MyVietnam net
*
* @author: Minh Nguyen
* @author: Mai Nguyen
*/
package net.myvietnam.mvncore.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpSession;
import net.myvietnam.mvncore.MVNCoreResourceBundle;
import net.myvietnam.mvncore.exception.BadInputException;
import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
import net.myvietnam.mvncore.web.GenericRequest;
public final class GenericParamUtil {
private GenericParamUtil() { // prevent instantiation
}
//private static String contextPath = (new ParamOptions()).contextPath;
//private static String serverPath = (new ParamOptions()).serverPath;//@todo combine 2 line to a static block
private static DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
public static String getParameter(GenericRequest request, String param) {
String ret = request.getParameter(param);
if (ret == null) ret = "";
return ret.trim();
}
public static String getParameterFilter(GenericRequest request, String param) {
return DisableHtmlTagFilter.filter(getParameter(request, param));
}
//獲取表單,URL參數,
public static String getParameter(GenericRequest request, String param, boolean checkEmpty)
throws BadInputException {
String ret = request.getParameter(param);
if (ret == null) ret = "";
ret = ret.trim();
if ( checkEmpty && (ret.length() == 0) ) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_allow_to_be_empty", new Object[] {DisableHtmlTagFilter.filter(param)});
throw new BadInputException(localizedMessage);
}
return ret;
}
public static String getParameterFilter(GenericRequest request, String param, boolean checkEmpty)
throws BadInputException {
return DisableHtmlTagFilter.filter(getParameter(request, param, checkEmpty));
}
/** @todo review this method */
public static String getParameterSafe(GenericRequest request, String param, boolean checkEmpty)
throws BadInputException {
String ret = getParameter(request, param, checkEmpty);
if ( (ret.indexOf('<') != -1) ||
(ret.indexOf('>') != -1)) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.parameter_safe", new Object[] {DisableHtmlTagFilter.filter(param)});
throw new BadInputException(localizedMessage);
}
return ret;
}
public static int getParameterInt(GenericRequest request, String param)
throws BadInputException {
String inputStr = getParameter(request, param, true);
int ret;
try {
ret = Integer.parseInt(inputStr);
} catch (NumberFormatException e) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "int"});
throw new BadInputException(localizedMessage);
}
return ret;
}
public static int getParameterUnsignedInt(GenericRequest request, String param)
throws BadInputException {
int retValue = getParameterInt(request, param);
if (retValue < 0) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object[] {DisableHtmlTagFilter.filter(param)});
throw new BadInputException(localizedMessage);
}
return retValue;
}
public static int getParameterInt(GenericRequest request, String param, int defaultValue)
throws BadInputException {
String inputStr = getParameter(request, param, false);
if (inputStr.length() == 0) {
return defaultValue;
}
int ret;
try {
ret = Integer.parseInt(inputStr);
} catch (NumberFormatException e) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "int"});
throw new BadInputException(localizedMessage);
}
return ret;
}
public static int getParameterUnsignedInt(GenericRequest request, String param, int defaultValue)
throws BadInputException {
int retValue = getParameterInt(request, param, defaultValue);
if (retValue < 0) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object[] {DisableHtmlTagFilter.filter(param)});
throw new BadInputException(localizedMessage);
}
return retValue;
}
public static long getParameterLong(GenericRequest request, String param)
throws BadInputException {
String inputStr = getParameter(request, param, true);
long ret;
try {
ret = Long.parseLong(inputStr);
} catch (NumberFormatException e) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "long"});
throw new BadInputException(localizedMessage);
}
return ret;
}
public static long getParameterLong(GenericRequest request, String param, long defaultValue)
throws BadInputException {
String inputStr = getParameter(request, param, false);
if (inputStr.length() == 0) {
return defaultValue;
}
long ret;
try {
ret = Long.parseLong(inputStr);
} catch (NumberFormatException e) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "long"});
throw new BadInputException(localizedMessage);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -