⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loccounter.java

📁 PSP个人软件过程第二个练习
💻 JAVA
字号:
package psp0_2;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.StringTokenizer;
/**
 * LOCCounter: count the line of certain file
 * @author  QianYin
 * @version  Thur Sep 20 23:05:00 2006        
*/
public class LOCCounter 
{

	private String filePath;
	private LinkedList classRecords;
	private int totalSize;
	
	
	/**
	 * method
	 * @param path
	 */
	public LOCCounter(String path) 
	{
		super();
		// TODO Auto-generated constructor stub
		this.filePath = path;
		this.totalSize = 0;
		this.classRecords = new LinkedList();
		
	}
	
	
	
	/**
	 * method
	 * analyze the line one by one
	 */
	public void analyze() 
	{
		String str = null;
		BufferedReader input = null;
		
		//tags
		boolean createRecord = false;   //true when a new class will begin
		boolean createItem = false;     //true when a new method will begin
		boolean inComment = false;      //true when the current line is a comment
		boolean newMethod = false;    //true when the comment is for a new method
		boolean newClass = false;       //true when the comment is for a new class
		
		
		
		ClassRecord currentRecord = null;  
		MethodItem currentItem = null;   
		
		
		//open the input stream
		try 
		{
		  input = new BufferedReader(new FileReader(this.filePath));
		} catch (Exception e) 
		{
			// TODO Auto-generated catch block
			//System.out.println("wrong path");
			//e.printStackTrace();
		}
		
		
		try
		{
			while ((str = input.readLine())!=null)
			{
				if(this.isBlank(str))//ignore the blank line
					continue;
				
				if(this.isSimpleComment(str))//ignore the line begin with//
					continue;
				
				
				
				
				StringTokenizer tokens = new StringTokenizer(str);
				
				String firstToken = tokens.nextToken();//get the first token of the line
				
				if(!inComment && firstToken.equals("import" ))//the beginning of a file
				{
					this.autoIncrease();
				}//condition 1
				else if(!inComment && firstToken.equals("package" ))//the beginning of a file
				{
					this.autoIncrease();
				}
				else if(firstToken.equalsIgnoreCase("/**"))//begin of the comment
				{
					inComment = true;
					continue;
				}//codition 2
				else if(firstToken.equalsIgnoreCase("*/"))//end of the comment
				{
					inComment = false;
					
					if(newMethod)
					{
						newMethod = false;
						createItem = true;
					}
					
					
					if(newClass)
					{
						newClass = false;
						createRecord = true;
					}
				}//condition 3
				else if(inComment)//if it is in a comment
				{
					String next = null;
					if(tokens.hasMoreTokens())
					{
						next = tokens.nextToken();//quit the * tag
						if(next.equalsIgnoreCase("method"))
						    newMethod = true; 
						else if(next.equalsIgnoreCase("@author"))
							//System.out.println("author");
							newClass = true;   
					}
						
					
					
					
				}//condition 4
				else //if it is not in the comment
				{
					if(createRecord)//the defining line for a class
					{
						currentRecord = new ClassRecord(str);
						currentItem = null;
						currentRecord.autoIncSize();
						this.autoIncrease();
						//System.out.println("create record");
						this.classRecords.add(currentRecord);
						createRecord = false;
						continue;
					}
					else if(createItem)//the defining line for a method
					{
						//System.out.println("create item " + str);
						currentItem = new MethodItem(str);
						currentItem.autoIncSize();
						currentRecord.autoIncSize();
						this.autoIncrease();
						//System.out.println("create item");
						currentRecord.addItem(currentItem);
						createItem = false;
						
						continue;
					}
					else 
					{
						if(currentRecord != null)
						{
							if(currentItem == null)
							{
								//System.out.println("add variable  " + str);
								currentRecord.autoIncSize();//inside a class but not a method
								this.autoIncrease();
							}
							else
							{
								currentItem.autoIncSize();//inside a method
								currentRecord.autoIncSize();
								this.autoIncrease();
							}
							    
						}
					}
				}//codition 5
			}
		}catch(Exception e)
		{
			
		}
		
		
		
		
		//close the input stream
		try {
			input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		
		
		//modify the last item error
		for(int i = 0; i < this.classRecords.size(); i++)
		{
			
			 ClassRecord cr = (ClassRecord)this.classRecords.get(i);
			
			 int Methodnum = cr.getItemNum();
			 
			 if(Methodnum >= 1)
			 {
				 MethodItem f = (MethodItem)(cr.getItems().get(Methodnum - 1));
				 f.autoDecSize();
			 }
			
		}
			
	}

	
	
	
	
	/**
	 * method
	 * print the result 
	 * @param path
	 */
	public void printResult(String path)
	{
		
		try {
			PrintWriter output = new PrintWriter (new FileOutputStream(path));
			output.println("file :" + this.filePath);
			output.println("total size :" + this.totalSize);
			
			for(int i = 0; i < this.classRecords.size(); i++)
			{
				
				ClassRecord cr = (ClassRecord)this.classRecords.get(i);
				output.println("    class name :    " + cr.getDescription());
				output.println("    total size " + cr.getSize());
				output.println("    total items "+ cr.getItemNum());
				
				
				LinkedList list = cr.getItems();
				
				for(int j = 0; j < list.size(); j++)
				{
					MethodItem f = (MethodItem)list.get(j);
					
					
					output.println(f.getDescription()+ "   " + "size  " + f.getSize());
				}
			
			}
			
			
			output.close();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * method
	 * @param str
	 * @return
	 */
	private boolean isBlank(String str)
	{
		boolean result = false;
		
		int i = str.trim().length();
		
		if(i == 0)
			result = true;
		
		return result;
	
	}
	
	
	
	/**
	 * method
	 * @param str
	 * @return
	 */
	private boolean isSimpleComment(String str)
	{
		boolean result = false;
		
		StringTokenizer tokens  = new StringTokenizer(str);
		if(tokens.countTokens() != 0)
		{
			if(tokens.nextToken().equalsIgnoreCase("//"))
				result = true;
		}
		
		
		
		return result;
	}

	
	
	
	/**
	 * method
	 *
	 */
	private void autoIncrease()
	{
		this.totalSize++;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -