001: package com.ibatis.sqlmap.engine.builder.xml;
002:
003: import com.ibatis.common.beans.Probe;
004: import com.ibatis.common.beans.ProbeFactory;
005: import com.ibatis.common.resources.Resources;
006:
007: import com.ibatis.sqlmap.engine.cache.CacheModel;
008: import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
009: import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
010: import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap;
011: import com.ibatis.sqlmap.engine.mapping.result.BasicResultMap;
012: import com.ibatis.sqlmap.engine.mapping.result.Discriminator;
013: import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
014: import com.ibatis.sqlmap.engine.scope.ErrorContext;
015: import com.ibatis.sqlmap.engine.type.DomTypeMarker;
016: import com.ibatis.sqlmap.engine.type.TypeHandler;
017: import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
018:
019: import javax.sql.DataSource;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Properties;
024:
025: public abstract class BaseParser {
026:
027: private static final Probe PROBE = ProbeFactory.getProbe();
028:
029: protected final Variables vars;
030:
031: protected BaseParser(Variables vars) {
032: this .vars = vars;
033: }
034:
035: public TypeHandler resolveTypeHandler(
036: TypeHandlerFactory typeHandlerFactory, Class clazz,
037: String propertyName, String javaType, String jdbcType) {
038: return resolveTypeHandler(typeHandlerFactory, clazz,
039: propertyName, javaType, jdbcType, false);
040: }
041:
042: public TypeHandler resolveTypeHandler(
043: TypeHandlerFactory typeHandlerFactory, Class clazz,
044: String propertyName, String javaType, String jdbcType,
045: boolean useSetterToResolve) {
046: TypeHandler handler = null;
047: if (clazz == null) {
048: // Unknown
049: handler = typeHandlerFactory.getUnkownTypeHandler();
050: } else if (DomTypeMarker.class.isAssignableFrom(clazz)) {
051: // DOM
052: handler = typeHandlerFactory.getTypeHandler(String.class,
053: jdbcType);
054: } else if (java.util.Map.class.isAssignableFrom(clazz)) {
055: // Map
056: if (javaType == null) {
057: handler = typeHandlerFactory.getUnkownTypeHandler(); //BUG 1012591 - typeHandlerFactory.getTypeHandler(java.lang.Object.class, jdbcType);
058: } else {
059: try {
060: Class javaClass = Resources.classForName(javaType);
061: handler = typeHandlerFactory.getTypeHandler(
062: javaClass, jdbcType);
063: } catch (Exception e) {
064: throw new RuntimeException(
065: "Error. Could not set TypeHandler. Cause: "
066: + e, e);
067: }
068: }
069: } else if (typeHandlerFactory.getTypeHandler(clazz, jdbcType) != null) {
070: // Primitive
071: handler = typeHandlerFactory
072: .getTypeHandler(clazz, jdbcType);
073: } else {
074: // JavaBean
075: if (javaType == null) {
076: if (useSetterToResolve) {
077: Class type = PROBE.getPropertyTypeForSetter(clazz,
078: propertyName);
079: handler = typeHandlerFactory.getTypeHandler(type,
080: jdbcType);
081: } else {
082: Class type = PROBE.getPropertyTypeForGetter(clazz,
083: propertyName);
084: handler = typeHandlerFactory.getTypeHandler(type,
085: jdbcType);
086: }
087: } else {
088: try {
089: Class javaClass = Resources.classForName(javaType);
090: handler = typeHandlerFactory.getTypeHandler(
091: javaClass, jdbcType);
092: } catch (Exception e) {
093: throw new RuntimeException(
094: "Error. Could not set TypeHandler. Cause: "
095: + e, e);
096: }
097: }
098: }
099: return handler;
100: }
101:
102: public String applyNamespace(String id) {
103: String newId = id;
104: if (vars.currentNamespace != null
105: && vars.currentNamespace.length() > 0 && id != null
106: && id.indexOf('.') < 0) {
107: newId = vars.currentNamespace + "." + id;
108: }
109: return newId;
110: }
111:
112: /**
113: * Variables the parser uses. This "struct" like class is necessary because
114: * anonymous inner classes do not have access to non-final member fields of the parent class.
115: * This way, we can make the Variables instance final, and use all of its public fields as
116: * variables for parsing state.
117: */
118: protected static class Variables {
119: public ErrorContext errorCtx = new ErrorContext();
120:
121: public Properties txProps = new Properties();
122: public Properties dsProps = new Properties();
123: public ErrorContext errorContext = new ErrorContext();
124: public Properties properties;
125:
126: public XmlConverter sqlMapConv;
127: public XmlConverter sqlMapConfigConv;
128:
129: public String currentResource = "SQL Map XML Config File";
130: public String currentNamespace = null;
131:
132: public ExtendedSqlMapClient client;
133: public SqlMapExecutorDelegate delegate;
134: public TypeHandlerFactory typeHandlerFactory;
135: public DataSource dataSource;
136:
137: public boolean useStatementNamespaces = false;
138:
139: public Integer defaultStatementTimeout = null;
140:
141: // SQL Map Vars
142: public Properties currentProperties;
143: public CacheModel currentCacheModel;
144: public BasicResultMap currentResultMap;
145: public BasicParameterMap currentParameterMap;
146: public MappedStatement currentStatement;
147: public List parameterMappingList;
148: public List resultMappingList;
149: public int resultMappingIndex;
150: public Map sqlIncludes = new HashMap();
151: public Discriminator discriminator;
152: }
153:
154: }
|