字符串过滤器

1.主要功能:
将当前input目录下的所有java文件找出,将文件中以的双引号括起来的字符以及用单引号括起来的单字符找出以与其配对的字符串代替 即:
如果在文件中找到String name="Michelangelo"。String nick="ppwf"
把"Michelangelo"用Global.RES_STR_1来代替。
把"ppwf"用Global.RES_STR_2来代替。
把配对的信息用配对格式:

Global.RES_STR_1="Michelangelo"
Global.RES_STR_2="ppwf"

放到当前output目录里的"Global.txt"中,而修改或的java文件也存入其中。
当前目录下孩有一map目录,存放的是字符串配对的hashMap(Michelangelo与Global.RES_STR_1是一对)的序列化后的文件

2.类介绍
FilterRes:主类
GetDirList:获得当前目录下的java文件
LoadMap:存放字符串配对
Parser:解析

3.注意点:
1.转码
不能使用FileReader读取源文件,FileReader是以你当前OS默认字符编码的方式读取文件的,如果源文件是UTF8的,而OS是简体中文(GB2312),FileReader就会以GB2312的字符编码方式读这些UTF8码,就出错了.应该使用DataInputStream或者InputStream来读取源文件.PrintWriter和FileReader具有同样的问题。应强调的是在java中任何直接输出String的IO操作都会有这个问题:使用默认的OS字符编码输出文件.

2.序列化
记住,对象流不序列化static或transient。

/**
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author Michelangelo
 * @version 0.2
 */

public class FilterRes
{
 private GetDirList aG = new GetDirList();
 private Parser aP = new Parser();
 private static String encoding = null;
 private static String global = null;
 public FilterRes(String dir)
 {
  String none[] = {};
  aG.getList(none, dir);//从目录下获得所有的以java喂后缀的文件名
  if(null != encoding)
  {
   aP.setEncoding(encoding);
  }
  if(null != global)
  {
   aP.setGlobal(global);
  }
 }
 public void act()
 {
  for (int i = 0; i < aG.list.length; i++)
  {
   aP.Read(aG.list[i]);
  }
  aP.StoreMap();
 }
 public static void main(String args[])
 {
  String dir = "./input";//从当前目录下的“input”中获得待解析字符串的java文件

  if(args.length == 1)
  {
   encoding = args[0];
  }
  else if(args.length == 2)
  {
   encoding = args[0];
   global = args[1];
  }
  FilterRes aS = new FilterRes(dir);
  aS.act();
 }
}
       
/**
 * <p>Copyright: Copyright (c) 2003</p>
 * <p></p>
 * @author Michelaangelo
 * @version 0.2
 */

