EMF的一些总结(2)——关于EMF的序列化
對于EMF的序列化有幾個比較重要的接口:Resource,ResourceSet,Resource.Factory,URIConverter。這些接口的主要作用就是保存模型到持久化存儲介質,或者從持久化存儲介質加載模型。
1.關于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 fregment標識了resource內部的某個具體的內容。如:file:/c:/dir1/dir2/myfile.xml#loc中的#loc。 EMF通過帶有fregment的URI來訪問資源中的EObjects。
2.關于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.
3.關于Resource和ResourceSet
Resource 表示一個持久化的EOjbects的容器;ResourceSet表示一組Resource的集合,集合中的Resource同時創建或加載。 Resource中比較重要的就是save和load方法,還有通過URI fregments訪問資源中的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對象。
4.關于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()方法的算法(引用原文):
Check for a factory in the protocolToFactoryMap, using the scheme of the URI.
If nothing was found, check the extensionToFactoryMap using the file extension of the URI.
If still nothing was found, check the extensionToFactoryMap using the DEFAULT_EXTENSION (that is, the wildcard character "*").
If no extension match was found, call the delegatedGetFactory() method. This allows you to supply your own factory registry, with its own lookup criteria.
If a descriptor was found, instead of an actual factory, call the createFactory() method on the descriptor to create the factory.
Finally, return the factory if one was found, or null.
tip:emf缺省的序列化方式是XMI。因此,如果沒有找到相應注冊的factory缺省的就會返回以*注冊的缺省的factory,這個factory是針對XMI的factory,即XMIResourceFactoryImpl。如,對于 XMIResourceFactoryImpl的擴展點聲明:
?
<extension?point?=?"org.eclipse.emf.ecore.extension_parser">??<parser?type="*"
?????class="org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl"/>
</extension>
??getExtensionToFactoryMap().put("*",?new?XMIResourceFactoryImpl());
轉載于:https://www.cnblogs.com/youngerbaby/archive/2006/05/07/393043.html
總結
以上是生活随笔為你收集整理的EMF的一些总结(2)——关于EMF的序列化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做完输卵管造影后一般几个月能怀孕?
- 下一篇: DNF可以转区吗?可以的话怎么转