?? paramchecker.java
字號:
/*****************************************************************************
* (C) Copyright 2004 。
* 保留對所有使用、復(fù)制、修改和發(fā)布整個軟件和相關(guān)文檔的權(quán)利。
* 本計(jì)算機(jī)程序受著作權(quán)法和國際公約的保護(hù),未經(jīng)授權(quán)擅自復(fù)制或
* 傳播本程序的全部或部分,可能受到嚴(yán)厲的民事和刑事制裁,并
* 在法律允許的范圍內(nèi)受到最大可能的起訴。
*/
/*****************************************************************************
* @作者:Golden Peng
* @版本: 1.0
* @時(shí)間: 2002-10-08
*/
/*****************************************************************************
* 修改記錄清單
* 修改人 :
* 修改記錄:
* 修改時(shí)間:
* 修改描述:
*
*/
package com.corp.bisc.ebiz.base;
/**
* 此處插入類型描述。
* 創(chuàng)建日期:(2002-5-15 23:53:10)
* @author:Administrator
*/
import java.util.*;
import org.w3c.dom.*;
import com.corp.bisc.ebiz.exception.*;
public class ParamChecker extends ObjectBase
{
final public static String ENUM_PREFIX = "[";
final public static String ENUM_SUBFIX = "]";
final public static String ENUM_SEPSYM = "|";
final public static String RANGE_PREFIX = "{";
final public static String RANGE_SUBFIX = "}";
final public static String RANGE_SEPSYM = "...";
final public static int PARAM_RANGE_ENUM = 1;
final public static int PARAM_RANGE_TOP = 2;
final public static int PARAM_RANGE_BOTTOM = 4;
final public static int PARAM_TYPE_INT = 1;
final public static int PARAM_TYPE_BOOLEAN = 2;
final public static int PARAM_TYPE_CHAR = 3;
final public static int PARAM_TYPE_STRING = 4;
final public static int PARAM_TYPE_FLOAT = 5;
final public static int PARAM_TYPE_DOUBLE = 6;
protected String paramName = null;
protected boolean isRequired = false;
protected String defaultValue = null;
protected Hashtable enumValues = null;
protected int bottomInt = 0;
protected int topInt = 0;
protected float bottomFloat = 0.0f;
protected float topFloat = 0.0f;
protected double bottomDouble = 0.0;
protected double topDouble = 0.0;
protected char topChar = 0;
protected char bottomChar = 0;
protected int valueType = PARAM_TYPE_STRING;
protected int rangeType = 0;
/**
* ParamChecker 構(gòu)造子注解。
*/
public ParamChecker() {
super();
}
public String getParamName()
{
return paramName;
}
public Object getValidValue(String value) throws InvalidParameterException
{
enter("getValidValue(String)");
if (value == null)
{
if (isRequired)
throw new InvalidParameterException(paramName + '=' + value);
value = defaultValue;
}
if (value != null)
{
Object o = null;
try
{
switch(valueType)
{
case PARAM_TYPE_CHAR:
if (value.length() != 1)
throw new InvalidParameterException(paramName + '=' + value);
o = new Character(value.charAt(0));
break;
case PARAM_TYPE_INT:
o = Integer.valueOf(value);
break;
case PARAM_TYPE_FLOAT:
o = Float.valueOf(value);
break;
case PARAM_TYPE_DOUBLE:
o = Double.valueOf(value);
break;
case PARAM_TYPE_BOOLEAN:
o = Boolean.valueOf(value);
default:
o = value;
}
}
catch(NumberFormatException e)
{
throw new InvalidParameterException(paramName + '=' + value);
}
if (hasEnums())
{
if (enumValues.get(value) == null)
throw new InvalidParameterException(paramName + '=' + value);
}
else
{
boolean result = true;
if (hasTop())
{
switch(valueType)
{
case PARAM_TYPE_CHAR:
if (topChar < ((Character)o).charValue())
result = false;
break;
case PARAM_TYPE_INT:
if (topInt < ((Integer)o).intValue())
result = false;
break;
case PARAM_TYPE_FLOAT:
if (topFloat < ((Float)o).floatValue())
result = false;
break;
case PARAM_TYPE_DOUBLE:
if (topDouble < ((Double)o).doubleValue())
result = false;
break;
}
if (!result)
throw new InvalidParameterException(paramName + '=' + value);
}
if (hasBottom())
{
switch(valueType)
{
case PARAM_TYPE_CHAR:
if (bottomChar > ((Character)o).charValue())
result = false;
break;
case PARAM_TYPE_INT:
if (bottomInt > ((Integer)o).intValue())
result = false;
break;
case PARAM_TYPE_FLOAT:
if (bottomFloat > ((Float)o).floatValue())
result = false;
break;
case PARAM_TYPE_DOUBLE:
if (bottomDouble > ((Double)o).doubleValue())
result = false;
break;
}
if (!result)
throw new InvalidParameterException(paramName + '=' + value);
}
}
leave("getValidValue(String)");
return o;
}
leave("getValidValue(String)");
return null;
}
public boolean hasBottom()
{
return (rangeType & PARAM_RANGE_BOTTOM) != 0;
}
public boolean hasEnums()
{
return (rangeType & PARAM_RANGE_ENUM) != 0;
}
public boolean hasTop()
{
return (rangeType & PARAM_RANGE_TOP) != 0;
}
public void init(Node cfgNode) throws InvalidConfigException
{
enter("init(Node)");
NamedNodeMap attribs = cfgNode.getAttributes();
Node nameNode = attribs.getNamedItem("name");
Node defVNode = attribs.getNamedItem("default");
Node reqNode = attribs.getNamedItem("required");
Node typeNode = attribs.getNamedItem("type");
Node valueNode = attribs.getNamedItem("value");
if (nameNode == null)
throw new InvalidConfigException("action/parameters/param/@name");
else
paramName = nameNode.getNodeValue();
if (defVNode != null)
defaultValue = defVNode.getNodeValue();
if (reqNode != null)
{
String reqText = reqNode.getNodeValue();
if (reqText.equalsIgnoreCase("true"))
isRequired = true;
else if (reqText.equalsIgnoreCase("false"))
isRequired = false;
else
throw new InvalidConfigException("action/parameters/param/[@required=" + reqText + "]");
}
if (typeNode != null)
{
String typeText = typeNode.getNodeValue();
if (typeText.equals("int"))
valueType = PARAM_TYPE_INT;
else if (typeText.equals("bool"))
valueType = PARAM_TYPE_BOOLEAN;
else if (typeText.equals("char"))
valueType = PARAM_TYPE_CHAR;
else if (typeText.equals("string"))
valueType = PARAM_TYPE_STRING;
else if (typeText.equals("float"))
valueType = PARAM_TYPE_FLOAT;
else if (typeText.equals("double"))
valueType = PARAM_TYPE_DOUBLE;
else
throw new InvalidConfigException("action/parameters/param/[@type=" + typeText + "]");
}
if (valueNode != null)
{
String rangeText = valueNode.getNodeValue();
if (!parseRange(rangeText))
throw new InvalidConfigException("action/parameters/param/[@value=" + rangeText + "]");
}
leave("init(Node)");
}
protected boolean parseRange(String rangeText)
{
enter("parseRange(String)");
int len = rangeText.length();
if (rangeText.startsWith(ENUM_PREFIX) && rangeText.endsWith(ENUM_SUBFIX))
{
rangeType = PARAM_RANGE_ENUM;
String newText = rangeText.substring(ENUM_PREFIX.length() , len - ENUM_PREFIX.length() - ENUM_SUBFIX.length());
StringTokenizer tokens = new StringTokenizer(newText , ENUM_SEPSYM);
while(tokens.hasMoreTokens())
{
String token = tokens.nextToken();
enumValues.put(token , token);
}
leave("parseRange(String)");
return true;
}
else if (rangeText.startsWith(RANGE_PREFIX) && rangeText.endsWith(RANGE_SUBFIX))
{
String newText = rangeText.substring(RANGE_PREFIX.length() , len - RANGE_PREFIX.length() - RANGE_SUBFIX.length());
int sepIndex = newText.indexOf(RANGE_SEPSYM);
if (sepIndex == -1)
{
leave("parseRange(String)");
return false;
}
String bottomText = newText.substring(0 , sepIndex);
String topText = newText.substring(sepIndex + RANGE_SEPSYM.length());
if (bottomText.length() != 0)
rangeType |= PARAM_RANGE_BOTTOM;
if (topText.length() != 0)
rangeType |= PARAM_RANGE_TOP;
try
{
switch(valueType)
{
case PARAM_TYPE_CHAR:
if (bottomText.length() > 1 || topText.length() > 1)
{
leave("parseRange(String)");
return false;
}
if (hasBottom())
bottomChar = bottomText.charAt(0);
if (hasTop())
topChar = topText.charAt(0);
break;
case PARAM_TYPE_INT:
if (hasBottom())
bottomInt = Integer.parseInt(bottomText);
if (hasTop())
topInt = Integer.parseInt(topText);
break;
case PARAM_TYPE_FLOAT:
if (hasBottom())
bottomFloat = Float.parseFloat(bottomText);
if (hasTop())
topFloat = Float.parseFloat(topText);
break;
case PARAM_TYPE_DOUBLE:
if (hasBottom())
bottomDouble = Double.parseDouble(bottomText);
if (hasTop())
topDouble = Double.parseDouble(topText);
break;
default:
leave("parseRange(String)");
return false;
}
}
catch(NumberFormatException e)
{
leave("parseRange(String)");
return false;
}
}
leave("parseRange(String)");
return false;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -