Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 88716 invoked from network); 10 Aug 2006 14:11:15 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 10 Aug 2006 14:11:15 -0000 Received: (qmail 36336 invoked by uid 500); 10 Aug 2006 14:11:13 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 36260 invoked by uid 500); 10 Aug 2006 14:11:12 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 36245 invoked by uid 99); 10 Aug 2006 14:11:12 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Aug 2006 07:11:12 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS X-Spam-Check-By: apache.org Received: from [192.87.106.226] (HELO ajax.apache.org) (192.87.106.226) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Aug 2006 07:11:11 -0700 Received: from ajax.apache.org (localhost [127.0.0.1]) by ajax.apache.org (Postfix) with ESMTP id EA0D16ACA9 for ; Thu, 10 Aug 2006 15:10:43 +0100 (BST) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Apache Wiki To: commons-dev@jakarta.apache.org Date: Thu, 10 Aug 2006 14:10:43 -0000 Message-ID: <20060810141043.5044.37519@ajax.apache.org> Subject: [Jakarta-commons Wiki] Update of "Betwixt/TipsAndHints/CustomObjectStringConverter" by JesseSweetland X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Dear Wiki user, You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification. The following page has been changed by JesseSweetland: http://wiki.apache.org/jakarta-commons/Betwixt/TipsAndHints/CustomObjectStringConverter New page: Betwixt only allows the registration of a single object/string converter strategy class. This class serves as a mediator to route conversion calls to helper classes depending based on type. Converters are registered and by deregistered via calls to the register and deregister methods, respectively, and resolved via the {@link #resolve} method. Only one ObjectStringConverter can be registered per class at any given time. If there is no ObjectStringConverter registered for a given class, then the resolve method checks to see if there is an ObjectStringConverter registered for its super class or any of its interfaces (recursively). The first ObjectStringConverter found in the class hierarchy is used. (Note that if the class implements many interfaces, and there are converters for more than one of those interfaces, the resulting behavior will be undefined.) {{{ import java.util.*; import org.apache.commons.betwixt.expression.*; import org.apache.commons.betwixt.strategy.*; /** * * * @author Jesse Sweetland */ public class CustomObjectStringConverter extends ObjectStringConverter { private final Map _converters = new HashMap(); public void register(ObjectStringConverter converter, Class forType) { _converters.put(forType, converter); } public void deregister(Class forType) { _converters.remove(forType); } public ObjectStringConverter resolve(Class forType) { int minDistance = Integer.MAX_VALUE; ObjectStringConverter converter = null; for(Map.Entry entry: _converters.entrySet()) { int distance = getDistance(entry.getKey(), forType); if((distance >= 0) && (distance < minDistance)) { minDistance = distance; converter = entry.getValue(); } } return converter; } private int getDistance(Class superClass, Class type) { if(superClass.isAssignableFrom(type)) { Class temp = type; int distance = 0; while(temp != null) { if(temp.equals(superClass)) return distance; for(Class ifc: temp.getInterfaces()) { if(ifc.equals(superClass)) return distance; } distance++; temp = temp.getSuperclass(); } } return -1; } public String objectToString(Object object, Class type, Context context) { if(object == null) return null; ObjectStringConverter converter = resolve(type); if(converter != null) { return converter.objectToString(object, type, context); } else { return super.objectToString(object, type, context); } } public Object stringToObject(String value, Class type, Context context) { if(value == null) return null; ObjectStringConverter converter = resolve(type); if(converter != null) { return converter.stringToObject(value, type, context); } else { return super.stringToObject(value, type, context); } } } }}} Example: {{{ StringWriter sw = new StringWriter(); BeanWriter bw = new BeanWriter(sw); CustomObjectStringConverter osc = new CustomObjectStringConverter(); osc.register(new EnumObjectStringConverter(), Enum.class); osc.register(new DateObjectStringConverter(), Date.class); bw.getBindingConfiguration().setObjectStringConverter(osc); }}} --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org