1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.strategy;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.betwixt.Options;
22 import org.apache.commons.betwixt.expression.Context;
23
24 /***
25 * <p>Strategy class for string <-> object conversions.
26 * Implementations of this interface are used by Betwixt to perform
27 * string <-> object conversions.
28 * This performs only the most basic conversions.
29 * Most applications will use a subclass.
30 * </p>
31 * <p>It is strongly recommended that (in order to support round tripping)
32 * that <code>objectToString</code> and <code>stringToObject</code>
33 * are inverse functions.
34 * In other words, given the same flavour, context and type the applying
35 * objectToString to the result of stringToObject should be equal to the
36 * original input.
37 * </p>
38 * @author Robert Burrell Donkin
39 * @since 0.5
40 */
41 public class ObjectStringConverter implements Serializable {
42
43 /*** Standard name for option giving flavour */
44 public static final String FLAVOUR_OPTION_NAME
45 = "org.apache.commons.betwixt.flavour";
46
47 /***
48 * Converts an object to a string representation.
49 * This basic implementation returns object.toString()
50 * or an empty string if the given object is null.
51 *
52 * @param object the object to be converted, possibly null
53 * @param type the property class of the object, not null
54 * @param flavour a string allow symantic differences in formatting to be communicated
55 * @param context the context, not null
56 * @deprecated 0.7 use {@link #objectToString(Object, Class, Context)} instead.
57 * The preferred way to support flavours is by setting the
58 * <code>org.apache.commons.betwixt.FLAVOUR</code> option.
59 * This can then be retrieved by calling {@link Context#getOptions()}
60 * @return a String representation, not null
61 */
62 public String objectToString(Object object, Class type, String flavour, Context context) {
63 if ( object != null ) {
64 return object.toString();
65 }
66 return "";
67 }
68
69 /***
70 * Converts a string representation to an object.
71 * It is acceptable for an implementation to return the string if it cannot convert
72 * the string to the given class type.
73 * This basic implementation just returns a string.
74 *
75 * @param value the String to be converted
76 * @param type the property class to be returned (if possible), not null
77 * @param flavour a string allow symantic differences in formatting to be communicated
78 * @param context the context, not null
79 * @deprecated 0.7 use {@link #stringToObject(String, Class, Context)} instead.
80 * The preferred way to support flavours is by setting the
81 * <code>org.apache.commons.betwixt.FLAVOUR</code> option.
82 * This can then be retrieved by calling {@link Context#getOptions()}
83 * @return an Object converted from the String, not null
84 */
85 public Object stringToObject(String value, Class type, String flavour, Context context) {
86 return value;
87 }
88
89
90 /***
91 * Converts an object to a string representation.
92 * This basic implementation returns object.toString()
93 * or an empty string if the given object is null.
94 *
95 * @since 0.7
96 * @param object the object to be converted, possibly null
97 * @param type the property class of the object, not null
98 * @param context the context, not null
99 * @return a String representation, not null
100 */
101 public String objectToString(Object object, Class type, Context context) {
102 String flavour = getFlavour(context);
103 return objectToString(object, type, flavour, context);
104 }
105
106 /***
107 * Converts a string representation to an object.
108 * It is acceptable for an implementation to return the string if it cannot convert
109 * the string to the given class type.
110 * This basic implementation just returns a string.
111 *
112 * @since 0.7
113 * @param value the String to be converted
114 * @param type the property class to be returned (if possible), not null
115 * @param context the context, not null
116 * @return an Object converted from the String, not null
117 */
118 public Object stringToObject(String value, Class type, Context context) {
119 String flavour = getFlavour(context);
120 return stringToObject(value, type, flavour, context);
121 }
122
123 /***
124 * Gets the current flavour from the context.
125 * @param context <code>Context</code>, not null
126 */
127 private String getFlavour(Context context) {
128 String flavour = null;
129 Options options = context.getOptions();
130 if (options != null) {
131 flavour = options.getValue(FLAVOUR_OPTION_NAME);
132 }
133 return flavour;
134 }
135 }