Java实现自动查找多语言Resource Bundle文件是否包含有重复项的工具

15 篇文章 0 订阅
8 篇文章 0 订阅

一个自动查找多语言Resource Bundle的properties文件是否包含有重复项的工具,如果有重复Item,则提示出来,便于删除重复的资源项。包括两个功能:1、查找同一个文件中是否有重复项。2、查找多语言的各Bundle文件中的Item是否完全一致,有否缺漏。



import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileReader;

import java.util.Enumeration;

import java.util.Hashtable;

import java.util.Properties;





public class RedundancyChecker {

	public final static String filepath_en_US="conf/spring/resources/Vmm_en_US.properties";

	public final static String filepath_zh_CN="conf/spring/resources/Vmm_zh_CN.properties";

	public final static String filepath_zh_TW="conf/spring/resources/Vmm_zh_TW.properties";

	

	public Properties loadFile(String path)

	{

		Properties prop = null;

		try 

		{

			BufferedInputStream inBuff = new BufferedInputStream(

					new FileInputStream(path));

			prop = new Properties();

			prop.load(inBuff);

			inBuff.close();

		} 

		catch (Exception e) 

		{

			e.printStackTrace();			

		}

		return prop;

	}

	

	public boolean isKeysTheSameInTwoFiles(String filepath1,String filepath2)

	{

		

		boolean result=true;

		int count=0;

		

		Properties props1=loadFile(filepath1);

		Properties props2=loadFile(filepath2);

		

		Enumeration keys1 = props1.keys();

		

		String fileName1=filepath1.substring(filepath1.lastIndexOf("/")+1);

		String fileName2=filepath2.substring(filepath2.lastIndexOf("/")+1);



		



		while(keys1.hasMoreElements())

		{

		  String key1 =(String)keys1.nextElement();



		  if(!props2.containsKey(key1))		 

		  {



               System.err.println("ERROR when "+fileName1+"-->"+fileName2);

               System.err.println(count+++":key ["+key1+"] in "+fileName1+" not contained in "+fileName2);

			  result=false;

		  }



		}

		if(result==true)

		{

			System.out.println(fileName1+"-->"+fileName2+" keys the same"); 

		}

		return result;

	}

	public boolean isKeysDuplicateInOneFile(String filepath)

	{

		boolean result= true;

		BufferedReader br= null;

		Hashtable hash=new Hashtable();

		String filename=filepath.substring(filepath.lastIndexOf("/")+1,filepath.length());

		try 

		{

			br= new BufferedReader(new FileReader(filepath));

			String line=br.readLine();

			while(line!=null )

			{

				if(line.contains("="))

				{

				String key =line.substring(0, line.indexOf("="));

				if(hash.containsKey(key))

				{

					System.err.println("ERROR:key ["+key+"] already exists in "+filename);	

					result=false;

				}

				else

				{

				  hash.put(key, key);

				}

				}

				line=br.readLine();

			}

			

		} catch (Exception e) 

		{

			e.printStackTrace();

		}

		if(result ==true)

		{

			System.out.println(filename+": identical keys size:"+hash.size());

		}

		return result;

	}

	

	public boolean checkAll()

	{

		

		System.out.println();

		boolean b1=isKeysDuplicateInOneFile(filepath_en_US);

		boolean b2=isKeysDuplicateInOneFile(filepath_zh_CN);

		boolean b3=isKeysDuplicateInOneFile(filepath_zh_TW);

		

		System.out.println();		

		boolean b4=isKeysTheSameInTwoFiles(filepath_en_US,filepath_zh_CN);

		boolean b5=isKeysTheSameInTwoFiles(filepath_en_US,filepath_zh_TW);

		

		boolean b6=isKeysTheSameInTwoFiles(filepath_zh_CN,filepath_en_US);

		boolean b7=isKeysTheSameInTwoFiles(filepath_zh_CN,filepath_zh_TW);

		

		boolean b8=isKeysTheSameInTwoFiles(filepath_zh_TW,filepath_en_US);

		boolean b9=isKeysTheSameInTwoFiles(filepath_zh_TW,filepath_zh_CN);

		

		System.out.println();

		

		if(b1 &&b2 &&b3 &&b4 &&b5 &&b6 &&b7 &&b8 &&b9)

		{

			

			System.out.println("Right.All Files' Keys are THE SAME!");

			System.out.println("-------------OK---------------------");

		}

		else

		{

			System.out.println("attention:there are some Errors!");

		}



		

		return true;

	}

	



	public static void main(String[] args) 

	{

		RedundancyChecker aChecker = new RedundancyChecker();

	    aChecker.checkAll();



	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值