Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 910 invoked from network); 8 Feb 2004 13:28:52 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 8 Feb 2004 13:28:52 -0000 Received: (qmail 61745 invoked by uid 500); 8 Feb 2004 13:28:42 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 61722 invoked by uid 500); 8 Feb 2004 13:28:42 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 61682 invoked from network); 8 Feb 2004 13:28:42 -0000 Received: from unknown (HELO main.gmane.org) (80.91.224.249) by daedalus.apache.org with SMTP; 8 Feb 2004 13:28:42 -0000 Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1ApozK-00035D-00 for ; Sun, 08 Feb 2004 14:28:42 +0100 Received: from pd9e73c8d.dip.t-dialin.net ([217.231.60.141]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun Feb 8 13:28:42 2004 Received: from nospam_st by pd9e73c8d.dip.t-dialin.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun Feb 8 13:28:42 2004 X-Injected-Via-Gmane: http://gmane.org/ To: commons-user@jakarta.apache.org From: Sebastian Subject: Re: Betwixt: Setting up BeanWriter to render a bean property as attribute rather than element Date: Sun, 08 Feb 2004 14:28:40 +0100 Lines: 93 Message-ID: References: <401BA5D6.4050307@gmx.net> <78DE608E-5699-11D8-A81E-003065DC754C@blueyonder.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: pd9e73c8d.dip.t-dialin.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en In-Reply-To: <78DE608E-5699-11D8-A81E-003065DC754C@blueyonder.co.uk> Sender: news X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Hi Robert, based on the refactoring branch I created a TypeMapper that looks like this: public class BetwixtTypeMapper extends SimpleTypeMapper { protected HashSet attributes = new HashSet(); protected HashSet elements = new HashSet(); public synchronized void registerAsAttribute(String propertyName) { if (elements.contains(propertyName)) elements.remove(propertyName); attributes.add(propertyName); } public synchronized void unregisterAttribute(String propertyName) { attributes.remove(propertyName); } public synchronized void registerAsElement(String propertyName) { if (attributes.contains(propertyName)) attributes.remove(propertyName); elements.add(propertyName); } public synchronized void unregisterElement(String propertyName) { elements.remove(propertyName); } public Binding bind( String propertyName, Class propertyType, IntrospectionConfiguration configuration) { if (attributes.contains(propertyName)) return SimpleTypeMapper.Binding.ATTRIBUTE; if (elements.contains(propertyName)) return SimpleTypeMapper.Binding.ELEMENT; return configuration.isAttributesForPrimitives() ? SimpleTypeMapper.Binding.ATTRIBUTE : SimpleTypeMapper.Binding.ELEMENT; } } It can be used like this: BetwixtTypeMapper btm = new BetwixtTypeMapper(); btm.registerAsAttribute("id"); ic.setSimpleTypeMapper(btm); XMLIntrospector xi = beanReader.getXMLIntrospector(); xi.getConfiguration().setSimpleTypeMapper(btm); If you like you can add this typemapper as an example to the betwixt distribution. Sebastian robert burrell donkin wrote: > hey Sebastian > > i have some good news and some less good news: > > the good news is that i've added a pluggable strategy for binding simple > types (primitives at the moment). i hope that you should be able to > subclass the new SimpleTypeMapper class with something a litte like: > > /** Implementation binds strings to elements but everything else to > attributes */ > class StringsAsElementsSimpleTypeMapper extends SimpleTypeMapper { > > /** > * Binds strings to elements but everything else to attributes > */ > public Binding bind( > String propertyName, > Class propertyType, > IntrospectionConfiguration configuration) { > if (String.class.equals(propertyType)) { > return SimpleTypeMapper.Binding.ELEMENT; > } > return SimpleTypeMapper.Binding.ATTRIBUTE; > } > > } > > the less good news is that i needed to add it on the refactoring branch > (since the code involved on CVS HEAD has been refactored on the branch). > so you'll need to grab the REFACTORING-BRANCH_2004-01-13 version of the > source from cvs and build it yourself (probably using > http://maven.apache.org). > > i may get round to adding some documentation on it but again this will > be on the branch. > > - robert --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org