expr.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,371 行 · 第 1/3 页

JAVA
1,371
字号
        break;      default:        os.print((char) ch);        break;      }    }  }  /**   * Write to the stream.   *   * @param out the output stream   * @param value the value to be written.   */  public static void toStreamEscaped(Writer out, Object value)    throws IOException  {    if (value == null)      return;    else if (value instanceof Reader) {      toStreamEscaped(out, (Reader) value);      return;    }    String string = value.toString();    int length = string.length();    for (int i = 0; i < length; i++) {      int ch = string.charAt(i);      switch (ch) {      case '<':        out.write("&lt;");        break;      case '>':        out.write("&gt;");        break;      case '&':        out.write("&amp;");        break;      case '\'':        out.write("&#039;");        break;      case '"':        out.write("&#034;");        break;      default:        out.write((char) ch);        break;      }    }  }  /**   * Write to the stream escaping XML reserved characters.   *   * @param out the output stream.   * @param value the value to be converted.   */  public static void toStreamEscaped(WriteStream out, Object value)    throws IOException  {    if (value == null)      return;    String string = value.toString();    int length = string.length();    for (int i = 0; i < length; i++) {      int ch = string.charAt(i);      switch (ch) {      case '<':        out.print("&lt;");        break;      case '>':        out.print("&gt;");        break;      case '&':        out.print("&amp;");        break;      case '\'':        out.print("&#039;");        break;      case '"':        out.print("&#034;");        break;      default:        out.print((char) ch);        break;      }    }  }  /**   * Write to the stream escaping XML reserved characters.   *   * @param out the output stream.   * @param value the value to be converted.   */  public static void toStreamEscaped(Writer out, Reader in)    throws IOException  {    if (in == null)      return;        int ch;    while ((ch = in.read()) >= 0) {      switch (ch) {      case '<':        out.write("&lt;");        break;      case '>':        out.write("&gt;");        break;      case '&':        out.write("&amp;");        break;      case '\'':        out.write("&#039;");        break;      case '"':        out.write("&#034;");        break;      default:        out.write((char) ch);        break;      }    }  }    /**   * Write to the *.java stream escaping Java reserved characters.   *   * @param out the output stream to the *.java code.   *   * @param value the value to be converted.   */  public static void printEscaped(WriteStream os, ReadStream is)    throws IOException  {    int ch;        while ((ch = is.readChar()) >= 0) {      switch (ch) {      case '\\':        os.print("\\\\");        break;      case '\n':        os.print("\\n");        break;      case '\r':        os.print("\\r");        break;      case '\"':        os.print("\\\"");        break;      default:        os.print((char) ch);        break;      }    }  }  // XXX: does this belong in JSP?  public static void setProperty(Object target, String property, Object value)    throws ELException, JspException  {    if (target instanceof Map) {      Map<String,Object> map = (Map) target;            if (value != null)        map.put(property, value);      else        map.remove(property);    }    else if (target != null) {      Method method = null;      try {        method = BeanUtil.getSetMethod(target.getClass(), property);      } catch (Exception e) {        throw new JspException(e);      }      if (method == null)        throw new JspException(L.l("can't find property `{0}' in `{1}'",                                  property, target.getClass()));      Class type = method.getParameterTypes()[0];      try {	int code = _typeMap.get(type);	switch (code) {	case BOOLEAN:	  value = toBoolean(value, null) ? Boolean.TRUE : Boolean.FALSE;	  break;	  	case BYTE:	  value = new Byte((byte) toLong(value, null));	  break;	  	case SHORT:	  value = new Short((short) toLong(value, null));	  break;	  	case INT:	  value = new Integer((int) toLong(value, null));	  break;	  	case LONG:	  value = new Long((long) toLong(value, null));	  break;	  	case FLOAT:	  value = new Float((float) toDouble(value, null));	  break;	  	case DOUBLE:	  value = new Double((double) toDouble(value, null));	  break;	  	case BOOLEAN_OBJ:	  if (value != null)	    value = toBoolean(value, null) ? Boolean.TRUE : Boolean.FALSE;	  break;	  	case BYTE_OBJ:	  if (value != null)	    value = new Byte((byte) toLong(value, null));	  break;	  	case SHORT_OBJ:	  if (value != null)	    value = new Short((short) toLong(value, null));	  break;	  	case INT_OBJ:	  if (value != null)	    value = new Integer((int) toLong(value, null));	  break;	  	case LONG_OBJ:	  if (value != null)	    value = new Long((long) toLong(value, null));	  break;	  	case FLOAT_OBJ:	  if (value != null)	    value = new Float((float) toDouble(value, null));	  break;	  	case DOUBLE_OBJ:	  if (value != null)	    value = new Double((double) toDouble(value, null));	  break;	case STRING:	  if (value != null)	    value = String.valueOf(value);	  break;	  	default:	  break;	}	        method.invoke(target, new Object[] { value });      } catch (Exception e) {        throw new JspException(e);      }    }    else {      // jsp/1c2l and JSTL TCK for exception type      throw new javax.servlet.jsp.JspException(L.l("null is an invalid c:set target value."));    }  }  protected static boolean isDoubleString(Object obj)  {    if (! (obj instanceof String))      return false;    String s = (String) obj;    int len = s.length();    for (int i = 0; i < len; i++) {      char ch = s.charAt(i);      if (ch == '.' || ch == 'e' || ch == 'E')        return true;    }    return false;  }    public static Object coerceToType(Object obj, Class<?> targetType)    throws ELException  {    CoerceType type = _coerceMap.get(targetType);    if (type == null)      return obj;    switch (type) {    case BOOLEAN:      return Expr.toBoolean(obj, null) ? Boolean.FALSE : Boolean.TRUE;    case CHARACTER:      return Expr.toCharacter(obj, null);    case BYTE:      return new Byte((byte) Expr.toLong(obj, null));    case SHORT:      return new Short((short) Expr.toLong(obj, null));    case INTEGER:      return new Integer((int) Expr.toLong(obj, null));    case LONG:      return new Long(Expr.toLong(obj, null));    case FLOAT:      return new Float((float) Expr.toDouble(obj, null));    case DOUBLE:      return new Double(Expr.toDouble(obj, null));    case STRING:      if (obj == null)	return "";      else	return obj.toString();    case BIG_DECIMAL:      return Expr.toBigDecimal(obj, null);    case BIG_INTEGER:      return Expr.toBigInteger(obj, null);    }    return null;  }  /**   * Returns an error object   */  public static Object error(Throwable e, ELContext env)    throws ELException  {    if (env == null) {      // jsp/1b56      throw new ELException(e);    }    else if (env instanceof ExprEnv && ((ExprEnv) env).isIgnoreException()) {      log.log(Level.FINE, e.toString(), e);      return null;    }    else if (e instanceof RuntimeException)      throw (RuntimeException) e;    else      throw new ELException(e);  }  public int hashCode()  {    return toString().hashCode();  }    public boolean equals(Object o)  {    if (this == o)      return true;    else if (! (o instanceof Expr))      return false;    return toString().equals(o.toString());  }  abstract public String toString();  /**   * Returns an error object   */  public static Object invocationError(Throwable e)    throws ELException  {    if (e instanceof InvocationTargetException && e.getCause() != null)      e = e.getCause();    if (e instanceof RuntimeException)      throw (RuntimeException) e;    else if (e instanceof Error)      throw (Error) e;    else      throw new ELException(e);  }  private enum CoerceType {    BOOLEAN,    CHARACTER,    STRING,    INTEGER,    DOUBLE,    LONG,    FLOAT,    SHORT,    BYTE,    BIG_INTEGER,    BIG_DECIMAL,    VOID  };  static {    _coerceMap.put(boolean.class, CoerceType.BOOLEAN);    _coerceMap.put(Boolean.class, CoerceType.BOOLEAN);        _coerceMap.put(byte.class, CoerceType.BYTE);    _coerceMap.put(Byte.class, CoerceType.BYTE);        _coerceMap.put(short.class, CoerceType.SHORT);    _coerceMap.put(Short.class, CoerceType.SHORT);        _coerceMap.put(int.class, CoerceType.INTEGER);    _coerceMap.put(Integer.class, CoerceType.INTEGER);        _coerceMap.put(long.class, CoerceType.LONG);    _coerceMap.put(Long.class, CoerceType.LONG);        _coerceMap.put(float.class, CoerceType.FLOAT);    _coerceMap.put(Float.class, CoerceType.FLOAT);        _coerceMap.put(double.class, CoerceType.DOUBLE);    _coerceMap.put(Double.class, CoerceType.DOUBLE);        _coerceMap.put(char.class, CoerceType.CHARACTER);    _coerceMap.put(Character.class, CoerceType.CHARACTER);        _coerceMap.put(String.class, CoerceType.STRING);        _coerceMap.put(BigDecimal.class, CoerceType.BIG_DECIMAL);    _coerceMap.put(BigInteger.class, CoerceType.BIG_INTEGER);        _coerceMap.put(void.class, CoerceType.VOID);  }  static {    _typeMap.put(boolean.class, BOOLEAN);    _typeMap.put(byte.class, BYTE);    _typeMap.put(short.class, SHORT);    _typeMap.put(int.class, INT);    _typeMap.put(long.class, LONG);    _typeMap.put(float.class, FLOAT);    _typeMap.put(double.class, DOUBLE);        _typeMap.put(Boolean.class, BOOLEAN_OBJ);    _typeMap.put(Byte.class, BYTE_OBJ);    _typeMap.put(Short.class, SHORT_OBJ);    _typeMap.put(Integer.class, INT_OBJ);    _typeMap.put(Long.class, LONG_OBJ);    _typeMap.put(Float.class, FLOAT_OBJ);    _typeMap.put(Double.class, DOUBLE_OBJ);        _typeMap.put(String.class, STRING);  }}

⌨️ 快捷键说明

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