package zxj;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */

import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;

public class CounterAction extends Action
{


        //方法自定义处



     public ActionForward execute(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
       throws Exception
       {
              //处理逻辑
              //变量自定义处
               String str = ""; //存储源文件全文
               String temp = ""; //暂时存储源文件文件
               int count = 0; //记录源文件总长度
               int result[] = new int[26]; //统计26个字母的使用量
               int otherf = 0; //记录其它非字母数量
               String returnStr = ""; //返回值
               float rate[] = new float[27];



               //初始化数组
               for (int i = 0; i < 26; i++)
               {
                   result[i] = 0;
               }
               for(int i=0;i<27;i++)
               {
                   rate[i]=0;
               }
               CounterForm f=(CounterForm)form;
               String resource=(String)f.getResource();
               //处理中文目录和文件传输问题
               byte b1[] = resource.getBytes("iso8859-1");
               resource = new String(b1);
               System.out.println("取得了参数"+resource);
               System.out.println("resource长度"+resource.length());
               if(resource.length()==0)
               {
                   returnStr = "error1";
               }
               else
               {
                   try
                   {

                       //文件路径符号转换
                        resource = resource.replace('\\','/');

                        //文件对象化
                        File resourceFile = new File(resource);
                        //判断源文件和目标文件是否存在，是否为普通文件
                        if(!resourceFile.exists()||!resourceFile.isFile())
                        {
                             returnStr = "error2";
                        }
                        else
                        {
                                //读入文件
                                FileReader fread = new FileReader(resourceFile);

                                //放入到缓冲区
                                BufferedReader   buffIn = new BufferedReader(fread);

                                //文件读入处理,按行读入
                                while((temp=buffIn.readLine())!=null)
                                {
                                        //字符集转换，解决中文传输问题
                                        byte b[] = temp.getBytes("iso8859-1");
                                        // byte b[] = temp.getBytes();
                                         temp = new String(b);
                                         str = str+temp;
                                  }
                                count = str.length();

                                //将全文所有大写字母变成小写字母
                                str = str.toLowerCase();
                                char a = 'a';
                                byte b[] = str.getBytes();

                                for(int i = 0;i < count;i++)
                                {
                                    if((b[i]-a)<0||(b[i]-a)>26)
                                    {
                                        otherf++;
                                    }
                                    else
                                    {
                                        result[(b[i]-a)]++;
                                    }
                                }
                                //使用频率处理
                                for(int i=0;i<26;i++)
                                {
                                    System.out.println(result[i]+"="+count+"="+result[i]/count);
                                    rate[i]= (float)result[i]/count;
                                    System.out.println(rate[i]);
                                }
                                rate[26] = otherf/count;

                          }
                          //将数值传递给CounterForm
                          f.setResult(result);
                          f.setOtherf(otherf);
                          f.setTotal(count);
                          f.setRate(rate);
                          System.out.println(rate[26]);
                         //返回成功页面
                          returnStr = "success";
                    }
                    catch (Exception de)
                    {
                       returnStr = "error";
                   }
               }
                return (mapping.findForward(returnStr));

    }
    }
