SqlMapConfigParser

mengsun 2011-01-14

packagecom.ibatis.sqlmap.engine.builder.xml;

importcom.ibatis.common.resources.*;

importcom.ibatis.common.xml.*;

importcom.ibatis.sqlmap.client.*;

importcom.ibatis.sqlmap.engine.config.*;

importcom.ibatis.sqlmap.engine.transaction.*;

importcom.ibatis.sqlmap.engine.datasource.*;

importcom.ibatis.sqlmap.engine.mapping.result.*;

importorg.w3c.dom.Node;

importjava.io.*;

importjava.util.Properties;

//璇ョ被涓昏鏄鐞嗚В鏋怱QLMap閰嶇疆鏂囦欢

publicclassSqlMapConfigParser{

//parser鍙橀噺鏄В鏋怷ML鐨勬牳蹇�

protectedfinalNodeletParserparser=newNodeletParser();

//state鍙橀噺鍏呭綋涓�釜涓棿鍙橀噺鐨勫鍣紝鎴栬�鏄竴涓厤缃俊鎭殑闂ㄦ埛

privateXmlParserStatestate=newXmlParserState();

privatebooleanusingStreams=false;

publicSqlMapConfigParser(){

//楠岃瘉dtd鏂囦欢锛岃缃獙璇佸弬鏁�

parser.setValidation(true);

parser.setEntityResolver(newSqlMapClasspathEntityResolver());

//杩涜鍒濆鍖栫殑璧嬪�澶勭悊

addSqlMapConfigNodelets();

addGlobalPropNodelets();

addSettingsNodelets();

addTypeAliasNodelets();

addTypeHandlerNodelets();

addTransactionManagerNodelets();

addSqlMapNodelets();

addResultObjectFactoryNodelets();

}

publicSqlMapClientparse(Readerreader,Propertiesprops){

if(props!=null)

state.setGlobalProps(props);

returnparse(reader);

}

//鍩轰簬瀛楃妯″紡瑙f瀽XML閰嶇疆鏂囦欢

publicSqlMapClientparse(Readerreader){

try{

usingStreams=false;

parser.parse(reader);

returnstate.getConfig().getClient();

}catch(Exceptione){

thrownewRuntimeException("Erroroccurred.Cause:"+e,e);

}

}

publicSqlMapClientparse(InputStreaminputStream,Propertiesprops){

if(props!=null)

state.setGlobalProps(props);

returnparse(inputStream);

}

//鍩轰簬瀛楄妭妯″紡瑙f瀽XML閰嶇疆鏂囦欢

publicSqlMapClientparse(InputStreaminputStream){

try{

usingStreams=true;

parser.parse(inputStream);

returnstate.getConfig().getClient();

}catch(Exceptione){

thrownewRuntimeException("Erroroccurred.Cause:"+e,e);

}

}

//澶勭悊sqlMapConfig鏍硅妭鐐逛俊鎭紝琛ㄦ槑鍏跺凡缁忕粨鏉�

privatevoidaddSqlMapConfigNodelets(){

parser.addNodelet("/sqlMapConfig/end()",newNodelet(){

publicvoidprocess(Nodenode)throwsException{

state.getConfig().finalizeSqlMapConfig();

}

});

}

//澶勭悊鑺傜偣/sqlMapConfig/properties淇℃伅锛屼骇鐢熶竴涓叏灞�殑Properties

privatevoidaddGlobalPropNodelets(){

parser.addNodelet("/sqlMapConfig/properties",newNodelet(){

publicvoidprocess(Nodenode)throwsException{

//鑾峰緱/sqlMapConfig/properties鑺傜偣涓嬬殑鍏ㄩ儴灞炴�鍊硷紝骞舵妸杩欎簺灞炴�鍊艰祴鍊肩粰Properties绫诲瀷鐨勫彉閲廰ttributes

Propertiesattributes=NodeletUtils.parseAttributes(node,

state.getGlobalProps());

Stringresource=attributes.getProperty("resource");

Stringurl=attributes.getProperty("url");

//鐢眘tate鍙橀噺浣滀负涓棿浣撲紶閫掍俊鎭�

state.setGlobalProperties(resource,url);

}

});

}

//澶勭悊鑺傜偣/sqlMapConfig/settings淇℃伅锛屼骇鐢熷叏灞�彉閲�

privatevoidaddSettingsNodelets(){

parser.addNodelet("/sqlMapConfig/settings",newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesattributes=NodeletUtils.parseAttributes(node,

state.getGlobalProps());

SqlMapConfigurationconfig=state.getConfig();

StringclassInfoCacheEnabledAttr=attributes

.getProperty("classInfoCacheEnabled");

booleanclassInfoCacheEnabled=(classInfoCacheEnabledAttr==null||"true"

.equals(classInfoCacheEnabledAttr));

config.setClassInfoCacheEnabled(classInfoCacheEnabled);

StringlazyLoadingEnabledAttr=attributes

.getProperty("lazyLoadingEnabled");

booleanlazyLoadingEnabled=(lazyLoadingEnabledAttr==null||"true"

.equals(lazyLoadingEnabledAttr));

config.setLazyLoadingEnabled(lazyLoadingEnabled);

StringstatementCachingEnabledAttr=attributes

.getProperty("statementCachingEnabled");

booleanstatementCachingEnabled=(statementCachingEnabledAttr==null||"true"

.equals(statementCachingEnabledAttr));

config.setStatementCachingEnabled(statementCachingEnabled);

StringcacheModelsEnabledAttr=attributes

.getProperty("cacheModelsEnabled");

booleancacheModelsEnabled=(cacheModelsEnabledAttr==null||"true"

.equals(cacheModelsEnabledAttr));

config.setCacheModelsEnabled(cacheModelsEnabled);

StringenhancementEnabledAttr=attributes

.getProperty("enhancementEnabled");

booleanenhancementEnabled=(enhancementEnabledAttr==null||"true"

.equals(enhancementEnabledAttr));

config.setEnhancementEnabled(enhancementEnabled);

StringuseColumnLabelAttr=attributes

.getProperty("useColumnLabel");

booleanuseColumnLabel=(useColumnLabelAttr==null||"true"

.equals(useColumnLabelAttr));

config.setUseColumnLabel(useColumnLabel);

StringforceMultipleResultSetSupportAttr=attributes

.getProperty("forceMultipleResultSetSupport");

booleanforceMultipleResultSetSupport="true"

.equals(forceMultipleResultSetSupportAttr);

config

.setForceMultipleResultSetSupport(forceMultipleResultSetSupport);

StringdefaultTimeoutAttr=attributes

.getProperty("defaultStatementTimeout");

IntegerdefaultTimeout=defaultTimeoutAttr==null?null

:Integer.valueOf(defaultTimeoutAttr);

config.setDefaultStatementTimeout(defaultTimeout);

StringuseStatementNamespacesAttr=attributes

.getProperty("useStatementNamespaces");

booleanuseStatementNamespaces="true"

.equals(useStatementNamespacesAttr);

state.setUseStatementNamespaces(useStatementNamespaces);

}

});

}

