?? jspruntimelibrary.java
字號:
/*
* @(#)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 + -