동적 수정 xml 파일

5119 단어 Utils
다음 글 에서 properties 파일 을 동적 으로 수정 합 니 다.FileWacthdog.java 는 부모 클래스 로 서 코드 는 다음 과 같 습 니 다.
 
import org.apache.commons.digester.Digester;

/**
 * 

* Info: collectionsFavourableActivity.xml ,
* common-digester digester 。 *

*

* Date: 2014-4-28 14:22:54 *

* @author zhangyq * */ public class CollectionsFavourableActiDynamicLoading { //------- ---------------------------------- private static CollectionsFavourableActivity activity; public static CollectionsFavourableActivity getActivity() { return activity; } /** * * @param configFilename * @param delay */ public static void configureAndLoad(String configFilename, long delay) { PropertyWatchdog pdog = new PropertyWatchdog(configFilename); pdog.setDelay(delay); pdog.start(); } //-------------------------------------- -------------------------- /** * Info: * @author zhangyq */ static class PropertyWatchdog extends FileWatchdog { /** * * @param configFileName */ PropertyWatchdog(String filename){ super(filename); } @Override protected void doOnChange() { try { Digester digester = new Digester(); digester.addObjectCreate("CollectionsFavourableActivity", CollectionsFavourableActivity.class); digester.addSetProperties("CollectionsFavourableActivity"); activity = (CollectionsFavourableActivity) digester.parse(file); } catch (Exception e) { return; } } } //--------------------------------------end end-------------------------- }

 Digester 라 이브 러 리 를 사용 하여 xml 파일 을 분석 합 니 다.위 에서 분석 한 xml 파일 은 다음 과 같 습 니 다.
 



 Digester 라 이브 러 리 분석 후 생 성 된 대상 클래스 는:
 
 
public class CollectionsFavourableActivity {

//----------------------------------------------- Instance Constants ----------------------
	private String avtivitySatrtDate;//      
	private String avtivityEndDate;//      
	
	private Double upperLimit;//    
	private Double lowerLimit;//    
	
	private int maxCountEveryMounth;//        

	/**
	 *       
	 */
	public CollectionsFavourableActivity(){
		super();
	}
	
	/**
	 *    	1000 ( )-5000 	10	10
	 *	  	5001-10000 			50	50
	 *	  	10001-50000( ) 	80	80
	 *    	50001-200000 ) 	120	120
	 *	  	200001   			150	150
	 * Info:		                    
	 * @param 		orderPrice		    
	 * @return            
	 */
	public static double cal(double orderPrice){
		double result = 0.0;
		if (orderPrice < 1000 && orderPrice > 0) {
			result = orderPrice;
		} else if (orderPrice >=1000 && orderPrice <= 5000) {
			result = orderPrice - 10;
		}else if (orderPrice > 5000 && orderPrice <= 10000) {
			result = orderPrice - 50;
		}else if (orderPrice > 10000 && orderPrice <= 50000) {
			result = orderPrice - 80;
		}else if (orderPrice > 50000 && orderPrice <= 200000) {
			result = orderPrice - 120;
		}else if (orderPrice >  200000) {
			result = orderPrice - 150;
		}
		return result ;
	}
	
	
	@Override
	public String toString() {
		
		return "{    :"+avtivitySatrtDate+"     :"+avtivityEndDate+"\r
"+ " :"+upperLimit.doubleValue()+" :"+lowerLimit.doubleValue()+"\r
"+ " :"+maxCountEveryMounth+"}"; } //----------------------------------- start getter setter ----------------------------- public Double getUpperLimit() { return upperLimit; } public void setUpperLimit(Double upperLimit) { this.upperLimit = upperLimit; } public Double getLowerLimit() { return lowerLimit; } public void setLowerLimit(Double lowerLimit) { this.lowerLimit = lowerLimit; } public int getMaxCountEveryMounth() { return maxCountEveryMounth; } public void setMaxCountEveryMounth(int maxCountEveryMounth) { this.maxCountEveryMounth = maxCountEveryMounth; } public String getAvtivitySatrtDate() { return avtivitySatrtDate; } public void setAvtivitySatrtDate(String avtivitySatrtDate) { this.avtivitySatrtDate = avtivitySatrtDate; } public String getAvtivityEndDate() { return avtivityEndDate; } public void setAvtivityEndDate(String avtivityEndDate) { this.avtivityEndDate = avtivityEndDate; } //----------------------------------- end getter setter ----------------------------- }

 테스트 사용:
CollectionsFavourableActiDynamicLoading.configureAndLoad(configFilename, delay);
CollectionsFavourableActivity  activity = CollectionsFavourableActiDynamicLoading.getActivity();

 
 

좋은 웹페이지 즐겨찾기