|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 unknowIfGuestInDream. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of unknowIfGuestInDream, any associated website, nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY |
| 20 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +package com.tlcsdm.core.util; |
| 29 | + |
| 30 | +import cn.hutool.core.io.resource.ResourceUtil; |
| 31 | +import org.junit.jupiter.api.BeforeAll; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 34 | +import org.w3c.dom.Document; |
| 35 | +import org.w3c.dom.Element; |
| 36 | +import org.w3c.dom.Node; |
| 37 | +import org.w3c.dom.NodeList; |
| 38 | +import org.xml.sax.SAXException; |
| 39 | + |
| 40 | +import javax.xml.parsers.DocumentBuilder; |
| 41 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 42 | +import javax.xml.parsers.ParserConfigurationException; |
| 43 | +import javax.xml.xpath.XPath; |
| 44 | +import javax.xml.xpath.XPathConstants; |
| 45 | +import javax.xml.xpath.XPathExpressionException; |
| 46 | +import javax.xml.xpath.XPathFactory; |
| 47 | +import java.io.File; |
| 48 | +import java.io.IOException; |
| 49 | +import java.util.Arrays; |
| 50 | +import java.util.HashMap; |
| 51 | +import java.util.LinkedHashMap; |
| 52 | +import java.util.Map; |
| 53 | + |
| 54 | +/** |
| 55 | + * 解析mtpj构建ccrl命令 |
| 56 | + */ |
| 57 | +@ExtendWith(JunitMethodExecutionTimeExtension.class) |
| 58 | +class CSCcrlParseTest { |
| 59 | + |
| 60 | + private static File project = null; |
| 61 | + private static String compilerPath = null; |
| 62 | + private static File compileFile = null; |
| 63 | + private static String projectName = null; |
| 64 | + private static String deviceFamily = null; |
| 65 | + private static String[] tagNames = null; |
| 66 | + |
| 67 | + @BeforeAll |
| 68 | + static void init() { |
| 69 | + project = new File(ResourceUtil.getResource("util/ccrl.mtpj").getFile()); |
| 70 | + compilerPath = "C:\\Program Files (x86)\\Renesas Electronics\\CS+\\CC\\CC-RL\\V1.12.01\\bin"; |
| 71 | + compileFile = new File(compilerPath); |
| 72 | + projectName = "ccrl_project"; |
| 73 | + deviceFamily = "RL78"; |
| 74 | + tagNames = new String[]{"COptionG-0", "COptionGLine-0", "GeneralOptionCpu-0", |
| 75 | + //Language standard specification |
| 76 | + "COptionLangC-0", "CppOptionLangOfCppSourceFile-0", |
| 77 | + //Preprocessor control |
| 78 | + "COptionD-0", "COptionU-0", "COptionI-0", "COptionPreinclude-0", |
| 79 | + //Optimization |
| 80 | + "COptionOsize-0", "COptionOunroll-0", "COptionOdeleteStaticFunc-0", |
| 81 | + "COptionOinlineLevel-0", "COptionOinlineSize-0", "COptionOpipeline-0", "COptionOtailCall-0", "COptionOintermodule-0", |
| 82 | + "COptionOaliasAnsi-0", "COptionOsameCode-0 ", "COptionObranchChaining-0", "COptionOalign-0", "COptionGoptimize-0", |
| 83 | + //Code generation changing |
| 84 | + "COptionDblSize-0", "COptionSignedChar-0", "COptionSignedBitfield-0", "COptionSwitch-0", "COptionVolatile-0", |
| 85 | + "COptionMergeString-0", "COptionPack-0", "COptionStuffBSS-0", "COptionStuffData-0", "COptionStuffConst-0", |
| 86 | + "COptionStackProtector-0", "COptionStackProtectorValue-0", "COptionControlFlowIntegrity-0", "COptionInsertNopWithLabel-0", |
| 87 | + //Extensions |
| 88 | + "COptionLargeVariable-0", |
| 89 | + //Compiler transition support |
| 90 | + "COptionConvertCc-0", "COptionUnalignedPointerForCA78K0R-0", |
| 91 | + //rlink |
| 92 | + //Input control |
| 93 | + "LinkOptionDefine-0", "LinkOptionInput-0", "LinkOptionBinary-0", "LinkOptionEntry-0", "LinkOptionEntryPoint-0", "LibOptionAllowDuplicateModuleName-0", |
| 94 | + //Output control |
| 95 | + "HexOptionForm-0", "LinkOptionDebug-0", "LinkOptionRom-0", "HexOptionSpace-0", "HexOptionSpaceValue-0", "LinkOptionMessage-0", |
| 96 | + "LinkOptionVect-0", "LinkOptionVectN-0", |
| 97 | + //List output |
| 98 | + "LinkOptionShowVector-0", "LinkOptionShowStruct-0", "LinkOptionShowRelocationAttribute-0", "LinkOptionShowCFI-0", |
| 99 | + "LinkOptionShowSymbol-0", "LinkOptionShowReference-0", "LinkOptionShowXreference-0", "LinkOptionShowTotalSize-0", |
| 100 | + //Optimization |
| 101 | + "COptionGoptimize-0", "LinkOptionOptimize-0", "LinkOptionOptimizeSymbolDelete-0", "LinkOptionOptimizeBranch-0", |
| 102 | + "LinkOptionSectionForbid-0", "LinkOptionAbsoluteForbid-0", "LinkOptionSymbolForbid-0", |
| 103 | + //Section specification |
| 104 | + "LinkOptionStart-0", "LinkOptionFSymbol-0", "LinkOptionUserOptByte-0", "LinkOptionUserOptByteValue-0", "LinkOptionOcdbg-0", |
| 105 | + "LinkOptionOcdbgValue-0", "LinkOptionSecurityOptByte-0", "LinkOptionSecurityOptByteValue-0", "LinkOptionSecurityId-0", |
| 106 | + "LinkOptionFlashSecurityId-0", "LinkOptionAutoSectionLayout-0", "LinkOptionSplitSection-0", "LinkOptionDspMemoryArea-0", |
| 107 | + "LinkOptionDebugMonitorSetting-0", "LinkOptionDebugMonitor-0", "LinkOptionOcdTr-0", |
| 108 | + //Other |
| 109 | + "LinkOptionCompress-0", "LinkOptionMemory-0", "LinkOptionTotalSize-0", "LinkOptionLogo-0", |
| 110 | + //dspasm |
| 111 | + "DSPAsmOptionOutputAsm-0", "DSPAsmOptionOutputVerilog-0", "DSPAsmOptionTextmacro-0", "DSPAsmOptionDefine-0", |
| 112 | + "DSPAsmOptionAllowTextMacroRedefine-0", "DSPAsmOptionCore-0", "DSPAsmOptionCodeSectionStart-0", "DSPAsmOptionDataSectionStart-0", |
| 113 | + "DSPAsmOptionNoDebugInfo-0", "DSPAsmOptionLabel-0", "DSPAsmOptionMacroIdentifyExact-0" |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void parseXpath() { |
| 119 | + // 构造 XPath 查询语句(用 | 分隔) |
| 120 | + String xpathExpr = Arrays.stream(tagNames) |
| 121 | + .map(tag -> "//" + tag) |
| 122 | + .reduce((a, b) -> a + " | " + b) |
| 123 | + .orElse(""); |
| 124 | + |
| 125 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 126 | + Document document = null; |
| 127 | + try { |
| 128 | + // 加载 XML 文档 |
| 129 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 130 | + document = builder.parse(project); |
| 131 | + |
| 132 | + // 执行 XPath 查询 |
| 133 | + XPath xpath = XPathFactory.newInstance().newXPath(); |
| 134 | + NodeList nodeList = (NodeList) xpath.evaluate(xpathExpr, document, XPathConstants.NODESET); |
| 135 | + |
| 136 | + // 构造标签->值映射 |
| 137 | + Map<String, String> result = new HashMap<>(); |
| 138 | + |
| 139 | + // 把找到的节点填入映射 |
| 140 | + for (int i = 0; i < nodeList.getLength(); i++) { |
| 141 | + Node node = nodeList.item(i); |
| 142 | + result.put(node.getNodeName(), node.getTextContent().trim()); |
| 143 | + } |
| 144 | + |
| 145 | + // 输出结果 |
| 146 | + for (String tag : tagNames) { |
| 147 | + String value = result.getOrDefault(tag, null); |
| 148 | + if (value != null) { |
| 149 | + System.out.println(tag + " = " + value); |
| 150 | + } else { |
| 151 | + System.out.println(tag + " 未找到"); |
| 152 | + } |
| 153 | + } |
| 154 | + } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e) { |
| 155 | + throw new RuntimeException(e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void parseDoc() { |
| 161 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 162 | + Document document = null; |
| 163 | + try { |
| 164 | + // 加载 XML 文档 |
| 165 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 166 | + document = builder.parse(project); |
| 167 | + |
| 168 | + // 结果保存为 Map:标签名 -> 内容(如果不存在则为 null 或 "") |
| 169 | + Map<String, String> result = new LinkedHashMap<>(); |
| 170 | + |
| 171 | + Element root = document.getDocumentElement(); |
| 172 | + |
| 173 | + for (String tag : tagNames) { |
| 174 | + NodeList nodes = root.getElementsByTagName(tag); |
| 175 | + if (nodes != null && nodes.getLength() > 0) { |
| 176 | + String value = nodes.item(0).getTextContent().trim(); |
| 177 | + result.put(tag, value); |
| 178 | + } else { |
| 179 | + result.put(tag, null); // 或者 "" / "未找到" |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + // 输出结果 |
| 184 | + for (Map.Entry<String, String> entry : result.entrySet()) { |
| 185 | + System.out.println(entry.getKey() + " = " + (entry.getValue() != null ? entry.getValue() : "【未找到】")); |
| 186 | + } |
| 187 | + } catch (ParserConfigurationException | SAXException | IOException e) { |
| 188 | + throw new RuntimeException(e); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments