Source Code Cross Referenced for CommonFns.java in  » Ajax » zk » org » zkoss » xel » fn » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » zk » org.zkoss.xel.fn 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.