|
|
|
|
|
|
Re: Exception when referencing between Resources in same ResourceSet [message #1809965 is a reply to message #1809962] |
Fri, 26 July 2019 15:12 |
|
yes the uri stuff you do is terrible suspiciously too
public class MyDslHandler extends AbstractHandler {
@Inject
XtextLiveScopeResourceSetProvider rsp;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Object firstElement = structuredSelection.getFirstElement();
if (firstElement instanceof IFile) {
IFile file = (IFile) firstElement;
URI modelURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
XtextResourceSet resourceSet = (XtextResourceSet) rsp.get(file.getProject());
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.getResource(modelURI, true);
Model model = (Model) resource.getContents().get(0);
Model modelForObject = MyDslFactory.eINSTANCE.createModel();
First newObject = MyDslFactory.eINSTANCE.createFirst();
newObject.setName("newObject");
modelForObject.getElements().add(newObject);
URI objectURI = modelURI.trimSegments(1).appendSegment("newObject."+file.getFileExtension());
Resource objectResource = resourceSet.createResource(objectURI);
objectResource.getContents().add(modelForObject);
try {
objectResource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
Reference reference = MyDslFactory.eINSTANCE.createReference();
reference.setReference(newObject);
Second second = MyDslFactory.eINSTANCE.createSecond();
second.setItem(reference);
model.getElements().add(second);
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}
return null;
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.02549 seconds