TLD生成

package j2ee.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Tld文件生成器
 * @author kht
 */
public class TldFileCreator {
	
	
	public static final Integer REQUIRED_FIELD = 1;
	
	public static final Integer P_FIELD = 0;
	
	/*
	 * 存放不希望被添加到tld文件中的属性
	 */
	private static List<String> noWantDefinedFields = new ArrayList<String>();
	
	/*
	 * 存放必须设置的属性
	 */
	private static List<String> requiredFields = new ArrayList<String>();

	/**
	 * 批量设置不想被添加到tld文件中的属性
	 * @author kht
	 * @param noWantDefinedFields
	 */
	public static void setNoWantDefinedFields(List<String> noWantDefinedFields){
		for(int i=0;noWantDefinedFields!=null&&i<noWantDefinedFields.size();i++){
			TldFileCreator.noWantDefinedFields.add(noWantDefinedFields.get(i));
		}
	}
	
	/**
	 * 批量设置必须填写的属性
	 * @author kht
	 * @param requiredFields
	 */
	public static void setRequiredFields(List<String> requiredFields){
		for(int i=0;requiredFields!=null&&i<requiredFields.size();i++){
			TldFileCreator.requiredFields.add(requiredFields.get(i));
		}
	}
	
	/**
	 * 生成Tld文件
	 * @author kht
	 * @param fileName 文件名
	 * @param fileCreatePath 保存路径
	 * @param shortName tag shortname
	 * @param tagObjMap 以tag的name为键,类路径为值的哈希表
	 */
	public static void createTld(String fileName,String fileCreatePath,String shortName,Map<String,String> tagObjMap){
		initNoWantDefinedField();
		StringBuffer sgb = createTagFileHeader(shortName);
		sgb = createTagFileBody(sgb, tagObjMap);
		sgb = createTagFileFooter(sgb);
		fileOutput(fileName, fileCreatePath, sgb);
	}
	
	/*
	 * 输出tld文件
	 * @param fileName
	 * @param fileCreatePath
	 * @param tagFile
	 */
	private static void fileOutput(String fileName,String fileCreatePath,StringBuffer tagFile){
		BufferedOutputStream bos = null;
		try{
			bos = new BufferedOutputStream(new FileOutputStream(new File(fileCreatePath+fileName+".tld")));
			bos.write(tagFile.toString().getBytes());
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try {
				bos.close();
			} catch (IOException e) {
			}
		}
	}
	
	/**
	 * 得到需要添加到标签库中的属性
	 * 只能提取当前对象内部定义的属性,不能得到父类的属性
	 * 可以通过第二个参数传入父类的属性
	 * key为处理方式  value为属性列表
	 */
	protected static Map<Integer,Set<String>> getTagDefineFields(Object obj,Map<Integer,Set<String>> fieldNameMap){
		Field[] fields = null;
		if(obj!=null&&(fields=obj.getClass().getDeclaredFields()).length>0){
			Map<Integer,Set<String>> map = fieldNameMap;
			if(map==null) map = new HashMap<Integer,Set<String>>();
			for(int i=0;i<fields.length;i++){
				String fieldName = fields[i].getName();
				if(noWantDefinedFields.contains(fieldName)) continue;
				if(requiredFields.contains(fieldName)){
					Set<String> rfList = map.get(TldFileCreator.REQUIRED_FIELD);
					if(rfList==null){
						rfList = new HashSet<String>();
						map.put(TldFileCreator.REQUIRED_FIELD, rfList);
					}
					rfList.add(fieldName);
				}else{
					Set<String> pfList = map.get(TldFileCreator.P_FIELD);
					if(pfList==null){
						pfList = new HashSet<String>();
						map.put(TldFileCreator.P_FIELD, pfList);
					}
					pfList.add(fieldName);
				}
			}
			return map;
		}
		
		return fieldNameMap;
	}
	
	/**
	 * 得到该标签对象的所有要添加的属性
	 * @param obj
	 * @return
	 */
	protected static Map<Integer,Set<String>> getTagObjAllFields(Object obj){
		return getTagDefineFields(obj, null);
	}
	
