View Javadoc
1   /*
2   
3    * Copyright 2005 Philipp Erlacher
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.exolab.castor.xml.parsing.primitive.objects;
18  
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * This class is used as a factory to get an instance of a class with a given
29   * value.
30   */
31  public class PrimitiveObjectFactory {
32  
33      private Map<Class<?>, Class<? extends PrimitiveObject>> typeHandlers = new HashMap<Class<?>, Class<? extends PrimitiveObject>>();
34  
35      private final Log logger = LogFactory.getLog(this.getClass());
36      
37      private static PrimitiveObjectFactory primitiveObjectFactory;
38      
39      public static synchronized PrimitiveObjectFactory getInstance() {
40         if (primitiveObjectFactory == null) {
41            primitiveObjectFactory = new PrimitiveObjectFactory();
42         }
43         return primitiveObjectFactory;
44      }
45      
46      private PrimitiveObjectFactory() {
47          typeHandlers.put(String.class, PrimitiveString.class);
48  
49          typeHandlers.put(Enum.class, PrimitiveEnum.class);
50  
51          typeHandlers.put(Integer.TYPE, PrimitiveInteger.class);
52          typeHandlers.put(Integer.class, PrimitiveInteger.class);
53  
54          typeHandlers.put(Boolean.TYPE, PrimitiveBoolean.class);
55          typeHandlers.put(Boolean.class, PrimitiveBoolean.class);
56  
57          typeHandlers.put(Double.TYPE, PrimitiveDouble.class);
58          typeHandlers.put(Double.class, PrimitiveDouble.class);
59  
60          typeHandlers.put(Long.TYPE, PrimitiveLong.class);
61          typeHandlers.put(Long.class, PrimitiveLong.class);
62  
63          typeHandlers.put(Character.TYPE, PrimitiveChar.class);
64          typeHandlers.put(Character.class, PrimitiveChar.class);
65  
66          typeHandlers.put(Short.TYPE, PrimitiveShort.class);
67          typeHandlers.put(Short.class, PrimitiveShort.class);
68  
69          typeHandlers.put(Float.TYPE, PrimitiveFloat.class);
70          typeHandlers.put(Float.class, PrimitiveFloat.class);
71  
72          typeHandlers.put(Byte.TYPE, PrimitiveByte.class);
73          typeHandlers.put(Byte.class, PrimitiveByte.class);
74  
75          typeHandlers.put(BigInteger.class, PrimitiveBigInteger.class);
76  
77          typeHandlers.put(BigDecimal.class, PrimitiveBigDecimal.class);
78      }
79      
80      /**
81       * returns an instantiated Object
82       * 
83       * @return
84       */
85      public Object getObject(Class<?> type, String value) {
86  
87          PrimitiveObject handler = lookupHandler(type);
88  
89          if (handler == null) {
90              handler = getDefaultHandler();
91          }
92  
93          if (type != String.class) {
94              value = trimNumericValues(value);
95          }
96  
97          return handler.getObject(type, value);
98      }
99  
100     /**
101      * Looks up a handler from the map for the given type. <br>
102      * Returns null if there isn't any suitable handler.
103      * 
104      * @param type
105      * @return a handler to instantiate the given class
106      */
107     private PrimitiveObject lookupHandler(Class<?> type) {
108        
109        PrimitiveObject instance = null;
110 
111         if (type == null) {
112             return null;
113         }
114 
115         Class<? extends PrimitiveObject> result = typeHandlers.get(type);
116 
117         if (result == null) {
118             result = typeHandlers.get(type.getSuperclass());
119         }
120 
121         if (result != null) {
122            try {
123               instance = result.newInstance();
124            } catch (InstantiationException e) {
125               this.logger.error("Problem instantiating an instance of " + result.getName());
126               e.printStackTrace();
127            } catch (IllegalAccessException e) {
128               this.logger.error("Problem accessing default constructor of " + result.getName());
129               e.printStackTrace();
130            }
131         }
132         
133         return instance;
134     }
135 
136     /**
137      * Trim any numeric values
138      * 
139      * @param value
140      *            Value to be trimmed, can be null
141      * @return trimmed value or null
142      */
143     private String trimNumericValues(String value) {
144         if (value == null) {
145             return null;
146         }
147 
148         return value.trim();
149     }
150 
151     /**
152      * returns a default Handler for Object instantiation
153      * 
154      * @return primitiveObject Primitive
155      */
156     private PrimitiveObject getDefaultHandler() {
157         return new PrimitiveObject();
158     }
159 
160 }