diff options
Diffstat (limited to 'src/cppcheckplus/control/MyContorlUtil.java')
-rw-r--r-- | src/cppcheckplus/control/MyContorlUtil.java | 118 |
1 files changed, 20 insertions, 98 deletions
diff --git a/src/cppcheckplus/control/MyContorlUtil.java b/src/cppcheckplus/control/MyContorlUtil.java index e7e3d07..8befdc7 100644 --- a/src/cppcheckplus/control/MyContorlUtil.java +++ b/src/cppcheckplus/control/MyContorlUtil.java | |||
@@ -28,6 +28,7 @@ import java.io.IOException; | |||
28 | import java.io.InputStream; | 28 | import java.io.InputStream; |
29 | import java.util.ArrayList; | 29 | import java.util.ArrayList; |
30 | import java.util.List; | 30 | import java.util.List; |
31 | import java.util.Map; | ||
31 | import javax.imageio.ImageIO; | 32 | import javax.imageio.ImageIO; |
32 | import javax.swing.BorderFactory; | 33 | import javax.swing.BorderFactory; |
33 | import javax.swing.Box; | 34 | import javax.swing.Box; |
@@ -35,7 +36,6 @@ import javax.swing.Icon; | |||
35 | import javax.swing.ImageIcon; | 36 | import javax.swing.ImageIcon; |
36 | import javax.swing.JLabel; | 37 | import javax.swing.JLabel; |
37 | import javax.swing.JMenuItem; | 38 | import javax.swing.JMenuItem; |
38 | import javax.swing.JOptionPane; | ||
39 | import javax.swing.UIManager; | 39 | import javax.swing.UIManager; |
40 | import javax.swing.plaf.FontUIResource; | 40 | import javax.swing.plaf.FontUIResource; |
41 | import javax.xml.parsers.DocumentBuilder; | 41 | import javax.xml.parsers.DocumentBuilder; |
@@ -48,7 +48,7 @@ import org.w3c.dom.Document; | |||
48 | import org.w3c.dom.Element; | 48 | import org.w3c.dom.Element; |
49 | import org.w3c.dom.Node; | 49 | import org.w3c.dom.Node; |
50 | import org.w3c.dom.NodeList; | 50 | import org.w3c.dom.NodeList; |
51 | import toolsconfig.ToolsConfig; | 51 | import org.yaml.snakeyaml.Yaml; |
52 | import toolsconfig.ToolsRegistry; | 52 | import toolsconfig.ToolsRegistry; |
53 | 53 | ||
54 | 54 | ||
@@ -318,116 +318,38 @@ public class MyContorlUtil { | |||
318 | return menuBar; | 318 | return menuBar; |
319 | } | 319 | } |
320 | 320 | ||
321 | |||
322 | public static void fixSettingPath(String xml, String tool, String newPath) { | ||
323 | try { | ||
324 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
325 | DocumentBuilder db = dbf.newDocumentBuilder(); | ||
326 | Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); | ||
327 | Element root = doc.getDocumentElement(); | ||
328 | NodeList tools = root.getChildNodes(); | ||
329 | if (tools != null) { | ||
330 | for (int i = 0; i < tools.getLength(); i++) { | ||
331 | org.w3c.dom.Node menu = tools.item(i); | ||
332 | if (menu.getNodeType() == Node.ELEMENT_NODE) { | ||
333 | if (menu.getNodeName().equalsIgnoreCase("tool")) { | ||
334 | org.w3c.dom.Node att = | ||
335 | menu.getAttributes().getNamedItem("id"); | ||
336 | String toolName = | ||
337 | MyContorlUtil.getStringAttribute(menu, | ||
338 | "toolName"); | ||
339 | if (tool.equals("cppcheck") && | ||
340 | toolName.equals("cppcheck")) { | ||
341 | org.w3c.dom.Node attribute = | ||
342 | menu.getAttributes() | ||
343 | .getNamedItem("filePath"); | ||
344 | if (attribute != null) { | ||
345 | attribute.setNodeValue( | ||
346 | newPath.replaceAll("\\\\", "\\/")); | ||
347 | } | ||
348 | } else if (tool.equals("flawfinder") && | ||
349 | toolName.equals("flawfinder")) { | ||
350 | org.w3c.dom.Node attribute = | ||
351 | menu.getAttributes() | ||
352 | .getNamedItem("filePath"); | ||
353 | if (attribute != null) { | ||
354 | attribute.setNodeValue( | ||
355 | newPath.replaceAll("\\\\", "\\/")); | ||
356 | } | ||
357 | } | ||
358 | } | ||
359 | } | ||
360 | } | ||
361 | } | ||
362 | |||
363 | Transformer transformer = | ||
364 | TransformerFactory.newInstance().newTransformer(); | ||
365 | DOMSource source = new DOMSource(doc); | ||
366 | File file = new File(ClassLoader.getSystemResource(xml).toURI()); | ||
367 | FileOutputStream outstream = | ||
368 | new FileOutputStream("src/cppcheckplus/control/settings.xml"); | ||
369 | StreamResult reslut = new StreamResult(outstream); | ||
370 | transformer.transform(source, reslut); | ||
371 | outstream.close(); | ||
372 | |||
373 | } catch (Exception ex) { | ||
374 | ex.printStackTrace(); | ||
375 | } | ||
376 | } | ||
377 | |||
378 | public static void loadToolsRoad(String xml, UIFrame ui, | 321 | public static void loadToolsRoad(String xml, UIFrame ui, |
379 | ActionListener action) { | 322 | ActionListener action) { |
380 | try { | 323 | Yaml yaml = new Yaml(); |
381 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
382 | DocumentBuilder db = dbf.newDocumentBuilder(); | ||
383 | Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); | ||
384 | Element root = doc.getDocumentElement(); | ||
385 | NodeList tools = root.getChildNodes(); | ||
386 | 324 | ||
325 | try (InputStream input = Main.class.getClassLoader() | ||
326 | .getResourceAsStream(xml)) { | ||
327 | Map<String, Object> data = yaml.load(input); | ||
387 | MyRootMenu checkBar = new MyRootMenu(); | 328 | MyRootMenu checkBar = new MyRootMenu(); |
388 | String toolName, filePath, para; | 329 | // 遍历map的所有key-value |
389 | MyMenuItem menuItem; | 330 | data.forEach((key, value) -> { |
390 | if (tools == null) { | 331 | Map<String, Object> tool = (Map<String, Object>) value; |
391 | // 弹窗提示 | ||
392 | JOptionPane.showMessageDialog(null, "Error: 工具配置文件未找到", | ||
393 | "提示", 2); | ||
394 | return; | ||
395 | } | ||
396 | for (int i = 0; i < tools.getLength(); i++) { | ||
397 | org.w3c.dom.Node menu = tools.item(i); | ||
398 | if (menu.getNodeType() != Node.ELEMENT_NODE || | ||
399 | !menu.getNodeName().equalsIgnoreCase("tool")) { | ||
400 | continue; | ||
401 | } | ||
402 | toolName = MyContorlUtil.getStringAttribute(menu, "toolName"); | ||
403 | filePath = MyContorlUtil.getStringAttribute(menu, "filePath"); | ||
404 | para = MyContorlUtil.getStringAttribute(menu, "para"); | ||
405 | |||
406 | ToolsConfig tool = ToolsRegistry.getTool(toolName); | ||
407 | if (tool != null) { | ||
408 | tool.init(filePath, para, ui); | ||
409 | continue; | ||
410 | } | ||
411 | try { | 332 | try { |
412 | Class<?> clazz = Class.forName("toolsconfig." + toolName); | 333 | Class<?> clazz = Class.forName("toolsconfig." + key); |
413 | Object obj = clazz.getDeclaredConstructor().newInstance(); | 334 | Object obj = clazz.getDeclaredConstructor().newInstance(); |
414 | ToolsRegistry.getTool(toolName).init(filePath, para, ui); | 335 | ToolsRegistry.getTool(key).init(tool.get("path").toString(), |
336 | tool.get("params").toString(), ui); | ||
415 | } catch (Exception e) { | 337 | } catch (Exception e) { |
416 | e.printStackTrace(); | 338 | e.printStackTrace(); |
417 | } | 339 | } |
418 | menuItem = new MyMenuItem(); | 340 | MyMenuItem menuItem = new MyMenuItem(); |
419 | menuItem.setText(toolName); | 341 | menuItem.setText(key); |
420 | menuItem.setToolTipText(toolName); | 342 | menuItem.setToolTipText(key); |
421 | menuItem.setActionCommand(toolName); | 343 | menuItem.setActionCommand(key); |
422 | 344 | ||
423 | menuItem.addActionListener(action); | 345 | menuItem.addActionListener(action); |
424 | checkBar.add(menuItem); | 346 | checkBar.add(menuItem); |
425 | } | 347 | }); |
426 | 348 | ||
427 | checkBar.setText("检工具"); | 349 | checkBar.setText("检工具"); |
428 | ui.mymenubar.add(checkBar, 1); | 350 | ui.mymenubar.add(checkBar, 1); |
429 | } catch (Exception ex) { | 351 | } catch (Exception e) { |
430 | ex.printStackTrace(); | 352 | e.printStackTrace(); |
431 | } | 353 | } |
432 | } | 354 | } |
433 | 355 | ||