//澶勭悊鑺傜偣/sqlMapConfig/typeAlias淇℃伅锛屽鐞嗗埆鍚嶏紙typeAlias锛夎浆鍖�

privatevoidaddTypeAliasNodelets(){

parser.addNodelet("/sqlMapConfig/typeAlias",newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesprop=NodeletUtils.parseAttributes(node,state

.getGlobalProps());

Stringalias=prop.getProperty("alias");

Stringtype=prop.getProperty("type");

//鑾峰彇鍒悕鍙婂叾瀵瑰簲鐨凜lass绫诲瀷锛屾敞鍐屽埌TypeHandlerFactory宸ュ巶瀵硅薄涓�

state.getConfig().getTypeHandlerFactory().putTypeAlias(alias,

type);

}

});

}

//澶勭悊鑺傜偣/sqlMapConfig/typeHandler淇℃伅锛屽鍔犳柊鐨刯dbcType

privatevoidaddTypeHandlerNodelets(){

parser.addNodelet("/sqlMapConfig/typeHandler",newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesprop=NodeletUtils.parseAttributes(node,state

.getGlobalProps());

StringjdbcType=prop.getProperty("jdbcType");

StringjavaType=prop.getProperty("javaType");

Stringcallback=prop.getProperty("callback");

javaType=state.getConfig().getTypeHandlerFactory()

.resolveAlias(javaType);

callback=state.getConfig().getTypeHandlerFactory()

.resolveAlias(callback);

state.getConfig().newTypeHandler(

Resources.classForName(javaType),jdbcType,

Resources.instantiate(callback));

}

});

}

