java emf存储数据库_EMF基础知识积累

在项目中用到EMF积累的一些基础知识,希望对大家有所帮助。

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

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

3.关于URI(Uniform Resource Identifier)

在EMF中是通过URI来标识package的,并且通过URI来唯一的确定resources。URI包括三个部分:a scheme, a scheme-specific part和an optional fragment。scheme主要决定了访问

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

4.关于URIConverter

URIConverter的作用是normalize一个输入URI,使之成为一个实际的针对某个resource的URI。它可以把namespace URIs(比如:http:///com/example/epo2.ecore)映射到物理文件的URIs,或者重定向旧的(或别名)的URI参考到一个具体的实际的location。一个URIConverter维护一个URI到URI的映射集合。比如,把一个命名空间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()方法只是简单的通过映射的map把key替换成了相应的value。

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

5.关于Resource和ResourceSet

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

Resource resource =…

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

Item item =…

String fragment = resource.getURIFragment(item);

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

ResourceSet中有些重要的方法:

createResource()创建一个空的Resource;

getResource()通过resource的URI来创建Resource;

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

6.关于Resource.Factory

用来创建Resource,resource factory要注册到Registry实例中。一个factory可以通过多种方式的URIs来注册,包括URI scheme或者URI的extension。在插件方式的应用中,通过扩展点的方式在插件加载的时候注册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.

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

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

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

("*", new XMIResourceFactoryImpl());

7. Resource通过两种方式实现XML和XMI。

1) XML

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

There are several reasons why you might want to useOPTION_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

XMI和XML基本相同,XMIResourceImpl就是

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值