import java.io.*;
public class GetDirList
{
 protected static String[] list;
 public void getList(String[] t, String aDir)
 {
  try
  {
   File path = new File(aDir);
   if (t.length == 0)
   {
    list = path.list();
   }
   else
   {
    list = path.list(new DirFilter("java"));
   }
   for (int i = 0; i < list.length; i++)
   {
    if (new File(list[i]).isDirectory())
    {
     getList(t, list[i]);
    }
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
}
class DirFilter implements FilenameFilter
{
 String afn;
 DirFilter(String afn)
 {
  this.afn = afn;
 }
 public boolean accept(File dir, String name)
 {
  String f = new File(name).getName();
  return f.indexOf(afn) != -1;
 }
}


/**
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author Michelangelo
 * @version 0.2
 */
import java.io.*;
import java.util.*;
public class LoadMap implements Serializable
{
 public Map aMap = new HashMap();
 public LoadMap()
 {
 }
 public void put(String a, String b)
 {
  aMap.put(a, b);
 }
}


/**
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company:</p>
 * @author Michelangelo
 * @version 0.2
 */
import java.io.*;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.*;

public class Parser {
    private String CRLF = "/r/n";
    private String ENCODING = "UTF8";
    private String GLOBAL = "Global";
    private File mapFile = new File("./map/Map.out");

    private int flag = 0;
    private LoadMap aLoad;
    private StringBuffer aStringB = new StringBuffer();
    int reached = 0; //遇到"//"则pass
    int reachOther = 0; //  遇到"/*    */"
    public Parser() {
        try {
            aLoad = new LoadMap();
            if (!mapFile.exists()) {
                aLoad.put("ppwf_omni", "Michelangelo");
                ObjectOutputStream o =
                        new ObjectOutputStream(new FileOutputStream(mapFile));
                o.writeObject(aLoad);
                o.close();
            }
            ObjectInputStream in =
                    new ObjectInputStream(new FileInputStream(mapFile));
            aLoad = (LoadMap) in.readObject();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setEncoding(String _encoding) {
        ENCODING = _encoding;
    }

    public void setGlobal(String _global) {
        GLOBAL = _global;
    }

    public void StoreMap() {
        try {
            ObjectOutputStream o =
                    new ObjectOutputStream(new FileOutputStream(mapFile));
            o.writeObject(aLoad);
            o.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Read(String aFile) {
        try {
            PrintWriter out =
                    new PrintWriter(
                            new BufferedWriter(new FileWriter("./output/" +
                    aFile)));
            RandomAccessFile aRFile =
                    new RandomAccessFile("./output/" + GLOBAL + ".txt", "rw");

            aRFile.seek(aRFile.length());
            String f = "./input/" + aFile;
            FileInputStream fis = new FileInputStream(f);
            InputStreamReader fr = new InputStreamReader(fis, ENCODING);
            int c = 0; //从文件中读取?/uFFFD个字?/uFFFD
            int pre = 0;
            //判断是否已读到文件结?/uFFFD
            while (c != -1) {
                c = fr.read(); //从文件中继续读取数据

                if (c == 47) { //判断是否有注?/uFFFD
                    if (reached == 1) {
                        while (c != 13) {
                            String b = "" + (char) c;
                            out.write(b);
                            c = fr.read();
                        }
                        reached = 0;
                        reachOther = 0;
                    } else {
                        reached = 1;
                        reachOther = 1;
                    }
                }
                if (c == 42) { //"星号"
                    if (reachOther == 1 && pre == 47) {
                        pre = 42; //前一个读到的数据
                        while (c != 47 && pre != 42) {
                            if (c == 13 || c == 10) { //判断是否为断行字?/uFFFD
                                out.write(c); //输出分行标签                            
                            } else {
                                String b = "" + (char) c;
                                out.write(b);
                            }
                            pre = c;
                            c = fr.read();
                        }
                        String b = "" + (char) c;
                        out.write(b);
                        reachOther = 0;
                    }
                }
                if (c == 39 || c == 34) {
                    flag = c;
                    c = fr.read();
                    while (c != flag) {
                        aStringB.append((char) c);
                        c = fr.read();
                    }
                    c = fr.read();
                    String to = aStringB.toString();

                    String judgeChar;
                    if (39 == flag) {
                        judgeChar="@";
                        to = "/'" + to + "/'";
                    } else {
                        judgeChar="";
                        to = "/"" + to + "/"";
                    }
                    if (aLoad.aMap.containsKey(to) != true) {
                        String part1 = GLOBAL + ".RES_STR_" + aLoad.aMap.size();
                        aLoad.aMap.put(to, part1);
                        String temp = "";

                        temp = "Global.RES_STR_"
                               + (aLoad.aMap.size() - 1)
                         + "="
                               + to
                               + CRLF;

                        byte[] tb = temp.getBytes(ENCODING);
                        aRFile.write(tb);
                    }

                    byte[] dbBytes =
                            ("/""+judgeChar+ (String) aLoad.aMap.get(to) + "/"").
                            getBytes(ENCODING);
                    String a = new String(dbBytes);
                    out.write(a); //写入对应的解析后的文件(替代的部分)
                    aStringB = new StringBuffer();
                    flag = 0;
                }
                if ( -1 != c) {
                    String b = "" + (char) c;
                    byte[] adbBytes = b.getBytes(ENCODING);
                    b = new String(adbBytes);
                    out.write(b); //写入对应的解析后的文件(其他的部分)
                }
                pre = c;
            }

            fr.close();
            aRFile.close();
            out.flush();
            out.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值