load protobuf Model

29 views
Skip to first unread message

wangm...@gmail.com

unread,
Jan 23, 2015, 4:48:58 AM1/23/15
to proto...@googlegroups.com
Hi :

      i am using Protobuf-dt,now i want to load protobuf model  for example :

   
public EObject loadModel(URI uri, URI root) {
 
// resolve the input uri, in case it is a relative path
 URI absURI
= uri.resolve(root);
 
if (! uri.equals(absURI)) {
 
// add this pair to URI converter so that others can get the URI by its relative path
 resourceSet
.getURIConverter().getURIMap().put(uri, absURI);
 
}
 
 
// load root model
 
Resource resource = null;
 
try {
 resource
= resourceSet.getResource(absURI, true);
 resource
.load(Collections.EMPTY_MAP);
 
} catch (IOException e) {
 e
.printStackTrace();
 
return null;
 
}
 
EObject model = resource.getContents().get(0);
 
 
// load all its imports recursively
 
for (Iterator<String> it = fileHandlerRegistry.get(absURI.fileExtension()).importsIterator(model); it.hasNext();) {
 
String importURIStr = it.next();
 URI importURI
= URI.createURI(importURIStr);
 URI resolvedURI
= importURI.resolve(absURI);
 
 
// add this pair to URI converter so that others can get the URI by its relative path
 resourceSet
.getURIConverter().getURIMap().put(importURI, resolvedURI);
 
 loadModel
(resolvedURI, root);
 
}
 
return model;
 
}


but when resource = resourceSet.getResource(absURI, true);  error:
1) null returned by binding at com.google.eclipse.protobuf.ProtobufRuntimeModule.configureExtensionRegistry(ProtobufRuntimeModule.java:75)
 but com
.google.eclipse.protobuf.scoping.ProtoDescriptorProvider.registry is not @Nullable
 
while locating com.google.eclipse.protobuf.scoping.ExtensionRegistryProvider
 
while locating org.eclipse.core.runtime.IExtensionRegistry
   
for field at com.google.eclipse.protobuf.scoping.ProtoDescriptorProvider.registry(Unknown Source)

is there any method to load protobuf model?

i try createInjectorAndDoEMFRegistration override but still
  public Injector createInjectorAndDoEMFRegistration() {
       
// the FrancaPackage might not be registered because the EMF model is not part of the DSL
       
if (! EPackage.Registry.INSTANCE.containsKey(com.google.eclipse.protobuf.protobuf.ProtobufPackage.eNS_URI)) {
           
EPackage.Registry.INSTANCE.put(com.google.eclipse.protobuf.protobuf.ProtobufPackage.eNS_URI, com.google.eclipse.protobuf.protobuf.ProtobufPackage.eINSTANCE);
       
}
       
return super.createInjectorAndDoEMFRegistration();

   
}




wangm...@gmail.com

unread,
Jan 23, 2015, 4:54:57 AM1/23/15
to proto...@googlegroups.com
add :  i want use protobuf-dt stanalone with acceleo 
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(key, Object);

does the protobuf-dt  provide facroty 
like resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE); to registerResourceFactories

thank


在 2015年1月23日星期五 UTC+8下午5:48:58,wangm...@gmail.com写道:

Klaus Birken

unread,
Mar 14, 2016, 4:38:41 PM3/14/16
to protobuf-dt
Hi,

I had the same problem - and found the cause for it and also a workaround. Actually, the solution requires a patch in protobuf-dt code.

The problem occurs because the following provider's get() method will return null if the code is not running in the Eclipse IDE (which is the case if running standalone). 

@Singleton public class ExtensionRegistryProvider implements Provider<IExtensionRegistry> {
  @Override public IExtensionRegistry get() {
    return Platform.getExtensionRegistry();
  }
}

Google Guice's injector will call this get() method and complain about the null-result during injection of field ProtoDescriptorProvider.registry. This default behavior of Guice can be changed by declaring the field as @Nullable:

@Singleton public class ProtoDescriptorProvider {


 [...]

 @Inject @Nullable private IExtensionRegistry registry;
 
...


Guice allows to use any @Nullable annotation (it just checks for the simple name). If you do not have any @Nullable annotation at hand, simply use this one:

@Target({ FIELD })

@Retention(RUNTIME)

@interface Nullable { }


The final step is to add some checks for registry==null in the code of class ProtoDescriptorProvider. Just find the only occurrence of field registry and add a null check:

private ProtoDescriptorInfo descriptorInfoFromExtensionPoint() {

       // registry will be null if not running in the Eclipse IDE

       if (registry == null) {

          return null;

       }

...

 


That's it, hope this helps!
Klaus
Reply all
Reply to author
Forward
0 new messages