写各种java或jsp的时候,一不小心就会多写了一些中文的标点字符,特别是jsp中的javascript,里面出了错都没有提示,郁闷的要死。
心想要是有这么一个检查中文字符小程序就好了。网上找了很久,都没找到自己想要的。于是自己写:
注:主要是检查',' ,'。' ,'、',';','’','‘','”','“',':' 这些字符,不让它秘密的隐藏在jsp中
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class UnsupportedCharCheck{
/**
* @param args
*/
public static void main(String[] args) {
try {
test("D:/helloworld/index.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void test(String articleLoction) throws IOException {
File article = new File(articleLoction);
char[] unsupportedChars = { ',' ,'。' ,'、',';','’','‘','”','“',':'};//' '不能表示中文空格?那..
Map unsupportedCharsAndLineNo = new HashMap();
Map unsupportedCharsFound = new HashMap();
int lineNum = 0;
int commentedNum = 0;
int multiCommentBegin = -1;//标注多行注释<!--的起始位置
int multiCommentEnd = -1;//标注多行注释<!--的起始位置
boolean mc = false;
boolean problem_flag = false;
BufferedReader bf = new BufferedReader(new FileReader(article));
String lineStr = "";
while((lineStr=bf.readLine())!=null){
lineNum++;
if(lineStr.startsWith("//"))continue;
if(mc)
if(((multiCommentEnd=lineStr.indexOf("-->")))==-1){
continue;
}else {
lineStr = lineStr.substring(multiCommentEnd+3);
mc = false;
}
while((multiCommentBegin=lineStr.indexOf("<!--"))!=-1){
if(((multiCommentEnd=lineStr.indexOf("-->")))!=-1){
lineStr = lineStr.substring(0, multiCommentBegin)+lineStr.substring(multiCommentEnd+3);
mc = false;
}else {
lineStr = lineStr.substring(0, multiCommentBegin);
mc = true;
}
}
if(mc)
if(((multiCommentEnd=lineStr.indexOf("-->")))==-1){
continue;
}else {
lineStr = lineStr.substring(multiCommentEnd+3);
mc = false;
}
//System.out.println(lineStr);
int commentBegin = 0;
String errStr = "";
for(int i=0;i<lineStr.length();i++) {
if(commentBegin==2)break;
if(multiCommentBegin!=-1);
char c = lineStr.charAt(i);
if(c=='/'){
commentBegin += 1;
if(commentBegin == 2) {
commentedNum ++;
commentBegin = 0;
}
continue;
}else if( commentBegin == 1){
commentBegin = 0;
}
for(int j=0;j<unsupportedChars.length;j++) {
if(c==unsupportedChars[j]) {
if(errStr.length()==0) {
errStr += c;
}else {
errStr += "|" + c;
}
if(!unsupportedCharsFound.containsKey(c)) {
unsupportedCharsFound.put(c,lineNum);
//System.out.println("FOUND UNSUPPORTED CHAR: '" + c +"' AT LINE " +lineNum);
}else{
//System.out.println("FOUND UNSUPPORTED CHAR: '" + c +"' AT LINE " +lineNum);
}
unsupportedCharsAndLineNo.put(lineNum, c);
break;
}
}
}
if(errStr.length()!=0) {
problem_flag = true;
System.out.println("FOUND UNSUPPORTED CHARS: " + errStr +" AT LINE " +lineNum);
}
}
if(!problem_flag)System.out.println("CONGRATULATIONS! NO UNSUPPORTED CHARS FOUND~~");
}
}