Posted on 2007-10-10 14:50
G_G 阅读(1592)
评论(1) 编辑 收藏 所属分类:
javaGeneral
在WEB开发中
测试 在 C/S 而 实际运行 B/S 造成配置文件 在硬编码时总是改 。
但看 hibernate 的配置文件 确可以很好工作 ,没办法读源码
在框架中 发现 ConfigHelper类起到了作用 。
结合自己开发需求,写出了自己的 带查询文件功能类
扩展的主要部分在
findFile方法 这用了一下 简单的数据结构有兴趣的可以看看
测试:
文件skynet.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<man id='1'>
<name>刘凯毅</name>
<avg>24</avg>
</man>
<man id='2'>
<name>heha</name>
<avg>25</avg>
</man>
</xml-body>
测试类
package test.config;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.XMLDocumentContainer;
import junit.framework.TestCase;
public class SkynetConfigTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
public void testConfigFile(){
System.out.println(
//find file in System user.dir -> skynet.xml
SkynetConfig.getResourceAsFile(System.getProperty("user.dir") ,
"skynet.xml").getPath()
);
System.out.println(
//find file in System java.class.path -> skynet.xml
SkynetConfig.getResourceAsFile(System.getProperty("java.class.path") ,
"skynet.xml").getPath()
);
}
public void testConfigURL(){
JXPathContext jx = JXPathContext.newContext(new XMLDocumentContainer(
// in System user.dir,java.class.path find url:skynet
SkynetConfig.getResourceAsURL("skynet.xml")
));
System.out.println( jx.getValue("//man[avg='24']/@id") );
System.out.println( jx.getValue("//man[avg='24']/name") );
}
public void testConfigStream(){
System.out.println( SkynetConfig.getResourceAsStream("skynet.xml") );
}
}
结果:
E:\src3\rlzy15\lmisWeb\WEB-INF\classes\test\config\skynet.xml
E:\src3\rlzy15\lmisWeb\WEB-INF\classes\test\config\skynet.xml
1
刘凯毅
java.io.FileInputStream@1551d7f
SkynetConfig 类package test.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.XMLDocumentContainer;
import org.hibernate.util.ConfigHelper;
public class SkynetConfig {
public static final URL getResourceAsURL(final String path) {
URL url = null;
// First, try to locate this resource through the current
// context classloader.
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader!=null) {
url = contextClassLoader.getResource(path);
}
if (url != null)
return url;
// Next, try to locate this resource through this class's classloader
url = ConfigHelper.class.getClassLoader().getResource(path);
if (url != null)
return url;
// Next, try to locate this resource through the system classloader
url = ClassLoader.getSystemClassLoader().getResource(path);
if(url !=null)
return url;
File ff = getResourceAsFile(System.getProperty("user.dir") ,path);
if(ff==null)
ff = getResourceAsFile(System.getProperty("java.class.path") , path);
try {
url = new URL("file:/"+ff.getPath());
} catch (Exception e) {e.printStackTrace();}
return url;
}
public static InputStream getResourceAsStream(final String resource) {
String stripped = resource.startsWith("/") ?
resource.substring(1) : resource;
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader!=null) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
SkynetConfig.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = SkynetConfig.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
File ff = getResourceAsFile(System.getProperty("user.dir") ,resource);
if(ff==null)
ff = getResourceAsFile(System.getProperty("java.class.path") , resource);
try {
stream = new FileInputStream(ff);
if(stream==null)
throw new Exception( resource + " not found" );
} catch (Exception e) {e.printStackTrace();}
}
return stream;
}
public static File getResourceAsFile(String str,String findff){
String[] sfd = str.split(";");
for(int i=0;i<sfd.length;i++){
File file = new File(sfd[i]);
if( file.isDirectory()){
File[] cfs = file.listFiles();
for(int j=0;j<cfs.length;j++){
File ff = findFile(cfs[j],findff);
if(ff==null)continue;
else return ff;
}
}
}
return null ;
}
private static File findFile(File file,String findff){
if(file.isFile() && file.exists() && isFileEqu(file,findff) ) return file ;
else if( file.isDirectory() ){
File[] cfs = file.listFiles();
for(int i=0;i<cfs.length;i++){
File ff = findFile(cfs[i],findff);
if(ff==null)continue;
if( isFileEqu(ff,findff) ){
return ff;
}
}
}
return null ;
}
private static boolean isFileEqu(File ff,String findff){
String path = ff.getPath().trim() ;
String findfft = findff.trim() ;
if( path.lastIndexOf( findfft )>0)return true;
return false;
}
}