📄 jspruntimelibrary.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.runtime;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
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.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import org.apache.jasper.Constants;
import org.apache.jasper.JasperException;
import org.apache.jasper.compiler.Localizer;
/**
* 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
*
* @author Mandar Raje
* @author Shawn Bayern
*/
public class JspRuntimeLibrary {
private static final String SERVLET_EXCEPTION
= "javax.servlet.error.exception";
private static final String JSP_EXCEPTION
= "javax.servlet.jsp.jspException";
protected static class PrivilegedIntrospectHelper
implements PrivilegedExceptionAction {
private Object bean;
private String prop;
private String value;
private ServletRequest request;
private String param;
private boolean ignoreMethodNF;
PrivilegedIntrospectHelper(Object bean, String prop,
String value, ServletRequest request,
String param, boolean ignoreMethodNF)
{
this.bean = bean;
this.prop = prop;
this.value = value;
this.request = request;
this.param = param;
this.ignoreMethodNF = ignoreMethodNF;
}
public Object run() throws JasperException {
internalIntrospecthelper(
bean,prop,value,request,param,ignoreMethodNF);
return null;
}
}
/**
* Returns the value of the javax.servlet.error.exception request
* attribute value, if present, otherwise the value of the
* javax.servlet.jsp.jspException request attribute value.
*
* This method is called at the beginning of the generated servlet code
* for a JSP error page, when the "exception" implicit scripting language
* variable is initialized.
*/
public static Throwable getThrowable(ServletRequest request) {
Throwable error = (Throwable) request.getAttribute(SERVLET_EXCEPTION);
if (error == null) {
error = (Throwable) request.getAttribute(JSP_EXCEPTION);
if (error != null) {
/*
* The only place that sets JSP_EXCEPTION is
* PageContextImpl.handlePageException(). It really should set
* SERVLET_EXCEPTION, but that would interfere with the
* ErrorReportValve. Therefore, if JSP_EXCEPTION is set, we
* need to set SERVLET_EXCEPTION.
*/
request.setAttribute(SERVLET_EXCEPTION, error);
}
}
return error;
}
public static boolean coerceToBoolean(String s) {
if (s == null || s.length() == 0)
return false;
else
return Boolean.valueOf(s).booleanValue();
}
public static byte coerceToByte(String s) {
if (s == null || s.length() == 0)
return (byte) 0;
else
return Byte.valueOf(s).byteValue();
}
public static char coerceToChar(String s) {
if (s == null || s.length() == 0) {
return (char) 0;
} else {
// this trick avoids escaping issues
return (char)(int) s.charAt(0);
}
}
public static double coerceToDouble(String s) {
if (s == null || s.length() == 0)
return (double) 0;
else
return Double.valueOf(s).doubleValue();
}
public static float coerceToFloat(String s) {
if (s == null || s.length() == 0)
return (float) 0;
else
return Float.valueOf(s).floatValue();
}
public static int coerceToInt(String s) {
if (s == null || s.length() == 0)
return 0;
else
return Integer.valueOf(s).intValue();
}
public static short coerceToShort(String s) {
if (s == null || s.length() == 0)
return (short) 0;
else
return Short.valueOf(s).shortValue();
}
public static long coerceToLong(String s) {
if (s == null || s.length() == 0)
return (long) 0;
else
return Long.valueOf(s).longValue();
}
public static Object coerce(String s, Class target) {
boolean isNullOrEmpty = (s == null || s.length() == 0);
if (target == Boolean.class) {
if (isNullOrEmpty) {
s = "false";
}
return new Boolean(s);
} else if (target == Byte.class) {
if (isNullOrEmpty)
return new Byte((byte) 0);
else
return new Byte(s);
} else if (target == Character.class) {
if (isNullOrEmpty)
return new Character((char) 0);
else
return new Character(s.charAt(0));
} else if (target == Double.class) {
if (isNullOrEmpty)
return new Double(0);
else
return new Double(s);
} else if (target == Float.class) {
if (isNullOrEmpty)
return new Float(0);
else
return new Float(s);
} else if (target == Integer.class) {
if (isNullOrEmpty)
return new Integer(0);
else
return new Integer(s);
} else if (target == Short.class) {
if (isNullOrEmpty)
return new Short((short) 0);
else
return new Short(s);
} else if (target == Long.class) {
if (isNullOrEmpty)
return new Long(0);
else
return new Long(s);
} else {
return null;
}
}
// __begin convertMethod
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) ) {
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) {
throw new JasperException(ex);
}
}
// __end convertMethod
// __begin introspectMethod
public static void introspect(Object bean, ServletRequest request)
throws JasperException
{
Enumeration e = request.getParameterNames();
while ( e.hasMoreElements() ) {
String name = (String) e.nextElement();
String value = request.getParameter(name);
introspecthelper(bean, name, value, request, name, true);
}
}
// __end introspectMethod
// __begin introspecthelperMethod
public static void introspecthelper(Object bean, String prop,
String value, ServletRequest request,
String param, boolean ignoreMethodNF)
throws JasperException
{
if( Constants.IS_SECURITY_ENABLED ) {
try {
PrivilegedIntrospectHelper dp =
new PrivilegedIntrospectHelper(
bean,prop,value,request,param,ignoreMethodNF);
AccessController.doPrivileged(dp);
} catch( PrivilegedActionException pe) {
Exception e = pe.getException();
throw (JasperException)e;
}
} else {
internalIntrospecthelper(
bean,prop,value,request,param,ignoreMethodNF);
}
}
private static void internalIntrospecthelper(Object bean, String prop,
String value, ServletRequest request,
String param, boolean ignoreMethodNF)
throws JasperException
{
Method method = null;
Class type = null;
Class propertyEditorClass = null;
try {
java.beans.BeanInfo info
= java.beans.Introspector.getBeanInfo(bean.getClass());
if ( info != null ) {
java.beans.PropertyDescriptor pd[]
= info.getPropertyDescriptors();
for (int i = 0 ; i < pd.length ; i++) {
if ( pd[i].getName().equals(prop) ) {
method = pd[i].getWriteMethod();
type = pd[i].getPropertyType();
propertyEditorClass = pd[i].getPropertyEditorClass();
break;
}
}
}
if ( method != null ) {
if (type.isArray()) {
if (request == null) {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.setproperty.noindexset"));
}
Class t = type.getComponentType();
String[] values = request.getParameterValues(param);
//XXX Please check.
if(values == null) return;
if(t.equals(String.class)) {
method.invoke(bean, new Object[] { values });
} else {
Object tmpval = null;
createTypedArray (prop, bean, method, values, t,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -