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

📄 jspruntimelibrary.java

📁 JspRuntime自动填充机制;向对jsp深入了解的可以
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * @(#)JspRuntimeLibrary.java	1.0 2004-04-06
 * 转换字符集支持及错误恢复支持由 BeanSoft Studio 开发完成.
 * (c) 2004 BeanSoft Studio 版权所有. 保留所有权利.
 * --------------------------------------------------------------------
 * 再次分发和使用本程序的源代码和二进制方式, 不论是否修改, 只要同意下列
 * 条件即可:
 *
 * 1. 再次分发的源代码必须保留上述版权声明, 本条款和下列条款.
 *
 * 2. 再次分发的二进制代码必须复制此版权声明和文档中的分发条件, 以及/或者
 *    发布时所附带的材料.
 *
 * 3. 包含本软件所发布的最终用户文档(如果有的话)必须包含下列感谢:
 *    " 本产品包含 BeanSoft Studio 开发的软件(beansoftstudio@yahoo.com.cn)."
 *    或者, 这个感谢可以包含在软件自身中, 例如出现在通常第三方感谢所在的地方.
 *
 * 4. 名称 "BeanSoft Studio", "LiuChangjiong", "BeanSoft", 未经事先的书面许可,
 *    不能用来提升派生自本软件的产品. 要获得书面许可, 请联系
 *    beansoftstudio@yahoo.com.cn.
 *
 * 5. 派生自本软件的产品未经事先的书面许可不能命名为或者包含下列字样:
 *    "BeanSoft Studio", "BeanSoft", "LiuChangjiong".
 *
 * 本软件按照原样提供, 除此之外不提供任何明示的或者暗含的担保, 即使事先预计
 * 可能发生各种损失, 也无济于事, 本人概不负责任何索赔和任何责任.
 *
 * 如有任何疑问, 请联系 beansoftstudio@yahoo.com.cn.
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package studio.beansoft.jasper.runtime;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;


import studio.beansoft.jasper.Constants;
import studio.beansoft.jasper.JasperException;

/**
 * Bunch of util methods that are used by code generated for useBean,
 * getProperty and setProperty.
 *
 * The __begin, __end stuff is there so that the JSP engine can
 * actually parse this file and inline them if people don't want
 * runtime dependencies on this class. However, I'm not sure if that
 * works so well right now. It got forgotten at some point. -akv
 *
 * Documents added by BeanSoft Studio:
 * 本类可以读取 servlet request 中的参数并对 JavaBean 对象的属性根据名字进行自动
 * 赋值, 并且已经提供了故障恢复功能, 即不抛出任何异常.
 *
 * 在 JSP 和 Servlet 中可以调用下列代码:
 * studio.beansoft.jasper.runtime.JspRuntimeLibrary.introspect(myBean, request);
 * 来完成和 <%jsp:setProperty name="myBean" property="*"/> 同样的操作(除了不再出错时中止).
 *
 * 也可以自动处理字符串的编码问题, 例如从 Tomcat 下的默认 ServletRequest 对象
 * 字符集编码(ISO8859-1)转换到 GBK 编码可以调用下列代码:
 * studio.beansoft.jasper.runtime.JspRuntimeLibrary.introspect(myBean, request,
 * 	"GBK");
 *
 * 还可以显式的指定源字符集到目标字符集的编码, 例如从 UTF8 编码转换到 GBK 编码:
 * studio.beansoft.jasper.runtime.JspRuntimeLibrary.introspect(myBean, request,
 * 	"UTF-8", "GBK");
 *
 * @author LiuChangjiong
 * @version 1.0
 * 2004-3-31
 *
 * Origianl:
 * @author Mandar Raje
 */
public class JspRuntimeLibrary {

	/**
	 *
	 * PrivilegedIntrospectEncodingHelper, added by BeanSoft Studio at 2004-04-06 to
	 * support encoding convertion.
	 *
	 * Obfusecate setttings:
	 * @preserve
	 *
	 * @author LiuChangjiong
	 * @version 1.0
	 * 2004-3-31
	 */
	protected static class PrivilegedIntrospectEncodingHelper
		implements PrivilegedExceptionAction {

		private Object bean;
		private String prop;
		private String value;
		private ServletRequest request;
		private String param;
		private boolean ignoreMethodNF;
		private String sourceEncoding;
		private String targetEncoding;

		PrivilegedIntrospectEncodingHelper(
			Object bean,
			String prop,
			String value,
			ServletRequest request,
			String param,
			boolean ignoreMethodNF,
			String sourceEncoding,
			String targetEncoding) {
			this.bean = bean;
			this.prop = prop;
			this.value = value;
			this.request = request;
			this.param = param;
			this.ignoreMethodNF = ignoreMethodNF;
			this.sourceEncoding = sourceEncoding;
			this.targetEncoding = targetEncoding;
		}

		public Object run() throws JasperException {
			internalIntrospecthelper(
				bean,
				prop,
				value,
				request,
				param,
				ignoreMethodNF,
				sourceEncoding,
				targetEncoding);
			return null;
		}
	}

	// __begin convertMethod
	/**
	 * 将输入的字符串转换为目标类型(会出错).
	 * Note: 修改出错处理, 出现异常时只返回空值,
	 * 即可改变 Bean 赋值出错时发生中断的问题.
	 */
	public static Object convert(
		String propertyName,
		String s,
		Class t,
		Class propertyEditorClass)
		throws JasperException {
		try {
			if (s == null) {
				if (t.equals(Boolean.class) || t.equals(Boolean.TYPE))
					s = "false";
				else
					return null;
			}
			if (propertyEditorClass != null) {
				return getValueFromBeanInfoPropertyEditor(
					t,
					propertyName,
					s,
					propertyEditorClass);
			} else if (t.equals(Boolean.class) || t.equals(Boolean.TYPE)) {
				if (s.equalsIgnoreCase("on") || s.equalsIgnoreCase("true"))
					s = "true";
				else
					s = "false";
				return new Boolean(s);
			} else if (t.equals(Byte.class) || t.equals(Byte.TYPE)) {
				return new Byte(s);
			} else if (t.equals(Character.class) || t.equals(Character.TYPE)) {
				return s.length() > 0 ? new Character(s.charAt(0)) : null;
			} else if (t.equals(Short.class) || t.equals(Short.TYPE)) {
				return new Short(s);
			} else if (t.equals(Integer.class) || t.equals(Integer.TYPE)) {
				//System.err.println("Convert to Integer: " + s);
				return new Integer(s);
			} else if (t.equals(Float.class) || t.equals(Float.TYPE)) {
				return new Float(s);
			} else if (t.equals(Long.class) || t.equals(Long.TYPE)) {
				return new Long(s);
			} else if (t.equals(Double.class) || t.equals(Double.TYPE)) {
				return new Double(s);
			} else if (t.equals(String.class)) {
				return s;
			} else if (t.equals(java.io.File.class)) {
				return new java.io.File(s);
			} else if (t.getName().equals("java.lang.Object")) {
				return new Object[] { s };
			} else {
				return getValueFromPropertyEditorManager(t, propertyName, s);
			}
		} catch (Exception ex) {
			// Modified by BeanSoft Studio -- 只返回空值, 不报错
			System.out.println(ex);
			return null;
			// Old code below:
			//throw new JasperException(ex);
		}
	}
	// __end convertMethod

	// __begin introspectMethod
	/**
	 * 用 ServletRequest 的默认字符集实现 ServletRequest 到 Bean 值的自动填充工作.
	 * 实现和 Tomcat 默认的操作相同的功能.
	 *
	 * @param bean JavaBean 对象
	 * @param request ServletRequest 对象
	 */
	public static void introspect(Object bean, ServletRequest request) {
		//throws JasperException {
		introspect(bean, request, null);
	}
	// __end introspectMethod

	// __begin introspectMethod
	/**
	 * 从默认字符集转换到目标字符集并进行 ServletRequest 到 Bean 值的自动填充工作.
	 *
	 * @param bean JavaBean 对象
	 * @param request ServletRequest 对象
	 * @param encoding 目标字符集(如果要转换到中文字符集, 此参数应该为 "GB2312"
	 * 或者 "GBK"), 如果参数为空, 则不做任何转码
	 */
	public static void introspect(Object bean, ServletRequest request, String encoding) {
		//throws JasperException {
		introspect(bean, request, null, encoding);
	}
	// __end introspectMethod

	// __begin introspectMethod
	/**
	 * 用给定字符编码转换实现 ServletRequest 到 Bean 值的自动填充工作.
	 *
	 * @param bean JavaBean 对象
	 * @param request ServletRequest 对象
	 * @param sourceEncoding ServeltRequest 的源字符集(如果为空, 则默认为 ISO8859-1)
	 * @param targetEncoding 目标字符集, 例如 "GB2312" 或者 "GBK"
	 */
	public static void introspect(Object bean, ServletRequest request,
		String sourceEncoding, String targetEncoding) {
		//throws JasperException {
		// Check default encoding
		if(sourceEncoding == null || sourceEncoding.length() == 0) {
        	sourceEncoding = "ISO8859-1";
		}

		try {
			Enumeration e = request.getParameterNames();
			while (e.hasMoreElements()) {
				String name = (String) e.nextElement();
				String value = request.getParameter(name);
				// 字符集转换, 仅当目标字符集非空时进行此操作
				if(value != null && targetEncoding != null && targetEncoding.length() > 0) {
					try {
						value = new String(value.getBytes(sourceEncoding), targetEncoding);
					} catch (UnsupportedEncodingException exception) {
						exception.printStackTrace();
					}
				}

				introspecthelper(bean, name, value, request, name, true,
					sourceEncoding, targetEncoding);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	// __end introspectMethod

	// __begin introspecthelperMethod
	public static void introspecthelper(
		Object bean,
		String prop,
		String value,
		ServletRequest request,
		String param,
		boolean ignoreMethodNF,
		String sourceEncoding,
		String targetEncoding)
		throws JasperException {
		if (System.getSecurityManager() != null) {
			try {
				PrivilegedIntrospectEncodingHelper dp =

⌨️ 快捷键说明

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