From 0dd8ef8ae98e81d980812537ad9f559ec5998f2f Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Wed, 26 Mar 2025 20:48:39 +0800 Subject: Change tools config to yaml, to prepare for add rule set As planned, it's time to develop rule set. We should let users themselves to decide which rule to use, so it's necessary to make a whole set. Besides, the set shall be configurable. So, the rules set should has 2 levels: rules themselves, to be shown to users, as well as map from our abstract rules to rule in specific tools such as clang-tidy. Then it comes: how to make abstract rule set and the map to target rule? We can define abstract rules in string, and write the map in tool settings. That't why I change tool config from xml to yaml. In short, I want to achieve such an effect: ```yaml NullPointer: "Dont use null pointer" StructInit: "Struct Init" ``` and in tools config: ```yaml clangTidy: path: "/path/to/clang-tidy" params: "" rule_mapping: NullPointer: "xxx"(spercific rule name in clang-tidy) StructInit: "yyy" .... ``` Hoping it will work as the way I need. --- lib/snakeyaml-2.4.jar | Bin 0 -> 339838 bytes src/cppcheckplus/control/MyContorlUtil.java | 118 +++++----------------------- src/cppcheckplus/control/UIFrame.java | 5 +- src/cppcheckplus/control/menubar.xml | 1 + src/toolsconfig/settings.xml | 8 -- src/toolsconfig/tools.yaml | 11 +++ 6 files changed, 34 insertions(+), 109 deletions(-) create mode 100644 lib/snakeyaml-2.4.jar delete mode 100644 src/toolsconfig/settings.xml create mode 100644 src/toolsconfig/tools.yaml diff --git a/lib/snakeyaml-2.4.jar b/lib/snakeyaml-2.4.jar new file mode 100644 index 0000000..c8bb638 Binary files /dev/null and b/lib/snakeyaml-2.4.jar differ 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; import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.Box; @@ -35,7 +36,6 @@ import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.plaf.FontUIResource; import javax.xml.parsers.DocumentBuilder; @@ -48,7 +48,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import toolsconfig.ToolsConfig; +import org.yaml.snakeyaml.Yaml; import toolsconfig.ToolsRegistry; @@ -318,116 +318,38 @@ public class MyContorlUtil { return menuBar; } - - public static void fixSettingPath(String xml, String tool, String newPath) { - try { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); - Element root = doc.getDocumentElement(); - NodeList tools = root.getChildNodes(); - if (tools != null) { - for (int i = 0; i < tools.getLength(); i++) { - org.w3c.dom.Node menu = tools.item(i); - if (menu.getNodeType() == Node.ELEMENT_NODE) { - if (menu.getNodeName().equalsIgnoreCase("tool")) { - org.w3c.dom.Node att = - menu.getAttributes().getNamedItem("id"); - String toolName = - MyContorlUtil.getStringAttribute(menu, - "toolName"); - if (tool.equals("cppcheck") && - toolName.equals("cppcheck")) { - org.w3c.dom.Node attribute = - menu.getAttributes() - .getNamedItem("filePath"); - if (attribute != null) { - attribute.setNodeValue( - newPath.replaceAll("\\\\", "\\/")); - } - } else if (tool.equals("flawfinder") && - toolName.equals("flawfinder")) { - org.w3c.dom.Node attribute = - menu.getAttributes() - .getNamedItem("filePath"); - if (attribute != null) { - attribute.setNodeValue( - newPath.replaceAll("\\\\", "\\/")); - } - } - } - } - } - } - - Transformer transformer = - TransformerFactory.newInstance().newTransformer(); - DOMSource source = new DOMSource(doc); - File file = new File(ClassLoader.getSystemResource(xml).toURI()); - FileOutputStream outstream = - new FileOutputStream("src/cppcheckplus/control/settings.xml"); - StreamResult reslut = new StreamResult(outstream); - transformer.transform(source, reslut); - outstream.close(); - - } catch (Exception ex) { - ex.printStackTrace(); - } - } - public static void loadToolsRoad(String xml, UIFrame ui, ActionListener action) { - try { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); - Element root = doc.getDocumentElement(); - NodeList tools = root.getChildNodes(); + Yaml yaml = new Yaml(); + try (InputStream input = Main.class.getClassLoader() + .getResourceAsStream(xml)) { + Map data = yaml.load(input); MyRootMenu checkBar = new MyRootMenu(); - String toolName, filePath, para; - MyMenuItem menuItem; - if (tools == null) { - // 弹窗提示 - JOptionPane.showMessageDialog(null, "Error: 工具配置文件未找到", - "提示", 2); - return; - } - for (int i = 0; i < tools.getLength(); i++) { - org.w3c.dom.Node menu = tools.item(i); - if (menu.getNodeType() != Node.ELEMENT_NODE || - !menu.getNodeName().equalsIgnoreCase("tool")) { - continue; - } - toolName = MyContorlUtil.getStringAttribute(menu, "toolName"); - filePath = MyContorlUtil.getStringAttribute(menu, "filePath"); - para = MyContorlUtil.getStringAttribute(menu, "para"); - - ToolsConfig tool = ToolsRegistry.getTool(toolName); - if (tool != null) { - tool.init(filePath, para, ui); - continue; - } + // 遍历map的所有key-value + data.forEach((key, value) -> { + Map tool = (Map) value; try { - Class clazz = Class.forName("toolsconfig." + toolName); + Class clazz = Class.forName("toolsconfig." + key); Object obj = clazz.getDeclaredConstructor().newInstance(); - ToolsRegistry.getTool(toolName).init(filePath, para, ui); + ToolsRegistry.getTool(key).init(tool.get("path").toString(), + tool.get("params").toString(), ui); } catch (Exception e) { e.printStackTrace(); } - menuItem = new MyMenuItem(); - menuItem.setText(toolName); - menuItem.setToolTipText(toolName); - menuItem.setActionCommand(toolName); + MyMenuItem menuItem = new MyMenuItem(); + menuItem.setText(key); + menuItem.setToolTipText(key); + menuItem.setActionCommand(key); menuItem.addActionListener(action); checkBar.add(menuItem); - } + }); - checkBar.setText("检测工具"); + checkBar.setText("检查工具"); ui.mymenubar.add(checkBar, 1); - } catch (Exception ex) { - ex.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); } } diff --git a/src/cppcheckplus/control/UIFrame.java b/src/cppcheckplus/control/UIFrame.java index 499d056..100b82c 100644 --- a/src/cppcheckplus/control/UIFrame.java +++ b/src/cppcheckplus/control/UIFrame.java @@ -34,7 +34,6 @@ import javax.swing.JTextArea; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.plaf.TabbedPaneUI; -import toolsconfig.ToolsConfig; import toolsconfig.ToolsRegistry; // 构造界面 @@ -43,7 +42,7 @@ public class UIFrame extends JFrame { // XML配置文件路径 private final String menuBarXML = "cppcheckplus/control/menubar.xml"; private final String outlookPanelXML = "cppcheckplus/control/outlook.xml"; - private final String settingsXML = "toolsconfig/settings.xml"; + private final String settings = "toolsconfig/tools.yaml"; private final String stdclangXML = "cppcheckplus/control/stdclang.xml"; // 界面组件 private final MyContentPanel contentPanel; @@ -196,7 +195,7 @@ public class UIFrame extends JFrame { } }); - MyContorlUtil.loadToolsRoad(settingsXML, this, new ActionListener() { + MyContorlUtil.loadToolsRoad(settings, this, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String toolName = e.getActionCommand(); diff --git a/src/cppcheckplus/control/menubar.xml b/src/cppcheckplus/control/menubar.xml index 0aae26b..17ff0b8 100644 --- a/src/cppcheckplus/control/menubar.xml +++ b/src/cppcheckplus/control/menubar.xml @@ -9,6 +9,7 @@ + diff --git a/src/toolsconfig/settings.xml b/src/toolsconfig/settings.xml deleted file mode 100644 index 77dd785..0000000 --- a/src/toolsconfig/settings.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/src/toolsconfig/tools.yaml b/src/toolsconfig/tools.yaml new file mode 100644 index 0000000..0f9f013 --- /dev/null +++ b/src/toolsconfig/tools.yaml @@ -0,0 +1,11 @@ +clangTidy: + path: "clang-tidy.exe" + params: "-I C:\\Program Files\\LLVM\\lib\\clang\\17" + +cppcheck: + path: "C:\\Users\\we-unite\\Desktop\\Project\\Project\\code\\cppcheck\\bin\\debug\\cppcheck.exe" + params: "--enable=all" + +flawfinder: + path: "C:\\Users\\we-unite\\Desktop\\Project\\Project\\code\\flawfinder-2.0.19\\flawfinder-2.0.19\\flawfinder.py" + params: "" -- cgit v1.2.3-70-g09d2