1 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 ; 31 32 33 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 50 60 public Object convertPropertyValue(String value, Class targetClass) 61 throws UnsupportedOperationException { 62 if (value == null || "null".equals(value)) { 63 return null; 64 } 66 67 if (targetClass == String .class) { 68 return value; 69 } 71 if (targetClass == Boolean.TYPE || targetClass == Boolean .class) { 72 return Boolean.valueOf(value); 73 } 74 75 if (targetClass == Integer.TYPE || targetClass == Integer .class) { 76 return Integer.valueOf(value); 77 } 78 if (targetClass == Long.TYPE || targetClass == Long .class) { 79 return Long.valueOf(value); 80 } 81 if (targetClass == Short.TYPE || targetClass == Short .class) { 82 return Short.valueOf(value); 83 } 84 if (targetClass == Byte.TYPE || targetClass == Byte .class) { 85 return Byte.valueOf(value); 86 } 87 if (targetClass == Float.TYPE || targetClass == Float .class) { 88 return Float.valueOf(value); 89 } 90 if (targetClass == Double.TYPE || targetClass == Double .class) { 91 return Double.valueOf(value); 92 } 93 if (targetClass == Character.TYPE || targetClass == Character .class) { 94 return new Character (value.charAt(0)); 95 } 96 97 if (targetClass == StringBuffer .class) { 98 return new StringBuffer (value); 99 } 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 in = getClass().getClassLoader().getResourceAsStream(value); 130 styleSheet.read(in); 131 in.close(); 132 result = styleSheet; 133 } catch (Exception 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 ("cannot create object of type " + 145 targetClass.getName()); 146 } 147 148 149 } | Popular Tags |