You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2005/02/24 23:28:50 UTC

svn commit: r155264 - in jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt: expression/Context.java expression/MapEntryAdder.java expression/TypedUpdater.java io/AbstractBeanWriter.java io/read/ReadContext.java strategy/ObjectStringConverter.java

Author: rdonkin
Date: Thu Feb 24 14:28:47 2005
New Revision: 155264

URL: http://svn.apache.org/viewcvs?view=rev&rev=155264
Log:
Changed ObjectToStringConverter to make context available and to make options available from context. This is an alternative way to address the needs of Issue 33331.

Modified:
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/MapEntryAdder.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/TypedUpdater.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ObjectStringConverter.java

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java?view=diff&r1=155263&r2=155264
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java Thu Feb 24 14:28:47 2005
@@ -19,9 +19,11 @@
 import java.util.Map;
 
 import org.apache.commons.betwixt.BindingConfiguration;
+import org.apache.commons.betwixt.Options;
 import org.apache.commons.betwixt.strategy.IdStoringStrategy;
 import org.apache.commons.betwixt.strategy.ObjectStringConverter;
 import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
+import org.apache.commons.collections.ArrayStack;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -52,6 +54,8 @@
     private Object bean;
     /** Variables map */
     private Map variables;
+    /** Store options */
+    private ArrayStack optionStack = new ArrayStack();
     /** 
      * Logging uses commons-logging <code>Log</code> 
      * named <code>org.apache.commons.betwixt</code> 
@@ -278,6 +282,48 @@
      */
     public IdStoringStrategy getIdMappingStrategy() {
         return bindingConfiguration.getIdMappingStrategy();
+    }
+    
+    /**
+     * Gets the current <code>Options</code>.
+     * @return <code>Options</code> that currently apply
+     * or null if there are no current options.
+     */
+    public Options getOptions() {
+        Options results = null;
+        if (!optionStack.isEmpty()) {
+            results = (Options) optionStack.peek();
+        }
+        return results;
+    }
+
+    /**
+     * <p>Pushes the given <code>Options</code> onto the stack.
+     * </p><p>
+     * <strong>Note</strong> that code calling push should ensure that {@link #popOptions}
+     * is called once the options are no longer current.
+     * This ensures that the previous options are reinstated.
+     * </p>
+     * @param options newly current <code>Options</code>, not null 
+     */
+    public void pushOptions(Options options) {
+        optionStack.push(options);
+    }
+
+    /**
+     * <p>Pops the current options from the stack.
+     * The previously current options (if any exist)
+     * will be reinstated by this method.
+     * </p><p>
+     * <stong>Note</strong> code calling this method should
+     * have previsouly called {@link #popOptions}.
+     */
+    public void popOptions() {
+        if (optionStack.isEmpty()) {
+            log.info("Cannot pop options off empty stack");
+        } else {
+            optionStack.pop();
+        }
     }
     
 }

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/MapEntryAdder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/MapEntryAdder.java?view=diff&r1=155263&r2=155264
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/MapEntryAdder.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/MapEntryAdder.java Thu Feb 24 14:28:47 2005
@@ -192,13 +192,13 @@
             if ( key instanceof String ) {
                 // try to convert into primitive types
                 key = context.getObjectStringConverter()
-                        .stringToObject( (String) key, keyType, null, context );
+                        .stringToObject( (String) key, keyType, context );
             }
             
             if ( value instanceof String ) {
                 // try to convert into primitive types
                 value = context.getObjectStringConverter()
-                        .stringToObject( (String) value, valueType, null, context );
+                        .stringToObject( (String) value, valueType, context );
             }
             
             // special case for collection objects into arrays                    

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/TypedUpdater.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/TypedUpdater.java?view=diff&r1=155263&r2=155264
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/TypedUpdater.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/TypedUpdater.java Thu Feb 24 14:28:47 2005
@@ -50,7 +50,7 @@
                         log.trace("Converting primitive to " + valueType);
                     }
                     newValue = context.getObjectStringConverter()
-                        .stringToObject( (String) newValue, valueType, null, context );
+                        .stringToObject( (String) newValue, valueType, context );
                 }
                 if ( newValue != null ) {
                     // check that it is of the correct type

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java?view=diff&r1=155263&r2=155264
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java Thu Feb 24 14:28:47 2005
@@ -718,7 +718,8 @@
             if ( log.isTraceEnabled() ) {
                 log.trace( "Element " + elementDescriptor + " is empty." );
             }
-        
+            context.pushOptions(elementDescriptor.getOptions());
+            
             Attributes attributes = addNamespaceDeclarations(
                 new ElementAttributes( elementDescriptor, context ), namespaceUri);
             writeContext.setCurrentDescriptor(elementDescriptor);
@@ -732,7 +733,7 @@
             writeElementContent( elementDescriptor, context ) ;
             writeContext.setCurrentDescriptor(elementDescriptor);
             endElement( writeContext, namespaceUri, localName, qualifiedName );
-            
+            context.popOptions();
         }
     }
     
