CheckMsgPro

来源:互联网 发布:天猫淘宝货到付款 编辑:程序博客网 时间:2024/05/23 17:28
/*
 * =========================================================================
 *  Copyright @2005 NCS Pte. Ltd. All Rights Reserved
 *
 *  This software is confidential and proprietary to NCS Pte. Ltd. You shall
 *  use this software only in accordance with the terms of the license
 *  agreement you entered into with NCS.  No aspect or part or all of this
 *  software may be reproduced, modified or disclosed without full and
 *  direct written authorisation from NCS.
 *
 *  NCS SUPPLIES THIS SOFTWARE ON AN 'AS IS' BASIS. NCS MAKES NO
 *  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
 *  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
 *  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 *  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *  
 *  zili 2007-11-19 CheckMsgPro.java
 *
*/

package com.use;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
@author zili 主要用来验证一个properties的文件值是否唯一. To verify the key in the .properties
 *         document is unqiue or not.
 
*/

public class CheckMsgPro extends Thread {

    
private static Integer error = new Integer(0);

    
private File file;

    
private BufferedReader reader;

    
private static Integer leaveFileCount = new Integer(0);

    
public BufferedReader getReader() {
        
return reader;
    }


    
public void setReader(BufferedReader reader) {
        
this.reader = reader;
    }


    
public static Integer getError() {
        
return error;
    }


    
public static void setError(Integer error) {
        CheckMsgPro.error 
= error;
    }


    
public CheckMsgPro(File file) {
        
super();
        
this.file = file;
    }


    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        String fileName 
= ".";
        Date timeStart
=new Date();
        
if (args.length > 0{
            fileName 
= args[0];
        }

        File dir 
= new File(fileName);
        String path 
= dir.getAbsolutePath();
        String[] files 
= dir.list();
        
for (int i = 0; i < files.length; i++{
            
if (files[i].endsWith("properties")) {
                
// 处理中...
                synchronized (CheckMsgPro.leaveFileCount) {
                    CheckMsgPro.leaveFileCount 
= new Integer(
                            CheckMsgPro.leaveFileCount.intValue() 
+ 1);
                }

                File file 
= new File(path + "/" + files[i]);
                CheckMsgPro checkMsgPro 
= new CheckMsgPro(file);
                checkMsgPro.start();
            }

        }

        
while (true{
            
if (CheckMsgPro.leaveFileCount.intValue() == 0)
                
break;
        }

        
if (error.intValue() > 0)
            System.out.println(
"验证失败,请检查:一共存在[ " + error + " ]处冲突.");
        Date timeEnd
=new Date();
    }


    
/* (non-Javadoc)
     * @see java.lang.Thread#run()
     
*/

    
public void run() {
        
try {
            
this.checkFile(file);
        }
 catch (IOException e) {
            e.printStackTrace();
        }
 catch (Exception e) {
            e.printStackTrace();
        }
 finally {
            
if (reader != null{
                
try {
                    reader.close();
                }
 catch (IOException e1) {
                }

            }

        }

    }


    
/**
     * 
@param file
     * 
@return
     * 
@throws FileNotFoundException
     * 
@throws IOException
     
*/

    
private void checkFile(File file) throws FileNotFoundException, IOException {

        
this.reader = new BufferedReader(new FileReader(file));
        String tempString 
= null;
        
int line = 1;
        Map map 
= new HashMap(1000.8f);
        
// 一次读入一行,直到读入null为文件结束
        while ((tempString = reader.readLine()) != null{
            
// 显示行号
            
// System.out.println("line " + line + ": "
            
// + tempString);
            int index = tempString.indexOf('=');
            
if (index > 0 && !tempString.startsWith("#")) {
                tempString 
= tempString.substring(0, index).trim();
                
if (map.containsKey(tempString)) {
                    System.out.println(
"文       件 :" + file.getAbsoluteFile());
                    String msg 
= "";
                    msg 
+= "line " + map.get(tempString).toString()
                            
+ "  ==>  line " + line + "存在[ 1 ]处冲突. ";
                    
synchronized (error) {
                        error 
= new Integer(error.intValue() + 1);
                    }

                    System.out.println(msg);
                }

                map.put(tempString, line 
+ "");
            }

            line
++;
        }

        reader.close();
        
synchronized (leaveFileCount) {
            
// 完成一个文件的验证
            leaveFileCount = new Integer(leaveFileCount.intValue() - 1);
        }

    }


    
public File getFile() {
        
return file;
    }


    
public void setFile(File file) {
        
this.file = file;
    }

}

原创粉丝点击