今天看到两种使用EMF解析.xml为EMF模型的策略: 一种是通过如下代码: IFileEditorInput modelFile = (IFileEditorInput)getEditorInput(); URI resourceURI = URI.createPlatformResourceURI(modelFile.getFile().getFullPath().toString());; resource = editingDomain.getResourceSet().getResource(resourceURI, true ); 上面这种方式会引用一个AdapterFactoryEditingDomain类,但我们基于模型做编辑器时,有时并不需要用这个类,例如GEF,它具有自身的EditorDomain。 另外一种方式是使用EMF模型自动生成的Process,该类一般在模型的Util包下面,引用代码如下: IFileEditorInput modelFile = (IFileEditorInput)getEditorInput(); XMLProcessor processor = new DesignXMLProcessor(); resource = processor.load(new InputSource(new InputStreamReader(modelFile.getFile().getContents(), "GBK")), null); 其实,对于EMF而言,上面两种解释方式,归根到底都需要EMF获得 业务模型相关的解析器,对于第一种方式,EMF是如何获取到业务模型的解析器呢?主要是通过扩展的方式,扩展定义在模型的plugin.xml中,代码片断如下图所示: <extension point="org.eclipse.emf.ecore.extension_parser"> <parser type="design" class="com.neusoft.report.design.util.DesignResourceFactoryImpl" /> </extension> 这样,解析.xml文件时,EMF从ResourceFactory注册中,根据相应的type,获取解析器(DesignResourceFactoryImpl),完成解析。 |