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; 22 import java.util.HashMap; 23 import java.util.List; 24 import java.util.Map; 25 import java.util.Properties; 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 clazz, String propertyName, String javaType, String jdbcType) { 38 return resolveTypeHandler(typeHandlerFactory, clazz, propertyName, javaType, jdbcType, false); 39 } 40 41 public TypeHandler resolveTypeHandler(TypeHandlerFactory typeHandlerFactory, Class clazz, String propertyName, String javaType, String 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.class, jdbcType); 49 } else if (java.util.Map.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 javaClass = Resources.classForName(javaType); 56 handler = typeHandlerFactory.getTypeHandler(javaClass, jdbcType); 57 } catch (Exception 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 type = PROBE.getPropertyTypeForSetter(clazz, propertyName); 69 handler = typeHandlerFactory.getTypeHandler(type, jdbcType); 70 } else { 71 Class type = PROBE.getPropertyTypeForGetter(clazz, propertyName); 72 handler = typeHandlerFactory.getTypeHandler(type, jdbcType); 73 } 74 } else { 75 try { 76 Class javaClass = Resources.classForName(javaType); 77 handler = typeHandlerFactory.getTypeHandler(javaClass, jdbcType); 78 } catch (Exception e) { 79 throw new NestedRuntimeException("Error. Could not set TypeHandler. Cause: " + e, e); 80 } 81 } 82 } 83 return handler; 84 } 85 86 public String applyNamespace(String id) { 87 String 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 txProps = new Properties(); 104 public Properties dsProps = new Properties(); 105 public ErrorContext errorContext = new ErrorContext(); 106 public Properties properties; 107 108 public XmlConverter sqlMapConv; 109 public XmlConverter sqlMapConfigConv; 110 111 public String currentResource = "SQL Map XML Config File"; 112 public String currentNamespace = null; 113 114 public ExtendedSqlMapClient client; 115 public SqlMapExecutorDelegate delegate; 116 public TypeHandlerFactory typeHandlerFactory; 117 public DataSource dataSource; 118 119 public boolean useStatementNamespaces = false; 120 121 // SQL Map Vars 122 public Properties currentProperties; 123 public CacheModel currentCacheModel; 124 public BasicResultMap currentResultMap; 125 public BasicParameterMap currentParameterMap; 126 public MappedStatement currentStatement; 127 public List parameterMappingList; 128 public List resultMappingList; 129 public int resultMappingIndex; 130 public Map sqlIncludes = new HashMap(); 131 public Discriminator discriminator; 132 } 133 134 }