文件解析

package com.byd.portal.pagedesign.util;

import java.io.File;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class PortalUiLoader{	
	private static Log log=LogFactory.getLog(PortalUiLoader.class);
	private static JSONObject PORTALUICONFIGJSONOBJECT = null;
	static{		
		refresh(false);
	}
	
	/**
	 * 生成控件配置JSONObject
	 */
	public static void refresh(Boolean beta){
		PORTALUICONFIGJSONOBJECT = JSONObject.fromObject(getUiConfigFilesContent(beta));
	}
	
	/**
	 * 读取所有配置文件,生成字符串
	 */	
	private static String getUiConfigFilesContent(final Boolean beta){
		log.debug("beta"+beta);
		//文件路径
		String utfPath = PortalUiLoader.class.getClassLoader().getResource("portalUiConfig").getFile();
//		String utfPath = PortalUiLoader.class.getResource("portalUiConfig").getFile();
		String filePath = null;
		log.debug("utfPath::::"+utfPath);
		log.debug("filePath::::"+filePath);
		try {
			filePath = java.net.URLDecoder.decode(utfPath,"utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		File file = new File(filePath);		
		//配置文件集合 
		Iterator fileIterator = FileUtils.iterateFiles(file,new IOFileFilter(){
			@Override
			public boolean accept(File arg0) {
				//非测试模式下,则读取min文件
				if(beta){
					if(StringUtils.contains(arg0.getName(),"-min.properties")){
						return false;
					}else{
						return true;
					}
				}else{
					if(StringUtils.contains(arg0.getName(),"-min.properties")){
						return true;
					}else{
						return false;
					}	
				}
			}

			@Override
			public boolean accept(File arg0, String arg1) {
				// TODO Auto-generated method stub
				return false;
			}
			
		}, new IOFileFilter(){
			@Override
			public boolean accept(File arg0) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean accept(File arg0, String arg1) {
				// TODO Auto-generated method stub
				return false;
			}			
		});
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		boolean status = false;
		while(fileIterator.hasNext()){
			try {				
				File fileTmp = new File(fileIterator.next().toString());
				String str = FileUtils.readFileToString(fileTmp);
				if(status){
						sb.append(",");
				}
				sb.append(str);
				status = true;				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		sb.append("}");
		return sb.toString();
	}
	
	/**
	 * 这个是一个算法,请不要修改: 根据传入的控件名,返回控件所需要的Js文件引用路径集合
	 * @param comps 控件名数组
	 * @return LinkedHashMap Js文件引用路径字符串集合
	 */
	private static LinkedHashMap<String,String> getRequisiteJs(List<String> comps,LinkedHashMap<String,String> jsMap){
		int i = 0;
		String key = "";
		LinkedHashMap<String, String> lhs = new LinkedHashMap<String, String>();
		while(i<comps.size()){
			String component = comps.get(i);				
			JSONObject jo = PORTALUICONFIGJSONOBJECT.getJSONObject(component);			
			if(!jo.isNullObject() && jo instanceof JSONObject){
				if(StringUtils.equals(jo.getString("type").trim(),"js")){
					if(jo.get("requires") instanceof JSONArray && !jo.getJSONArray("requires").isEmpty()){
						List<String> compRequisiteJs = JSONArray.toList(jo.getJSONArray("requires"));			
						if(compRequisiteJs.size()>0){
							lhs.putAll(getRequisiteJs(compRequisiteJs,jsMap));
						}
					}				
					key = jo.getString("path");
					if(StringUtils.isNotEmpty(key)){
						if(jsMap.get(key) == null){
							lhs.put(key,"");										
						}
						jsMap.put(key,"");	
					}				
				}				
			}
			i++;
		}
		return lhs;
	}	
	
	/**
	 * 获取request中的LinkedHashMap(记录同一个请求中需加载的JS js标签)
	 */
	private static LinkedHashMap<String,String> getRequestRequisiteJs(HttpServletRequest request){
		if(request.getAttribute("jsMap") == null){
			request.setAttribute("jsMap", new LinkedHashMap<String, String>());
		}
		return (LinkedHashMap<String,String>)request.getAttribute("jsMap");
	}
	
	/**
	 * 根据传入的JS文件路径Map、控件集合,返回控件所需要的Js文件引用标签字符串
	 * @param libPath 控件路径Map
	 * @param comps   控件集合
	 * @param HttpServletRequest request
	 * @param beta    应用模式
	 * @return 控件所需要的Js文件引用标签字符串
	 */
	public static String outJavaScriptTag(Map<String,String> libPath,String[] comps,HttpServletRequest request,Boolean beta){
		if(beta){//debug模式下,需重新加载配置文件
			refresh(beta);
		}
		List<String> compList = new ArrayList<String>();	
		String javaScriptTag = "";

		for(int i = 0; i< comps.length; i++){
			compList.add(comps[i]);
		}		
		
		LinkedHashMap<String, String> lhs = getRequisiteJs(compList,getRequestRequisiteJs(request));
		Set<String> compJsSet = lhs.keySet();
		for(String compJs :  compJsSet){		
			String key = StringUtils.substring(compJs,StringUtils.indexOf(compJs, "{")+1, StringUtils.indexOf(compJs, "}"));
			String jsLib = libPath.get(key);
			String tag = "<script type=\"text/javascript\" src=\""+StringUtils.replace(compJs, "{"+key+"}", jsLib)+"\"></script>\n";
			javaScriptTag = javaScriptTag + tag;
		}		
		return javaScriptTag;
	}
	
	/**
	 * 根据传入的字符串,返回控件所需要的Js文件引用标签字符串
	 * @param paramterConfig
	 * @param paramterConfig:{'yui':'yahoo/ui/widget/','portalUi':'portalUi/ui/widget/','components':['animation','button','datatable']}
	 * @param HttpServletRequest request
	 * @param Boolean beta 应用模式
	 * @return 控件所需要的Js文件引用标签字符串
	 */
	public static String outJavaScriptTag(String paramterConfig,HttpServletRequest request,Boolean beta){
		log.debug("paramterConfig"+paramterConfig);
		if(beta){//debug模式下,需重新加载配置文件
			refresh(beta);
		}
		String javaScriptTag = "";

		log.debug("javascriptTag:"+javaScriptTag);
		List<String> compList = new ArrayList<String>();
		JSONObject paramterConfigJsonObject = JSONObject.fromObject(paramterConfig);
		JSONArray compArray = paramterConfigJsonObject.getJSONArray("components");
		
		for(int i = 0; i< compArray.size(); i++){
			compList.add(compArray.getString(i));
		}		
		
		LinkedHashMap<String,String> lhs = getRequisiteJs(compList,getRequestRequisiteJs(request));
		Set<String> compJsSet = lhs.keySet();
		for(String compJs :  compJsSet){
			String key = StringUtils.substring(compJs,StringUtils.indexOf(compJs, "{")+1, StringUtils.indexOf(compJs, "}"));
			String jslib =  paramterConfigJsonObject.getString(key);
			String tag = "<script type=\"text/javascript\" src=\""+StringUtils.replace(compJs, "{"+key+"}", jslib)+"\"></script>\n";
			javaScriptTag = javaScriptTag + tag;
		}	

		log.debug("javascriptTag1:"+javaScriptTag);
		return javaScriptTag;
	}	
	
	/**
	 * 根据传入的字符串,返回控件所需要的Js文件引用标签字符串
	 * @param paramterConfig
	 * @param paramterConfig:{'yui':'yahoo/ui/widget/','portalUi':'portalUi/ui/widget/','components':['animation','button','datatable']}
	 * @param HttpServletRequest request
	 * @return 控件所需要的Js文件引用标签字符串
	 */
	public static String outJavaScriptTag(String paramterConfig,HttpServletRequest request){	
		return outJavaScriptTag(paramterConfig,request,false);
	}
}
 
posted @ 2011-08-06 08:52  上善¤若水  阅读(144)  评论(0编辑  收藏  举报