⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleconfiguration.java

📁 这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用struts,hebinate,xml等技术,有丰富的tag,role,navigation,session,dictio
💻 JAVA
字号:
/*
 * Created on 2004-3-10
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.esimple.framework.configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.HashMap;
/**
 * 解析指定格式的xml配置文件<br>,格式基本如下<br>:
 * <cofig><br>
 * 		<group id="XX"><br>
 * 			<catalog id="XX" ><br>
 * 				<item id="XX">XXX</item><br>
 * 			</catalog>
 * 		</group><br>
 * </config>
 * <br>
 * 
 * @author steven
 *
 */
public class SimpleConfiguration {
	private final static String TAG_ROOT 	="config";
	private final static String TAG_LAY1 	="group";
	private final static String TAG_LAY2 	="catalog";
	private final static String TAG_LAY3 	="item";
	private final static String TAG_LAY_ID 	="id";
	
	private String fileName;
	private Configuration conf;

	protected Log logger = LogFactory.getLog(this.getClass());
		
	private HashMap root;
	private HashMap fastItems;
	public void setFileName(String fileName){
		this.fileName = fileName;
	}
	
	public String getFileName(){
		return this.fileName ;
	}
	
	public void init() throws Exception{
		ConfigurationReader parser = new ConfigurationReader();
		conf = parser.buildFromFile( fileName );
		root = new HashMap();
		fastItems = new HashMap();
		Configuration[] confGroup= conf.getChildren();
		for ( int i = 0 ; i < confGroup.length ; i++ ){
			Configuration group = confGroup[i];
			String groupID = group.getAttribute( TAG_LAY_ID );
			logger.debug("groupID:"+ groupID );
			HashMap groupValue = makeGroupValue(groupID,group);
			root.put(groupID , groupValue);
		}		
	}
	
	private HashMap makeGroupValue( String groupID,Configuration group ) throws Exception {
		Configuration[] catalogArray = group.getChildren();
		HashMap catalogValue = null;
		for ( int i = 0 ; i < catalogArray.length ; i++ ){
			Configuration catalog = catalogArray[i];
			String catalogID = catalog.getAttribute( TAG_LAY_ID );
			logger.debug("catalogID:"+  catalogID );
			catalogValue = makeCatalogValue( groupID,catalogID,catalog );
			catalogValue.put( catalogID , catalogValue);
		}	
		return 	catalogValue;
	}

	private HashMap makeCatalogValue( String groupID,String catalogID,Configuration catalog )  throws Exception{
		Configuration[] itemArray = catalog.getChildren();
		HashMap items = new HashMap();
		for ( int i = 0 ; i < itemArray.length ; i++ ){
			Configuration item = itemArray[i];
			String itemID = item.getAttribute( TAG_LAY_ID );
			String itemValue = item.getValue();
						
			items.put( itemID , itemValue);
			
			logger.debug( groupID + "." + catalogID + "." + itemID 
				+ ":" + itemValue
			);
			
			fastItems.put(
				groupID + "." + catalogID + "." + itemID,
				itemValue
			);
		}	
		return items;	
	}
	
	public Configuration getConfiguration(){
		return conf;
	}
	
	/**
	 * 读取一个group的值
	 * @param group
	 * @return HashMap
	 */
	public HashMap getGroup(String group) {
		if( null == group ) return null;
		return (HashMap) root.get( group );
	}
	
	/**
	 * 读取一个catalog的值
	 * @param group
	 * @param catalog
	 * @return HashMap
	 */
	public HashMap getCatalog(String group,String catalog) {
		if( null == group || null == catalog ) return null;
		Object groupMap = root.get( group );
		if( groupMap == null ) return null;
		return (HashMap)((HashMap)groupMap).get(catalog);
	}
	
	/**
	 * 读取配置文件中的一个item的值。
	 * @param group group id
	 * @param catalog 
	 * @param item
	 * @return value
	 */	
	public String getItemValue(String group,String catalog,String item){
		if( null == group || null == item  || null == item ) return null;
		String miscItemName = group + "." + catalog + "." + item;
		return getItemValue( miscItemName );
	}
	
	/**
	 * 读取配置文件中的一个item的值。
	 * @param misItemName 内容为 group.catalog.item
	 * @return String
	 */
	public String getItemValue(String misItemName){
		return fastItems.get( misItemName ).toString();
	}
	
	public void update( boolean backup ){
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -