如何强制EMF不操作引用id



有了以下代码,我正在加载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");

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

如果您正在从流、XML等加载data.bpmn。。。

EMF持久性API

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

void save(输出流输出流,映射选项)抛出IOException;void load(InputStream InputStream,Map选项)抛出IOException;您可能认为这意味着EMF资源本质上是"基于流的"。尽管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);

最新更新