KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > strategy > TypeBindingStrategy


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.betwixt.strategy;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Date JavaDoc;
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 JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc toString() {
99             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc type) {
147             if ( type == null ) {
148                 return false;
149                 
150             } else if ( type.isPrimitive() ) {
151                 return true;
152                 
153             } else if ( type.equals( Object JavaDoc.class ) ) {
154                 return false;
155             }
156             return type.getName().startsWith( "java.lang." )
157                 || Number JavaDoc.class.isAssignableFrom( type )
158                 || String JavaDoc.class.isAssignableFrom( type )
159                 || Date JavaDoc.class.isAssignableFrom( type )
160                 || java.sql.Date JavaDoc.class.isAssignableFrom( type )
161                 || java.sql.Time JavaDoc.class.isAssignableFrom( type )
162                 || java.sql.Timestamp JavaDoc.class.isAssignableFrom( type )
163                 || java.math.BigDecimal JavaDoc.class.isAssignableFrom( type )
164                 || java.math.BigInteger JavaDoc.class.isAssignableFrom( type );
165         }
166     }
167 }
168
Popular Tags