summaryrefslogtreecommitdiffstats
path: root/src/cppcheckplus/text/creatTable.java
blob: 0295eb61782e5f16f9207a754a6c8a6f667e8b70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package cppcheckplus.text;

import java.util.Vector;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cppcheckplus.control.MyContorlUtil;

public class creatTable {

	public String tableNameTemp = "";
	public Vector<DeflectRow> cppcheckplusResults = new Vector<DeflectRow>();
	public Vector<DeflectRow> flawfinderResults = new Vector<DeflectRow>();
	public Vector<DeflectRow> clangResults = new Vector<DeflectRow>();
	public Vector<DeflectRow> results = new Vector<DeflectRow>();
    public Vector<stdClangType> stdclang = new Vector<stdClangType>();
    
    public void initSTDClang(String xml){
		try
		{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(MyContorlUtil.getXMLFile(xml));
			Element root = doc.getDocumentElement();
			NodeList stdlist = root.getChildNodes();
			if (stdlist != null)
			{
				for (int i = 0; i < stdlist.getLength(); i++)
				{
					org.w3c.dom.Node menu = stdlist.item(i);
					if (menu.getNodeType() == Node.ELEMENT_NODE)
					{
						if (menu.getNodeName().equalsIgnoreCase("clangNode"))
						{
							stdClangType node = new stdClangType();
							node.cwe=MyContorlUtil.getStringAttribute(menu, "cwe");
							node.level=MyContorlUtil.getStringAttribute(menu, "level");
							node.target=MyContorlUtil.getStringAttribute(menu, "target");
							node.len=Integer.valueOf(MyContorlUtil.getStringAttribute(menu, "len"));
							node.type=MyContorlUtil.getStringAttribute(menu, "type");
							stdclang.add(node);
						}
					}
				}
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
    }
	
	public void makeTable() {// 统一并输出
		results.clear();
		for (int i = 0; i < cppcheckplusResults.size(); i++) {
			addObject(results,cppcheckplusResults.elementAt(i));
		}
		for (int i = 0; i < clangResults.size(); i++) {
			addObject(results,clangResults.elementAt(i));
		}
		for (int i = 0; i < flawfinderResults.size(); i++) {
			addObject(results,flawfinderResults.elementAt(i));
		}
	}
	
	public void clearall() {// 清除
		results.clear();
		clangResults.clear();
		flawfinderResults.clear();
		cppcheckplusResults.clear();
	}

	public static void addObject(Vector<DeflectRow> nodes, DeflectRow node) {
		if (!nodes.contains(node)) {
			nodes.add(node);
		}
		else{//重复检测的缺陷需要标注
			int nodesNum = nodes.size();
			for(int i=0; i<nodesNum; i++){
				if(nodes.elementAt(i).equals(node))
					nodes.elementAt(i).setIssure(1);
			}
		}
	}
	
	//按文件、类型和级别排序
	public void ord(int ordType) {//1文件,2类型,3级别
		//final int prime = 31;
		//Vector<DeflectRow> other = new Vector<DeflectRow>();
		if(ordType==1){//按照文件排序
			int memNum = results.size();
			for(int i=0; i<memNum-1; i++){
				for(int j=0; j<memNum-i-1; j++){
				    if(results.elementAt(j).getFile().hashCode() 
				    		> results.elementAt(j+1).getFile().hashCode()){
				    	DeflectRow temp=results.elementAt(j);
				    	results.set(j, results.elementAt(j+1));
				    	results.set(j+1, temp);		
				    }
				}
			}
		}
		else if(ordType==2){//按照类型排序
			int memNum = results.size();
			for(int i=0; i<memNum-1; i++){
				for(int j=0; j<memNum-i-1; j++){
				    if(results.elementAt(j).getType().hashCode() 
				    		> results.elementAt(j+1).getType().hashCode()){
				    	DeflectRow temp=results.elementAt(j);
				    	results.set(j, results.elementAt(j+1));
				    	results.set(j+1, temp);		
				    }
				}
			}
		}
		else if(ordType==3){//按照级别筛选
			int memNum = results.size();
			for(int i=0; i<memNum-1; i++){
				for(int j=0; j<memNum-i-1; j++){
				    if(results.elementAt(j).getLevel().hashCode() 
				    		> results.elementAt(j+1).getLevel().hashCode()){
				    	DeflectRow temp=results.elementAt(j);
				    	results.set(j, results.elementAt(j+1));
				    	results.set(j+1, temp);		
				    }
				}
			}
		}
	}

	public void toRowsOfCppcheckResult(String input) {//
		String[] temp = input.split("\\$");
		int size = temp.length;
		String until = null;
		String file = null;
		String line = null;
		String level = null;
		String type = null;
		String cwe = null;
		String description = null;
		if (size > 6) {
			if (temp[1].length() == 0)
				return;
			until = temp[0];
			file = temp[1];
			line = temp[2];
			level = temp[3];
			if (temp[3].equals("error"))
				level = "4";
			else if (temp[3].equals("warning"))
				level = "3";
			else if (temp[2].equals("style"))
				level = "1";
			else if (temp[2].equals("information"))
				level = "0";
			else
				level = "2";
			type = temp[4];
			description = temp[5];
			cwe = temp[6];
			DeflectRow node = new DeflectRow(until, file, line, level, type, cwe, description, 0);
			stdCppcheck(node);
			cppcheckplusResults.add(node);
		}
	}

	public void stdCppcheck(DeflectRow node) {
		String cwe = node.getCwe();
		int cwenum =  Integer.parseInt(cwe);
		switch(cwenum){ //cppcheck向flawfinder看齐
			case 134:
				node.setLevel("4");
				node.setType("format");
				break;
			case 367:
				node.setLevel("4");
				node.setType("race");
				break;
			case 120:
				node.setLevel("4");
				node.setType("buff");
				break;
			case 78:
				node.setLevel("4");
				node.setType("shell");
				break;
			case 327:
				node.setLevel("3");
				node.setType("random");
				break;
			case 119:
				node.setLevel("4");
				node.setType("buffer");
				break;
			case 362:
				node.setLevel("2");
				node.setType("misc");
				break;
			case 190:
				node.setLevel("2");
				node.setType("integer");
				break;
			default:
				;
		}
	}

	public void toRowsOfClang(String input) {
		String[] temp = input.split(":");
		int size = temp.length;
		String until = null;
		String file = null;
		String line = null;
		String column = null;
		String level = null;
		String type = null;
		String cwe = null;
		String description = null;
		if (size > 5) {
			until = "clang-tidy";
			file = temp[0] + ":" + temp[1];
			line = temp[2];
			column = temp[3];
			level = temp[4];// warning,note, error
			type = "";
			description = temp[5];
			cwe = "0";
			// 标准化
			DeflectRow node = new DeflectRow(until, file, line, level, type, cwe, description, 0);
			stdClang(node);
			clangResults.add(node);
		}
	}

	public void stdClang(DeflectRow node) {
		String describ = node.getDescription();
		int len = describ.length();
		boolean isFound = false;
		for (int i = 0; i < stdclang.size(); i++) {
			if(len>stdclang.elementAt(i).len){
				if (describ.substring(1, stdclang.elementAt(i).len).equals(stdclang.elementAt(i).target)) {
					node.setCwe(stdclang.elementAt(i).cwe);
					node.setLevel(stdclang.elementAt(i).level);
					node.setType(stdclang.elementAt(i).type);
					isFound=true;
					return;
				}
			}
		}
		if(!isFound){
			node.setCwe("未知");
			node.setLevel("2");
			node.setType("other");
		}
	}


	public void toRowsOfFlawfinder(String csvFileName) {// csv

		String until = null;
		// public String defObject = null;
		String file = null;
		String location = null;
		String level = null;
		String type = null;
		String function = null;
		String description = null;
		DeflectRow node = new DeflectRow(until, file, location, level, type, function, description, 0);
	}

	public boolean isInteger(String str){  
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
	}
	
	public void toRowsOfflawfinder(String input) {
		// File,Line,Column,Level,Category,Name,Warning,Suggestion,Note,CWEs,Context,Fingerprint
		String[] temp = input.split(",");
		int size = temp.length;
		String until = null;
		String file = null;
		String line = null;
		String column = null;
		String level = null;
		String category = null;
		String name = null;
		String warning = null;
		String suggestion = null;
		String note = null;
		String cwe = null;
		String context = null;
		String fingerprint = null;
		if (size > 11) {
			if(!isInteger(temp[1]))
				return;
			until = "flawfinder";
			file = temp[0];
			line = temp[1];
			column = temp[2];
			level = temp[3];
			category = temp[4];
			name = temp[5];
			warning = temp[6];
			suggestion = temp[7];
			note = temp[8];
			cwe = temp[9];
			context = temp[10];
			fingerprint = temp[11];
			DeflectRow node = new DeflectRow(until, file, line, level, category, cwe, warning, 0);
			flawfinderResults.add(node);
		}
	}

}