jsputil.java

来自「精通tomcat书籍原代码,希望大家共同学习」· Java 代码 · 共 1,109 行 · 第 1/3 页

JAVA
1,109
字号
	    }
	    buf.append(with);
	    begin = end + 1;
	}
	
	return buf.toString();
    }

    public static class ValidAttribute {
	String name;
	boolean mandatory;
	boolean rtexprvalue;	// not used now

	public ValidAttribute (String name, boolean mandatory,
            boolean rtexprvalue )
        {
	    this.name = name;
	    this.mandatory = mandatory;
            this.rtexprvalue = rtexprvalue;
        }

       public ValidAttribute (String name, boolean mandatory) {
            this( name, mandatory, false );
	}

	public ValidAttribute (String name) {
	    this (name, false);
	}
    }
    
    /**
     * Convert a String value to 'boolean'.
     * Besides the standard conversions done by
     * Boolean.valueOf(s).booleanValue(), the value "yes"
     * (ignore case) is also converted to 'true'. 
     * If 's' is null, then 'false' is returned.
     *
     * @param s the string to be converted
     * @return the boolean value associated with the string s
     */
    public static boolean booleanValue(String s) {
	boolean b = false;
	if (s != null) {
	    if (s.equalsIgnoreCase("yes")) {
		b = true;
	    } else {
		b = Boolean.valueOf(s).booleanValue();
	    }
	}
	return b;
    }

    /**
     * Returns the <tt>Class</tt> object associated with the class or
     * interface with the given string name.
     *
     * <p> The <tt>Class</tt> object is determined by passing the given string
     * name to the <tt>Class.forName()</tt> method, unless the given string
     * name represents a primitive type, in which case it is converted to a
     * <tt>Class</tt> object by appending ".class" to it (e.g., "int.class").
     */
    public static Class toClass(String type, ClassLoader loader)
	    throws ClassNotFoundException {

	Class c = null;
	int i0 = type.indexOf('[');
	int dims = 0;
	if (i0 > 0) {
	    // This is an array.  Count the dimensions
	    for (int i = 0; i < type.length(); i++) {
		if (type.charAt(i) == '[')
		    dims++;
	    }
	    type = type.substring(0, i0);
	}

	if ("boolean".equals(type))
	    c = boolean.class;
	else if ("char".equals(type))
	    c = char.class;
	else if ("byte".equals(type))
	    c =  byte.class;
	else if ("short".equals(type))
	    c = short.class;
	else if ("int".equals(type))
	    c = int.class;
	else if ("long".equals(type))
	    c = long.class;
	else if ("float".equals(type))
	    c = float.class;
	else if ("double".equals(type))
	    c = double.class;
	else if (type.indexOf('[') < 0)
	    c = loader.loadClass(type);

	if (dims == 0)
	    return c;

	if (dims == 1)
	    return java.lang.reflect.Array.newInstance(c, 1).getClass();

	// Array of more than i dimension
	return java.lang.reflect.Array.newInstance(c, new int[dims]).getClass();
    }

    /**
     * Produces a String representing a call to the EL interpreter.
     * @param expression a String containing zero or more "${}" expressions
     * @param expectedType the expected type of the interpreted result
     * @param fnmapvar Variable pointing to a function map.
     * @param XmlEscape True if the result should do XML escaping
     * @return a String representing a call to the EL interpreter.
     */
    public static String interpreterCall(boolean isTagFile,
					 String expression,
                                         Class expectedType,
                                         String fnmapvar,
                                         boolean XmlEscape ) 
    {
        /*
         * Determine which context object to use.
         */
	String jspCtxt = null;
	if (isTagFile)
	    jspCtxt = "this.getJspContext()";
	else
	    jspCtxt = "_jspx_page_context";

	/*
         * Determine whether to use the expected type's textual name
	 * or, if it's a primitive, the name of its correspondent boxed
	 * type.
         */
	String targetType = expectedType.getName();
	String primitiveConverterMethod = null;
	if (expectedType.isPrimitive()) {
	    if (expectedType.equals(Boolean.TYPE)) {
		targetType = Boolean.class.getName();
		primitiveConverterMethod = "booleanValue";
	    } else if (expectedType.equals(Byte.TYPE)) {
		targetType = Byte.class.getName();
		primitiveConverterMethod = "byteValue";
	    } else if (expectedType.equals(Character.TYPE)) {
		targetType = Character.class.getName();
		primitiveConverterMethod = "charValue";
	    } else if (expectedType.equals(Short.TYPE)) {
		targetType = Short.class.getName();
		primitiveConverterMethod = "shortValue";
	    } else if (expectedType.equals(Integer.TYPE)) {
		targetType = Integer.class.getName();
		primitiveConverterMethod = "intValue";
	    } else if (expectedType.equals(Long.TYPE)) {
		targetType = Long.class.getName();
		primitiveConverterMethod = "longValue";
	    } else if (expectedType.equals(Float.TYPE)) {
		targetType = Float.class.getName();
		primitiveConverterMethod = "floatValue";
	    } else if (expectedType.equals(Double.TYPE)) { 
		targetType = Double.class.getName();
		primitiveConverterMethod = "doubleValue";
	    }
	}
 
	if (primitiveConverterMethod != null) {
	    XmlEscape = false;
	}

	/*
         * Build up the base call to the interpreter.
         */
        // XXX - We use a proprietary call to the interpreter for now
        // as the current standard machinery is inefficient and requires
        // lots of wrappers and adapters.  This should all clear up once
        // the EL interpreter moves out of JSTL and into its own project.
        // In the future, this should be replaced by code that calls
        // ExpressionEvaluator.parseExpression() and then cache the resulting
        // expression objects.  The interpreterCall would simply select
        // one of the pre-cached expressions and evaluate it.
        // Note that PageContextImpl implements VariableResolver and
        // the generated Servlet/SimpleTag implements FunctionMapper, so
        // that machinery is already in place (mroth).
	targetType = toJavaSourceType(targetType);
	StringBuffer call = new StringBuffer(
             "(" + targetType + ") "
               + "org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate"
               + "(" + Generator.quote(expression) + ", "
               +       targetType + ".class, "
	       +       "(PageContext)" + jspCtxt 
               +       ", " + fnmapvar
	       + ", " + XmlEscape
               + ")");
 
	/*
         * Add the primitive converter method if we need to.
         */
	if (primitiveConverterMethod != null) {
	    call.insert(0, "(");
	    call.append(")." + primitiveConverterMethod + "()");
	}
 
	return call.toString();
    }

    /**
     * Validates the syntax of all ${} expressions within the given string.
     * @param where the approximate location of the expressions in the JSP page
     * @param expressions a string containing zero or more "${}" expressions
     * @param err an error dispatcher to use
     */
    public static void validateExpressions(Mark where,
                                           String expressions,
                                           Class expectedType,
                                           FunctionMapper functionMapper,
                                           ErrorDispatcher err)
            throws JasperException {

        try {
            JspUtil.expressionEvaluator.parseExpression( expressions, 
                expectedType, null );
        }
        catch( ELParseException e ) {
            err.jspError(where, "jsp.error.invalid.expression", expressions,
                e.toString() );
        }
        catch( ELException e ) {
            err.jspError(where, "jsp.error.invalid.expression", expressions,
                e.toString() );
        }
    }

    /**
     * Resets the temporary variable name.
     * (not thread-safe)
     */
    public static void resetTemporaryVariableName() {
        tempSequenceNumber = 0;
    }

    /**
     * Generates a new temporary variable name.
     * (not thread-safe)
     */
    public static String nextTemporaryVariableName() {
        return Constants.TEMP_VARIABLE_NAME_PREFIX + (tempSequenceNumber++);
    }

    public static String coerceToPrimitiveBoolean(String s,
						  boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToBoolean(" + s + ")";
	} else {
	    if (s == null || s.length() == 0)
		return "false";
	    else
		return Boolean.valueOf(s).toString();
	}
    }

    public static String coerceToBoolean(String s, boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "(Boolean) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Boolean.class)";
	} else {
	    if (s == null || s.length() == 0) {
		return "new Boolean(false)";
	    } else {
		// Detect format error at translation time
		return "new Boolean(" + Boolean.valueOf(s).toString() + ")";
	    }
	}
    }

    public static String coerceToPrimitiveByte(String s,
					       boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToByte(" + s + ")";
	} else {
	    if (s == null || s.length() == 0)
		return "(byte) 0";
	    else
		return "((byte)" + Byte.valueOf(s).toString() + ")";
	}
    }

    public static String coerceToByte(String s, boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "(Byte) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Byte.class)";
	} else {
	    if (s == null || s.length() == 0) {
		return "new Byte((byte) 0)";
	    } else {
		// Detect format error at translation time
		return "new Byte((byte)" + Byte.valueOf(s).toString() + ")";
	    }
	}
    }

    public static String coerceToChar(String s, boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToChar(" + s + ")";
	} else {
	    if (s == null || s.length() == 0) {
		return "(char) 0";
	    } else {
		char ch = s.charAt(0);
		// this trick avoids escaping issues
		return "((char) " + (int) ch + ")";
	    }
	}
    }

    public static String coerceToCharacter(String s, boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "(Character) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Character.class)";
	} else {
	    if (s == null || s.length() == 0) {
		return "new Character((char) 0)";
	    } else {
		char ch = s.charAt(0);
		// this trick avoids escaping issues
		return "new Character((char) " + (int) ch + ")";
	    }
	}
    }

    public static String coerceToPrimitiveDouble(String s,
						 boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToDouble(" + s + ")";
	} else {
	    if (s == null || s.length() == 0)
		return "(double) 0";
	    else
		return Double.valueOf(s).toString();
	}
    }

    public static String coerceToDouble(String s, boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "(Double) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Double.class)";
	} else {
	    if (s == null || s.length() == 0) {
		return "new Double(0)";
	    } else {
		// Detect format error at translation time
		return "new Double(" + Double.valueOf(s).toString() + ")";
	    }
	}
    }

    public static String coerceToPrimitiveFloat(String s,
						boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToFloat(" + s + ")";
	} else {
	    if (s == null || s.length() == 0)
		return "(float) 0";
	    else
		return Float.valueOf(s).toString() + "f";
	}
    }

    public static String coerceToFloat(String s, boolean isNamedAttribute) {
	if (isNamedAttribute) {
	    return "(Float) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Float.class)";
	} else {
	    if (s == null || s.length() == 0) {
		return "new Float(0)";
	    } else {
		// Detect format error at translation time
		return "new Float(" + Float.valueOf(s).toString() + "f)";

⌨️ 快捷键说明

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