白玉成的专栏

Eclipse,让我欢喜让我忧!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  17 随笔 :: 0 文章 :: 6 评论 :: 0 Trackbacks
在项目中用到EMF积累的一些基础知识,希望对大家有所帮助。

  1. EMF core 除了生成modelmodel implementation外,至少还生成两个接口:*Factory*Package。其中, Factory:生成实例的工厂;Package:提供一些静态的constants(例如:被用来生成方法的feature constants     )和一些访问模型元数据的公用方法。

 

2. EMF生成的方法都带有“@generated”标签。EMF重新生成代码时不会覆盖用户添加的方法(不带有@generated)。

 

3. 关于URIUniform Resource Identifier

EMF 中是通过URI来标识package的,并且通过URI来唯一的确定resourcesURI包括三个部分:a scheme, a scheme-specific partan optional fragmentscheme主要决定了访问

资源的协议;比如:Platform:/resource/……中的platformscheme-specific part包含一些authority, device和一些segments,如file:/c:/dir1/dir2/myfile.xmlfile是协议,没有authorityc:device,剩下的3个是segmentsURI fragment 标识了resource内部的某个具体的内容。如:file:/c:/dir1/dir2/myfile.xml#loc中的#loc EMF通过带有fragmentURI来访问资源中的EObjects

 

4. 关于URIConverter

URIConverter的作用是normalize一个输入URI,使之成为一个实际的针对某个resourceURI。它可以把namespace URIs(比如:http:///com/example/epo2.ecore)映射到物理文件的URIs, 或者重定向旧的(或别名)的URI参考到一个具体的实际的location。一个URIConverter维护一个URIURI的映射集合。比如,把一个命名空间URI映射到物理文件:

URIConverter converter = new URIConverterImpl();

URI uri1 = URI.createURI("http:///somemodel.ecore");

URI uri2 =URI.createURI("platform:/resource/project/somemodel.ecore");

converter.getURIMap().put(uri1, uri2);

在如下面代码:

URI normalized = converter.normalize(uri1);

System.out.println(normalized);

打印的结果是:platform:/resource/project/somemodel.ecore

URIConverter.normalize()方法只是简单的通过映射的mapkey替换成了相应的value

URIConverter的最原始是应用在resource sets,用来定位resources.

 

5. 关于ResourceResourceSet

Resource 表示一个持久化的EOjbects的容器;ResourceSet表示一组Resource的集合,集合中的Resource同时创建或加载。Resource中比较重要的就是saveload方法,还有通过URI fragments访问资源中的Object的机制,如:

Resource resource =…

Item item = (Item)resource.getEObject("//@orders.0/@items.2");

Item item =…

String fragment = resource.getURIFragment(item);

上面代码中的两个方法,getEObject通过带有fregmentURI获得一个EObject,与之相反的方法getURIFragment()通过EObject获得相应的fragment path

ResourceSet中有些重要的方法:

createResource()创建一个空的Resource

getResource()通过resourceURI来创建Resource

getEObject(),通过URI中的fregment来获得具体的EObject对象。

 

6. 关于Resource.Factory

用来创建Resourceresource factory 要注册到Registry实例中。一个factory 可以通过多种方式的URIs来注册,包括URI scheme或者URIextension。在插件方式的应用中,通过扩展点的方式在插件加载的时候注册descriptor。下面是Resource的源代码

public interface Resource extends Notifier

{

interface Factory

{

Resource createResource(URI uri);

interface Descriptor

{

Factory createFactory();

}

interface Registry

{

Factory getFactory(URI uri);

Map getProtocolToFactoryMap();

String DEFAULT_EXTENSION = "*";

Map getExtensionToFactoryMap();

Registry INSTANCE = new ResourceFactoryRegistryImpl

();

}

}

}

下面是Registry中的getFactory()方法的算法(引用原文):

1. Check for a factory in the protocolToFactoryMap, using

the scheme of the URI.

2. If nothing was found, check the extensionToFactoryMap

using the file extension of the URI.

3. If still nothing was found, check the

extensionToFactoryMap using the DEFAULT_EXTENSION

(that is, the wildcard character "*").

4. If no extension match was found, call the

delegatedGetFactory() method. This allows you to supply

your own factory registry, with its own lookup

criteria.

5. If a descriptor was found, instead of an actual

factory, call the createFactory() method on the

descriptor to create the factory.

6. Finally, return the factory if one was found, or null.

tipemf缺省的序列化方式是XMI。因此,如果没有找到相应注册的factory缺省的就会返回以*注册的缺省的factory,这个factory是针对XMIfactory,即XMIResourceFactoryImpl。如,对于XMIResourceFactoryImpl的扩展点声明:

<extension point = "org.eclipse.emf.ecore.extension_parser">

<parser type="*" class="org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl"/>

</extension>

当非插件应用的时候,可以通过手工的方式来注册factory,如:

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put

("*", new XMIResourceFactoryImpl());

 

7. Resource通过两种方式实现XMLXMI

1) XML

实现Resource存储的重要方法是save, 只有在方法eIsSet()返回true时,相关的属性和引用才会被序列化到xml中。对于存储的xml文件的格式可以通过设置OPTION_XML_MAP属性来完成,如下面原文:

There are several reasons why you might want to use OPTION_XML_MAP when saving a resource:

1. You want to save your data so that it conforms to a particular XML Schema.

2. You have changed your model, and you want the resource to produce XML documents that will work with the old version of the model.

3. You want to design your own XML format without changing your model.

2) XMI

XMIXML基本相同,XMIResourceImpl就是

XMLResourceImpl的简单继承,只是在引用objects时使用XMI IDs

posted on 2008-11-25 22:26 白玉成 阅读(492) 评论(0)  编辑  收藏 所属分类: EMF

只有注册用户登录后才能发表评论。


网站导航: