KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > template > DefaultPropertyValueConverter


1 /*
2  * $Id: DefaultPropertyValueConverter.java,v 1.6 2005/05/27 09:17:36 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.template;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.Resource;
19 import org.wings.SDimension;
20 import org.wings.SFont;
21 import org.wings.SIcon;
22 import org.wings.plaf.ComponentCG;
23 import org.wings.plaf.LookAndFeel;
24 import org.wings.resource.ClasspathResource;
25 import org.wings.style.CSSAttributeSet;
26 import org.wings.style.CSSStyleSheet;
27 import org.wings.style.StyleSheet;
28
29 import java.awt.*;
30 import java.io.InputStream JavaDoc;
31
32
33 /**
34  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
35  * @version $Revision: 1.6 $
36  */

37 public class DefaultPropertyValueConverter implements PropertyValueConverter {
38
39     public static final DefaultPropertyValueConverter INSTANCE =
40             new DefaultPropertyValueConverter();
41
42     private final transient static Log log = LogFactory.getLog(DefaultPropertyValueConverter.class);
43
44
45     public DefaultPropertyValueConverter() {
46
47     }
48     // implementation of org.wings.template.PropertyValueConverter interface
49

50     /**
51      * Describe <code>convertPropertyValue</code> method here.
52      *
53      * @param value an <code>Object</code> value
54      * @param targetClass a <code>Class</code> value
55      * @return <description>
56      * @throws UnsupportedOperationException if an error occurs
57      * @throws java.lang.UnsupportedOperationException
58      * <description>
59      */

60     public Object JavaDoc convertPropertyValue(String JavaDoc value, Class JavaDoc targetClass)
61             throws UnsupportedOperationException JavaDoc {
62         if (value == null || "null".equals(value)) {
63             return null;
64         } // end of if ()
65

66
67         if (targetClass == String JavaDoc.class) {
68             return value;
69         } // end of if ()
70

71         if (targetClass == Boolean.TYPE || targetClass == Boolean JavaDoc.class) {
72             return Boolean.valueOf(value);
73         }
74
75         if (targetClass == Integer.TYPE || targetClass == Integer JavaDoc.class) {
76             return Integer.valueOf(value);
77         }
78         if (targetClass == Long.TYPE || targetClass == Long JavaDoc.class) {
79             return Long.valueOf(value);
80         }
81         if (targetClass == Short.TYPE || targetClass == Short JavaDoc.class) {
82             return Short.valueOf(value);
83         }
84         if (targetClass == Byte.TYPE || targetClass == Byte JavaDoc.class) {
85             return Byte.valueOf(value);
86         }
87         if (targetClass == Float.TYPE || targetClass == Float JavaDoc.class) {
88             return Float.valueOf(value);
89         }
90         if (targetClass == Double.TYPE || targetClass == Double JavaDoc.class) {
91             return Double.valueOf(value);
92         }
93         if (targetClass == Character.TYPE || targetClass == Character JavaDoc.class) {
94             return new Character JavaDoc(value.charAt(0));
95         }
96
97         if (targetClass == StringBuffer JavaDoc.class) {
98             return new StringBuffer JavaDoc(value);
99         } // end of if ()
100

101         if (SIcon.class.isAssignableFrom(targetClass)) {
102             return LookAndFeel.makeIcon(value);
103         }
104
105         if (targetClass == Color.class) {
106             return LookAndFeel.makeColor(value);
107         }
108
109         if (targetClass == SDimension.class) {
110             return LookAndFeel.makeDimension(value);
111         }
112
113         if (targetClass == SFont.class) {
114             return TemplateUtil.parseFont(value);
115         }
116
117         if (Resource.class.isAssignableFrom(targetClass)) {
118             return new ClasspathResource(value);
119         }
120
121         if (CSSAttributeSet.class.isAssignableFrom(targetClass)) {
122             return LookAndFeel.makeAttributeSet(value);
123         }
124
125         if (StyleSheet.class.isAssignableFrom(targetClass)) {
126             StyleSheet result;
127             try {
128                 CSSStyleSheet styleSheet = new CSSStyleSheet();
129                 InputStream JavaDoc in = getClass().getClassLoader().getResourceAsStream(value);
130                 styleSheet.read(in);
131                 in.close();
132                 result = styleSheet;
133             } catch (Exception JavaDoc e) {
134                 log.warn("Exception", e);
135                 result = null;
136             }
137             return result;
138         }
139
140         if (ComponentCG.class.isAssignableFrom(targetClass)) {
141             return LookAndFeel.makeCG(value);
142         }
143
144         throw new UnsupportedOperationException JavaDoc("cannot create object of type " +
145                 targetClass.getName());
146     }
147
148
149 }
Popular Tags