@@ -797,6 +798,7 @@
                                     IntrospectionException {
                    
         if ( !ignoreElement( elementDescriptor, context ) ) {
+            context.pushOptions(elementDescriptor.getOptions());
             writeContext.setCurrentDescriptor(elementDescriptor);
             Attributes attributes = new IDElementAttributes( 
                         elementDescriptor, 
@@ -813,7 +815,7 @@
             writeElementContent( elementDescriptor, context ) ;
             writeContext.setCurrentDescriptor(elementDescriptor);
             endElement( writeContext, namespaceUri, localName, qualifiedName );
-
+            context.popOptions();
         } else if ( log.isTraceEnabled() ) {
             log.trace( "Element " + qualifiedName + " is empty." );
         }
@@ -879,7 +881,7 @@
                                 "",
                                 idrefAttributeName, 
                                 idrefAttributeName,
-                                "IDREF",
+                                "IDREF",    
                                 idrefAttributeValue);
         writeContext.setCurrentDescriptor(elementDescriptor);
         startElement( writeContext, uri, localName, qualifiedName, addNamespaceDeclarations(attributes, uri));        
@@ -1819,7 +1821,7 @@
     private String convertToString( Object value , Descriptor descriptor, Context context ) {
         return getBindingConfiguration()
             .getObjectStringConverter()
-                .objectToString( value, descriptor.getPropertyType(), null, context );
+                .objectToString( value, descriptor.getPropertyType(), context );
     }
     
     /**

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java?view=diff&r1=155263&r2=155264
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java Thu Feb 24 14:28:47 2005
@@ -20,6 +20,7 @@
 import org.apache.commons.betwixt.AttributeDescriptor;
 import org.apache.commons.betwixt.BindingConfiguration;
 import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.Options;
 import org.apache.commons.betwixt.XMLBeanInfo;
 import org.apache.commons.betwixt.XMLIntrospector;
 import org.apache.commons.betwixt.expression.Context;
@@ -59,9 +60,9 @@
 	private ArrayStack actionMappingStack = new ArrayStack();
 	/** Stack contains all beans created */
 	private ArrayStack objectStack = new ArrayStack();
-    
+    /** Stack contains element descriptors */
     private ArrayStack descriptorStack = new ArrayStack();
-    
+    /** Stack contains updaters */
     private ArrayStack updaterStack = new ArrayStack();
 
 	private Class rootClass;
@@ -198,6 +199,8 @@
             updaterStack.pop();
         }
         
+        popOptions();
+        
 		Object top = null;
 		if (!elementMappingStack.isEmpty()) {
 			top = elementMappingStack.pop();
@@ -284,11 +287,14 @@
             }
         }
         Updater updater = null;
+        Options options = null;
         if (nextDescriptor != null) {
             updater = nextDescriptor.getUpdater();
+            options = nextDescriptor.getOptions();
         }
         updaterStack.push(updater);
         descriptorStack.push(nextDescriptor);
+        pushOptions(options);
 	}
 
 	/**

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ObjectStringConverter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ObjectStringConverter.java?view=diff&r1=155263&r2=155264
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ObjectStringConverter.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ObjectStringConverter.java Thu Feb 24 14:28:47 2005
@@ -17,6 +17,7 @@
 
 import java.io.Serializable;
 
+import org.apache.commons.betwixt.Options;
 import org.apache.commons.betwixt.expression.Context;
 
 /** 
@@ -38,6 +39,10 @@
  */
 public class ObjectStringConverter implements Serializable {
     
+    /** Standard name for option giving flavour */
+    public static final String FLAVOUR_OPTION_NAME 
+        = "org.apache.commons.betwixt.flavour";
+    
     /**
       * Converts an object to a string representation.
       * This basic implementation returns object.toString() 
@@ -47,6 +52,10 @@
       * @param type the property class of the object, not null
       * @param flavour a string allow symantic differences in formatting to be communicated
       * @param context the context, not null
+      * @deprecated  use {@link #objectToString(Object, Class, Context)} instead. 
+      * The preferred way to support flavours is by setting the
+      * <code>org.apache.commons.betwixt.FLAVOUR</code> option.
+      * This can then be retrieved by calling {@link Context#getOptions()}
       * @return a String representation, not null
       */
     public String objectToString(Object object, Class type, String flavour, Context context) {
@@ -66,9 +75,58 @@
       * @param type the property class to be returned (if possible), not null
       * @param flavour a string allow symantic differences in formatting to be communicated
       * @param context the context, not null
+      * @deprecated use {@link #stringToObject(String, Class, Context)} instead.
+      * The preferred way to support flavours is by setting the
+      * <code>org.apache.commons.betwixt.FLAVOUR</code> option.
+      * This can then be retrieved by calling {@link Context#getOptions()}
       * @return an Object converted from the String, not null
       */
     public Object stringToObject(String value, Class type, String flavour, Context context) {
         return value;
+    }
+
+    
+    /**
+      * Converts an object to a string representation.
+      * This basic implementation returns object.toString() 
+      * or an empty string if the given object is null.
+      *
+      * @param object the object to be converted, possibly null
+      * @param type the property class of the object, not null
+      * @param context the context, not null
+      * @return a String representation, not null
+      */
+    public String objectToString(Object object, Class type, Context context) {
+        String flavour = getFlavour(context);
+        return objectToString(object, type, flavour, context);
+    }
+    
+    /**
+      * Converts a string representation to an object.
+      * It is acceptable for an implementation to return the string if it cannot convert 
+      * the string to the given class type.
+      * This basic implementation just returns a string.
+      * 
+      * @param value the String to be converted
+      * @param type the property class to be returned (if possible), not null
+      * @param context the context, not null
+      * @return an Object converted from the String, not null
+      */
+    public Object stringToObject(String value, Class type, Context context) {
+        String flavour = getFlavour(context);
+        return stringToObject(value, type, flavour, context);
+    }
+
+    /**
+     * Gets the current flavour from the context.
+     * @param context <code>Context</code>, not null
+     */
+    private String getFlavour(Context context) {
+        String flavour = null;
+        Options options = context.getOptions();
+        if (options != null) {
+            flavour = options.getValue(FLAVOUR_OPTION_NAME);
+        }
+        return flavour;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org