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

📄 pageinputcollector.java

📁 一种快速开发的Java Web架构,doc里有详细的设计文档和开发文档。
💻 JAVA
字号:
package com.hisoft.cottonbusiness.core.common;

import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.hisoft.cottonbusiness.core.util.RequestParamUtil;

public class PageInputCollector
{
	private static final Logger log = Logger.getLogger(PageInputCollector.class);

	private static final String CONFIGFILENAME = "metadata/InputTypeMapping.xml";

	private static URL configFile; 

	static
	{
		loadConfig(CONFIGFILENAME);
	}

	public static Map collectInputMap(HttpServletRequest request)
	{
		return RequestParamUtil.getInputMap(request);
	}
	
	public static Map collectInputMap(HttpServletRequest request, String pageId)
	{
		Map in = request.getParameterMap();
		Enumeration e = request.getParameterNames();
		Map out = new HashMap();
		while (e.hasMoreElements())
		{
			String key = e.nextElement().toString();
			String[] value = (String[]) in.get(key);

			String[][] types = loadTypeMapping(pageId);

			if (value.length == 1)
			{
				log.debug("key(String): " + key);
				out.put(key, value[0]);
				
				for (int i = 0; i < types.length; i++)
				{
					if (key.equals(types[i][0]))
					{
						log.debug("key: " + key);
						out.put(key, toTypedObject(value[0], types[i][1]));
						break;
					}
				}
			}
			else
			{
				String type = null;
				for (int i = 0; i < types.length; i++)
				{
					if (key.equals(types[i][0]))
					{
						type = types[i][1];
						break;
					}
				}

				Object[] obj = new Object[value.length];
				for (int i = 0; i < value.length; i++)
				{
					obj[i] = toTypedObject(value[i], type);
				}
				out.put(key, obj);
			}
		}

		return out;
	}

	private static void loadConfig(String resource)
	{
		log.debug("begin to load config file: ");
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		configFile = cl.getResource(resource);
		log.debug("config file: " + configFile.getPath());
	}

	private static String[][] loadTypeMapping(String pageId)
	{
		String[][] types = null;
		int length = 0;

		try
		{
			SAXReader reader = new SAXReader();
			Document doc = reader.read(configFile);
			Element root = doc.getRootElement();
			Element page = null;
			for (Iterator i = root.elementIterator(); i.hasNext();)
			{
				page = (Element) i.next();
				String id = page.attributeValue("id");
				if (id.equals(pageId))
				{
					break;
				}
			}

			length = page.elements("type").size();
			types = new String[length][2];
			int position = 0;
			for (Iterator i = page.elementIterator(); i.hasNext();)
			{
				Element type = (Element) i.next();
				String key = type.attributeValue("key");
				String value = type.getTextTrim();
				types[position][0] = key;
				types[position][1] = value;
				position++;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return types;
	}

	private static Object toTypedObject(String orgin, String type)
	{
		Object result = null;
		try
		{
			if ("Integer".equals(type))
			{
				try
				{
					result = new Integer(orgin);
				}
				catch (NumberFormatException e)
				{
					result = new Integer(0);
				}
			}
			else if ("Long".equals(type))
			{
				try
				{
					result = new Long(orgin);
				}
				catch (NumberFormatException e)
				{
					result = new Long(0);
				}
			}
			else if ("Float".equals(type))
			{
				try
				{
					result = new Float(orgin);
				}
				catch (NumberFormatException e)
				{
					result = new Float(0.0);
				}
			}
			else if ("Double".equals(type))
			{
				try
				{
					result = new Double(orgin);
				}
				catch (NumberFormatException e)
				{
					result = new Double(0.0);
				}
			}
			else if ("Short".equals(type))
			{
				try
				{
					result = new Short(orgin);
				}
				catch (NumberFormatException e)
				{
					result = new Short("0");
				}
			}
			else if ("Boolean".equals(type))
			{
				try
				{
					result = new Boolean(orgin);
				}
				catch (NumberFormatException e)
				{
					result = new Boolean("false");
				}
			}
			else if ("Character".equals(type))
			{
				result = new Character(orgin.toCharArray()[0]);
			}
			else
			{
				result = orgin;
			}
		}
		catch (Exception e)
		{
			log.debug("error: " + e.getMessage());
		}

		log.debug(result + ": " + result.getClass());
		return result;
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception
	{
		String[][] types = loadTypeMapping("createUser");
	}

}

⌨️ 快捷键说明

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