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