//澶勭悊鑺傜偣/sqlMapConfig/transactionManager淇℃伅锛屽寘鎷瑃ransactionManager鍜宒ataSource

privatevoidaddTransactionManagerNodelets(){

//澶勭悊鑺傜偣/sqlMapConfig/transactionManager/property鐨勪俊鎭紝杞寲鍒皌ransactionManager瀵硅薄鐨勭浉鍏冲睘鎬т腑

parser.addNodelet("/sqlMapConfig/transactionManager/property",

newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesattributes=NodeletUtils.parseAttributes(

node,state.getGlobalProps());

Stringname=attributes.getProperty("name");

Stringvalue=NodeletUtils.parsePropertyTokens(

attributes.getProperty("value"),state

.getGlobalProps());

state.getTxProps().setProperty(name,value);

}

});

//澶勭悊鑺傜偣/sqlMapConfig/transactionManager/锛岀敓鎴恡ransactionManager瀵硅薄骞惰繘琛岀粨鏉熷鐞�

parser.addNodelet("/sqlMapConfig/transactionManager/end()",

newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesattributes=NodeletUtils.parseAttributes(

node,state.getGlobalProps());

Stringtype=attributes.getProperty("type");

booleancommitRequired="true".equals(attributes

.getProperty("commitRequired"));

state.getConfig().getErrorContext().setActivity(

"configuringthetransactionmanager");

type=state.getConfig().getTypeHandlerFactory()

.resolveAlias(type);

TransactionManagertxManager;

try{

state.getConfig().getErrorContext().setMoreInfo(

"Checkthetransactionmanagertypeorclass.");

TransactionConfigconfig=(TransactionConfig)Resources

.instantiate(type);

config.setDataSource(state.getDataSource());

state

.getConfig()

.getErrorContext()

.setMoreInfo(

"Checkthetransactionmanagerpropertiesorconfiguration.");

config.setProperties(state.getTxProps());

config.setForceCommit(commitRequired);

config.setDataSource(state.getDataSource());

state.getConfig().getErrorContext().setMoreInfo(

null);

txManager=newTransactionManager(config);

}catch(Exceptione){

if(einstanceofSqlMapException){

throw(SqlMapException)e;

}else{

thrownewSqlMapException(

"ErrorinitializingTransactionManager.CouldnotinstantiateTransactionConfig.Cause:"

+e,e);

}

}

state.getConfig().setTransactionManager(txManager);

}

});

//澶勭悊鑺傜偣/sqlMapConfig/transactionManager/dataSource/property锛屽鐞哾ataSource鑺傜偣鐨刾roperty鍐呭

parser.addNodelet(

"/sqlMapConfig/transactionManager/dataSource/property",

newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesattributes=NodeletUtils.parseAttributes(

node,state.getGlobalProps());

Stringname=attributes.getProperty("name");

Stringvalue=NodeletUtils.parsePropertyTokens(

attributes.getProperty("value"),state

.getGlobalProps());

state.getDsProps().setProperty(name,value);

}

});

//澶勭悊鑺傜偣/sqlMapConfig/transactionManager/dataSource/end()锛屽姩鎬佸疄渚嬪寲dataSource瀵硅薄锛岀劧鍚庤繘琛屽睘鎬у垵濮嬪寲

