001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.template;
014:
015: import java.awt.Color;
016: import java.io.InputStream;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.wings.Resource;
021: import org.wings.SDimension;
022: import org.wings.SFont;
023: import org.wings.SIcon;
024: import org.wings.plaf.ComponentCG;
025: import org.wings.plaf.ResourceFactory;
026: import org.wings.resource.ClassPathResource;
027: import org.wings.style.CSSAttributeSet;
028: import org.wings.style.CSSStyleSheet;
029: import org.wings.style.StyleSheet;
030:
031: /**
032: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
033: */
034: public class DefaultPropertyValueConverter implements
035: PropertyValueConverter {
036:
037: public static final DefaultPropertyValueConverter INSTANCE = new DefaultPropertyValueConverter();
038:
039: private final transient static Log log = LogFactory
040: .getLog(DefaultPropertyValueConverter.class);
041:
042: public DefaultPropertyValueConverter() {
043:
044: }
045:
046: // implementation of org.wings.template.PropertyValueConverter interface
047:
048: /**
049: * Describe <code>convertPropertyValue</code> method here.
050: *
051: * @param value an <code>Object</code> value
052: * @param targetClass a <code>Class</code> value
053: * @return <description>
054: * @throws UnsupportedOperationException if an error occurs
055: * @throws java.lang.UnsupportedOperationException
056: * <description>
057: */
058: public Object convertPropertyValue(String value, Class targetClass)
059: throws UnsupportedOperationException {
060: if (value == null || "null".equals(value)) {
061: return null;
062: } // end of if ()
063:
064: if (targetClass == String.class) {
065: return value;
066: } // end of if ()
067:
068: if (targetClass == Boolean.TYPE || targetClass == Boolean.class) {
069: return Boolean.valueOf(value);
070: }
071:
072: if (targetClass == Integer.TYPE || targetClass == Integer.class) {
073: return Integer.valueOf(value);
074: }
075: if (targetClass == Long.TYPE || targetClass == Long.class) {
076: return Long.valueOf(value);
077: }
078: if (targetClass == Short.TYPE || targetClass == Short.class) {
079: return Short.valueOf(value);
080: }
081: if (targetClass == Byte.TYPE || targetClass == Byte.class) {
082: return Byte.valueOf(value);
083: }
084: if (targetClass == Float.TYPE || targetClass == Float.class) {
085: return Float.valueOf(value);
086: }
087: if (targetClass == Double.TYPE || targetClass == Double.class) {
088: return Double.valueOf(value);
089: }
090: if (targetClass == Character.TYPE
091: || targetClass == Character.class) {
092: return new Character(value.charAt(0));
093: }
094:
095: if (targetClass == StringBuffer.class) {
096: return new StringBuffer(value);
097: } // end of if ()
098:
099: if (SIcon.class.isAssignableFrom(targetClass)) {
100: return ResourceFactory.makeIcon(value);
101: }
102:
103: if (targetClass == Color.class) {
104: return ResourceFactory.makeColor(value);
105: }
106:
107: if (targetClass == SDimension.class) {
108: return ResourceFactory.makeDimension(value);
109: }
110:
111: if (targetClass == SFont.class) {
112: return TemplateUtil.parseFont(value);
113: }
114:
115: if (Resource.class.isAssignableFrom(targetClass)) {
116: return new ClassPathResource(value);
117: }
118:
119: if (CSSAttributeSet.class.isAssignableFrom(targetClass)) {
120: return ResourceFactory.makeAttributeSet(value);
121: }
122:
123: if (StyleSheet.class.isAssignableFrom(targetClass)) {
124: StyleSheet result;
125: try {
126: CSSStyleSheet styleSheet = new CSSStyleSheet();
127: InputStream in = Thread.currentThread()
128: .getContextClassLoader().getResourceAsStream(
129: 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 ResourceFactory.makeComponentCG(value);
142: }
143:
144: throw new UnsupportedOperationException(
145: "cannot create object of type " + targetClass.getName());
146: }
147:
148: }
|