- 4
- 0
- 约4.56千字
- 约 4页
- 2017-06-10 发布于北京
- 举报
从XML文件中读取数据,将其生成工厂的一个类。这个类最主要的一段程序就是初始化工厂,该功能归纳起来就是三部分功能:用各种方式尝试读取文件、以DOM的方式解析XML数据流、生成工厂。?*?初始化工厂。根据路径读取XML文件,将XML文件中的数据装载到工厂中?
?*?@param?path?XML的路径?
?*/??
public?void?initFactory(String?path){??
????if(findOnlyOneFileByClassPath(path)){return;}??
????if(findResourcesByUrl(path)){return;}??
????if(findResourcesByFile(path)){return;}??
????this.paths?=?new?String[]{path};??
}??
??
/**?
?*?初始化工厂。根据路径列表依次读取XML文件,将XML文件中的数据装载到工厂中?
?*?@param?paths?路径列表?
?*/??
public?void?initFactory(String[]?paths){??
????for(int?i=0;?ipaths.length;?i++){??
????????initFactory(paths[i]);??
????}??
????this.paths?=?paths;??
}??
??
/**?
?*?重新初始化工厂,初始化所需的参数,为上一次初始化工厂所用的参数。?
?*/??
public?void?reloadFactory(){??
????initFactory(this.paths);??
}??
??
/**?
?*?采用ClassLoader的方式试图查找一个文件,并调用codereadXmlStream()/code进行解析?
?*?@param?path?XML文件的路径?
?*?@return?是否成功?
?*/??
protected?boolean?findOnlyOneFileByClassPath(String?path){??
????boolean?success?=?false;??
????try?{??
????????Resource?resource?=?new?ClassPathResource(path,?this.getClass());??
????????resource.setFilter(this.getFilter());??
????????InputStream?is?=?resource.getInputStream();??
????????if(is==null){return?false;}??
????????readXmlStream(is);??
????????success?=?true;??
????}?catch?(SAXException?e)?{??
????????log.debug(Error?when?findOnlyOneFileByClassPath:+path,e);??
????}?catch?(IOException?e)?{??
????????log.debug(Error?when?findOnlyOneFileByClassPath:+path,e);??
????}?catch?(ParserConfigurationException?e)?{??
????????log.debug(Error?when?findOnlyOneFileByClassPath:+path,e);??
????}??
????return?success;??
}??
??
/**?
?*?采用URL的方式试图查找一个目录中的所有XML文件,并调用codereadXmlStream()/code进行解析?
?*?@param?path?XML文件的路径?
?*?@return?是否成功?
?*/??
protected?boolean?findResourcesByUrl(String?path){??
????boolean?success?=?false;??
????try?{??
????????ResourcePath?resourcePath?=?new?PathMatchResource(path,?this.getClass());??
????????resourcePath.setFilter(this.getFilter());??
????????Resource[]?loaders?=?resourcePath.getResources();??
????????for(in
原创力文档

文档评论(0)