parser.addNodelet("/sqlMapConfig/transactionManager/dataSource/end()",

newNodelet(){

publicvoidprocess(Nodenode)throwsException{

state.getConfig().getErrorContext().setActivity(

"configuringthedatasource");

Propertiesattributes=NodeletUtils.parseAttributes(

node,state.getGlobalProps());

Stringtype=attributes.getProperty("type");

Propertiesprops=state.getDsProps();

type=state.getConfig().getTypeHandlerFactory()

.resolveAlias(type);

try{

state.getConfig().getErrorContext().setMoreInfo(

"Checkthedatasourcetypeorclass.");

DataSourceFactorydsFactory=(DataSourceFactory)Resources

.instantiate(type);

state.getConfig().getErrorContext().setMoreInfo(

"Checkthedatasourcepropertiesorconfiguration.");

dsFactory.initialize(props);

state.setDataSource(dsFactory.getDataSource());

state.getConfig().getErrorContext().setMoreInfo(

null);

}catch(Exceptione){

if(einstanceofSqlMapException){

throw(SqlMapException)e;

}else{

thrownewSqlMapException(

"ErrorinitializingDataSource.CouldnotinstantiateDataSourceFactory.Cause:"

+e,e);

}

}

}

});

}

//澶勭悊鑺傜偣/sqlMapConfig/sqlMap锛屽幓鑾峰彇sqlMap鐨勬槧灏勬枃浠讹紝鐒跺悗瀵瑰悇涓槧灏勬枃浠惰繘琛岃鍙栧鐞�

protectedvoidaddSqlMapNodelets(){

parser.addNodelet("/sqlMapConfig/sqlMap",newNodelet(){

publicvoidprocess(Nodenode)throwsException{

state.getConfig().getErrorContext().setActivity(

"loadingtheSQLMapresource");

Propertiesattributes=NodeletUtils.parseAttributes(node,

state.getGlobalProps());

Stringresource=attributes.getProperty("resource");

Stringurl=attributes.getProperty("url");

if(usingStreams){

//閲囩敤瀛楄妭娴佹ā寮忔潵璇诲彇鏂囦欢

InputStreaminputStream=null;

if(resource!=null){

state.getConfig().getErrorContext().setResource(

resource);

inputStream=Resources.getResourceAsStream(resource);

}elseif(url!=null){

state.getConfig().getErrorContext().setResource(url);

inputStream=Resources.getUrlAsStream(url);

}else{

thrownewSqlMapException(

"The<sqlMap>elementrequireseitheraresourceoraurlattribute.");

}

newSqlMapParser(state).parse(inputStream);

}else{

//閲囩敤瀛楃娴佹ā寮忔潵璇诲彇鏂囦欢

Readerreader=null;

if(resource!=null){

state.getConfig().getErrorContext().setResource(

resource);

reader=Resources.getResourceAsReader(resource);

}elseif(url!=null){

state.getConfig().getErrorContext().setResource(url);

reader=Resources.getUrlAsReader(url);

}else{

thrownewSqlMapException(

"The<sqlMap>elementrequireseitheraresourceoraurlattribute.");

}

newSqlMapParser(state).parse(reader);

}

}

});

}

//澶勭悊鑺傜偣/sqlMapConfig/resultObjectFactory锛屽垵濮嬪寲鏂扮殑resultObjectFactory

privatevoidaddResultObjectFactoryNodelets(){

parser.addNodelet("/sqlMapConfig/resultObjectFactory",newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesattributes=NodeletUtils.parseAttributes(node,

state.getGlobalProps());

Stringtype=attributes.getProperty("type");

state.getConfig().getErrorContext().setActivity(

"configuringtheResultObjectFactory");

ResultObjectFactoryrof;

try{

rof=(ResultObjectFactory)Resources.instantiate(type);

state.getConfig().setResultObjectFactory(rof);

}catch(Exceptione){

thrownewSqlMapException(

"ErrorinstantiatingresultObjectFactory:"+type,

e);

}

}

});

parser.addNodelet("/sqlMapConfig/resultObjectFactory/property",

newNodelet(){

publicvoidprocess(Nodenode)throwsException{

Propertiesattributes=NodeletUtils.parseAttributes(

node,state.getGlobalProps());

Stringname=attributes.getProperty("name");

Stringvalue=NodeletUtils.parsePropertyTokens(

attributes.getProperty("value"),state

.getGlobalProps());

state.getConfig().getDelegate()

.getResultObjectFactory().setProperty(name,

value);

}

});

}

}

相关推荐