授权文件比较工具完整版

package com.runqianapp;



import java.util.ArrayList;

import java.util.List;

import org.jdom.Element;

import com.runqianapp.license.FunctionInfo;

import com.runqianapp.utils.XMLUtil;



/**

 * 获取version.xml信息类

 * @author LXY

 *

 */

public class ParseVersionXML {

	/**function节点存储列表*/

	List<FunctionInfo> funcList = new ArrayList();

	/**

	 * 解析version.xml,并将得到的function节点以FunctionInfo的形式存储到funcList中

	 */

	public void parse(){

		Element root = XMLUtil.getXMLRoot();

		// 读取功能点定义,并保存

		List funcs = root.getChild("functions").getChildren("function");

		for(int j=0; j<funcs.size(); j++) {

			// 读取功能点信息

			Element func = (Element)funcs.get(j);

			short funcID = Short.parseShort(func.getAttributeValue("id"));

			String funcName = func.getAttributeValue("name");

			// 保存功能信息

			FunctionInfo fi = new FunctionInfo(funcID, funcName);

			funcList.add(fi);

		}

	}

}

package com.runqianapp;



import java.util.ArrayList;

import java.util.List;

/**

 * 将功能ID转化为功能名称类

 * @author LXY

 *

 */

public class ConvertID {

	/**仅存在源授权文件中的功能点名称列表*/

	private  List <String> srcOnlyNameList = new ArrayList();

	/**仅存在目标授权文件中的功能点名称列表*/

	private  List <String> newOnlyNameList = new ArrayList();

	/**两授权文件共同存在的功能点名称列表*/

	private  List <String> commonNameList = new ArrayList();

	/**

	 * 将功能ID转化为功能名称方法

	 * @param lc

	 * @param pv

	 */

	public void Convert(LicenseCompare lc,ParseVersionXML pv){

		for(int i = 0;i<lc.srcOnlyList.size();i++){

			for(int j = 0;j<pv.funcList.size();j++){

				if(lc.srcOnlyList.get(i).equals(pv.funcList.get(j).getID())){

					srcOnlyNameList.add(pv.funcList.get(j).getName());

				}

			}

		}

		for(int i = 0;i<lc.newOnlyList.size();i++){

			for(int j = 0;j<pv.funcList.size();j++){

				if(lc.newOnlyList.get(i).equals(pv.funcList.get(j).getID())){

					newOnlyNameList.add(pv.funcList.get(j).getName());

				}

			}

		}

		for(int i = 0;i<lc.commonList.size();i++){

			for(int j = 0;j<pv.funcList.size();j++){

				if(lc.commonList.get(i).equals(pv.funcList.get(j).getID())){

					commonNameList.add(pv.funcList.get(j).getName());

				}

			}

		}

	}

	public List<String> getSrcOnlyNameList(){

		return this.srcOnlyNameList;

	}

	public List<String> getNewOnlyNameList(){

		return this.newOnlyNameList;

	}

	public List<String> getCommonNameList(){

		return this.commonNameList;

	}

	



}

 
package com.runqianapp;

import java.util.ArrayList;

import java.util.List;

import com.runqianapp.license.LicenseUtils;

/**

 * 授权文件功能点比较实现类

 * @author LXY

 *

 */

public class LicenseCompare {

	/**源授权文件中的功能ID列表*/

	List srcList = new ArrayList();

	/**目标授权文件中的功能ID列表*/

	List newList = new ArrayList();

	/**仅存在于源授权文件中的功能ID列表*/

	List srcOnlyList = new ArrayList();

	/**仅存在与目标授权文件中的功能ID列表*/

	List newOnlyList = new ArrayList();

	/**两授权文件共同存在的功能ID列表*/

	List commonList = new ArrayList();

	/**

	 * 构造方法,在获得LicenseCompare对象时可以直接拿到源授权文件功能列表ID

	 * @param srcLicenseFilePath 源授权文件路径

	 */

	public LicenseCompare(String srcLicenseFilePath) {

		srcList = LicenseUtils.getLicenseFunctions(srcLicenseFilePath);

		for(int i = 0;i<srcList.size();i++){

			commonList.add(srcList.get(i));

		}

	}

	

	/**

	 * 授权文件功能点比较方法

	 * @param newLicenseFilePath 目标授权文件路径

	 */

	public void Compare(String newLicenseFilePath) {

	    newList = LicenseUtils.getLicenseFunctions(newLicenseFilePath);	  

	    commonList.retainAll(newList);

//	    System.out.println(commonList);

		for(int i = 0;i<srcList.size();i++){

			boolean flag = false;

			for(int j = 0;j<newList.size();j++){

				if(srcList.get(i).equals(newList.get(j))){

					flag = true;

					break;

				}

			}

			if(flag == false){

				srcOnlyList.add(srcList.get(i));

			}

		}

		for(int i = 0;i<newList.size();i++){

			boolean flag = false;

			for(int j = 0;j<srcList.size();j++){

				if(newList.get(i).equals(srcList.get(j))){

					flag = true;

					break;

				}

			}

			if(flag == false){

				newOnlyList.add(newList.get(i));

			}

		}



	}



}

 
 

package com.runqianapp;



import com.runqianapp.utils.FileUtil;



