如何强制 EMF 不操纵引用 ID

How to force EMF not to manipulate reference ids

使用以下代码我正在加载 BPMN 模型。

// dummy URI, loading done through input stream
URI uri = URI.createURI("data.bpmn");
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(uri, "org.eclipse.bpmn2.content-type.xml");
resource.load(contentStream, null);

保存资源 resource.save(outputStream, null); 操作输出并将 data.bpmn# 添加到引用:

<bpmndi:BPMNShape id="BPMNShape_StartEvent_1" bpmnElement="data.bpmn#StartEvent_1">
    <dc:Bounds height="36.0" width="36.0" x="162.0" y="182.0"/>
        <bpmndi:BPMNLabel id="BPMNLabel_1" labelStyle="data.bpmn#BPMNLabelStyle_1">
            <dc:Bounds height="15.0" width="68.0" x="146.0" y="218.0"/>
        </bpmndi:BPMNLabel>
</bpmndi:BPMNShape>

输入流中看起来像这样的地方:

<bpmndi:BPMNShape id="BPMNShape_StartEvent_1" bpmnElement="StartEvent_1">
    <dc:Bounds height="36.0" width="36.0" x="162.0" y="182.0"/>
        <bpmndi:BPMNLabel id="BPMNLabel_1" labelStyle="BPMNLabelStyle_1">
            <dc:Bounds height="15.0" width="68.0" x="146.0" y="218.0"/>
        </bpmndi:BPMNLabel>
</bpmndi:BPMNShape>

有没有办法强制 EMF 不操纵引用?

改变这个:

 URI uri = URI.createURI("data.bpmn");

 URI.createPlatformResourceURI("/full/workspace/path/data.bpmn");

您必须使用 ResourceSet's/global 包注册表将 data.bpmn 中的任何包定义注册到 EPackage...

如果您从 Stream 加载 data.bpmn,XML 等...

The EMF Persistence API

Resource 接口包括第二个版本的 save() 和 load() 方法,其中包括一个流参数:

void save(OutputStream 输出流, 映射选项)抛出 IOException; void load(InputStream inputStream, Map options) 抛出 IOException; 您可能认为这意味着 EMF 资源本质上是 "stream based." 尽管与 EMF 一起使用的大多数资源往往是基于流的,包括 EMF 提供的 XML 资源,但不是基于流的(例如,关系数据库)资源也可以实现....

我是这样解决的:

ResourceSet resourceSet = new ResourceSetImpl();
XMLResource resource = (XMLResource) resourceSet.createResource(modelUri, "org.eclipse.bpmn2.content-type.xml");
XMLResource.URIHandler uriHandler = new URIHandlerImpl() {
    @Override
    public URI deresolve(URI uri) {
        // make sure references are stored without # URI prefix
        return URI.createURI(uri.fragment());
    }
};
resource.getDefaultSaveOptions().put(XMLResource.OPTION_URI_HANDLER, uriHandler);

resource.load(inputStream, null);