网站开发投票代码,重庆企业网站制作公司,wap网站开发 费用,海口seo网站推广关于EMF的序列化对于EMF的序列化有几个比较重要的接口#xff1a;Resource,ResourceSet,Resource.Factory,URIConverter。这些接口的主要作用就是保存模型到持久化存储介质#xff0c;或者从持久化存储介质加载模型。1#xff0e;关于URI#xff08;Uniform Resource Ident…关于EMF的序列化对于EMF的序列化有几个比较重要的接口Resource,ResourceSet,Resource.Factory,URIConverter。这些接口的主要作用就是保存模型到持久化存储介质或者从持久化存储介质加载模型。1关于URIUniform 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.xmlfile是协议没有authorityc:是device,剩下的3个是segments。URI fregment标识了resource内部的某个具体的内容。如file:/c:/dir1/dir2/myfile.xml#loc中的#loc。 EMF通过带有fregment的URI来访问资源中的EObjects。2关于URIConverterURIConverter的作用是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.ecoreURIConverter.normalize()方法只是简单的同过映射的map把key替换成了相应的value。URIConverter的最原始是应用在resource sets用来定位resources.3关于Resource和ResourceSetResource 表示一个持久化的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()创建一个空的ResourcegetResource()通过resource的URI来创建ResourcegetEObject()通过URI中的fregment来获得具体的EObject对象。4关于Resource.Factory用来创建Resourceresource 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. tipemf缺省的序列化方式是XMI。因此如果没有找到相应注册的factory缺省的就会返回以*注册的缺省的factory这个factory是针对XMI的factory即XMIResourceFactoryImpl。如对于 XMIResourceFactoryImpl的扩展点声明: extension point org.eclipse.emf.ecore.extension_parser parser type* classorg.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl//extension 当非插件应用的时候可以通过手工的方式来注册factory如 Resource.Factory.Registry.INSTANCE. getExtensionToFactoryMap().put(*, new XMIResourceFactoryImpl()); 转载于:https://www.cnblogs.com/youngerbaby/archive/2006/05/07/393043.html