解析${}.doc_第1页
解析${}.doc_第2页
解析${}.doc_第3页
解析${}.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package redolvePlaceHolder;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.Map;import java.util.Properties;import java.util.Map.Entry;/* * 此类借用了hibernate读取properties文件的代码, * 可以解析占位符比如$ * author afei * */public class TestMain private static final String PLACEHOLDER_START = $;private static Properties pro;public static void main(String args) try pro=buildProperty(perties);resolvePlaceHolders(pro);printProperties(pro); catch (IOException e) e.printStackTrace();public static void printProperties(Properties p)Iterator it=p.entrySet().iterator();while(it.hasNext()Entry ent=(Entry)it.next();System.out.println(ent.getKey()+ : +ent.getValue();/* * 解析占位符 * param properties */public static void resolvePlaceHolders(Properties properties) Iterator itr = properties.entrySet().iterator();while ( itr.hasNext() ) final Map.Entry entry = ( Map.Entry ) itr.next();final Object value = entry.getValue();if ( value != null & String.class.isInstance( value ) ) final String resolved = resolvePlaceHolder( ( String ) value );if ( !value.equals( resolved ) ) if ( resolved = null ) itr.remove();else entry.setValue( resolved );/* * 解析占位符具体操作 * param property * return */public static String resolvePlaceHolder(String property) if ( property.indexOf( PLACEHOLDER_START ) 0 ) return property;StringBuffer buff = new StringBuffer();char chars = property.toCharArray();for ( int pos = 0; pos chars.length; pos+ ) if ( charspos = $ ) / peek aheadif ( charspos+1 = ) / we have a placeholder, spin forward till we find the endString systemPropertyName = ;int x = pos + 2;for ( ; x = chars.length ) break;buff.append( charspos );String rtn = buff.toString();return isEmpty( rtn ) ? null : rtn;/* * 构造properties文件 * param path * throws IOException */public static Properties buildProperty(String path) throws IOExceptionInputStream is=getResourceAsStream(perties);pro=new Properties();pro.load(is);return pro;/* * 构造properties文件的流 * param resource * return */public static InputStream getResourceAsStream(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 ) stream = TestMain.class.getResourceAsStream( resource );if ( stream = null ) stream = TestMain.class.getClassLoader().getResourceAsStream( stripped );if ( stream = null ) throw new RuntimeException( resource + not found );return stream;/* * 获得系统属性 当然 你可以选择从别的地方获取值 * param systemPropertyName * return */private static String extractFromSystem(String systemPropertyName) try return System.getProperty( systemPropertyName );catch( Throwable t ) return nul

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论