Source Code Cross Referenced for GenericValue.java in  » Code-Analyzer » beautyJ » de » gulden » framework » amoda » generic » data » 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 » Code Analyzer » beautyJ » de.gulden.framework.amoda.generic.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Project: AMODA - Abstract Modeled Application
003:         * Class:   de.gulden.framework.amoda.generic.data.GenericValue
004:         * Version: snapshot-beautyj-1.1
005:         *
006:         * Date:    2004-09-29
007:         *
008:         * This is a snapshot version of the AMODA 0.2 development branch,
009:         * it is not released as a seperate version.
010:         * For AMODA, see http://amoda.berlios.de/.
011:         *  
012:         * This is licensed under the GNU Lesser General Public License (LGPL)
013:         * and comes with NO WARRANTY.
014:         *
015:         * Author:  Jens Gulden
016:         * Email:   amoda@jensgulden.de
017:         */
018:
019:        package de.gulden.framework.amoda.generic.data;
020:
021:        import de.gulden.framework.amoda.model.metadata.NamedValue;
022:        import java.awt.*;
023:        import java.io.*;
024:        import java.lang.*;
025:        import java.util.*;
026:
027:        /**
028:         * Class GenericValue.
029:         *  
030:         * @author  Jens Gulden
031:         * @version  snapshot-beautyj-1.1
032:         */
033:        public class GenericValue implements  NamedValue {
034:
035:            // ------------------------------------------------------------------------
036:            // --- fields                                                           ---
037:            // ------------------------------------------------------------------------
038:
039:            public static String[][] BOOLEAN_LITERALS = {
040:                    { "true", "yes", "on" }, { "false", "no", "off" } };
041:
042:            protected Class type;
043:
044:            protected Object object;
045:
046:            protected String name;
047:
048:            // ------------------------------------------------------------------------
049:            // --- constructors                                                     ---
050:            // ------------------------------------------------------------------------
051:
052:            public GenericValue() {
053:                // your code here
054:            }
055:
056:            public GenericValue(Object value) {
057:                this ();
058:                setType(value.getClass());
059:                set(value);
060:            }
061:
062:            // ------------------------------------------------------------------------
063:            // --- methods                                                          ---
064:            // ------------------------------------------------------------------------
065:
066:            public String getString() {
067:                return toString(get());
068:            }
069:
070:            public int getInt() {
071:                if (object instanceof  Number) {
072:                    return ((Number) object).intValue();
073:                } else if (object instanceof  String) {
074:                    try {
075:                        return Integer.valueOf((String) object).intValue();
076:                    } catch (NumberFormatException nfe) {
077:                        throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
078:                                "cannot convert " + ((String) object)
079:                                        + " to int");
080:                    }
081:                } else if (object == null) {
082:                    return 0;
083:                } else {
084:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
085:                            "cannot convert value of type "
086:                                    + object.getClass().getName() + " ["
087:                                    + object.toString() + "] to int");
088:                }
089:            }
090:
091:            public float getFloat() {
092:                if (object instanceof  Number) {
093:                    return ((Number) object).floatValue();
094:                } else if (object instanceof  String) {
095:                    try {
096:                        return Float.valueOf((String) object).floatValue();
097:                    } catch (NumberFormatException nfe) {
098:                        throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
099:                                "cannot convert " + ((String) object)
100:                                        + " to float");
101:                    }
102:                } else if (object == null) {
103:                    return 0.0f;
104:                } else {
105:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
106:                            "cannot convert value of type "
107:                                    + object.getClass().getName() + " ["
108:                                    + object.toString() + "] to float");
109:                }
110:            }
111:
112:            public double getDouble() {
113:                if (object instanceof  Number) {
114:                    return ((Number) object).doubleValue();
115:                } else if (object instanceof  String) {
116:                    try {
117:                        return Double.valueOf((String) object).doubleValue();
118:                    } catch (NumberFormatException nfe) {
119:                        throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
120:                                "cannot convert " + ((String) object)
121:                                        + " to double");
122:                    }
123:                } else if (object == null) {
124:                    return 0.0;
125:                } else {
126:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
127:                            "cannot convert value of type "
128:                                    + object.getClass().getName() + " ["
129:                                    + object.toString() + "] to double");
130:                }
131:            }
132:
133:            public boolean getBoolean() {
134:                if (object instanceof  Boolean) {
135:                    return ((Boolean) object).booleanValue();
136:                } else if (object instanceof  String) {
137:                    String boolString = (String) object;
138:                    return isBooleanLiteral(true, boolString);
139:                } else if (object instanceof  Number) {
140:                    return (((Number) object).doubleValue() != 0.0);
141:                } else if (object == null) {
142:                    return false;
143:                } else {
144:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
145:                            "cannot convert value of type "
146:                                    + object.getClass().getName() + " ["
147:                                    + object.toString() + "] to boolean");
148:                }
149:            }
150:
151:            public boolean isTypeAvailable(Class type) {
152:                // ...**** quick and drity ****************************************************
153:                if ((object != null)
154:                        && (type.isAssignableFrom(object.getClass()))) {
155:                    return true;
156:                } else if (type.isAssignableFrom(this .type)) {
157:                    return true;
158:                } else if (String.class.isAssignableFrom(type)) {
159:                    return true; // everything can be converted to string somehow
160:                } else if (Integer.class.isAssignableFrom(type)
161:                        || Double.class.isAssignableFrom(type)) {
162:                    return Number.class.isAssignableFrom(this .type);
163:                } else if (Boolean.class.isAssignableFrom(type)) {
164:                    return Boolean.class.isAssignableFrom(this .type)
165:                            || Number.class.isAssignableFrom(this .type)
166:                            || String.class.isAssignableFrom(this .type);
167:                } else {
168:                    return false;
169:                }
170:            }
171:
172:            public void parseString(String s) {
173:                if (type != null) { // type already known
174:                    try {
175:                        if (String.class.isAssignableFrom(type)) {
176:                            object = s;
177:                        } else if (Boolean.class.isAssignableFrom(type)) {
178:                            object = isBooleanLiteral(true, s.trim()) ? Boolean.TRUE
179:                                    : Boolean.FALSE; // ### Fix: make backward-compatible to Jdk1.2. Jens, 2002-10-27
180:                        } else if (Integer.class.isAssignableFrom(type)) {
181:                            object = Integer.valueOf(s.trim());
182:                        } else if (Float.class.isAssignableFrom(type)) {
183:                            object = Float.valueOf(s.trim());
184:                        } else if (Double.class.isAssignableFrom(type)) {
185:                            object = Double.valueOf(s.trim());
186:                        } else if (Class.class.isAssignableFrom(type)) {
187:                            try {
188:                                object = Class.forName(s.trim());
189:                            } catch (ClassNotFoundException cnfe) {
190:                                throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
191:                                        "string '"
192:                                                + s.trim()
193:                                                + "' is not a valid class name, class not found");
194:                            }
195:                        } else if (File.class.isAssignableFrom(type)) {
196:                            object = (new File(s.trim()));//.getCanonicalFile(); // test validity - disabled, allow empty string as path
197:                        } else if (Color.class.isAssignableFrom(type)) {
198:                            object = de.gulden.util.Toolbox
199:                                    .parseColor(s.trim());
200:                            if (object == null) {
201:                                throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
202:                                        "string '" + s
203:                                                + "' is not a valid color");
204:                            }
205:                        } else {
206:                            throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
207:                                    "cannot handle type '" + type.getName()
208:                                            + "' for parsing '" + s + "'");
209:                        }
210:                    } catch (NumberFormatException nfe) {
211:                        throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
212:                                "cannot parse string '" + s
213:                                        + "' as value for type "
214:                                        + type.getName());
215:                    }
216:                } else { // find out type from string
217:                    // boolean?
218:                    if (s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("true")
219:                            || s.equalsIgnoreCase("on")) {
220:                        set(Boolean.TRUE);
221:                        return;
222:                    } else if (s.equalsIgnoreCase("no")
223:                            || s.equalsIgnoreCase("false")
224:                            || s.equalsIgnoreCase("off")) {
225:                        set(Boolean.FALSE);
226:                        return;
227:                    }
228:                    // number?
229:                    try {
230:                        double d = Double.parseDouble(s);
231:                        double dRound = Math.round(d);
232:                        if (d == dRound) { // integer
233:                            set(new Integer((int) dRound));
234:                        } else { // real double
235:                            set(new Double(d));
236:                        }
237:                        return;
238:                    } catch (NumberFormatException nfe) {
239:                        /*// file?
240:                        java.io.File file=new java.io.File(s);
241:                        if (file.exists()) {
242:                            set(file);
243:                        } else {
244:                            // no special type found: use as plain string
245:                            set(s);
246:                        }*/
247:                        set(s); // treat as String type
248:                    }
249:                }
250:            }
251:
252:            public String toString(Object o) {
253:                if (object instanceof  Color) {
254:                    return de.gulden.util.Toolbox.toString((Color) object);
255:                } else if (object instanceof  File) {
256:                    return ((File) object).getAbsolutePath();
257:                } else if (o != null) {
258:                    return o.toString();
259:                } else {
260:                    return null;
261:                }
262:            }
263:
264:            public File getFile() {
265:                if (object instanceof  File) {
266:                    return ((File) object);
267:                } else if (object instanceof  String) {
268:                    File f = new File((String) object);
269:                    try {
270:                        f = f.getCanonicalFile();
271:                        return f;
272:                    } catch (java.io.IOException ioe) {
273:                        throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
274:                                "cannot convert value of type "
275:                                        + object.getClass().getName() + " ["
276:                                        + object.toString() + "] to File");
277:                    }
278:                } else if (object == null) {
279:                    return null;
280:                } else {
281:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
282:                            "cannot convert value of type "
283:                                    + object.getClass().getName() + " ["
284:                                    + object.toString() + "] to File");
285:                }
286:            }
287:
288:            public Color getColor() {
289:                if (object instanceof  java.awt.Color) {
290:                    return ((Color) object);
291:                } else if (object instanceof  String) {
292:                    Color c = de.gulden.util.Toolbox
293:                            .parseColor((String) object);
294:                    if (c != null) {
295:                        return c;
296:                    } else {
297:                        throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
298:                                "cannot convert '" + ((String) object)
299:                                        + " to Color");
300:                    }
301:                } else if (object == null) {
302:                    return null;
303:                } else {
304:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
305:                            "cannot convert value of type "
306:                                    + object.getClass().getName() + " ["
307:                                    + object.toString() + "] to Color");
308:                }
309:            }
310:
311:            public Class getType() {
312:                return type;
313:            }
314:
315:            public void setType(Class _type) {
316:                type = _type;
317:            }
318:
319:            public Object get() {
320:                return object;
321:            }
322:
323:            public Object set(Object _object) {
324:                Object old = object;
325:                if ((type == null) && (_object != null)) {
326:                    type = _object.getClass();
327:                }
328:                object = _object;
329:                return old;
330:            }
331:
332:            public String getName() {
333:                return name;
334:            }
335:
336:            public void setName(String _name) {
337:                name = _name;
338:            }
339:
340:            public Class getClassType() {
341:                if (object instanceof  Class) {
342:                    return ((Class) object);
343:                } else if (object instanceof  String) {
344:                    try {
345:                        Class c = Class.forName((String) object);
346:                        return c;
347:                    } catch (ClassNotFoundException cnfe) {
348:                        throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
349:                                "cannot convert '" + ((String) object)
350:                                        + " to Class");
351:                    }
352:                } else if (object == null) {
353:                    return null;
354:                } else {
355:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
356:                            "cannot convert value of type "
357:                                    + object.getClass().getName() + " ["
358:                                    + object.toString() + "] to Class");
359:                }
360:            }
361:
362:            public Object getClassInstance() {
363:                Class cl = getClassType();
364:                try {
365:                    return cl.newInstance();
366:                } catch (InstantiationException ie) {
367:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
368:                            "cannot create instance of " + cl.getName() + ": "
369:                                    + ie.getMessage());
370:                } catch (IllegalAccessException iae) {
371:                    throw new de.gulden.framework.amoda.model.data.IllegalTypeError(
372:                            "cannot create instance of " + cl.getName() + ": "
373:                                    + iae.getMessage());
374:                }
375:            }
376:
377:            // ------------------------------------------------------------------------
378:            // --- static method                                                    ---
379:            // ------------------------------------------------------------------------
380:
381:            public static boolean isBooleanLiteral(boolean booleanValue,
382:                    String s) {
383:                int b = booleanValue ? 0 : 1;
384:                for (int i = 0; i < BOOLEAN_LITERALS[b].length; i++) {
385:                    if (s.equalsIgnoreCase(BOOLEAN_LITERALS[b][i])) {
386:                        return true;
387:                    }
388:                }
389:                return false;
390:            }
391:
392:        } // end GenericValue
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.