打印 EObject?

发布于 2024-10-10 18:45:13 字数 867 浏览 0 评论 0原文

我正在编写一些 eclipse emf 代码,并希望打印 EObject 的内容(而不是将其存储到磁盘)。

这是我尝试的:

  public static void print(EObject obj) {
    Resource eResource = obj.eResource();
    try {
      eResource.save(System.out, null);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

但这给出了 NullPointerException。我已经尝试过这个:

  public static void print(EObject obj) {
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap()
        .put("*", new XMIResourceFactoryImpl());
    Resource resource = resourceSet.createResource(URI.createURI("dummyfile.xml"));
    resource.getContents().add(obj);
    try {
      resource.save(System.out, null);
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

这可行,但是如果不指定虚拟 URI 就无法打印到屏幕吗?

I am writing some eclipse emf code and would like to print the content of an EObject (not store it to disk).

Here is what I try:

  public static void print(EObject obj) {
    Resource eResource = obj.eResource();
    try {
      eResource.save(System.out, null);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

but that gives a NullPointerException. I have tried this instead:

  public static void print(EObject obj) {
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap()
        .put("*", new XMIResourceFactoryImpl());
    Resource resource = resourceSet.createResource(URI.createURI("dummyfile.xml"));
    resource.getContents().add(obj);
    try {
      resource.save(System.out, null);
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

This works, but is it not possible to print to screen without specifying a dummy URI??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2024-10-17 18:45:13

更新为包含 EcoreUtil.copy()

检查此代码。

Resource res = new XMLResourceImpl ();
res.getContents().add(EcoreUtil.copy(obj));
try {
  resource.save(System.out, null);
} catch (IOException ioe) {
  ioe.printStackTrace();
}

如果失败,那么你需要一个虚拟 URI

Resource res = new XMLResourceImpl (URI.createURI("dummyfile.xml"));
res.getContents().add(EcoreUtil.copy(obj));
try {
  resource.save(System.out, null);
} catch (IOException ioe) {
  ioe.printStackTrace();
}

Updated to include EcoreUtil.copy()

Check this code.

Resource res = new XMLResourceImpl ();
res.getContents().add(EcoreUtil.copy(obj));
try {
  resource.save(System.out, null);
} catch (IOException ioe) {
  ioe.printStackTrace();
}

If that fails then yes you need a dummy URI

Resource res = new XMLResourceImpl (URI.createURI("dummyfile.xml"));
res.getContents().add(EcoreUtil.copy(obj));
try {
  resource.save(System.out, null);
} catch (IOException ioe) {
  ioe.printStackTrace();
}
留蓝 2024-10-17 18:45:13

嗯,当我传递副本时:

Resource res = new XMLResourceImpl ();
res.getContents().add(ECoreUtil.copy(obj));
try {
  resource.save(System.out, null);
} catch (IOException ioe) {
  ioe.printStackTrace();
}

某些 xmi 属性未打印。但是,如果我多次调用上述方法并且不传递副本,我会收到 NullPointerException。我想我不理解这里的一些基本 EMF/遏制功能?

所以我更新的问题是:

如果在以下代码中使用模型,是否可以在不修改内容的情况下打印完整的 EObject 模型?

Hm when I pass a copy:

Resource res = new XMLResourceImpl ();
res.getContents().add(ECoreUtil.copy(obj));
try {
  resource.save(System.out, null);
} catch (IOException ioe) {
  ioe.printStackTrace();
}

some of the xmi attributes are not printed. But if I call the above method multiple times and DON't pass a copy I get a NullPointerException. I guess I am not understanding some basic EMF/Containment functionality here?

So my updated question is:

Is it possible to print a FULL EObject model without modifying the content if the model is used in the following code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文