KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > builder > xml > BaseParser


1 package com.ibatis.sqlmap.engine.builder.xml;
2
3 import com.ibatis.common.beans.Probe;
4 import com.ibatis.common.beans.ProbeFactory;
5 import com.ibatis.common.resources.Resources;
6 import com.ibatis.common.exception.NestedRuntimeException;
7 import com.ibatis.sqlmap.engine.cache.CacheModel;
8 import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
9 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
10 import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap;
11 import com.ibatis.sqlmap.engine.mapping.result.BasicResultMap;
12 import com.ibatis.sqlmap.engine.mapping.result.BasicResultMapping;
13 import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
14 import com.ibatis.sqlmap.engine.mapping.result.Discriminator;
15 import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
16 import com.ibatis.sqlmap.engine.scope.ErrorContext;
17 import com.ibatis.sqlmap.engine.type.DomTypeMarker;
18 import com.ibatis.sqlmap.engine.type.TypeHandler;
19 import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
20
21 import javax.sql.DataSource JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 public abstract class BaseParser {
28
29   private static final Probe PROBE = ProbeFactory.getProbe();
30
31   protected final Variables vars;
32
33   protected BaseParser(Variables vars) {
34     this.vars = vars;
35   }
36
37   public TypeHandler resolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Class JavaDoc clazz, String JavaDoc propertyName, String JavaDoc javaType, String JavaDoc jdbcType) {
38     return resolveTypeHandler(typeHandlerFactory, clazz, propertyName, javaType, jdbcType, false);
39   }
40
41   public TypeHandler resolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Class JavaDoc clazz, String JavaDoc propertyName, String JavaDoc javaType, String JavaDoc jdbcType, boolean useSetterToResolve) {
42     TypeHandler handler = null;
43     if (clazz == null) {
44       // Unknown
45
handler = typeHandlerFactory.getUnkownTypeHandler();
46     } else if (DomTypeMarker.class.isAssignableFrom(clazz)) {
47       // DOM
48
handler = typeHandlerFactory.getTypeHandler(String JavaDoc.class, jdbcType);
49     } else if (java.util.Map JavaDoc.class.isAssignableFrom(clazz)) {
50       // Map
51
if (javaType == null) {
52         handler = typeHandlerFactory.getUnkownTypeHandler(); //BUG 1012591 - typeHandlerFactory.getTypeHandler(java.lang.Object.class, jdbcType);
53
} else {
54         try {
55           Class JavaDoc javaClass = Resources.classForName(javaType);
56           handler = typeHandlerFactory.getTypeHandler(javaClass, jdbcType);
57         } catch (Exception JavaDoc e) {
58           throw new NestedRuntimeException("Error. Could not set TypeHandler. Cause: " + e, e);
59         }
60       }
61     } else if (typeHandlerFactory.getTypeHandler(clazz, jdbcType) != null) {
62       // Primitive
63
handler = typeHandlerFactory.getTypeHandler(clazz, jdbcType);
64     } else {
65       // JavaBean
66
if (javaType == null) {
67         if (useSetterToResolve) {
68           Class JavaDoc type = PROBE.getPropertyTypeForSetter(clazz, propertyName);
69           handler = typeHandlerFactory.getTypeHandler(type, jdbcType);
70         } else {
71           Class JavaDoc type = PROBE.getPropertyTypeForGetter(clazz, propertyName);
72           handler = typeHandlerFactory.getTypeHandler(type, jdbcType);
73         }
74       } else {
75         try {
76           Class JavaDoc javaClass = Resources.classForName(javaType);
77           handler = typeHandlerFactory.getTypeHandler(javaClass, jdbcType);
78         } catch (Exception JavaDoc e) {
79           throw new NestedRuntimeException("Error. Could not set TypeHandler. Cause: " + e, e);
80         }
81       }
82     }
83     return handler;
84   }
85
86   public String JavaDoc applyNamespace(String JavaDoc id) {
87     String JavaDoc newId = id;
88     if (vars.currentNamespace != null && vars.currentNamespace.length() > 0 && id != null && id.indexOf(".") < 0) {
89       newId = vars.currentNamespace + "." + id;
90     }
91     return newId;
92   }
93
94   /**
95    * Variables the parser uses. This "struct" like class is necessary because
96    * anonymous inner classes do not have access to non-final member fields of the parent class.
97    * This way, we can make the Variables instance final, and use all of its public fields as
98    * variables for parsing state.
99    */

100   protected static class Variables {
101     public ErrorContext errorCtx = new ErrorContext();
102
103     public Properties JavaDoc txProps = new Properties JavaDoc();
104     public Properties JavaDoc dsProps = new Properties JavaDoc();
105     public ErrorContext errorContext = new ErrorContext();
106     public Properties JavaDoc properties;
107
108     public XmlConverter sqlMapConv;
109     public XmlConverter sqlMapConfigConv;
110
111     public String JavaDoc currentResource = "SQL Map XML Config File";
112     public String JavaDoc currentNamespace = null;
113
114     public ExtendedSqlMapClient client;
115     public SqlMapExecutorDelegate delegate;
116     public TypeHandlerFactory typeHandlerFactory;
117     public DataSource JavaDoc dataSource;
118
119     public boolean useStatementNamespaces = false;
120
121     // SQL Map Vars
122
public Properties JavaDoc currentProperties;
123     public CacheModel currentCacheModel;
124     public BasicResultMap currentResultMap;
125     public BasicParameterMap currentParameterMap;
126     public MappedStatement currentStatement;
127     public List JavaDoc parameterMappingList;
128     public List JavaDoc resultMappingList;
129     public int resultMappingIndex;
130     public Map sqlIncludes = new HashMap JavaDoc();
131     public Discriminator discriminator;
132   }
133
134 }
135
Popular Tags