Hi,
the following is a patch against current CVS jakarta-commons/betwixt/src/java that implements a pluggable strategy for storing already processed bean ID values. The default implementation provided works exactly like it did before, but it allows me to override this behaviour.
The idea is that (while serializing certain object trees that have a lot of dependencies) not every instance that is referenced needs to show up in the resulting XML. This depends on the use you make of it: XML fragments designed to transfer individual pieces of information can be significantly smaller than those needed to fully recreate a complex object tree.
I'd be happy to commit this to the standard code base. Kind regards,
- Christian
--
Christian Aust mailto:[EMAIL PROTECTED] icq: 84500990 - Yahoo!: datenimperator - MSN: datenimperator PGP: A94E 0181 664D 27E3 F05A A751 6A7E 90D1 A0A3 DEC7
Index: org/apache/commons/betwixt/BindingConfiguration.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/BindingConfiguration.java,v retrieving revision 1.9 diff -u -r1.9 BindingConfiguration.java --- org/apache/commons/betwixt/BindingConfiguration.java 23 Aug 2004 19:35:29 -0000 1.9 +++ org/apache/commons/betwixt/BindingConfiguration.java 16 Feb 2005 09:30:37 -0000 @@ -18,6 +18,7 @@ import java.io.Serializable; import org.apache.commons.betwixt.strategy.DefaultObjectStringConverter; +import org.apache.commons.betwixt.strategy.IdStoringStrategy; import org.apache.commons.betwixt.strategy.ObjectStringConverter; import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy; @@ -48,6 +49,8 @@ private String classNameAttribute = "className"; /** Strategy for suppressing attributes with certain values when writing */ private ValueSuppressionStrategy valueSuppressionStrategy = ValueSuppressionStrategy.DEFAULT; + /** Strategy for storing and accessing ID values */ + private IdStoringStrategy idStoringStrategy = IdStoringStrategy.DEFAULT; /** * Constructs a BindingConfiguration with default properties. @@ -148,4 +151,18 @@ ValueSuppressionStrategy valueSuppressionStrategy) { this.valueSuppressionStrategy = valueSuppressionStrategy; } + + /** + * @return Returns the idStoringStrategy. + */ + public IdStoringStrategy getIdMappingStrategy() { + return idStoringStrategy; + } + + /** + * @param idStoringStrategy The idStoringStrategy to set. + */ + public void setIdMappingStrategy(IdStoringStrategy idMappingStrategy) { + this.idStoringStrategy = idMappingStrategy; + } } Index: org/apache/commons/betwixt/io/AbstractBeanWriter.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java,v retrieving revision 1.34 diff -u -r1.34 AbstractBeanWriter.java --- org/apache/commons/betwixt/io/AbstractBeanWriter.java 9 Nov 2004 16:15:54 -0000 1.34 +++ org/apache/commons/betwixt/io/AbstractBeanWriter.java 16 Feb 2005 09:30:38 -0000 @@ -19,7 +19,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; import org.apache.commons.betwixt.AttributeDescriptor; @@ -92,8 +91,6 @@ /** Log used for logging (Doh!) */ private Log log = LogFactory.getLog( AbstractBeanWriter.class ); - /** Map containing ID attribute values for beans */ - private HashMap idMap = new HashMap(); /** Stack containing beans - used to detect cycles */ private ArrayStack beanStack = new ArrayStack(); /** Used to generate ID attribute values*/ @@ -345,7 +342,7 @@ } else { pushBean ( context.getBean() ); if ( getBindingConfiguration().getMapIDs() ) { - ref = (String) idMap.get( context.getBean() ); + ref = getBindingConfiguration().getIdMappingStrategy().getReferenceFor(context, context.getBean()); } if ( ref == null ) { // this is the first time that this bean has be written @@ -353,7 +350,7 @@ if (idAttribute == null) { // use a generated id id = idGenerator.nextId(); - idMap.put( bean, id ); + getBindingConfiguration().getIdMappingStrategy().setReference(context, bean, id); if ( getBindingConfiguration().getMapIDs() ) { // write element with id @@ -396,7 +393,7 @@ // convert to string id = exp.toString(); } - idMap.put( bean, id); + getBindingConfiguration().getIdMappingStrategy().setReference(context, bean, id); // the ID attribute should be written automatically writeElement( Index: org/apache/commons/betwixt/strategy/ActionMappingStrategy.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/ActionMappingStrategy.java,v retrieving revision 1.2 diff -u -r1.2 ActionMappingStrategy.java --- org/apache/commons/betwixt/strategy/ActionMappingStrategy.java 13 Jun 2004 21:32:46 -0000 1.2 +++ org/apache/commons/betwixt/strategy/ActionMappingStrategy.java 16 Feb 2005 09:30:38 -0000 @@ -19,6 +19,7 @@ import org.apache.commons.betwixt.io.read.MappingAction; import org.apache.commons.betwixt.io.read.ReadContext; +import org.apache.commons.betwixt.strategy.DefaultActionMappingStrategy; import org.xml.sax.Attributes; /** Index: org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java =================================================================== RCS file: org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java diff -N org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,66 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.betwixt.strategy; + +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.betwixt.expression.Context; + +/** + * DefaultIdStoringStrategy stores every ID that given to it into + * an internal HashMap and returns it on request. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Christian Aust</a> + * @since 0.6.1 + */ +public class DefaultIdStoringStrategy extends IdStoringStrategy { + private Map idMap; + + /** + * Standard constructor uses a HashMap as its internal storage. + */ + public DefaultIdStoringStrategy() { + idMap = new HashMap(); + } + + /** + * Returns a String id for the given bean if it has been stored previously. + * Otherwise returns null. + * + * @param context current context + * @param bean the instance + * @return id as String, or null if not found + * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#getReferenceFor(org.apache.commons.betwixt.expression.Context, java.lang.Object) + */ + public String getReferenceFor(Context context, Object bean) { + return (String) idMap.get(bean); + } + + /** + * Stores an ID for the given instance and context. It will check first if + * this ID has been previously stored and will do nothing in that case. + * + * @param context current context + * @param bean current instance + * @param id the ID to store + * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#setReference(org.apache.commons.betwixt.expression.Context, java.lang.Object, java.lang.String) + */ + public void setReference(Context context, Object bean, String id) { + if (!idMap.containsKey(bean)) + idMap.put(bean, id); + } +} Index: org/apache/commons/betwixt/strategy/IdStoringStrategy.java =================================================================== RCS file: org/apache/commons/betwixt/strategy/IdStoringStrategy.java diff -N org/apache/commons/betwixt/strategy/IdStoringStrategy.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ org/apache/commons/betwixt/strategy/IdStoringStrategy.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,48 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.betwixt.strategy; + +import org.apache.commons.betwixt.expression.Context; + +/** + * IdStoringStrategy org.apache.commons.betwixt.strategy + * @author <a href="mailto:[EMAIL PROTECTED]">Christian Aust</a> + * @since 0.6.1 + */ +public abstract class IdStoringStrategy { + + public static IdStoringStrategy DEFAULT = new DefaultIdStoringStrategy(); + + /** + * Retrieve a reference for the given instance. + * + * @param context current context + * @param bean the instance + * @return id as String, or null + */ + public abstract String getReferenceFor(Context context, Object bean); + + /** + * Store an instance reference for later retrieval. + * + * @param context current context + * @param bean the instance + * @param id the id to use + */ + public abstract void setReference(Context context, Object bean, String id); + +}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]