summaryrefslogtreecommitdiffstats
path: root/src/cppcheckplus/text/creatTable.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cppcheckplus/text/creatTable.java')
-rw-r--r--src/cppcheckplus/text/creatTable.java321
1 files changed, 321 insertions, 0 deletions
diff --git a/src/cppcheckplus/text/creatTable.java b/src/cppcheckplus/text/creatTable.java
new file mode 100644
index 0000000..0295eb6
--- /dev/null
+++ b/src/cppcheckplus/text/creatTable.java
@@ -0,0 +1,321 @@
1package cppcheckplus.text;
2
3import java.util.Vector;
4import java.util.regex.Pattern;
5
6import javax.xml.parsers.DocumentBuilder;
7import javax.xml.parsers.DocumentBuilderFactory;
8
9import org.w3c.dom.Document;
10import org.w3c.dom.Element;
11import org.w3c.dom.Node;
12import org.w3c.dom.NodeList;
13
14import cppcheckplus.control.MyContorlUtil;
15
16public class creatTable {
17
18 public String tableNameTemp = "";
19 public Vector<DeflectRow> cppcheckplusResults = new Vector<DeflectRow>();
20 public Vector<DeflectRow> flawfinderResults = new Vector<DeflectRow>();
21 public Vector<DeflectRow> clangResults = new Vector<DeflectRow>();
22 public Vector<DeflectRow> results = new Vector<DeflectRow>();
23 public Vector<stdClangType> stdclang = new Vector<stdClangType>();
24
25 public void initSTDClang(String xml){
26 try
27 {
28 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
29 DocumentBuilder db = dbf.newDocumentBuilder();
30 Document doc = db.parse(MyContorlUtil.getXMLFile(xml));
31 Element root = doc.getDocumentElement();
32 NodeList stdlist = root.getChildNodes();
33 if (stdlist != null)
34 {
35 for (int i = 0; i < stdlist.getLength(); i++)
36 {
37 org.w3c.dom.Node menu = stdlist.item(i);
38 if (menu.getNodeType() == Node.ELEMENT_NODE)
39 {
40 if (menu.getNodeName().equalsIgnoreCase("clangNode"))
41 {
42 stdClangType node = new stdClangType();
43 node.cwe=MyContorlUtil.getStringAttribute(menu, "cwe");
44 node.level=MyContorlUtil.getStringAttribute(menu, "level");
45 node.target=MyContorlUtil.getStringAttribute(menu, "target");
46 node.len=Integer.valueOf(MyContorlUtil.getStringAttribute(menu, "len"));
47 node.type=MyContorlUtil.getStringAttribute(menu, "type");
48 stdclang.add(node);
49 }
50 }
51 }
52 }
53 }
54 catch (Exception ex)
55 {
56 ex.printStackTrace();
57 }
58 }
59
60 public void makeTable() {// 统一并输出
61 results.clear();
62 for (int i = 0; i < cppcheckplusResults.size(); i++) {
63 addObject(results,cppcheckplusResults.elementAt(i));
64 }
65 for (int i = 0; i < clangResults.size(); i++) {
66 addObject(results,clangResults.elementAt(i));
67 }
68 for (int i = 0; i < flawfinderResults.size(); i++) {
69 addObject(results,flawfinderResults.elementAt(i));
70 }
71 }
72
73 public void clearall() {// 清除
74 results.clear();
75 clangResults.clear();
76 flawfinderResults.clear();
77 cppcheckplusResults.clear();
78 }
79
80 public static void addObject(Vector<DeflectRow> nodes, DeflectRow node) {
81 if (!nodes.contains(node)) {
82 nodes.add(node);
83 }
84 else{//重复检测的缺陷需要标注
85 int nodesNum = nodes.size();
86 for(int i=0; i<nodesNum; i++){
87 if(nodes.elementAt(i).equals(node))
88 nodes.elementAt(i).setIssure(1);
89 }
90 }
91 }
92
93 //按文件、类型和级别排序
94 public void ord(int ordType) {//1文件,2类型,3级别
95 //final int prime = 31;
96 //Vector<DeflectRow> other = new Vector<DeflectRow>();
97 if(ordType==1){//按照文件排序
98 int memNum = results.size();
99 for(int i=0; i<memNum-1; i++){
100 for(int j=0; j<memNum-i-1; j++){
101 if(results.elementAt(j).getFile().hashCode()
102 > results.elementAt(j+1).getFile().hashCode()){
103 DeflectRow temp=results.elementAt(j);
104 results.set(j, results.elementAt(j+1));
105 results.set(j+1, temp);
106 }
107 }
108 }
109 }
110 else if(ordType==2){//按照类型排序
111 int memNum = results.size();
112 for(int i=0; i<memNum-1; i++){
113 for(int j=0; j<memNum-i-1; j++){
114 if(results.elementAt(j).getType().hashCode()
115 > results.elementAt(j+1).getType().hashCode()){
116 DeflectRow temp=results.elementAt(j);
117 results.set(j, results.elementAt(j+1));
118 results.set(j+1, temp);
119 }
120 }
121 }
122 }
123 else if(ordType==3){//按照级别筛选
124 int memNum = results.size();
125 for(int i=0; i<memNum-1; i++){
126 for(int j=0; j<memNum-i-1; j++){
127 if(results.elementAt(j).getLevel().hashCode()
128 > results.elementAt(j+1).getLevel().hashCode()){
129 DeflectRow temp=results.elementAt(j);
130 results.set(j, results.elementAt(j+1));
131 results.set(j+1, temp);
132 }
133 }
134 }
135 }
136 }
137
138 public void toRowsOfCppcheckResult(String input) {//
139 String[] temp = input.split("\\$");
140 int size = temp.length;
141 String until = null;
142 String file = null;
143 String line = null;
144 String level = null;
145 String type = null;
146 String cwe = null;
147 String description = null;
148 if (size > 6) {
149 if (temp[1].length() == 0)
150 return;
151 until = temp[0];
152 file = temp[1];
153 line = temp[2];
154 level = temp[3];
155 if (temp[3].equals("error"))
156 level = "4";
157 else if (temp[3].equals("warning"))
158 level = "3";
159 else if (temp[2].equals("style"))
160 level = "1";
161 else if (temp[2].equals("information"))
162 level = "0";
163 else
164 level = "2";
165 type = temp[4];
166 description = temp[5];
167 cwe = temp[6];
168 DeflectRow node = new DeflectRow(until, file, line, level, type, cwe, description, 0);
169 stdCppcheck(node);
170 cppcheckplusResults.add(node);
171 }
172 }
173
174 public void stdCppcheck(DeflectRow node) {
175 String cwe = node.getCwe();
176 int cwenum = Integer.parseInt(cwe);
177 switch(cwenum){ //cppcheck向flawfinder看齐
178 case 134:
179 node.setLevel("4");
180 node.setType("format");
181 break;
182 case 367:
183 node.setLevel("4");
184 node.setType("race");
185 break;
186 case 120:
187 node.setLevel("4");
188 node.setType("buff");
189 break;
190 case 78:
191 node.setLevel("4");
192 node.setType("shell");
193 break;
194 case 327:
195 node.setLevel("3");
196 node.setType("random");
197 break;
198 case 119:
199 node.setLevel("4");
200 node.setType("buffer");
201 break;
202 case 362:
203 node.setLevel("2");
204 node.setType("misc");
205 break;
206 case 190:
207 node.setLevel("2");
208 node.setType("integer");
209 break;
210 default:
211 ;
212 }
213 }
214
215 public void toRowsOfClang(String input) {
216 String[] temp = input.split(":");
217 int size = temp.length;
218 String until = null;
219 String file = null;
220 String line = null;
221 String column = null;
222 String level = null;
223 String type = null;
224 String cwe = null;
225 String description = null;
226 if (size > 5) {
227 until = "clang-tidy";
228 file = temp[0] + ":" + temp[1];
229 line = temp[2];
230 column = temp[3];
231 level = temp[4];// warning,note, error
232 type = "";
233 description = temp[5];
234 cwe = "0";
235 // 标准化
236 DeflectRow node = new DeflectRow(until, file, line, level, type, cwe, description, 0);
237 stdClang(node);
238 clangResults.add(node);
239 }
240 }
241
242 public void stdClang(DeflectRow node) {
243 String describ = node.getDescription();
244 int len = describ.length();
245 boolean isFound = false;
246 for (int i = 0; i < stdclang.size(); i++) {
247 if(len>stdclang.elementAt(i).len){
248 if (describ.substring(1, stdclang.elementAt(i).len).equals(stdclang.elementAt(i).target)) {
249 node.setCwe(stdclang.elementAt(i).cwe);
250 node.setLevel(stdclang.elementAt(i).level);
251 node.setType(stdclang.elementAt(i).type);
252 isFound=true;
253 return;
254 }
255 }
256 }
257 if(!isFound){
258 node.setCwe("未知");
259 node.setLevel("2");
260 node.setType("other");
261 }
262 }
263
264
265 public void toRowsOfFlawfinder(String csvFileName) {// csv
266
267 String until = null;
268 // public String defObject = null;
269 String file = null;
270 String location = null;
271 String level = null;
272 String type = null;
273 String function = null;
274 String description = null;
275 DeflectRow node = new DeflectRow(until, file, location, level, type, function, description, 0);
276 }
277
278 public boolean isInteger(String str){
279 Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
280 return pattern.matcher(str).matches();
281 }
282
283 public void toRowsOfflawfinder(String input) {
284 // File,Line,Column,Level,Category,Name,Warning,Suggestion,Note,CWEs,Context,Fingerprint
285 String[] temp = input.split(",");
286 int size = temp.length;
287 String until = null;
288 String file = null;
289 String line = null;
290 String column = null;
291 String level = null;
292 String category = null;
293 String name = null;
294 String warning = null;
295 String suggestion = null;
296 String note = null;
297 String cwe = null;
298 String context = null;
299 String fingerprint = null;
300 if (size > 11) {
301 if(!isInteger(temp[1]))
302 return;
303 until = "flawfinder";
304 file = temp[0];
305 line = temp[1];
306 column = temp[2];
307 level = temp[3];
308 category = temp[4];
309 name = temp[5];
310 warning = temp[6];
311 suggestion = temp[7];
312 note = temp[8];
313 cwe = temp[9];
314 context = temp[10];
315 fingerprint = temp[11];
316 DeflectRow node = new DeflectRow(until, file, line, level, category, cwe, warning, 0);
317 flawfinderResults.add(node);
318 }
319 }
320
321}