	/**
	 * 创建文件体
	 */
	private static StringBuffer createTagFileBody(StringBuffer sgf,Map<String,String> tagObjMap){
		Set<String> keySet = tagObjMap.keySet();
		for(String name : keySet){
			String clazz = tagObjMap.get(name);
			try {
				Object obj = Class.forName(clazz).newInstance();
				Map<Integer,Set<String>> fieldsMap = getTagObjAllFields(obj);
				if(fieldsMap!=null){
					sgf.append("\t<tag>");
					sgf.append("\n");
					sgf.append("\t\t<name>"+name+"</name>");
					sgf.append("\n");
					sgf.append("\t\t<tagclass>"+clazz+"</tagclass>");
					sgf.append("\n");
					Set<String> fields = fieldsMap.get(TldFileCreator.REQUIRED_FIELD);
					if(fields!=null&&fields.size()>0){
						for(String attr : fields){
							sgf.append("\t\t<attribute>");
							sgf.append("\n");
							sgf.append("\t\t\t<name>"+attr+"</name>");
							sgf.append("\n");
							sgf.append("\t\t\t<required>true</required>");
							sgf.append("\n");
							sgf.append("\t\t\t<rtexprvalue>false</rtexprvalue>");sgf.append("\n");
							sgf.append("\t\t</attribute>");sgf.append("\n");
						}
					}
					fields = fieldsMap.get(TldFileCreator.P_FIELD);
					if(fields!=null&&fields.size()>0){
						for(String attr : fields){
							sgf.append("\t\t<attribute>");sgf.append("\n");
							sgf.append("\t\t\t<name>"+attr+"</name>");sgf.append("\n");
							sgf.append("\t\t\t<required>false</required>");sgf.append("\n");
							sgf.append("\t\t\t<rtexprvalue>false</rtexprvalue>");sgf.append("\n");
							sgf.append("\t\t</attribute>");sgf.append("\n");
						}
					}
					sgf.append("\t</tag>");
					sgf.append("\n");sgf.append("\n");
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return sgf;
	}
	
	/**
	 * 创建头部内容
	 * @return
	 */
	protected static StringBuffer createTagFileHeader(String shortName){
		StringBuffer sgb = new StringBuffer();
		sgb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sgb.append("\n");
		sgb.append("<!DOCTYPE taglib PUBLIC \"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN\"");
		sgb.append("\n");
		sgb.append("\"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd\">");
		sgb.append("\n\n");
		sgb.append("<taglib>");
		sgb.append("\n");
		sgb.append("\t<tlibversion>1.0</tlibversion>");
		sgb.append("\n");
		sgb.append("\t<jspversion>1.1</jspversion>");
		sgb.append("\n");
		sgb.append("\t<shortname>"+shortName+"</shortname>");
		sgb.append("\n");
		return sgb;
	}
	
	/**
	 * 创建尾部内容
	 * @param sgb
	 * @return
	 */
	protected static StringBuffer createTagFileFooter(StringBuffer sgb){
		sgb.append("</taglib>");
		return sgb;
	}
	
	/**
	 * 初始化不想被添加到tld文件的属性
	 */
	protected static void initNoWantDefinedField(){
		noWantDefinedFields.add("serialVersionUID");
	}
	
	public static void main(String[] args) {
		String classPrefix = "j2ee.tag.sftd.";
		List<String> requiredField = new ArrayList<String>();
		requiredField.add("id");
		setRequiredFields(requiredField);
		List<String> noWantDefinedFields = new ArrayList<String>();
		noWantDefinedFields.add("TODAY_SELE_VALUE");
		noWantDefinedFields.add("YESTODAY_SELE_VALUE");
		noWantDefinedFields.add("TOMORROW_SELE_VALUE");
		noWantDefinedFields.add("MONTH_BEGIN_SELE_VALUE");
		noWantDefinedFields.add("MONTH_AFTER_SELE_VALUE");
		noWantDefinedFields.add("PREFIX");
		noWantDefinedFields.add("COMBO_PREFIX");
		setNoWantDefinedFields(noWantDefinedFields);
		Map<String,String> tagObjMap = new HashMap<String,String>();
		//SFTD.tld file
		tagObjMap.put("checkBoxSingle", classPrefix+"CheckBox"+"Tag");
		tagObjMap.put("DropDownDefinedList", classPrefix+"DropDownDefinedList"+"Tag");
		tagObjMap.put("DropDownLookup", classPrefix+"DropDownLookup"+"Tag");
		tagObjMap.put("DropDownMulti", classPrefix+"DropDownMulti"+"Tag");
		tagObjMap.put("FilterItem", classPrefix+"FilterItem"+"Tag");
		tagObjMap.put("HierarchyGrid2", classPrefix+"HierarchyGrid2"+"Tag");
		tagObjMap.put("HierarchyGrid", classPrefix+"HierarchyGrid"+"Tag");
		tagObjMap.put("LookupDependency", classPrefix+"LookupDependency"+"Tag");
		tagObjMap.put("RadioButtonDefinedList", classPrefix+"RadioButtonDefinedList"+"Tag");
		tagObjMap.put("Scheduler", classPrefix+"Scheduler"+"Tag");
		tagObjMap.put("SingleRecordGrid", classPrefix+"SingleRecordGrid"+"Tag");
		tagObjMap.put("SliderControl", classPrefix+"SliderControl"+"Tag");
		tagObjMap.put("SuperEditGrid", classPrefix+"SuperEditGrid"+"Tag");
		tagObjMap.put("SuperGrid", classPrefix+"SuperGrid"+"Tag");
		tagObjMap.put("SuperGroupingGrid", classPrefix+"SuperGroupingGrid"+"Tag");
		tagObjMap.put("TextBoxDateKeyword", classPrefix+"TextBoxDateKeyword"+"Tag");
		tagObjMap.put("TextBoxDate", classPrefix+"TextBoxDate"+"Tag");
		tagObjMap.put("TextBoxDefinedList", classPrefix+"TextBoxDefinedList"+"Tag");
		tagObjMap.put("TextBoxDouble", classPrefix+"TextBoxDouble"+"Tag");
		tagObjMap.put("TextBoxInt", classPrefix+"TextBoxInt"+"Tag");
		tagObjMap.put("TextBoxLong", classPrefix+"TextBoxLong"+"Tag");
		tagObjMap.put("TextBoxLookupDrop", classPrefix+"TextBoxLookupDrop"+"Tag");
		tagObjMap.put("TextBoxLookup", classPrefix+"TextBoxLookup"+"Tag");
		tagObjMap.put("TextBoxMoney", classPrefix+"TextBoxMoney"+"Tag");
		tagObjMap.put("TextBoxString", classPrefix+"TextBoxString"+"Tag");
		tagObjMap.put("UploadControl", classPrefix+"UploadControl"+"Tag");
		tagObjMap.put("ValueimageColumnCollection", classPrefix+"ValueimageColumnCollection"+"Tag");
		tagObjMap.put("ValueimageColumn", classPrefix+"ValueimageColumn"+"Tag");
		tagObjMap.put("Valueimage", classPrefix+"Valueimage"+"Tag");
		tagObjMap.put("CommonView", classPrefix+"CommonView"+"Tag");
		createTld("SFTD", "f:\\", "sftd", tagObjMap);
		
		//SFTL.tld file
		classPrefix = "j2ee.tag.sftl.";
		tagObjMap = new HashMap<String,String>();
		tagObjMap.put("BarBreadCrumbs", classPrefix+"BarBreadCrumbs"+"Tag");
		tagObjMap.put("BarButtons", classPrefix+"BarButtons"+"Tag");
		tagObjMap.put("BarErrorHandler", classPrefix+"BarErrorHandler"+"Tag");
		tagObjMap.put("BarFooter", classPrefix+"BarFooter"+"Tag");
		tagObjMap.put("BarPanRecords", classPrefix+"BarPanRecords"+"Tag");
		tagObjMap.put("ButtonControl", classPrefix+"ButtonControl"+"Tag");
		tagObjMap.put("Buttons", classPrefix+"Buttons"+"Tag");
		tagObjMap.put("ColumnControl", classPrefix+"ColumnControl"+"Tag");
		tagObjMap.put("PageFrame", classPrefix+"PageFrame"+"Tag");
		tagObjMap.put("Page", classPrefix+"Page"+"Tag");
		tagObjMap.put("PanelControl", classPrefix+"PanelControl"+"Tag");
		tagObjMap.put("PopupLink", classPrefix+"PopupLink"+"Tag");
		tagObjMap.put("RowControl", classPrefix+"RowControl"+"Tag");
		tagObjMap.put("Sections", classPrefix+"Sections"+"Tag");
		tagObjMap.put("Section", classPrefix+"Section"+"Tag");
		tagObjMap.put("SetupGroup", classPrefix+"SetupGroup"+"Tag");
		tagObjMap.put("Sliders", classPrefix+"Sliders"+"Tag");
		tagObjMap.put("SubPanel", classPrefix+"SubPanel"+"Tag");
		tagObjMap.put("TPColumn", classPrefix+"TPColumn"+"Tag");
		createTld("SFTL", "F:\\", "sftl", tagObjMap);
	}
}
posted @ 2011-08-06 09:16  上善¤若水  阅读(152)  评论(0编辑  收藏  举报