001: /* CommonFns.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Apr 20 18:35:21 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.xel.fn;
020:
021: import java.util.Collection;
022: import java.util.Map;
023: import java.lang.reflect.Field;
024: import java.math.BigDecimal;
025:
026: import org.zkoss.lang.Classes;
027: import org.zkoss.mesg.Messages;
028: import org.zkoss.util.resource.Labels;
029: import org.zkoss.util.logging.Log;
030:
031: /**
032: * Functions used with EL.
033: *
034: * @author tomyeh
035: */
036: public class CommonFns {
037: private static final Log log = Log.lookup(CommonFns.class);
038:
039: protected CommonFns() {
040: }
041:
042: /** Converts the specified object to a boolean.
043: */
044: public static boolean toBoolean(Object val) {
045: return ((Boolean) Classes.coerce(boolean.class, val))
046: .booleanValue();
047: }
048:
049: /** Converts the specified object to a string.
050: */
051: public static String toString(Object val) {
052: return (String) Classes.coerce(String.class, val);
053: }
054:
055: /** Converts the specified object to a number.
056: */
057: public static Number toNumber(Object val) {
058: return (Number) Classes.coerce(Number.class, val);
059: }
060:
061: /** Converts the specified object to an integer.
062: */
063: public static int toInt(Object val) {
064: return ((Integer) Classes.coerce(int.class, val)).intValue();
065: }
066:
067: /** Converts the specified object to a (big) decimal.
068: */
069: public static BigDecimal toDecimal(Object val) {
070: return (BigDecimal) Classes.coerce(BigDecimal.class, val);
071: }
072:
073: /** Converts the specified object to an character.
074: */
075: public static char toChar(Object val) {
076: return ((Character) Classes.coerce(char.class, val))
077: .charValue();
078: }
079:
080: /** Tests whehter an object, o, is an instance of a class, c.
081: */
082: public static boolean isInstance(Object c, Object o) {
083: if (c instanceof Class) {
084: return ((Class) c).isInstance(o);
085: } else if (c instanceof String) {
086: try {
087: return Classes.forNameByThread((String) c)
088: .isInstance(o);
089: } catch (ClassNotFoundException ex) {
090: throw new IllegalArgumentException("Class not found: "
091: + c);
092: }
093: } else {
094: throw new IllegalArgumentException("Unknown class: " + c);
095: }
096: }
097:
098: /** Returns the label or message of the specified key.
099: * <ul>
100: * <li>If key is "mesg:class:MMM", Messages.get(class.MMM) is called</li>
101: * <li>Otherwise, {@link Labels#getLabel} is called.
102: * </ul>
103: */
104: public static final String getLabel(String key) {
105: if (key == null)
106: return "";
107:
108: if (key.startsWith("mesg:")) {
109: final int j = key.lastIndexOf(':');
110: if (j > 5) {
111: final String clsnm = key.substring(5, j);
112: final String fldnm = key.substring(j + 1);
113: try {
114: final Class cls = Classes.forNameByThread(clsnm);
115: final Field fld = cls.getField(fldnm);
116: return Messages.get(((Integer) fld.get(null))
117: .intValue());
118: } catch (ClassNotFoundException ex) {
119: log.warning("Class not found: " + clsnm, ex);
120: } catch (NoSuchFieldException ex) {
121: log.warning("Field not found: " + fldnm, ex);
122: } catch (IllegalAccessException ex) {
123: log.warning("Field not accessible: " + fldnm, ex);
124: }
125: } else if (log.debugable()) {
126: log.debug("Not a valid format: " + key);
127: }
128: }
129: return Labels.getLabel(key);
130: }
131:
132: /** Returns the length of an array, string, collection or map.
133: */
134: public static final int length(Object o) {
135: if (o instanceof String) {
136: return ((String) o).length();
137: } else if (o == null) {
138: return 0;
139: } else if (o instanceof Collection) {
140: return ((Collection) o).size();
141: } else if (o instanceof Map) {
142: return ((Map) o).size();
143: } else if (o instanceof Object[]) {
144: return ((Object[]) o).length;
145: } else if (o instanceof int[]) {
146: return ((int[]) o).length;
147: } else if (o instanceof long[]) {
148: return ((long[]) o).length;
149: } else if (o instanceof short[]) {
150: return ((short[]) o).length;
151: } else if (o instanceof byte[]) {
152: return ((byte[]) o).length;
153: } else if (o instanceof char[]) {
154: return ((char[]) o).length;
155: } else if (o instanceof double[]) {
156: return ((double[]) o).length;
157: } else if (o instanceof float[]) {
158: return ((float[]) o).length;
159: } else {
160: throw new IllegalArgumentException(
161: "Unknown object for length: " + o.getClass());
162: }
163: }
164:
165: /** Instantiates the specified class.
166: */
167: public static final Object new_(Object o) throws Exception {
168: if (o instanceof String) {
169: return Classes.newInstanceByThread((String) o);
170: } else if (o instanceof Class) {
171: return ((Class) o).newInstance();
172: } else {
173: throw new IllegalArgumentException(
174: "Unknow object for new: " + o);
175: }
176: }
177: }
|