1 | package de.ugoe.cs.swe.bnftools.utils;
|
---|
2 |
|
---|
3 | import java.util.Iterator;
|
---|
4 |
|
---|
5 | import org.eclipse.emf.common.util.URI;
|
---|
6 | import org.eclipse.emf.ecore.EObject;
|
---|
7 | import org.eclipse.emf.ecore.util.EcoreUtil;
|
---|
8 | import org.eclipse.xtext.resource.IEObjectDescription;
|
---|
9 | import org.eclipse.xtext.resource.IResourceDescription;
|
---|
10 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
11 | import org.eclipse.xtext.resource.XtextResource;
|
---|
12 | import org.eclipse.xtext.util.concurrent.IUnitOfWork;
|
---|
13 | import com.google.common.base.Predicate;
|
---|
14 | import com.google.common.collect.Iterables;
|
---|
15 |
|
---|
16 | public class EObjectResolver implements
|
---|
17 | IUnitOfWork<IEObjectDescription, XtextResource> {
|
---|
18 | private IResourceDescriptions resourceDescriptions;
|
---|
19 | private EObject element;
|
---|
20 |
|
---|
21 | public EObjectResolver(EObject element,
|
---|
22 | IResourceDescriptions resourceDescriptions) {
|
---|
23 | this.element = element;
|
---|
24 | this.resourceDescriptions = resourceDescriptions;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public IEObjectDescription exec(XtextResource state) throws Exception {
|
---|
28 | if (element != null) {
|
---|
29 | final URI eObjectURI = EcoreUtil.getURI(element);
|
---|
30 | IResourceDescription resourceDescription = resourceDescriptions
|
---|
31 | .getResourceDescription(eObjectURI.trimFragment());
|
---|
32 | if (resourceDescription != null) {
|
---|
33 | Iterator<IEObjectDescription> eObjectDescriptions = Iterables
|
---|
34 | .filter(resourceDescription.getExportedObjects(),
|
---|
35 | new Predicate<IEObjectDescription>() {
|
---|
36 | public boolean apply(
|
---|
37 | IEObjectDescription input) {
|
---|
38 | return input.getEObjectURI().equals(
|
---|
39 | eObjectURI);
|
---|
40 | }
|
---|
41 | }).iterator();
|
---|
42 | if (eObjectDescriptions.hasNext()) {
|
---|
43 | return eObjectDescriptions.next();
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | return null;
|
---|
48 | }
|
---|
49 | } |
---|