fix(approval): BPMN 流程图净化中文 id,修 bpmn-js「no diagram to display」

UI 走查实证:迁移的 BPMN 常用中文做 process/diagram id(如「招投标工程评审流程」),
bpmn-js/moddle 的 NCName 校验拒非 ASCII id → 整图不渲染(illegal ID)。
ProcessDiagramController.sanitizeBpmnIds 把非法(非 ASCII NCName)id 及其所有引用
(id/bpmnElement/sourceRef/targetRef/...)一致重写为安全 ASCII id(稳定哈希),
只动 id/引用类属性、不碰 name(保中文节点名),并同步重映射当前/历史节点 id。
单测 ProcessDiagramControllerTest 4 绿;浏览器 live:25 形状渲染+当前节点绿高亮+中文标签正常。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-26 08:45:25 +08:00
parent c95eaee0a5
commit 1be949cc04
2 changed files with 102 additions and 3 deletions

View File

@@ -95,13 +95,79 @@ public class ProcessDiagramController {
.distinct() .distinct()
.collect(Collectors.toList()); .collect(Collectors.toList());
// 迁移的 BPMN 常用中文做 process/diagram id(如「招投标工程评审流程」),bpmn-js/moddle 的 NCName
// 校验拒非 ASCII id → 整图「no diagram to display」。服务端把非法 id 及其所有引用一致重写为安全 ASCII id
// (只动 id/ref 类属性,不碰 name;活动 id 多为 sid-* 合法),并同步重映射当前/历史节点 id。
List<String> mutableActive = new java.util.ArrayList<>(active);
List<String> mutableFinished = new java.util.ArrayList<>(finished);
String safeXml = sanitizeBpmnIds(bpmnXml, mutableActive, mutableFinished);
resp.put("found", true); resp.put("found", true);
resp.put("bpmnXml", bpmnXml); resp.put("bpmnXml", safeXml);
resp.put("activeActivityIds", active); resp.put("activeActivityIds", mutableActive);
resp.put("finishedActivityIds", finished); resp.put("finishedActivityIds", mutableFinished);
return ResponseEntity.ok(resp); return ResponseEntity.ok(resp);
} }
/** id/引用类属性(需为合法 NCName;name/value 等展示属性不在内,避免改坏中文节点名)。 */
private static final java.util.regex.Pattern ID_ATTR = java.util.regex.Pattern.compile(
"\\b(id|bpmnElement|sourceRef|targetRef|attachedToRef|processRef|default)\\s*=\\s*\"([^\"]+)\"");
/**
* 把 BPMN 里非法(非 ASCII NCName)的 id 及其全部引用一致重写为安全 ASCII id(稳定哈希),
* 使 bpmn-js 能解析渲染。只重写 id/引用类属性,name 等展示属性不动。同步重映射 active/finished 节点 id。
*/
static String sanitizeBpmnIds(String xml, List<String> active, List<String> finished) {
if (xml == null || xml.isEmpty()) {
return xml;
}
// 1. 收集所有非法 id 值 → 安全 id 映射
java.util.Map<String, String> map = new java.util.LinkedHashMap<>();
java.util.regex.Matcher scan = ID_ATTR.matcher(xml);
while (scan.find()) {
String val = scan.group(2);
if (!isValidNcName(val) && !map.containsKey(val)) {
map.put(val, "n" + Integer.toHexString(val.hashCode() & 0x7fffffff));
}
}
if (map.isEmpty()) {
return xml;
}
// 2. 仅重写 id/引用类属性(appendReplacement 精确,不误伤 name=)
java.util.regex.Matcher rw = ID_ATTR.matcher(xml);
StringBuilder sb = new StringBuilder();
while (rw.find()) {
String attr = rw.group(1);
String val = rw.group(2);
String repl = map.getOrDefault(val, val);
rw.appendReplacement(sb, java.util.regex.Matcher.quoteReplacement(attr + "=\"" + repl + "\""));
}
rw.appendTail(sb);
// 3. 同步重映射返回的当前/历史节点 id
active.replaceAll(id -> map.getOrDefault(id, id));
finished.replaceAll(id -> map.getOrDefault(id, id));
return sb.toString();
}
/** ASCII NCName:首字符字母/下划线,其余字母/数字/下划线/连字符/点;非 ASCII(中文等)视为非法。 */
private static boolean isValidNcName(String s) {
if (s == null || s.isEmpty()) {
return false;
}
char c0 = s.charAt(0);
if (!((c0 >= 'A' && c0 <= 'Z') || (c0 >= 'a' && c0 <= 'z') || c0 == '_')) {
return false;
}
for (int i = 1; i < s.length(); i++) {
char c = s.charAt(i);
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
|| c == '_' || c == '-' || c == '.')) {
return false;
}
}
return true;
}
/** 读流程定义 BPMN XML(getProcessModel 返回部署的 BPMN 资源流)。读失败返回 null(前端降级)。 */ /** 读流程定义 BPMN XML(getProcessModel 返回部署的 BPMN 资源流)。读失败返回 null(前端降级)。 */
private String readProcessModel(String procDefId) { private String readProcessModel(String procDefId) {
try (InputStream is = repositoryService.getProcessModel(procDefId)) { try (InputStream is = repositoryService.getProcessModel(procDefId)) {

View File

@@ -117,4 +117,37 @@ class ProcessDiagramControllerTest {
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(false)); .andExpect(jsonPath("$.found").value(false));
} }
@Test
void sanitizeBpmnIds_中文id重写为安全ascii并同步引用与节点() {
// 迁移 BPMN 用中文 process id + 中文 diagram id,bpmn-js 拒非 ASCII NCName。重写后引用须一致,name 不动。
String xml = "<definitions>"
+ "<process id=\"招投标流程\" name=\"招投标流程\">"
+ "<userTask id=\"sid-A\"/>"
+ "<sequenceFlow id=\"f1\" sourceRef=\"招投标流程\" targetRef=\"sid-A\"/>"
+ "</process>"
+ "<bpmndi:BPMNDiagram id=\"BPMNDiagram_招投标流程\">"
+ "<bpmndi:BPMNPlane bpmnElement=\"招投标流程\"/>"
+ "</bpmndi:BPMNDiagram></definitions>";
List<String> active = new java.util.ArrayList<>(List.of("sid-A"));
List<String> finished = new java.util.ArrayList<>(List.of("招投标流程"));
String out = ProcessDiagramController.sanitizeBpmnIds(xml, active, finished);
// 中文 id 不再出现在 id/ref 属性里,但 name 保留中文
org.junit.jupiter.api.Assertions.assertFalse(out.contains("id=\"招投标流程\""));
org.junit.jupiter.api.Assertions.assertFalse(out.contains("bpmnElement=\"招投标流程\""));
org.junit.jupiter.api.Assertions.assertFalse(out.contains("sourceRef=\"招投标流程\""));
org.junit.jupiter.api.Assertions.assertTrue(out.contains("name=\"招投标流程\"")); // 展示名不动
org.junit.jupiter.api.Assertions.assertTrue(out.contains("id=\"sid-A\"")); // 合法 id 不动
// 同一中文 id 的所有引用映射到同一安全 id(process id 与 sourceRef/bpmnElement 一致)
java.util.regex.Matcher m = java.util.regex.Pattern.compile("<process id=\"(n[0-9a-f]+)\"").matcher(out);
org.junit.jupiter.api.Assertions.assertTrue(m.find());
String safe = m.group(1);
org.junit.jupiter.api.Assertions.assertTrue(out.contains("sourceRef=\"" + safe + "\""));
org.junit.jupiter.api.Assertions.assertTrue(out.contains("bpmnElement=\"" + safe + "\""));
// 历史节点 id 同步重映射,当前节点 sid-A 不变
org.junit.jupiter.api.Assertions.assertEquals(safe, finished.get(0));
org.junit.jupiter.api.Assertions.assertEquals("sid-A", active.get(0));
}
} }