【问题标题】:Using UML2 to parse a XMI file, how to handle href properties?使用UML2解析XMI文件,如何处理href属性?
【发布时间】:2013-08-01 13:49:10
【问题描述】:

我使用UML2 解析一个包含UML shema 的XMI 文件。

这是我的Modelio生成的.uml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<uml:Model xmi:version="2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" xmi:id="_PuKnYPqnEeKOHL0-05J3_Q" name="xmitojava">
  <eAnnotations xmi:id="_PuKnYfqnEeKOHL0-05J3_Q" source="Objing">
    <contents xmi:type="uml:Property" xmi:id="_PuKnYvqnEeKOHL0-05J3_Q" name="exporterVersion">
      <defaultValue xmi:type="uml:LiteralString" xmi:id="_PuKnY_qnEeKOHL0-05J3_Q" value="2.2"/>
    </contents>
  </eAnnotations>
  <ownedComment xmi:id="_PuKnZPqnEeKOHL0-05J3_Q">
    <body></body>
  </ownedComment>
  <packagedElement xmi:type="uml:Class" xmi:id="_PuKnZfqnEeKOHL0-05J3_Q" name="User">
    <ownedAttribute xmi:id="_PuKnZvqnEeKOHL0-05J3_Q" name="login" visibility="protected" isUnique="false" isReadOnly="true">
      <type xmi:type="uml:PrimitiveType" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String"/>
    </ownedAttribute>
  </packagedElement>
  <packagedElement xmi:type="uml:Class" xmi:id="_PuKnZ_qnEeKOHL0-05J3_Q" name="Group"/>
</uml:Model>

可以注意到User类中,login的类型是这样定义的:

 <type xmi:type="uml:PrimitiveType" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String"/>

所以我的问题是:如何处理这个案子?确实,我尝试以这种方式解析此文档:

URI typesUri = URI.createFileURI(pathToMyUmlFile);

ResourceSet set = new ResourceSetImpl();
set.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
set.createResource(typesUri);

Resource r = set.getResource(typesUri, true);

Model model = (Model) EcoreUtil.getObjectByType(r.getContents(), UMLPackage.Literals.MODEL);

但在那之后,我的名为login 的属性的类型为空。

感谢您的帮助:)

【问题讨论】:

    标签: java parsing uml xmi


    【解决方案1】:

    这是一个常见问题。来自MDT/UML2 FAQ

    resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
    
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
    Map uriMap = resourceSet.getURIConverter().getURIMap();
    URI uri = URI.createURI("jar:file:/C:/eclipse/plugins/org.eclipse.uml2.uml.resources_<version>.jar!/"); // for example
    uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
    uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
    uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));
    

    【讨论】:

    • 是的,我试过了,但它不起作用,类型仍然是 null :(
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多