1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt.strategy;
17
18 import java.io.Serializable;
19 import java.util.Date;
20
21 /***
22 * Determines the way that a type (of object) should be bound
23 * by Betwixt.
24 *
25 * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
26 */
27 public abstract class TypeBindingStrategy {
28
29 /***
30 * The default Betwixt <code>TypeBindingStrategy</code> implementation.
31 * Since the default implementation has no state,
32 * a singleton instance can be provided.
33 */
34 public static final TypeBindingStrategy DEFAULT = new Default();
35
36 /***
37 * Gets the binding type to be used for the given Java type.
38 * @param type <code>Class</code> for which the binding type is to be determined,
39 * not null
40 * @return <code>BindingType</code> enumeration indicating the type of binding,
41 * not null
42 */
43 public abstract BindingType bindingType(Class type);
44
45
46 /***
47 * Enumerates the possible general ways that Betwixt can map a Java type to an XML type.
48 * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
49 */
50 public static final class BindingType implements Serializable {
51
52 private static final int COMPLEX_INDICATOR = 1;
53 private static final int PRIMITIVE_INDICATOR = 2;
54
55 /***
56 * Indicates that the java type should be bound to a complex xml type.
57 * A complex xml type may have child elements and attributes.
58 * Betwixt determines the mapping for a java bean bound to a complex type.
59 */
60 public static final BindingType COMPLEX = new BindingType(COMPLEX_INDICATOR);
61
62 /***
63 * Indicates that the type should be bound as a Java primitive.
64 * Betwixt may bind this to an attribute or a simple xml type.
65 * Which is determined by the configuration for binding primitives.
66 */
67 public static final BindingType PRIMITIVE = new BindingType(PRIMITIVE_INDICATOR);
68
69 private int type;
70
71 private BindingType(int type) {
72 this.type = type;
73 }
74
75
76 /***
77 * @see java.lang.Object#equals(java.lang.Object)
78 */
79 public boolean equals(Object object) {
80 boolean result = false;
81 if (object instanceof BindingType) {
82 BindingType bindingType = (BindingType) object;
83 result = (type == bindingType.type);
84 }
85 return result;
86 }
87
88 /***
89 * @see java.lang.Object#hashCode()
90 */
91 public int hashCode() {
92 return type;
93 }
94
95 /***
96 * @see java.lang.Object#toString()
97 */
98 public String toString() {
99 StringBuffer buffer = new StringBuffer();
100 buffer.append("BindingType: ");
101 switch (type) {
102 case (COMPLEX_INDICATOR):
103 buffer.append("COMPLEX");
104 break;
105
106 case (PRIMITIVE_INDICATOR):
107 buffer.append("PRIMITIVE");
108 break;
109 }
110
111 return buffer.toString();
112 }
113 }
114
115 /***
116 * The default <code>TypeBindingStrategy</code> used by Betwixt.
117 * This implementation recognizes all the usual Java primitive wrappers
118 * (plus a few more that will in most typical use cases be regarded in the same way).
119 * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
120 */
121 public static final class Default extends TypeBindingStrategy {
122
123 /***
124 * Gets the binding type to be used for the given Java type.
125 * This implementation recognizes all the usual Java primitive wrappers
126 * (plus a few more that will in most typical use cases be regarded in the same way).
127 * @param type <code>Class</code> for which the binding type is to be determined,
128 * not null
129 * @return <code>BindingType</code> enumeration indicating the type of binding,
130 * not null
131 */
132 public BindingType bindingType(Class type) {
133 BindingType result = BindingType.COMPLEX;
134 if (isStandardPrimitive(type)) {
135 result = BindingType.PRIMITIVE;
136 }
137
138 return result;
139 }
140
141 /***
142 * is the given type one of the standard Betwixt primitives?
143 * @param type <code>Class</code>, not null
144 * @return true if the type is one of the standard Betwixt primitives
145 */
146 protected boolean isStandardPrimitive(Class type) {
147 if ( type == null ) {
148 return false;
149
150 } else if ( type.isPrimitive() ) {
151 return true;
152
153 } else if ( type.equals( Object.class ) ) {
154 return false;
155 }
156 return type.getName().startsWith( "java.lang." )
157 || Number.class.isAssignableFrom( type )
158 || String.class.isAssignableFrom( type )
159 || Date.class.isAssignableFrom( type )
160 || java.sql.Date.class.isAssignableFrom( type )
161 || java.sql.Time.class.isAssignableFrom( type )
162 || java.sql.Timestamp.class.isAssignableFrom( type )
163 || java.math.BigDecimal.class.isAssignableFrom( type )
164 || java.math.BigInteger.class.isAssignableFrom( type );
165 }
166 }
167 }