01: /*
02: * Copyright 2004 Clinton Begin
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 com.ibatis.sqlmap.engine.exchange;
17:
18: import com.ibatis.sqlmap.engine.type.DomTypeMarker;
19: import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
20:
21: import java.util.List;
22: import java.util.Map;
23:
24: /**
25: * Factory for DataExchange objects
26: */
27: public class DataExchangeFactory {
28:
29: private final DataExchange domDataExchange;
30: private final DataExchange listDataExchange;
31: private final DataExchange mapDataExchange;
32: private final DataExchange primitiveDataExchange;
33: private final DataExchange complexDataExchange;
34:
35: private TypeHandlerFactory typeHandlerFactory;
36:
37: /**
38: * Constructor for the factory
39: * @param typeHandlerFactory - a type handler factory for the factory
40: */
41: public DataExchangeFactory(TypeHandlerFactory typeHandlerFactory) {
42: this .typeHandlerFactory = typeHandlerFactory;
43: domDataExchange = new DomDataExchange(this );
44: listDataExchange = new ListDataExchange(this );
45: mapDataExchange = new ComplexDataExchange(this );
46: primitiveDataExchange = new PrimitiveDataExchange(this );
47: complexDataExchange = new ComplexDataExchange(this );
48: }
49:
50: /**
51: * Getter for the type handler factory
52: * @return - the type handler factory
53: */
54: public TypeHandlerFactory getTypeHandlerFactory() {
55: return typeHandlerFactory;
56: }
57:
58: /**
59: * Get a DataExchange object for the passed in Class
60: * @param clazz - the class to get a DataExchange object for
61: * @return - the DataExchange object
62: */
63: public DataExchange getDataExchangeForClass(Class clazz) {
64: DataExchange dataExchange = null;
65: if (clazz == null) {
66: dataExchange = complexDataExchange;
67: } else if (DomTypeMarker.class.isAssignableFrom(clazz)) {
68: dataExchange = domDataExchange;
69: } else if (List.class.isAssignableFrom(clazz)) {
70: dataExchange = listDataExchange;
71: } else if (Map.class.isAssignableFrom(clazz)) {
72: dataExchange = mapDataExchange;
73: } else if (typeHandlerFactory.getTypeHandler(clazz) != null) {
74: dataExchange = primitiveDataExchange;
75: } else {
76: dataExchange = new JavaBeanDataExchange(this);
77: }
78: return dataExchange;
79: }
80:
81: }
|