⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paramchecker.java

📁 JFrame-----java系统通用框架 可以研究一下哦
💻 JAVA
字号:
/*****************************************************************************
* (C) Copyright 2004 。
* 保留对所有使用、复制、修改和发布整个软件和相关文档的权利。
* 本计算机程序受著作权法和国际公约的保护,未经授权擅自复制或
* 传播本程序的全部或部分,可能受到严厉的民事和刑事制裁,并
* 在法律允许的范围内受到最大可能的起诉。
*/

 /*****************************************************************************
  * @作者:Golden Peng
  * @版本: 1.0
  * @时间: 2002-10-08
  */
 /*****************************************************************************
  * 修改记录清单
  * 修改人  :
  * 修改记录:
  * 修改时间:
  * 修改描述:
  *
  */
package com.corp.bisc.ebiz.base;

/**
 * 此处插入类型描述。
 * 创建日期:(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 构造子注解。
 */
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;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -