expr.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,371 行 · 第 1/3 页
JAVA
1,371 行
} @Override public Class<?> getExpectedType() { return Object.class; } @Override public Class<?> getType(ELContext context) throws PropertyNotFoundException, ELException { Object value = getValue(context); if (value == null) return null; else return value.getClass(); } // // Static convenience methods // /** * Returns true for a double or double-equivalent. */ public static boolean isDouble(Object o) { if (o instanceof Double) return true; else if (o instanceof Float) return true; else if (! (o instanceof String)) return false; else { String s = (String) o; 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; } } /** * Converts some unknown value to a string. * * @param value the value to be converted. * * @return the string-converted value. */ public static String toStringWithNull(Object value, ELContext env) { if (value == null || value instanceof String) return (String) value; else return value.toString(); } /** * Converts some unknown value to a string. * * @param value the value to be converted. * * @return the string-converted value. */ public static String toString(Object value, ELContext env) { if (value == null) return ""; else if (value instanceof String) return (String) value; else return value.toString(); } /** * Converts some unknown value to a string. * * @param value the value to be converted. * * @return the string-converted value. */ public static String toString(long value, ELContext env) { return String.valueOf(value); } /** * Converts some unknown value to a string. * * @param value the value to be converted. * * @return the string-converted value. */ public static String toString(double value, ELContext env) { return String.valueOf(value); } /** * Converts some unknown value to a string. * * @param value the value to be converted. * * @return the string-converted value. */ public static String toString(boolean value, ELContext env) { return String.valueOf(value); } /** * Converts some unknown value to a string. * * @param value the value to be converted. * * @return the string-converted value. */ public static String toString(char value, ELContext env) { return String.valueOf(value); } /** * Converts some unknown value to a string. * * @param value the value to be converted. * * @return the string-converted value. */ public static char toCharacter(Object value, ELContext env) throws ELException { if (value == null) return (char) 0; else if (value instanceof Character) { return ((Character) value).charValue(); } else if (value instanceof String) { String s = (String) value; if (s == null || s.length() == 0) return (char) 0; else return s.charAt(0); } else if (value instanceof Number) { Number number = (Number) value; return (char) number.intValue(); } else if (value instanceof Boolean) { ELException e = new ELException(L.l("can't convert {0} to character.", value.getClass().getName())); throw e; /* error(e, env); return (char) 0; */ } else return (char) toLong(value, env); } /** * Converts some unknown value to a boolean. * * @param value the value to be converted. * * @return the boolean-converted value. */ public static boolean toBoolean(Object value, ELContext env) throws ELException { if (value == null || value.equals("")) return false; else if (value instanceof Boolean) return ((Boolean) value).booleanValue(); else if (value instanceof String) return value.equals("true") || value.equals("yes"); else { ELException e = new ELException(L.l("can't convert {0} to boolean.", value.getClass().getName())); // jsp/18s1 throw e; /* error(e, env); return false; */ } } /** * Converts some unknown value to a double. * * @param value the value to be converted. * * @return the double-converted value. */ public static double toDouble(Object value, ELContext env) throws ELException { if (value == null) return 0; else if (value instanceof Number) { double dValue = ((Number) value).doubleValue(); if (Double.isNaN(dValue)) return 0; else return dValue; } else if (value.equals("")) return 0; else if (value instanceof String) { double dValue = Double.parseDouble((String) value); if (Double.isNaN(dValue)) return 0; else return dValue; } else if (value instanceof Character) { // jsp/18s7 return ((Character) value).charValue(); } else { ELException e = new ELException(L.l("can't convert {0} to double.", value.getClass().getName())); // error(e, env); // return 0; throw e; } } /** * Converts some unknown value to a big decimal * * @param value the value to be converted. * * @return the BigDecimal-converted value. */ public static BigDecimal toBigDecimal(Object value, ELContext env) throws ELException { if (value == null) return BIG_DECIMAL_ZERO; else if (value instanceof BigDecimal) return (BigDecimal) value; else if (value instanceof Number) { double dValue = ((Number) value).doubleValue(); return new BigDecimal(dValue); } else if (value.equals("")) return BIG_DECIMAL_ZERO; else if (value instanceof String) { return new BigDecimal((String) value); } else if (value instanceof Character) { return new BigDecimal(((Character) value).charValue()); } else { ELException e = new ELException(L.l("can't convert {0} to BigDecimal.", value.getClass().getName())); error(e, env); return BIG_DECIMAL_ZERO; } } /** * Converts some unknown value to a big integer * * @param value the value to be converted. * * @return the BigInteger-converted value. */ public static BigInteger toBigInteger(Object value, ELContext env) throws ELException { if (value == null) return BIG_INTEGER_ZERO; else if (value instanceof BigInteger) return (BigInteger) value; else if (value instanceof Number) { // return new BigInteger(value.toString()); return new BigDecimal(value.toString()).toBigInteger(); } else if (value.equals("")) return BIG_INTEGER_ZERO; else if (value instanceof String) { return new BigInteger((String) value); } else if (value instanceof Character) { return new BigInteger(String.valueOf((int) ((Character) value).charValue())); } else { ELException e = new ELException(L.l("can't convert {0} to BigInteger.", value.getClass().getName())); error(e, env); return BIG_INTEGER_ZERO; } } /** * Converts some unknown value to a long. * * @param value the value to be converted. * * @return the long-converted value. */ public static long toLong(Object value, ELContext env) throws ELException { if (value == null) return 0; else if (value instanceof Number) return ((Number) value).longValue(); else if (value.equals("")) return 0; else if (value instanceof String) { try { return (long) Double.parseDouble((String) value); } catch (Exception e) { throw new ELException(e); } } else if (value instanceof Character) { // jsp/18s6 return ((Character) value).charValue(); } else { ELException e = new ELException(L.l("can't convert {0} to long.", value.getClass().getName())); // error(e, env); // return 0; throw e; } } /** * Write to the stream. * * @param out the output stream * @param value the value to be written. * * @return true for null */ public static boolean toStream(JspWriter out, Object value, boolean isEscaped) throws IOException { if (value == null) return true; if (isEscaped) toStreamEscaped(out, value); else toStream(out, value); return false; } /** * Write to the stream. * * @param out the output stream * @param value the value to be written. */ public static void toStream(WriteStream out, Object value) throws IOException { if (value == null) return; else if (value instanceof String) out.print((String) value); else if (value instanceof Reader) { out.writeStream((Reader) value); } else out.print(value.toString()); } /** * Write to the stream. * * @param out the output stream * @param value the value to be written. */ public static void toStream(JspWriter out, Object value) throws IOException { if (value == null) return; else if (value instanceof String) out.print((String) value); else if (value instanceof Reader) { Reader reader = (Reader) value; int ch; while ((ch = reader.read()) > 0) { out.print((char) ch); } } else out.print(value.toString()); } /** * 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 printEscapedString(WriteStream os, String string) throws IOException { int length = string.length(); for (int i = 0; i < length; i++) { char ch = string.charAt(i); switch (ch) { case '\\': os.print("\\\\"); break; case '\n': os.print("\\n"); break; case '\r': os.print("\\r"); break; case '\"': os.print("\\\"");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?