CheckStyle 用到的代码

文件1:check.xml


    
    
    
    
    
    

    
        
    
    
        
            
        
    
    
        
            
        
    
    
        
            
        
        
        
        
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
        
        
    


文件2:checkstyle_sccms.xml
其中一个文件检查结果



























































































文件3:解析代码

CheckStyleErrorParse.java

package chances.com.cn.dom4j;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.digester3.Digester;
import org.xml.sax.SAXException;

import chances.com.cn.dom4j.entity.CheckFile;
import chances.com.cn.dom4j.entity.CheckStyle;
import chances.com.cn.dom4j.entity.FileError;

import com.mysql.jdbc.PreparedStatement;

public class CheckStyleErrorParse {

    public static final String TABLE_NAME = "checkstyle_error";
    public static final String ID = "id";
    public static final String FILENAME = "file_name";
    public static final String COLUMN = "ccolumn";
    public static final String LINE = "line";
    public static final String SEVERITY = "severity";
    public static final String MESSAGE = "message";
    public static final String SOURCE = "source";
    public static final String PROJECT = "project";
    public static final String PROJECT_NAME = "sccms";

    public static Connection conn = null;
    public static Statement stmt = null;

    private static final String INSERT_SQL = "insert into " + TABLE_NAME + "("
                    + FILENAME + "," + COLUMN + "," + LINE + "," + SEVERITY
                    + ","+ MESSAGE + "," + SOURCE + "," + PROJECT
                    + ")values(?,?,?,?,?,?,?)";

    public static void main(String[] args) throws IOException, SAXException {
        Digester digester = new Digester();

        String checkstyle = "checkstyle";
        String checkFile = checkstyle + "/file";
        String Error = checkFile + "/error";

        // 设置解析规则
        digester.setValidating(false);
        digester.addObjectCreate(checkstyle, CheckStyle.class);
        digester.addSetProperties(checkstyle);
        digester.addObjectCreate(checkFile, CheckFile.class);
        digester.addSetProperties(checkFile);
        digester.addObjectCreate(Error, FileError.class);
        digester.addSetProperties(Error);
        digester.addSetNext(Error, "addFileError");
        digester.addSetNext(checkFile, "addCheckFile");
        try {
            // 解析XML文件,并得到ROOT元素
            CheckStyle checkStyle = (CheckStyle) digester
                            .parse(CheckStyleErrorParse.class
                                            .getResourceAsStream("checkstyle_"
                                                            + PROJECT_NAME
                                                            + ".xml"));

            System.out.println(" CheckStyle 的版本: " + checkStyle.getVersion());
            System.out.println(" 共有多少个文件: "
                            + checkStyle.getCheckFileList().size());
            System.out.println(" ***************************** ");

            Connection conn = getConnection(); // 获取连接
            PreparedStatement stm = (PreparedStatement) conn
                            .prepareStatement(INSERT_SQL);
            for (CheckFile checkFile1 : checkStyle.getCheckFileList()) {
                System.out.println(" 共有多少个错误: "
                                + checkFile1.getFileErrorList().size());

                for (FileError CheckStyleError : checkFile1.getFileErrorList()) {
                    stm.setString(1, checkFile1.getName());
                    stm.setString(2, CheckStyleError.getColumn());
                    stm.setString(3, CheckStyleError.getLine());
                    stm.setString(4, CheckStyleError.getSeverity());
                    stm.setString(5, CheckStyleError.getMessage());
                    stm.setString(6, CheckStyleError.getSource());
                    stm.setString(7, PROJECT_NAME);
                    stm.addBatch();
                }
                stm.executeBatch();
            }
            stm.close(); // 关闭连接
            System.out.println("插入数据库已结束");

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        try {

            conn.close();
        }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static Connection getConnection() {

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            // 建立到MySQL的连接
            conn = DriverManager.getConnection(
                            "jdbc:mysql://192.168.220.126:3306/qingqian2015",
                            "user",
                            "pwd");
        }
        catch (InstantiationException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        catch (IllegalAccessException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        catch (ClassNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

}

实体类CheckStyle.java
package chances.com.cn.dom4j.entity;

import java.util.ArrayList;
import java.util.List;

public class CheckStyle {
    private String version;
    private List checkFileList = new ArrayList();
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
    public List getCheckFileList() {
        return checkFileList;
    }
    
    public void addCheckFile(CheckFile checkFile){
        checkFileList.add(checkFile);
    }
    
}


实体类CheckFile.java
package chances.com.cn.dom4j.entity;

import java.util.ArrayList;
import java.util.List;

public class CheckFile {
    private String name;
    private List fileErrorList = new ArrayList();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List getFileErrorList() {
        return fileErrorList;
    }

    public void addFileError(FileError fileError) {
        fileErrorList.add(fileError);
    }
}

实体类FileError.java
package chances.com.cn.dom4j.entity;

public class FileError {
    private String line;
    private String severity;
    private String column;
    private String message;
    private String source;


    public String getLine() {
        return line;
    }

    public void setLine(String line) {
        this.line = line;
    }

    public String getSeverity() {
        return severity;
    }

    public void setSeverity(String severity) {
        this.severity = severity;
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }
}

文件:cleanup.xml



































































































































































文件:codeTemplates.xml


你可能感兴趣的:(CheckStyle 用到的代码)