public class Main {

	/**

	 * @param args

	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub

	    String srcFile = "D:\\集深数据\\1206\\GEEZNV5.0\\tomcat\\webapps\\reportmis\\WEB-INF\\test.lic";

	    String newFile = "F:\\test.lic";

	    /**结果输出对象*/

	    StringBuffer result = new StringBuffer();  

	    

	    

	    /**解析version.xml文件*/

	    ParseVersionXML pv = new ParseVersionXML();

	    pv.parse();

	    /**获得授权文件差异功能点的ID列表*/

		LicenseCompare lc = new LicenseCompare(srcFile);

		lc.Compare(srcFile);

		/**转化功能点ID为功能点名称*/

		ConvertID ct = new ConvertID();

		ct.Convert(lc,pv);

		

		

		/**以下是结果输出*/

//		System.out.println("仅源授权文件中存在的功能ID:"+lc.srcOnlyList);

//		System.out.println("仅目标授权文件中存在的功能ID:"+lc.newList);		

		result.append("**********************************").append("\r\n")

		  	  .append("两授权文件共同存在的功能点:"+"(共计"+ct.getCommonNameList().size()+"个)").append("\r\n")

		  	  .append("**********************************").append("\r\n");

		System.out.println("**********************************");

		System.out.println("两授权文件共同存在的功能点:"+"(共计"+ct.getCommonNameList().size()+"个)");

		System.out.println("**********************************");

		for(int i = 0;i<ct.getCommonNameList().size();i++){

			System.out.println(ct.getCommonNameList().get(i));

			result.append(ct.getCommonNameList().get(i)).append("\r\n");

		}		

		result.append("**********************************").append("\r\n")

			  .append("仅源授权文件中存在的功能点:"+"(共计"+ct.getSrcOnlyNameList().size()+"个)").append("\r\n")

			  .append("**********************************").append("\r\n");

		System.out.println("**********************************");

		System.out.println("仅源授权文件中存在的功能点:"+"(共计"+ct.getSrcOnlyNameList().size()+"个)");

		System.out.println("**********************************");

		for(int i = 0;i<ct.getSrcOnlyNameList().size();i++){

			System.out.println(ct.getSrcOnlyNameList().get(i));

			result.append(ct.getSrcOnlyNameList().get(i)).append("\r\n");

		}

		result.append("**********************************").append("\r\n")

		  	  .append("仅目标授权文件中存在的功能点:"+"(共计"+ct.getNewOnlyNameList().size()+"个)").append("\r\n")

		  	  .append("**********************************").append("\r\n");

		System.out.println("**********************************");

		System.out.println("仅目标授权文件中存在的功能点:"+"(共计"+ct.getNewOnlyNameList().size()+"个)");

		System.out.println("**********************************");

		for(int i = 0;i<ct.getNewOnlyNameList().size();i++){

			System.out.println(ct.getNewOnlyNameList().get(i));

			result.append(ct.getNewOnlyNameList().get(i)).append("\r\n");

		}

		FileUtil.writeFile(result.toString());

	}



}

package com.runqianapp.utils;



import java.io.FileOutputStream;

import java.io.PrintWriter;

/**

 * 文件工具类

 * @author LXY

 *

 */

public class FileUtil{

	private static String outFilePath = System.getProperty("user.dir")+"\\result.txt";

	/**

	 * 将结果输出到文件方法

	 * @param st   要输出的字符串

	 * @param outFilePath   输出路径

	 */

	public static void writeFile(String st){

		//outFilePath = outFilePath.substring(0,outFilePath.lastIndexOf("\\"))+"\\result.txt"; //将结果输出到当前jar包目录上一级的resutl.txt文档中

		FileOutputStream fos=null; 

		  try {   

			  fos=new FileOutputStream(outFilePath);  

			  PrintWriter pw=new PrintWriter(fos);   			   				 

			  pw.write(st);   			

			  pw.flush(); 

			  }catch(Exception e){   

				  e.printStackTrace(); 

				  System.out.println("输出结果文件出错,程序退出:");

				  System.exit(0);

			  }finally{   

				  try {    

					  fos.close();   

				      }catch(Exception e){    

				    	  e.printStackTrace();   

				      } 

			  }	

		

	}

}


package com.runqianapp.utils;



import java.io.File;

import java.io.IOException;



import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

/**

 * XML工具类

 * @author LXY

 *

 */

public class XMLUtil {

	private static String versionPath = System.getProperty("user.dir")+"/version.xml";

	/**

	 * 根据xml路径得到rootElement 

	 * @param xmlPath

	 * @return

	 * @throws IOException 

	 */

	public static Element getXMLRoot()  {

		SAXBuilder builder = new SAXBuilder();

		Document doc = null;

		Element root = null;

		try {

			File file = new File(versionPath);

			doc = builder.build(file);

			root = doc.getRootElement();

		} catch (JDOMException e) {

			e.printStackTrace();

			System.out.println("解析配置文件出错,程序退出:");

			System.exit(0);

		} catch (IOException e) {

			e.printStackTrace();

			System.out.println(versionPath+"文件路径错误,程序退出:");

			System.exit(0);

		}

		return root;

	}

}





你可能感兴趣的:(文件)