wl_management/src/test/java/com/lz/mysql/ControlScanner.java

156 lines
4.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.lz.mysql;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ControlScanner {
private String filePath;//文件地址
static final String[] notesSym = {"/*", "*/", "//", "\\n"};//注释符号,成对出现,偶数为起始符号,奇数为结束符号
private String packageName;//包名
private String name;//类名
List<String> importList = new ArrayList<>();//引入的包名
List<FunctionDes> functionDesList = new ArrayList<>();//方法对象集合
List<AttributeDes> attributeDesList = new ArrayList<>();//属性对象集合
public void startScanner(String filePath){
File file = new File(filePath);
if(file.exists()){
//
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String strLine = null;
int lineCount = 0;
while(null != (strLine = bufferedReader.readLine())){
strLine = pretreatment(strLine);
CheckType checkType = checkLineStart(strLine);
strLine = strLine.substring(checkType.getStart());
checkLineEnd(checkType, strLine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
//文件异常
} catch (IOException e) {
e.printStackTrace();
//读取文件失败
}
}
}
private String pretreatment(String strLine){
strLine = strLine.replace("\t", " ");
strLine = removeMoreSpace(strLine);
strLine = strLine.replace("\r\n", "\n");
return strLine;
}
private String removeMoreSpace(String strLine){
int start = 0;
while(strLine.charAt(start) == ' '){
start++;
}
if(start > 0){
strLine = strLine.substring(start);
}
String newLine;
return strLine;
}
private CheckType checkLineStart(String strLine){
CheckType checkType = new CheckType();
switch (strLine.charAt(0)){
case '/':// /* //
if(strLine.charAt(1) == '/'){
checkType.setType(0);
//checkType.setEnd(true);
}else if(strLine.charAt(1) == '*'){
checkType.setType(1);
/*if(strLine.length() > 3 &&
strLine.charAt(strLine.length() - 1) == '/' &&
strLine.charAt(strLine.length() - 2) == '*'){
checkType.setEnd(true);
}*/
}
checkType.setStart(2);
break;
case 'i'://import
if(strLine.startsWith("import ")){
checkType.setType(2);
checkType.setStart(7);
}
break;
case 'p':// public private proteced package
if(strLine.startsWith("public ")){
checkType.setType(3);
checkType.setStart(7);
}else if(strLine.startsWith("private ")){
checkType.setType(4);
checkType.setStart(8);
}else if(strLine.startsWith("proteced ")){
checkType.setType(5);
checkType.setStart(9);
}else if(strLine.startsWith("package ")){
checkType.setType(6);
checkType.setStart(8);
}
break;
default:
checkType.setType(50);
checkType.setStart(0);
break;
}
/*if((type == 0 || type > 2)
&& strLine.charAt(strLine.length() - 1) == ';'){
checkType.setEnd(true);
}*/
return checkType;
}
private CheckType checkLineEnd(CheckType checkType, String strLine){
if(checkType.getType() > 1){//处理非注释代码中的多余的空格替换成一个,引号内的不处理
}
return checkType;
}
public class CheckType{
//1// 2/* 3import 4public 5private 6proteced 7package 50未找到关键字
private int type;
private int start;
private boolean isEnd;//一行代码是否读取完毕
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public boolean isEnd() {
return isEnd;
}
public void setEnd(boolean end) {
isEnd = end;
}
}
}